]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/distutils/sysconfig.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / distutils / sysconfig.py
CommitLineData
4710c53d 1"""Provide access to Python's configuration information. The specific\r
2configuration variables available depend heavily on the platform and\r
3configuration. The values may be retrieved using\r
4get_config_var(name), and the list of variables is available via\r
5get_config_vars().keys(). Additional convenience functions are also\r
6available.\r
7\r
8Written by: Fred L. Drake, Jr.\r
9Email: <fdrake@acm.org>\r
10"""\r
11\r
12__revision__ = "$Id$"\r
13\r
14import os\r
15import re\r
16import string\r
17import sys\r
18\r
19from distutils.errors import DistutilsPlatformError\r
20\r
21# These are needed in a couple of spots, so just compute them once.\r
22PREFIX = os.path.normpath(sys.prefix)\r
23EXEC_PREFIX = os.path.normpath(sys.exec_prefix)\r
24\r
25# Path to the base directory of the project. On Windows the binary may\r
26# live in project/PCBuild9. If we're dealing with an x64 Windows build,\r
27# it'll live in project/PCbuild/amd64.\r
28project_base = os.path.dirname(os.path.abspath(sys.executable))\r
29if os.name == "nt" and "pcbuild" in project_base[-8:].lower():\r
30 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))\r
31# PC/VS7.1\r
32if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():\r
33 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,\r
34 os.path.pardir))\r
35# PC/AMD64\r
36if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower():\r
37 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,\r
38 os.path.pardir))\r
39\r
40# python_build: (Boolean) if true, we're either building Python or\r
41# building an extension with an un-installed Python, so we use\r
42# different (hard-wired) directories.\r
43# Setup.local is available for Makefile builds including VPATH builds,\r
44# Setup.dist is available on Windows\r
45def _python_build():\r
46 for fn in ("Setup.dist", "Setup.local"):\r
47 if os.path.isfile(os.path.join(project_base, "Modules", fn)):\r
48 return True\r
49 return False\r
50python_build = _python_build()\r
51\r
52\r
53def get_python_version():\r
54 """Return a string containing the major and minor Python version,\r
55 leaving off the patchlevel. Sample return values could be '1.5'\r
56 or '2.2'.\r
57 """\r
58 return sys.version[:3]\r
59\r
60\r
61def get_python_inc(plat_specific=0, prefix=None):\r
62 """Return the directory containing installed Python header files.\r
63\r
64 If 'plat_specific' is false (the default), this is the path to the\r
65 non-platform-specific header files, i.e. Python.h and so on;\r
66 otherwise, this is the path to platform-specific header files\r
67 (namely pyconfig.h).\r
68\r
69 If 'prefix' is supplied, use it instead of sys.prefix or\r
70 sys.exec_prefix -- i.e., ignore 'plat_specific'.\r
71 """\r
72 if prefix is None:\r
73 prefix = plat_specific and EXEC_PREFIX or PREFIX\r
74\r
75 if os.name == "posix":\r
76 if python_build:\r
77 buildir = os.path.dirname(sys.executable)\r
78 if plat_specific:\r
79 # python.h is located in the buildir\r
80 inc_dir = buildir\r
81 else:\r
82 # the source dir is relative to the buildir\r
83 srcdir = os.path.abspath(os.path.join(buildir,\r
84 get_config_var('srcdir')))\r
85 # Include is located in the srcdir\r
86 inc_dir = os.path.join(srcdir, "Include")\r
87 return inc_dir\r
88 return os.path.join(prefix, "include", "python" + get_python_version())\r
89 elif os.name == "nt":\r
90 return os.path.join(prefix, "include")\r
91 elif os.name == "os2":\r
92 return os.path.join(prefix, "Include")\r
93 else:\r
94 raise DistutilsPlatformError(\r
95 "I don't know where Python installs its C header files "\r
96 "on platform '%s'" % os.name)\r
97\r
98\r
99def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):\r
100 """Return the directory containing the Python library (standard or\r
101 site additions).\r
102\r
103 If 'plat_specific' is true, return the directory containing\r
104 platform-specific modules, i.e. any module from a non-pure-Python\r
105 module distribution; otherwise, return the platform-shared library\r
106 directory. If 'standard_lib' is true, return the directory\r
107 containing standard Python library modules; otherwise, return the\r
108 directory for site-specific modules.\r
109\r
110 If 'prefix' is supplied, use it instead of sys.prefix or\r
111 sys.exec_prefix -- i.e., ignore 'plat_specific'.\r
112 """\r
113 if prefix is None:\r
114 prefix = plat_specific and EXEC_PREFIX or PREFIX\r
115\r
116 if os.name == "posix":\r
117 libpython = os.path.join(prefix,\r
118 "lib", "python" + get_python_version())\r
119 if standard_lib:\r
120 return libpython\r
121 else:\r
122 return os.path.join(libpython, "site-packages")\r
123\r
124 elif os.name == "nt":\r
125 if standard_lib:\r
126 return os.path.join(prefix, "Lib")\r
127 else:\r
128 if get_python_version() < "2.2":\r
129 return prefix\r
130 else:\r
131 return os.path.join(prefix, "Lib", "site-packages")\r
132\r
133 elif os.name == "os2":\r
134 if standard_lib:\r
135 return os.path.join(prefix, "Lib")\r
136 else:\r
137 return os.path.join(prefix, "Lib", "site-packages")\r
138\r
139 else:\r
140 raise DistutilsPlatformError(\r
141 "I don't know where Python installs its library "\r
142 "on platform '%s'" % os.name)\r
143\r
144\r
145def customize_compiler(compiler):\r
146 """Do any platform-specific customization of a CCompiler instance.\r
147\r
148 Mainly needed on Unix, so we can plug in the information that\r
149 varies across Unices and is stored in Python's Makefile.\r
150 """\r
151 if compiler.compiler_type == "unix":\r
152 (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \\r
153 get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',\r
154 'CCSHARED', 'LDSHARED', 'SO')\r
155\r
156 if 'CC' in os.environ:\r
157 cc = os.environ['CC']\r
158 if 'CXX' in os.environ:\r
159 cxx = os.environ['CXX']\r
160 if 'LDSHARED' in os.environ:\r
161 ldshared = os.environ['LDSHARED']\r
162 if 'CPP' in os.environ:\r
163 cpp = os.environ['CPP']\r
164 else:\r
165 cpp = cc + " -E" # not always\r
166 if 'LDFLAGS' in os.environ:\r
167 ldshared = ldshared + ' ' + os.environ['LDFLAGS']\r
168 if 'CFLAGS' in os.environ:\r
169 cflags = opt + ' ' + os.environ['CFLAGS']\r
170 ldshared = ldshared + ' ' + os.environ['CFLAGS']\r
171 if 'CPPFLAGS' in os.environ:\r
172 cpp = cpp + ' ' + os.environ['CPPFLAGS']\r
173 cflags = cflags + ' ' + os.environ['CPPFLAGS']\r
174 ldshared = ldshared + ' ' + os.environ['CPPFLAGS']\r
175\r
176 cc_cmd = cc + ' ' + cflags\r
177 compiler.set_executables(\r
178 preprocessor=cpp,\r
179 compiler=cc_cmd,\r
180 compiler_so=cc_cmd + ' ' + ccshared,\r
181 compiler_cxx=cxx,\r
182 linker_so=ldshared,\r
183 linker_exe=cc)\r
184\r
185 compiler.shared_lib_extension = so_ext\r
186\r
187\r
188def get_config_h_filename():\r
189 """Return full pathname of installed pyconfig.h file."""\r
190 if python_build:\r
191 if os.name == "nt":\r
192 inc_dir = os.path.join(project_base, "PC")\r
193 else:\r
194 inc_dir = project_base\r
195 else:\r
196 inc_dir = get_python_inc(plat_specific=1)\r
197 if get_python_version() < '2.2':\r
198 config_h = 'config.h'\r
199 else:\r
200 # The name of the config.h file changed in 2.2\r
201 config_h = 'pyconfig.h'\r
202 return os.path.join(inc_dir, config_h)\r
203\r
204\r
205def get_makefile_filename():\r
206 """Return full pathname of installed Makefile from the Python build."""\r
207 if python_build:\r
208 return os.path.join(os.path.dirname(sys.executable), "Makefile")\r
209 lib_dir = get_python_lib(plat_specific=1, standard_lib=1)\r
210 return os.path.join(lib_dir, "config", "Makefile")\r
211\r
212\r
213def parse_config_h(fp, g=None):\r
214 """Parse a config.h-style file.\r
215\r
216 A dictionary containing name/value pairs is returned. If an\r
217 optional dictionary is passed in as the second argument, it is\r
218 used instead of a new dictionary.\r
219 """\r
220 if g is None:\r
221 g = {}\r
222 define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")\r
223 undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")\r
224 #\r
225 while 1:\r
226 line = fp.readline()\r
227 if not line:\r
228 break\r
229 m = define_rx.match(line)\r
230 if m:\r
231 n, v = m.group(1, 2)\r
232 try: v = int(v)\r
233 except ValueError: pass\r
234 g[n] = v\r
235 else:\r
236 m = undef_rx.match(line)\r
237 if m:\r
238 g[m.group(1)] = 0\r
239 return g\r
240\r
241\r
242# Regexes needed for parsing Makefile (and similar syntaxes,\r
243# like old-style Setup files).\r
244_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")\r
245_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")\r
246_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")\r
247\r
248def parse_makefile(fn, g=None):\r
249 """Parse a Makefile-style file.\r
250\r
251 A dictionary containing name/value pairs is returned. If an\r
252 optional dictionary is passed in as the second argument, it is\r
253 used instead of a new dictionary.\r
254 """\r
255 from distutils.text_file import TextFile\r
256 fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)\r
257\r
258 if g is None:\r
259 g = {}\r
260 done = {}\r
261 notdone = {}\r
262\r
263 while 1:\r
264 line = fp.readline()\r
265 if line is None: # eof\r
266 break\r
267 m = _variable_rx.match(line)\r
268 if m:\r
269 n, v = m.group(1, 2)\r
270 v = v.strip()\r
271 # `$$' is a literal `$' in make\r
272 tmpv = v.replace('$$', '')\r
273\r
274 if "$" in tmpv:\r
275 notdone[n] = v\r
276 else:\r
277 try:\r
278 v = int(v)\r
279 except ValueError:\r
280 # insert literal `$'\r
281 done[n] = v.replace('$$', '$')\r
282 else:\r
283 done[n] = v\r
284\r
285 # do variable interpolation here\r
286 while notdone:\r
287 for name in notdone.keys():\r
288 value = notdone[name]\r
289 m = _findvar1_rx.search(value) or _findvar2_rx.search(value)\r
290 if m:\r
291 n = m.group(1)\r
292 found = True\r
293 if n in done:\r
294 item = str(done[n])\r
295 elif n in notdone:\r
296 # get it on a subsequent round\r
297 found = False\r
298 elif n in os.environ:\r
299 # do it like make: fall back to environment\r
300 item = os.environ[n]\r
301 else:\r
302 done[n] = item = ""\r
303 if found:\r
304 after = value[m.end():]\r
305 value = value[:m.start()] + item + after\r
306 if "$" in after:\r
307 notdone[name] = value\r
308 else:\r
309 try: value = int(value)\r
310 except ValueError:\r
311 done[name] = value.strip()\r
312 else:\r
313 done[name] = value\r
314 del notdone[name]\r
315 else:\r
316 # bogus variable reference; just drop it since we can't deal\r
317 del notdone[name]\r
318\r
319 fp.close()\r
320\r
321 # strip spurious spaces\r
322 for k, v in done.items():\r
323 if isinstance(v, str):\r
324 done[k] = v.strip()\r
325\r
326 # save the results in the global dictionary\r
327 g.update(done)\r
328 return g\r
329\r
330\r
331def expand_makefile_vars(s, vars):\r
332 """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in\r
333 'string' according to 'vars' (a dictionary mapping variable names to\r
334 values). Variables not present in 'vars' are silently expanded to the\r
335 empty string. The variable values in 'vars' should not contain further\r
336 variable expansions; if 'vars' is the output of 'parse_makefile()',\r
337 you're fine. Returns a variable-expanded version of 's'.\r
338 """\r
339\r
340 # This algorithm does multiple expansion, so if vars['foo'] contains\r
341 # "${bar}", it will expand ${foo} to ${bar}, and then expand\r
342 # ${bar}... and so forth. This is fine as long as 'vars' comes from\r
343 # 'parse_makefile()', which takes care of such expansions eagerly,\r
344 # according to make's variable expansion semantics.\r
345\r
346 while 1:\r
347 m = _findvar1_rx.search(s) or _findvar2_rx.search(s)\r
348 if m:\r
349 (beg, end) = m.span()\r
350 s = s[0:beg] + vars.get(m.group(1)) + s[end:]\r
351 else:\r
352 break\r
353 return s\r
354\r
355\r
356_config_vars = None\r
357\r
358def _init_posix():\r
359 """Initialize the module as appropriate for POSIX systems."""\r
360 g = {}\r
361 # load the installed Makefile:\r
362 try:\r
363 filename = get_makefile_filename()\r
364 parse_makefile(filename, g)\r
365 except IOError, msg:\r
366 my_msg = "invalid Python installation: unable to open %s" % filename\r
367 if hasattr(msg, "strerror"):\r
368 my_msg = my_msg + " (%s)" % msg.strerror\r
369\r
370 raise DistutilsPlatformError(my_msg)\r
371\r
372 # load the installed pyconfig.h:\r
373 try:\r
374 filename = get_config_h_filename()\r
375 parse_config_h(file(filename), g)\r
376 except IOError, msg:\r
377 my_msg = "invalid Python installation: unable to open %s" % filename\r
378 if hasattr(msg, "strerror"):\r
379 my_msg = my_msg + " (%s)" % msg.strerror\r
380\r
381 raise DistutilsPlatformError(my_msg)\r
382\r
383 # On MacOSX we need to check the setting of the environment variable\r
384 # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so\r
385 # it needs to be compatible.\r
386 # If it isn't set we set it to the configure-time value\r
387 if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:\r
388 cfg_target = g['MACOSX_DEPLOYMENT_TARGET']\r
389 cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')\r
390 if cur_target == '':\r
391 cur_target = cfg_target\r
392 os.environ['MACOSX_DEPLOYMENT_TARGET'] = cfg_target\r
393 elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):\r
394 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'\r
395 % (cur_target, cfg_target))\r
396 raise DistutilsPlatformError(my_msg)\r
397\r
398 # On AIX, there are wrong paths to the linker scripts in the Makefile\r
399 # -- these paths are relative to the Python source, but when installed\r
400 # the scripts are in another directory.\r
401 if python_build:\r
402 g['LDSHARED'] = g['BLDSHARED']\r
403\r
404 elif get_python_version() < '2.1':\r
405 # The following two branches are for 1.5.2 compatibility.\r
406 if sys.platform == 'aix4': # what about AIX 3.x ?\r
407 # Linker script is in the config directory, not in Modules as the\r
408 # Makefile says.\r
409 python_lib = get_python_lib(standard_lib=1)\r
410 ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')\r
411 python_exp = os.path.join(python_lib, 'config', 'python.exp')\r
412\r
413 g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)\r
414\r
415 elif sys.platform == 'beos':\r
416 # Linker script is in the config directory. In the Makefile it is\r
417 # relative to the srcdir, which after installation no longer makes\r
418 # sense.\r
419 python_lib = get_python_lib(standard_lib=1)\r
420 linkerscript_path = string.split(g['LDSHARED'])[0]\r
421 linkerscript_name = os.path.basename(linkerscript_path)\r
422 linkerscript = os.path.join(python_lib, 'config',\r
423 linkerscript_name)\r
424\r
425 # XXX this isn't the right place to do this: adding the Python\r
426 # library to the link, if needed, should be in the "build_ext"\r
427 # command. (It's also needed for non-MS compilers on Windows, and\r
428 # it's taken care of for them by the 'build_ext.get_libraries()'\r
429 # method.)\r
430 g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %\r
431 (linkerscript, PREFIX, get_python_version()))\r
432\r
433 global _config_vars\r
434 _config_vars = g\r
435\r
436\r
437def _init_nt():\r
438 """Initialize the module as appropriate for NT"""\r
439 g = {}\r
440 # set basic install directories\r
441 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)\r
442 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)\r
443\r
444 # XXX hmmm.. a normal install puts include files here\r
445 g['INCLUDEPY'] = get_python_inc(plat_specific=0)\r
446\r
447 g['SO'] = '.pyd'\r
448 g['EXE'] = ".exe"\r
449 g['VERSION'] = get_python_version().replace(".", "")\r
450 g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))\r
451\r
452 global _config_vars\r
453 _config_vars = g\r
454\r
455\r
456def _init_os2():\r
457 """Initialize the module as appropriate for OS/2"""\r
458 g = {}\r
459 # set basic install directories\r
460 g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)\r
461 g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)\r
462\r
463 # XXX hmmm.. a normal install puts include files here\r
464 g['INCLUDEPY'] = get_python_inc(plat_specific=0)\r
465\r
466 g['SO'] = '.pyd'\r
467 g['EXE'] = ".exe"\r
468\r
469 global _config_vars\r
470 _config_vars = g\r
471\r
472\r
473def get_config_vars(*args):\r
474 """With no arguments, return a dictionary of all configuration\r
475 variables relevant for the current platform. Generally this includes\r
476 everything needed to build extensions and install both pure modules and\r
477 extensions. On Unix, this means every variable defined in Python's\r
478 installed Makefile; on Windows and Mac OS it's a much smaller set.\r
479\r
480 With arguments, return a list of values that result from looking up\r
481 each argument in the configuration variable dictionary.\r
482 """\r
483 global _config_vars\r
484 if _config_vars is None:\r
485 func = globals().get("_init_" + os.name)\r
486 if func:\r
487 func()\r
488 else:\r
489 _config_vars = {}\r
490\r
491 # Normalized versions of prefix and exec_prefix are handy to have;\r
492 # in fact, these are the standard versions used most places in the\r
493 # Distutils.\r
494 _config_vars['prefix'] = PREFIX\r
495 _config_vars['exec_prefix'] = EXEC_PREFIX\r
496\r
497 if sys.platform == 'darwin':\r
498 kernel_version = os.uname()[2] # Kernel version (8.4.3)\r
499 major_version = int(kernel_version.split('.')[0])\r
500\r
501 if major_version < 8:\r
502 # On Mac OS X before 10.4, check if -arch and -isysroot\r
503 # are in CFLAGS or LDFLAGS and remove them if they are.\r
504 # This is needed when building extensions on a 10.3 system\r
505 # using a universal build of python.\r
506 for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',\r
507 # a number of derived variables. These need to be\r
508 # patched up as well.\r
509 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):\r
510 flags = _config_vars[key]\r
511 flags = re.sub('-arch\s+\w+\s', ' ', flags)\r
512 flags = re.sub('-isysroot [^ \t]*', ' ', flags)\r
513 _config_vars[key] = flags\r
514\r
515 else:\r
516\r
517 # Allow the user to override the architecture flags using\r
518 # an environment variable.\r
519 # NOTE: This name was introduced by Apple in OSX 10.5 and\r
520 # is used by several scripting languages distributed with\r
521 # that OS release.\r
522\r
523 if 'ARCHFLAGS' in os.environ:\r
524 arch = os.environ['ARCHFLAGS']\r
525 for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',\r
526 # a number of derived variables. These need to be\r
527 # patched up as well.\r
528 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):\r
529\r
530 flags = _config_vars[key]\r
531 flags = re.sub('-arch\s+\w+\s', ' ', flags)\r
532 flags = flags + ' ' + arch\r
533 _config_vars[key] = flags\r
534\r
535 # If we're on OSX 10.5 or later and the user tries to\r
536 # compiles an extension using an SDK that is not present\r
537 # on the current machine it is better to not use an SDK\r
538 # than to fail.\r
539 #\r
540 # The major usecase for this is users using a Python.org\r
541 # binary installer on OSX 10.6: that installer uses\r
542 # the 10.4u SDK, but that SDK is not installed by default\r
543 # when you install Xcode.\r
544 #\r
545 m = re.search('-isysroot\s+(\S+)', _config_vars['CFLAGS'])\r
546 if m is not None:\r
547 sdk = m.group(1)\r
548 if not os.path.exists(sdk):\r
549 for key in ('LDFLAGS', 'BASECFLAGS', 'LDSHARED',\r
550 # a number of derived variables. These need to be\r
551 # patched up as well.\r
552 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):\r
553\r
554 flags = _config_vars[key]\r
555 flags = re.sub('-isysroot\s+\S+(\s|$)', ' ', flags)\r
556 _config_vars[key] = flags\r
557\r
558 if args:\r
559 vals = []\r
560 for name in args:\r
561 vals.append(_config_vars.get(name))\r
562 return vals\r
563 else:\r
564 return _config_vars\r
565\r
566def get_config_var(name):\r
567 """Return the value of a single variable using the dictionary\r
568 returned by 'get_config_vars()'. Equivalent to\r
569 get_config_vars().get(name)\r
570 """\r
571 return get_config_vars().get(name)\r