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