Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 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. |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 7 | |
| 8 | Written by: Fred L. Drake, Jr. |
| 9 | Email: <fdrake@acm.org> |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 10 | """ |
| 11 | |
Greg Ward | 82d71ca | 2000-06-03 00:44:30 +0000 | [diff] [blame] | 12 | __revision__ = "$Id$" |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 63236cf | 2007-05-25 18:39:29 +0000 | [diff] [blame] | 14 | import io |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 15 | import os |
| 16 | import re |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 17 | import sys |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 19 | from .errors import DistutilsPlatformError |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 20 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 21 | # These are needed in a couple of spots, so just compute them once. |
Greg Ward | cf6bea3 | 2000-04-10 01:15:06 +0000 | [diff] [blame] | 22 | PREFIX = os.path.normpath(sys.prefix) |
| 23 | EXEC_PREFIX = os.path.normpath(sys.exec_prefix) |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 24 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 25 | # Path to the base directory of the project. On Windows the binary may |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 26 | # live in project/PCBuild9. If we're dealing with an x64 Windows build, |
| 27 | # it'll live in project/PCbuild/amd64. |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 28 | project_base = os.path.dirname(os.path.abspath(sys.executable)) |
| 29 | if os.name == "nt" and "pcbuild" in project_base[-8:].lower(): |
| 30 | project_base = os.path.abspath(os.path.join(project_base, os.path.pardir)) |
Christian Heimes | d9a4d1d | 2008-01-01 14:42:15 +0000 | [diff] [blame] | 31 | # PC/VS7.1 |
| 32 | if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower(): |
| 33 | project_base = os.path.abspath(os.path.join(project_base, os.path.pardir, |
| 34 | os.path.pardir)) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 35 | # PC/AMD64 |
| 36 | if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower(): |
| 37 | project_base = os.path.abspath(os.path.join(project_base, os.path.pardir, |
| 38 | os.path.pardir)) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 39 | |
Fred Drake | 16c8d70 | 2002-06-04 15:28:21 +0000 | [diff] [blame] | 40 | # python_build: (Boolean) if true, we're either building Python or |
| 41 | # building an extension with an un-installed Python, so we use |
| 42 | # different (hard-wired) directories. |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 43 | # Setup.local is available for Makefile builds including VPATH builds, |
| 44 | # Setup.dist is available on Windows |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 45 | def _python_build(): |
| 46 | for fn in ("Setup.dist", "Setup.local"): |
| 47 | if os.path.isfile(os.path.join(project_base, "Modules", fn)): |
| 48 | return True |
| 49 | return False |
| 50 | python_build = _python_build() |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 51 | |
Martin v. Löwis | df37c8c | 2005-03-03 11:08:03 +0000 | [diff] [blame] | 52 | def get_python_version(): |
Andrew M. Kuchling | 0ff98b9 | 2002-11-14 01:43:00 +0000 | [diff] [blame] | 53 | """Return a string containing the major and minor Python version, |
| 54 | leaving off the patchlevel. Sample return values could be '1.5' |
| 55 | or '2.2'. |
| 56 | """ |
| 57 | return sys.version[:3] |
| 58 | |
| 59 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 60 | def get_python_inc(plat_specific=0, prefix=None): |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 61 | """Return the directory containing installed Python header files. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 62 | |
| 63 | If 'plat_specific' is false (the default), this is the path to the |
| 64 | non-platform-specific header files, i.e. Python.h and so on; |
| 65 | otherwise, this is the path to platform-specific header files |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 66 | (namely pyconfig.h). |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 67 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 68 | If 'prefix' is supplied, use it instead of sys.prefix or |
| 69 | sys.exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 70 | """ |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 71 | if prefix is None: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 72 | prefix = plat_specific and EXEC_PREFIX or PREFIX |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 73 | if os.name == "posix": |
Andrew M. Kuchling | c14fa30 | 2001-01-17 15:16:52 +0000 | [diff] [blame] | 74 | if python_build: |
Fred Drake | 16c8d70 | 2002-06-04 15:28:21 +0000 | [diff] [blame] | 75 | base = os.path.dirname(os.path.abspath(sys.executable)) |
| 76 | if plat_specific: |
| 77 | inc_dir = base |
| 78 | else: |
| 79 | inc_dir = os.path.join(base, "Include") |
| 80 | if not os.path.exists(inc_dir): |
| 81 | inc_dir = os.path.join(os.path.dirname(base), "Include") |
| 82 | return inc_dir |
Martin v. Löwis | df37c8c | 2005-03-03 11:08:03 +0000 | [diff] [blame] | 83 | return os.path.join(prefix, "include", "python" + get_python_version()) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 84 | elif os.name == "nt": |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 85 | return os.path.join(prefix, "include") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 86 | elif os.name == "mac": |
Neal Norwitz | 80a3e0a | 2002-06-26 22:05:33 +0000 | [diff] [blame] | 87 | if plat_specific: |
Martin v. Löwis | 23b44a3 | 2003-10-24 20:09:23 +0000 | [diff] [blame] | 88 | return os.path.join(prefix, "Mac", "Include") |
Neal Norwitz | 80a3e0a | 2002-06-26 22:05:33 +0000 | [diff] [blame] | 89 | else: |
Martin v. Löwis | 23b44a3 | 2003-10-24 20:09:23 +0000 | [diff] [blame] | 90 | return os.path.join(prefix, "Include") |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 91 | elif os.name == "os2": |
| 92 | return os.path.join(prefix, "Include") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 93 | else: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 94 | raise DistutilsPlatformError( |
| 95 | "I don't know where Python installs its C header files " |
| 96 | "on platform '%s'" % os.name) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 97 | |
| 98 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 99 | def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 100 | """Return the directory containing the Python library (standard or |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 101 | site additions). |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 102 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 103 | If 'plat_specific' is true, return the directory containing |
| 104 | platform-specific modules, i.e. any module from a non-pure-Python |
| 105 | module distribution; otherwise, return the platform-shared library |
| 106 | directory. If 'standard_lib' is true, return the directory |
| 107 | containing standard Python library modules; otherwise, return the |
| 108 | directory for site-specific modules. |
| 109 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 110 | If 'prefix' is supplied, use it instead of sys.prefix or |
| 111 | sys.exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 112 | """ |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 113 | if prefix is None: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 114 | prefix = plat_specific and EXEC_PREFIX or PREFIX |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 115 | |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 116 | if os.name == "posix": |
Greg Ward | cf6bea3 | 2000-04-10 01:15:06 +0000 | [diff] [blame] | 117 | libpython = os.path.join(prefix, |
Andrew M. Kuchling | 0ff98b9 | 2002-11-14 01:43:00 +0000 | [diff] [blame] | 118 | "lib", "python" + get_python_version()) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 119 | if standard_lib: |
| 120 | return libpython |
| 121 | else: |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 122 | return os.path.join(libpython, "site-packages") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 123 | elif os.name == "nt": |
| 124 | if standard_lib: |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 125 | return os.path.join(prefix, "Lib") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 126 | else: |
Martin v. Löwis | df37c8c | 2005-03-03 11:08:03 +0000 | [diff] [blame] | 127 | if get_python_version() < "2.2": |
Greg Ward | f17efb9 | 2001-08-23 20:53:27 +0000 | [diff] [blame] | 128 | return prefix |
| 129 | else: |
| 130 | return os.path.join(PREFIX, "Lib", "site-packages") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 131 | elif os.name == "mac": |
Greg Ward | dc9fe8a | 2000-08-02 01:49:40 +0000 | [diff] [blame] | 132 | if plat_specific: |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 133 | if standard_lib: |
Jack Jansen | 212a2e1 | 2001-09-04 12:01:49 +0000 | [diff] [blame] | 134 | return os.path.join(prefix, "Lib", "lib-dynload") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 135 | else: |
Jack Jansen | 212a2e1 | 2001-09-04 12:01:49 +0000 | [diff] [blame] | 136 | return os.path.join(prefix, "Lib", "site-packages") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 137 | else: |
| 138 | if standard_lib: |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 139 | return os.path.join(prefix, "Lib") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 140 | else: |
Jack Jansen | 212a2e1 | 2001-09-04 12:01:49 +0000 | [diff] [blame] | 141 | return os.path.join(prefix, "Lib", "site-packages") |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 142 | elif os.name == "os2": |
| 143 | if standard_lib: |
| 144 | return os.path.join(PREFIX, "Lib") |
| 145 | else: |
| 146 | return os.path.join(PREFIX, "Lib", "site-packages") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 147 | else: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 148 | raise DistutilsPlatformError( |
| 149 | "I don't know where Python installs its library " |
| 150 | "on platform '%s'" % os.name) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 151 | |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 152 | |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 153 | def customize_compiler(compiler): |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 154 | """Do any platform-specific customization of a CCompiler instance. |
| 155 | |
| 156 | Mainly needed on Unix, so we can plug in the information that |
| 157 | varies across Unices and is stored in Python's Makefile. |
Greg Ward | bb7baa7 | 2000-06-25 02:09:14 +0000 | [diff] [blame] | 158 | """ |
| 159 | if compiler.compiler_type == "unix": |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 160 | (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \ |
Tim Peters | fffc4b7 | 2005-05-18 02:18:09 +0000 | [diff] [blame] | 161 | get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS', |
Brett Cannon | 08cd598 | 2005-04-24 22:26:38 +0000 | [diff] [blame] | 162 | 'CCSHARED', 'LDSHARED', 'SO') |
Greg Ward | bb7baa7 | 2000-06-25 02:09:14 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 164 | if 'CC' in os.environ: |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 165 | cc = os.environ['CC'] |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 166 | if 'CXX' in os.environ: |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 167 | cxx = os.environ['CXX'] |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 168 | if 'LDSHARED' in os.environ: |
Anthony Baxter | 22dcf66 | 2004-10-13 15:54:17 +0000 | [diff] [blame] | 169 | ldshared = os.environ['LDSHARED'] |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 170 | if 'CPP' in os.environ: |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 171 | cpp = os.environ['CPP'] |
| 172 | else: |
| 173 | cpp = cc + " -E" # not always |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 174 | if 'LDFLAGS' in os.environ: |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 175 | ldshared = ldshared + ' ' + os.environ['LDFLAGS'] |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 176 | if 'CFLAGS' in os.environ: |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 177 | cflags = opt + ' ' + os.environ['CFLAGS'] |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 178 | ldshared = ldshared + ' ' + os.environ['CFLAGS'] |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 179 | if 'CPPFLAGS' in os.environ: |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 180 | cpp = cpp + ' ' + os.environ['CPPFLAGS'] |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 181 | cflags = cflags + ' ' + os.environ['CPPFLAGS'] |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 182 | ldshared = ldshared + ' ' + os.environ['CPPFLAGS'] |
| 183 | |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 184 | cc_cmd = cc + ' ' + cflags |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 185 | compiler.set_executables( |
Andrew M. Kuchling | 29c8623 | 2002-11-04 19:53:24 +0000 | [diff] [blame] | 186 | preprocessor=cpp, |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 187 | compiler=cc_cmd, |
| 188 | compiler_so=cc_cmd + ' ' + ccshared, |
Gustavo Niemeyer | 6b01685 | 2002-11-05 16:12:02 +0000 | [diff] [blame] | 189 | compiler_cxx=cxx, |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 190 | linker_so=ldshared, |
| 191 | linker_exe=cc) |
| 192 | |
| 193 | compiler.shared_lib_extension = so_ext |
Greg Ward | bb7baa7 | 2000-06-25 02:09:14 +0000 | [diff] [blame] | 194 | |
| 195 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 196 | def get_config_h_filename(): |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 197 | """Return full pathname of installed pyconfig.h file.""" |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 198 | if python_build: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 199 | if os.name == "nt": |
| 200 | inc_dir = os.path.join(project_base, "PC") |
| 201 | else: |
| 202 | inc_dir = project_base |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 203 | else: |
| 204 | inc_dir = get_python_inc(plat_specific=1) |
Martin v. Löwis | df37c8c | 2005-03-03 11:08:03 +0000 | [diff] [blame] | 205 | if get_python_version() < '2.2': |
Marc-André Lemburg | 7cf92fa | 2001-07-26 18:06:58 +0000 | [diff] [blame] | 206 | config_h = 'config.h' |
| 207 | else: |
| 208 | # The name of the config.h file changed in 2.2 |
| 209 | config_h = 'pyconfig.h' |
| 210 | return os.path.join(inc_dir, config_h) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 211 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 212 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 213 | def get_makefile_filename(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 214 | """Return full pathname of installed Makefile from the Python build.""" |
Andrew M. Kuchling | c14fa30 | 2001-01-17 15:16:52 +0000 | [diff] [blame] | 215 | if python_build: |
Fred Drake | 16c8d70 | 2002-06-04 15:28:21 +0000 | [diff] [blame] | 216 | return os.path.join(os.path.dirname(sys.executable), "Makefile") |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 217 | lib_dir = get_python_lib(plat_specific=1, standard_lib=1) |
| 218 | return os.path.join(lib_dir, "config", "Makefile") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 219 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 220 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 221 | def parse_config_h(fp, g=None): |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 222 | """Parse a config.h-style file. |
| 223 | |
| 224 | A dictionary containing name/value pairs is returned. If an |
| 225 | optional dictionary is passed in as the second argument, it is |
| 226 | used instead of a new dictionary. |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 227 | """ |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 228 | if g is None: |
| 229 | g = {} |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 230 | define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") |
| 231 | undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 232 | # |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 233 | while True: |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 234 | line = fp.readline() |
| 235 | if not line: |
| 236 | break |
| 237 | m = define_rx.match(line) |
| 238 | if m: |
| 239 | n, v = m.group(1, 2) |
Jeremy Hylton | a5f4c07 | 2002-11-05 20:11:08 +0000 | [diff] [blame] | 240 | try: v = int(v) |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 241 | except ValueError: pass |
| 242 | g[n] = v |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 243 | else: |
| 244 | m = undef_rx.match(line) |
| 245 | if m: |
| 246 | g[m.group(1)] = 0 |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 247 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 248 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 249 | |
| 250 | # Regexes needed for parsing Makefile (and similar syntaxes, |
| 251 | # like old-style Setup files). |
| 252 | _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") |
| 253 | _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") |
| 254 | _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") |
| 255 | |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 256 | def parse_makefile(fn, g=None): |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 257 | """Parse a Makefile-style file. |
| 258 | |
| 259 | A dictionary containing name/value pairs is returned. If an |
| 260 | optional dictionary is passed in as the second argument, it is |
| 261 | used instead of a new dictionary. |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 262 | """ |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 263 | from distutils.text_file import TextFile |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 264 | fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1) |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 265 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 266 | if g is None: |
| 267 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 268 | done = {} |
| 269 | notdone = {} |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 270 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 271 | while True: |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 272 | line = fp.readline() |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 273 | if line is None: # eof |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 274 | break |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 275 | m = _variable_rx.match(line) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 276 | if m: |
| 277 | n, v = m.group(1, 2) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 278 | v = v.strip() |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 279 | if "$" in v: |
| 280 | notdone[n] = v |
| 281 | else: |
Jeremy Hylton | a5f4c07 | 2002-11-05 20:11:08 +0000 | [diff] [blame] | 282 | try: v = int(v) |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 283 | except ValueError: pass |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 284 | done[n] = v |
| 285 | |
| 286 | # do variable interpolation here |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 287 | while notdone: |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 288 | for name in list(notdone): |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 289 | value = notdone[name] |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 290 | m = _findvar1_rx.search(value) or _findvar2_rx.search(value) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 291 | if m: |
| 292 | n = m.group(1) |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 293 | found = True |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 294 | if n in done: |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 295 | item = str(done[n]) |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 296 | elif n in notdone: |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 297 | # get it on a subsequent round |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 298 | found = False |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 299 | elif n in os.environ: |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 300 | # do it like make: fall back to environment |
| 301 | item = os.environ[n] |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 302 | else: |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 303 | done[n] = item = "" |
| 304 | if found: |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 305 | after = value[m.end():] |
Martin v. Löwis | d7c795e | 2005-04-25 07:14:03 +0000 | [diff] [blame] | 306 | value = value[:m.start()] + item + after |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 307 | if "$" in after: |
| 308 | notdone[name] = value |
| 309 | else: |
Jeremy Hylton | a5f4c07 | 2002-11-05 20:11:08 +0000 | [diff] [blame] | 310 | try: value = int(value) |
Andrew M. Kuchling | b11bd03 | 2001-01-16 16:33:28 +0000 | [diff] [blame] | 311 | except ValueError: |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 312 | done[name] = value.strip() |
Andrew M. Kuchling | b11bd03 | 2001-01-16 16:33:28 +0000 | [diff] [blame] | 313 | else: |
| 314 | done[name] = value |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 315 | del notdone[name] |
| 316 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 317 | # bogus variable reference; just drop it since we can't deal |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 318 | del notdone[name] |
| 319 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 320 | fp.close() |
| 321 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 322 | # save the results in the global dictionary |
| 323 | g.update(done) |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 324 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 325 | |
| 326 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 327 | def expand_makefile_vars(s, vars): |
| 328 | """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in |
| 329 | 'string' according to 'vars' (a dictionary mapping variable names to |
| 330 | values). Variables not present in 'vars' are silently expanded to the |
| 331 | empty string. The variable values in 'vars' should not contain further |
| 332 | variable expansions; if 'vars' is the output of 'parse_makefile()', |
| 333 | you're fine. Returns a variable-expanded version of 's'. |
| 334 | """ |
| 335 | |
| 336 | # This algorithm does multiple expansion, so if vars['foo'] contains |
| 337 | # "${bar}", it will expand ${foo} to ${bar}, and then expand |
| 338 | # ${bar}... and so forth. This is fine as long as 'vars' comes from |
| 339 | # 'parse_makefile()', which takes care of such expansions eagerly, |
| 340 | # according to make's variable expansion semantics. |
| 341 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 342 | while True: |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 343 | m = _findvar1_rx.search(s) or _findvar2_rx.search(s) |
| 344 | if m: |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 345 | (beg, end) = m.span() |
| 346 | s = s[0:beg] + vars.get(m.group(1)) + s[end:] |
| 347 | else: |
| 348 | break |
| 349 | return s |
| 350 | |
| 351 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 352 | _config_vars = None |
| 353 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 354 | def _init_posix(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 355 | """Initialize the module as appropriate for POSIX systems.""" |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 356 | g = {} |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 357 | # load the installed Makefile: |
Greg Ward | a570c05 | 2000-05-23 23:14:00 +0000 | [diff] [blame] | 358 | try: |
| 359 | filename = get_makefile_filename() |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 360 | parse_makefile(filename, g) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 361 | except IOError as msg: |
Greg Ward | a570c05 | 2000-05-23 23:14:00 +0000 | [diff] [blame] | 362 | my_msg = "invalid Python installation: unable to open %s" % filename |
| 363 | if hasattr(msg, "strerror"): |
| 364 | my_msg = my_msg + " (%s)" % msg.strerror |
| 365 | |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 366 | raise DistutilsPlatformError(my_msg) |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 367 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 368 | # load the installed pyconfig.h: |
| 369 | try: |
| 370 | filename = get_config_h_filename() |
Guido van Rossum | 63236cf | 2007-05-25 18:39:29 +0000 | [diff] [blame] | 371 | parse_config_h(io.open(filename), g) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 372 | except IOError as msg: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 373 | my_msg = "invalid Python installation: unable to open %s" % filename |
| 374 | if hasattr(msg, "strerror"): |
| 375 | my_msg = my_msg + " (%s)" % msg.strerror |
| 376 | |
| 377 | raise DistutilsPlatformError(my_msg) |
| 378 | |
Jack Jansen | 6b08a40 | 2004-06-03 12:41:45 +0000 | [diff] [blame] | 379 | # On MacOSX we need to check the setting of the environment variable |
| 380 | # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so |
| 381 | # it needs to be compatible. |
Jack Jansen | be95462 | 2004-12-26 23:07:48 +0000 | [diff] [blame] | 382 | # If it isn't set we set it to the configure-time value |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 383 | if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 384 | cfg_target = g['MACOSX_DEPLOYMENT_TARGET'] |
Jack Jansen | 6b08a40 | 2004-06-03 12:41:45 +0000 | [diff] [blame] | 385 | cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '') |
Jack Jansen | be95462 | 2004-12-26 23:07:48 +0000 | [diff] [blame] | 386 | if cur_target == '': |
| 387 | cur_target = cfg_target |
| 388 | os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 389 | elif [int(x) for x in cfg_target.split('.')] > [int(x) for x in cur_target.split('.')]: |
Jack Jansen | 6b08a40 | 2004-06-03 12:41:45 +0000 | [diff] [blame] | 390 | my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure' |
| 391 | % (cur_target, cfg_target)) |
| 392 | raise DistutilsPlatformError(my_msg) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 393 | |
Greg Ward | 4f88028 | 2000-06-27 01:59:06 +0000 | [diff] [blame] | 394 | # On AIX, there are wrong paths to the linker scripts in the Makefile |
| 395 | # -- these paths are relative to the Python source, but when installed |
| 396 | # the scripts are in another directory. |
Neil Schemenauer | 1a02086 | 2001-02-16 03:31:13 +0000 | [diff] [blame] | 397 | if python_build: |
Andrew M. Kuchling | 6335773 | 2001-02-28 19:40:27 +0000 | [diff] [blame] | 398 | g['LDSHARED'] = g['BLDSHARED'] |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 399 | |
Martin v. Löwis | df37c8c | 2005-03-03 11:08:03 +0000 | [diff] [blame] | 400 | elif get_python_version() < '2.1': |
Andrew M. Kuchling | 045af6f | 2001-09-05 12:02:59 +0000 | [diff] [blame] | 401 | # The following two branches are for 1.5.2 compatibility. |
| 402 | if sys.platform == 'aix4': # what about AIX 3.x ? |
| 403 | # Linker script is in the config directory, not in Modules as the |
| 404 | # Makefile says. |
| 405 | python_lib = get_python_lib(standard_lib=1) |
| 406 | ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix') |
| 407 | python_exp = os.path.join(python_lib, 'config', 'python.exp') |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 408 | |
Andrew M. Kuchling | 045af6f | 2001-09-05 12:02:59 +0000 | [diff] [blame] | 409 | g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp) |
| 410 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 411 | global _config_vars |
| 412 | _config_vars = g |
Greg Ward | 66e966f | 2000-09-01 01:23:26 +0000 | [diff] [blame] | 413 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 414 | |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 415 | def _init_nt(): |
| 416 | """Initialize the module as appropriate for NT""" |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 417 | g = {} |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 418 | # set basic install directories |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 419 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 420 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 421 | |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 422 | # XXX hmmm.. a normal install puts include files here |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 423 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 424 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 425 | g['SO'] = '.pyd' |
Greg Ward | 82d71ca | 2000-06-03 00:44:30 +0000 | [diff] [blame] | 426 | g['EXE'] = ".exe" |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 427 | g['VERSION'] = get_python_version().replace(".", "") |
| 428 | g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable)) |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 429 | |
| 430 | global _config_vars |
| 431 | _config_vars = g |
Greg Ward | 82d71ca | 2000-06-03 00:44:30 +0000 | [diff] [blame] | 432 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 433 | |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 434 | def _init_mac(): |
| 435 | """Initialize the module as appropriate for Macintosh systems""" |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 436 | g = {} |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 437 | # set basic install directories |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 438 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 439 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 440 | |
| 441 | # XXX hmmm.. a normal install puts include files here |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 442 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 443 | |
Jack Jansen | dd13a20 | 2001-05-17 12:52:01 +0000 | [diff] [blame] | 444 | import MacOS |
| 445 | if not hasattr(MacOS, 'runtimemodel'): |
Guido van Rossum | 99f9baa | 2001-05-17 15:03:14 +0000 | [diff] [blame] | 446 | g['SO'] = '.ppc.slb' |
Jack Jansen | dd13a20 | 2001-05-17 12:52:01 +0000 | [diff] [blame] | 447 | else: |
| 448 | g['SO'] = '.%s.slb' % MacOS.runtimemodel |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 449 | |
| 450 | # XXX are these used anywhere? |
Greg Ward | cf6bea3 | 2000-04-10 01:15:06 +0000 | [diff] [blame] | 451 | g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib") |
| 452 | g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib") |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 453 | |
Jack Jansen | ab5320b | 2002-06-26 15:42:49 +0000 | [diff] [blame] | 454 | # These are used by the extension module build |
| 455 | g['srcdir'] = ':' |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 456 | global _config_vars |
| 457 | _config_vars = g |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 458 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 459 | |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 460 | def _init_os2(): |
| 461 | """Initialize the module as appropriate for OS/2""" |
| 462 | g = {} |
| 463 | # set basic install directories |
| 464 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 465 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
| 466 | |
| 467 | # XXX hmmm.. a normal install puts include files here |
| 468 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
| 469 | |
| 470 | g['SO'] = '.pyd' |
| 471 | g['EXE'] = ".exe" |
| 472 | |
| 473 | global _config_vars |
| 474 | _config_vars = g |
| 475 | |
| 476 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 477 | def get_config_vars(*args): |
| 478 | """With no arguments, return a dictionary of all configuration |
| 479 | variables relevant for the current platform. Generally this includes |
| 480 | everything needed to build extensions and install both pure modules and |
| 481 | extensions. On Unix, this means every variable defined in Python's |
| 482 | installed Makefile; on Windows and Mac OS it's a much smaller set. |
| 483 | |
| 484 | With arguments, return a list of values that result from looking up |
| 485 | each argument in the configuration variable dictionary. |
| 486 | """ |
| 487 | global _config_vars |
| 488 | if _config_vars is None: |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 489 | func = globals().get("_init_" + os.name) |
| 490 | if func: |
| 491 | func() |
| 492 | else: |
| 493 | _config_vars = {} |
| 494 | |
| 495 | # Normalized versions of prefix and exec_prefix are handy to have; |
| 496 | # in fact, these are the standard versions used most places in the |
| 497 | # Distutils. |
| 498 | _config_vars['prefix'] = PREFIX |
| 499 | _config_vars['exec_prefix'] = EXEC_PREFIX |
| 500 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 501 | if sys.platform == 'darwin': |
| 502 | kernel_version = os.uname()[2] # Kernel version (8.4.3) |
| 503 | major_version = int(kernel_version.split('.')[0]) |
| 504 | |
| 505 | if major_version < 8: |
| 506 | # On Mac OS X before 10.4, check if -arch and -isysroot |
| 507 | # are in CFLAGS or LDFLAGS and remove them if they are. |
| 508 | # This is needed when building extensions on a 10.3 system |
| 509 | # using a universal build of python. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 510 | for key in ('LDFLAGS', 'BASECFLAGS', |
| 511 | # a number of derived variables. These need to be |
| 512 | # patched up as well. |
| 513 | 'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 514 | flags = _config_vars[key] |
| 515 | flags = re.sub('-arch\s+\w+\s', ' ', flags) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 516 | flags = re.sub('-isysroot [^ \t]*', ' ', flags) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 517 | _config_vars[key] = flags |
| 518 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 519 | if args: |
| 520 | vals = [] |
| 521 | for name in args: |
| 522 | vals.append(_config_vars.get(name)) |
| 523 | return vals |
| 524 | else: |
| 525 | return _config_vars |
| 526 | |
| 527 | def get_config_var(name): |
| 528 | """Return the value of a single variable using the dictionary |
| 529 | returned by 'get_config_vars()'. Equivalent to |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame] | 530 | get_config_vars().get(name) |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 531 | """ |
| 532 | return get_config_vars().get(name) |