sysconfig.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. """Provide access to Python's configuration information. The specific
  2. configuration variables available depend heavily on the platform and
  3. configuration. The values may be retrieved using
  4. get_config_var(name), and the list of variables is available via
  5. get_config_vars().keys(). Additional convenience functions are also
  6. available.
  7. Written by: Fred L. Drake, Jr.
  8. Email: <[email protected]>
  9. """
  10. __revision__ = "$Id: sysconfig.py 52231 2006-10-08 17:41:25Z ronald.oussoren $"
  11. import os
  12. import re
  13. import string
  14. import sys
  15. from errors import DistutilsPlatformError
  16. # These are needed in a couple of spots, so just compute them once.
  17. PREFIX = os.path.normpath(sys.prefix)
  18. EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
  19. # python_build: (Boolean) if true, we're either building Python or
  20. # building an extension with an un-installed Python, so we use
  21. # different (hard-wired) directories.
  22. argv0_path = os.path.dirname(os.path.abspath(sys.executable))
  23. landmark = os.path.join(argv0_path, "Modules", "Setup")
  24. python_build = os.path.isfile(landmark)
  25. del argv0_path, landmark
  26. def get_python_version ():
  27. """Return a string containing the major and minor Python version,
  28. leaving off the patchlevel. Sample return values could be '1.5'
  29. or '2.2'.
  30. """
  31. return sys.version[:3]
  32. def get_python_inc(plat_specific=0, prefix=None):
  33. """Return the directory containing installed Python header files.
  34. If 'plat_specific' is false (the default), this is the path to the
  35. non-platform-specific header files, i.e. Python.h and so on;
  36. otherwise, this is the path to platform-specific header files
  37. (namely pyconfig.h).
  38. If 'prefix' is supplied, use it instead of sys.prefix or
  39. sys.exec_prefix -- i.e., ignore 'plat_specific'.
  40. """
  41. if prefix is None:
  42. prefix = plat_specific and EXEC_PREFIX or PREFIX
  43. if os.name == "posix":
  44. if python_build:
  45. base = os.path.dirname(os.path.abspath(sys.executable))
  46. if plat_specific:
  47. inc_dir = base
  48. else:
  49. inc_dir = os.path.join(base, "Include")
  50. if not os.path.exists(inc_dir):
  51. inc_dir = os.path.join(os.path.dirname(base), "Include")
  52. return inc_dir
  53. return os.path.join(prefix, "include", "python" + sys.version[:3])
  54. elif os.name == "nt":
  55. return os.path.join(prefix, "include")
  56. elif os.name == "mac":
  57. if plat_specific:
  58. return os.path.join(prefix, "Mac", "Include")
  59. else:
  60. return os.path.join(prefix, "Include")
  61. elif os.name == "os2":
  62. return os.path.join(prefix, "Include")
  63. else:
  64. raise DistutilsPlatformError(
  65. "I don't know where Python installs its C header files "
  66. "on platform '%s'" % os.name)
  67. def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
  68. """Return the directory containing the Python library (standard or
  69. site additions).
  70. If 'plat_specific' is true, return the directory containing
  71. platform-specific modules, i.e. any module from a non-pure-Python
  72. module distribution; otherwise, return the platform-shared library
  73. directory. If 'standard_lib' is true, return the directory
  74. containing standard Python library modules; otherwise, return the
  75. directory for site-specific modules.
  76. If 'prefix' is supplied, use it instead of sys.prefix or
  77. sys.exec_prefix -- i.e., ignore 'plat_specific'.
  78. """
  79. if prefix is None:
  80. prefix = plat_specific and EXEC_PREFIX or PREFIX
  81. if os.name == "posix":
  82. libpython = os.path.join(prefix,
  83. "lib", "python" + get_python_version())
  84. if standard_lib:
  85. return libpython
  86. else:
  87. return os.path.join(libpython, "site-packages")
  88. elif os.name == "nt":
  89. if standard_lib:
  90. return os.path.join(prefix, "Lib")
  91. else:
  92. if sys.version < "2.2":
  93. return prefix
  94. else:
  95. return os.path.join(PREFIX, "Lib", "site-packages")
  96. elif os.name == "mac":
  97. if plat_specific:
  98. if standard_lib:
  99. return os.path.join(prefix, "Lib", "lib-dynload")
  100. else:
  101. return os.path.join(prefix, "Lib", "site-packages")
  102. else:
  103. if standard_lib:
  104. return os.path.join(prefix, "Lib")
  105. else:
  106. return os.path.join(prefix, "Lib", "site-packages")
  107. elif os.name == "os2":
  108. if standard_lib:
  109. return os.path.join(PREFIX, "Lib")
  110. else:
  111. return os.path.join(PREFIX, "Lib", "site-packages")
  112. else:
  113. raise DistutilsPlatformError(
  114. "I don't know where Python installs its library "
  115. "on platform '%s'" % os.name)
  116. def customize_compiler(compiler):
  117. """Do any platform-specific customization of a CCompiler instance.
  118. Mainly needed on Unix, so we can plug in the information that
  119. varies across Unices and is stored in Python's Makefile.
  120. """
  121. if compiler.compiler_type == "unix":
  122. (cc, cxx, opt, basecflags, ccshared, ldshared, so_ext) = \
  123. get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS', 'CCSHARED', 'LDSHARED', 'SO')
  124. if os.environ.has_key('CC'):
  125. cc = os.environ['CC']
  126. if os.environ.has_key('CXX'):
  127. cxx = os.environ['CXX']
  128. if os.environ.has_key('LDSHARED'):
  129. ldshared = os.environ['LDSHARED']
  130. if os.environ.has_key('CPP'):
  131. cpp = os.environ['CPP']
  132. else:
  133. cpp = cc + " -E" # not always
  134. if os.environ.has_key('LDFLAGS'):
  135. ldshared = ldshared + ' ' + os.environ['LDFLAGS']
  136. if basecflags:
  137. opt = basecflags + ' ' + opt
  138. if os.environ.has_key('CFLAGS'):
  139. opt = opt + ' ' + os.environ['CFLAGS']
  140. ldshared = ldshared + ' ' + os.environ['CFLAGS']
  141. if os.environ.has_key('CPPFLAGS'):
  142. cpp = cpp + ' ' + os.environ['CPPFLAGS']
  143. opt = opt + ' ' + os.environ['CPPFLAGS']
  144. ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
  145. cc_cmd = cc + ' ' + opt
  146. compiler.set_executables(
  147. preprocessor=cpp,
  148. compiler=cc_cmd,
  149. compiler_so=cc_cmd + ' ' + ccshared,
  150. compiler_cxx=cxx,
  151. linker_so=ldshared,
  152. linker_exe=cc)
  153. compiler.shared_lib_extension = so_ext
  154. def get_config_h_filename():
  155. """Return full pathname of installed pyconfig.h file."""
  156. if python_build:
  157. inc_dir = os.curdir
  158. else:
  159. inc_dir = get_python_inc(plat_specific=1)
  160. if sys.version < '2.2':
  161. config_h = 'config.h'
  162. else:
  163. # The name of the config.h file changed in 2.2
  164. config_h = 'pyconfig.h'
  165. return os.path.join(inc_dir, config_h)
  166. def get_makefile_filename():
  167. """Return full pathname of installed Makefile from the Python build."""
  168. if python_build:
  169. return os.path.join(os.path.dirname(sys.executable), "Makefile")
  170. lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
  171. return os.path.join(lib_dir, "config", "Makefile")
  172. def parse_config_h(fp, g=None):
  173. """Parse a config.h-style file.
  174. A dictionary containing name/value pairs is returned. If an
  175. optional dictionary is passed in as the second argument, it is
  176. used instead of a new dictionary.
  177. """
  178. if g is None:
  179. g = {}
  180. define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
  181. undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
  182. #
  183. while 1:
  184. line = fp.readline()
  185. if not line:
  186. break
  187. m = define_rx.match(line)
  188. if m:
  189. n, v = m.group(1, 2)
  190. try: v = int(v)
  191. except ValueError: pass
  192. g[n] = v
  193. else:
  194. m = undef_rx.match(line)
  195. if m:
  196. g[m.group(1)] = 0
  197. return g
  198. # Regexes needed for parsing Makefile (and similar syntaxes,
  199. # like old-style Setup files).
  200. _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
  201. _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
  202. _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
  203. def parse_makefile(fn, g=None):
  204. """Parse a Makefile-style file.
  205. A dictionary containing name/value pairs is returned. If an
  206. optional dictionary is passed in as the second argument, it is
  207. used instead of a new dictionary.
  208. """
  209. from distutils.text_file import TextFile
  210. fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
  211. if g is None:
  212. g = {}
  213. done = {}
  214. notdone = {}
  215. while 1:
  216. line = fp.readline()
  217. if line is None: # eof
  218. break
  219. m = _variable_rx.match(line)
  220. if m:
  221. n, v = m.group(1, 2)
  222. v = string.strip(v)
  223. if "$" in v:
  224. notdone[n] = v
  225. else:
  226. try: v = int(v)
  227. except ValueError: pass
  228. done[n] = v
  229. # do variable interpolation here
  230. while notdone:
  231. for name in notdone.keys():
  232. value = notdone[name]
  233. m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
  234. if m:
  235. n = m.group(1)
  236. if done.has_key(n):
  237. after = value[m.end():]
  238. value = value[:m.start()] + str(done[n]) + after
  239. if "$" in after:
  240. notdone[name] = value
  241. else:
  242. try: value = int(value)
  243. except ValueError:
  244. done[name] = string.strip(value)
  245. else:
  246. done[name] = value
  247. del notdone[name]
  248. elif notdone.has_key(n):
  249. # get it on a subsequent round
  250. pass
  251. else:
  252. done[n] = ""
  253. after = value[m.end():]
  254. value = value[:m.start()] + after
  255. if "$" in after:
  256. notdone[name] = value
  257. else:
  258. try: value = int(value)
  259. except ValueError:
  260. done[name] = string.strip(value)
  261. else:
  262. done[name] = value
  263. del notdone[name]
  264. else:
  265. # bogus variable reference; just drop it since we can't deal
  266. del notdone[name]
  267. fp.close()
  268. # save the results in the global dictionary
  269. g.update(done)
  270. return g
  271. def expand_makefile_vars(s, vars):
  272. """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
  273. 'string' according to 'vars' (a dictionary mapping variable names to
  274. values). Variables not present in 'vars' are silently expanded to the
  275. empty string. The variable values in 'vars' should not contain further
  276. variable expansions; if 'vars' is the output of 'parse_makefile()',
  277. you're fine. Returns a variable-expanded version of 's'.
  278. """
  279. # This algorithm does multiple expansion, so if vars['foo'] contains
  280. # "${bar}", it will expand ${foo} to ${bar}, and then expand
  281. # ${bar}... and so forth. This is fine as long as 'vars' comes from
  282. # 'parse_makefile()', which takes care of such expansions eagerly,
  283. # according to make's variable expansion semantics.
  284. while 1:
  285. m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
  286. if m:
  287. (beg, end) = m.span()
  288. s = s[0:beg] + vars.get(m.group(1)) + s[end:]
  289. else:
  290. break
  291. return s
  292. _config_vars = None
  293. def _init_posix():
  294. """Initialize the module as appropriate for POSIX systems."""
  295. g = {}
  296. # load the installed Makefile:
  297. try:
  298. filename = get_makefile_filename()
  299. parse_makefile(filename, g)
  300. except IOError, msg:
  301. my_msg = "invalid Python installation: unable to open %s" % filename
  302. if hasattr(msg, "strerror"):
  303. my_msg = my_msg + " (%s)" % msg.strerror
  304. raise DistutilsPlatformError(my_msg)
  305. # On MacOSX we need to check the setting of the environment variable
  306. # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
  307. # it needs to be compatible.
  308. # If it isn't set we set it to the configure-time value
  309. if sys.platform == 'darwin' and g.has_key('MACOSX_DEPLOYMENT_TARGET'):
  310. cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
  311. cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
  312. if cur_target == '':
  313. cur_target = cfg_target
  314. os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
  315. if cfg_target != cur_target:
  316. my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
  317. % (cur_target, cfg_target))
  318. raise DistutilsPlatformError(my_msg)
  319. # On AIX, there are wrong paths to the linker scripts in the Makefile
  320. # -- these paths are relative to the Python source, but when installed
  321. # the scripts are in another directory.
  322. if python_build:
  323. g['LDSHARED'] = g['BLDSHARED']
  324. elif sys.version < '2.1':
  325. # The following two branches are for 1.5.2 compatibility.
  326. if sys.platform == 'aix4': # what about AIX 3.x ?
  327. # Linker script is in the config directory, not in Modules as the
  328. # Makefile says.
  329. python_lib = get_python_lib(standard_lib=1)
  330. ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
  331. python_exp = os.path.join(python_lib, 'config', 'python.exp')
  332. g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
  333. elif sys.platform == 'beos':
  334. # Linker script is in the config directory. In the Makefile it is
  335. # relative to the srcdir, which after installation no longer makes
  336. # sense.
  337. python_lib = get_python_lib(standard_lib=1)
  338. linkerscript_path = string.split(g['LDSHARED'])[0]
  339. linkerscript_name = os.path.basename(linkerscript_path)
  340. linkerscript = os.path.join(python_lib, 'config',
  341. linkerscript_name)
  342. # XXX this isn't the right place to do this: adding the Python
  343. # library to the link, if needed, should be in the "build_ext"
  344. # command. (It's also needed for non-MS compilers on Windows, and
  345. # it's taken care of for them by the 'build_ext.get_libraries()'
  346. # method.)
  347. g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
  348. (linkerscript, PREFIX, sys.version[0:3]))
  349. global _config_vars
  350. _config_vars = g
  351. def _init_nt():
  352. """Initialize the module as appropriate for NT"""
  353. g = {}
  354. # set basic install directories
  355. g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  356. g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  357. # XXX hmmm.. a normal install puts include files here
  358. g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  359. g['SO'] = '.pyd'
  360. g['EXE'] = ".exe"
  361. global _config_vars
  362. _config_vars = g
  363. def _init_mac():
  364. """Initialize the module as appropriate for Macintosh systems"""
  365. g = {}
  366. # set basic install directories
  367. g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  368. g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  369. # XXX hmmm.. a normal install puts include files here
  370. g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  371. import MacOS
  372. if not hasattr(MacOS, 'runtimemodel'):
  373. g['SO'] = '.ppc.slb'
  374. else:
  375. g['SO'] = '.%s.slb' % MacOS.runtimemodel
  376. # XXX are these used anywhere?
  377. g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
  378. g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
  379. # These are used by the extension module build
  380. g['srcdir'] = ':'
  381. global _config_vars
  382. _config_vars = g
  383. def _init_os2():
  384. """Initialize the module as appropriate for OS/2"""
  385. g = {}
  386. # set basic install directories
  387. g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  388. g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  389. # XXX hmmm.. a normal install puts include files here
  390. g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  391. g['SO'] = '.pyd'
  392. g['EXE'] = ".exe"
  393. global _config_vars
  394. _config_vars = g
  395. def get_config_vars(*args):
  396. """With no arguments, return a dictionary of all configuration
  397. variables relevant for the current platform. Generally this includes
  398. everything needed to build extensions and install both pure modules and
  399. extensions. On Unix, this means every variable defined in Python's
  400. installed Makefile; on Windows and Mac OS it's a much smaller set.
  401. With arguments, return a list of values that result from looking up
  402. each argument in the configuration variable dictionary.
  403. """
  404. global _config_vars
  405. if _config_vars is None:
  406. func = globals().get("_init_" + os.name)
  407. if func:
  408. func()
  409. else:
  410. _config_vars = {}
  411. # Normalized versions of prefix and exec_prefix are handy to have;
  412. # in fact, these are the standard versions used most places in the
  413. # Distutils.
  414. _config_vars['prefix'] = PREFIX
  415. _config_vars['exec_prefix'] = EXEC_PREFIX
  416. if sys.platform == 'darwin':
  417. kernel_version = os.uname()[2] # Kernel version (8.4.3)
  418. major_version = int(kernel_version.split('.')[0])
  419. if major_version < 8:
  420. # On Mac OS X before 10.4, check if -arch and -isysroot
  421. # are in CFLAGS or LDFLAGS and remove them if they are.
  422. # This is needed when building extensions on a 10.3 system
  423. # using a universal build of python.
  424. for key in ('LDFLAGS', 'BASECFLAGS',
  425. # The values below are derived from the earlier ones,
  426. # but subsitution has been by now.
  427. 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
  428. flags = _config_vars[key]
  429. flags = re.sub('-arch\s+\w+\s', ' ', flags)
  430. flags = re.sub('-isysroot [^ \t]*', ' ', flags)
  431. _config_vars[key] = flags
  432. if args:
  433. vals = []
  434. for name in args:
  435. vals.append(_config_vars.get(name))
  436. return vals
  437. else:
  438. return _config_vars
  439. def get_config_var(name):
  440. """Return the value of a single variable using the dictionary
  441. returned by 'get_config_vars()'. Equivalent to
  442. get_config_vars().get(name)
  443. """
  444. return get_config_vars().get(name)