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> |
| 10 | Initial date: 17-Dec-1998 |
| 11 | """ |
| 12 | |
Greg Ward | 82d71ca | 2000-06-03 00:44:30 +0000 | [diff] [blame] | 13 | __revision__ = "$Id$" |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 14 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 15 | import os |
| 16 | import re |
| 17 | import string |
| 18 | import sys |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 19 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 20 | from errors import DistutilsPlatformError |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 21 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 22 | # 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] | 23 | PREFIX = os.path.normpath(sys.prefix) |
| 24 | EXEC_PREFIX = os.path.normpath(sys.exec_prefix) |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 25 | |
Andrew M. Kuchling | c14fa30 | 2001-01-17 15:16:52 +0000 | [diff] [blame] | 26 | # Boolean; if it's true, we're still building Python, so |
| 27 | # we use different (hard-wired) directories. |
| 28 | |
| 29 | python_build = 0 |
| 30 | |
| 31 | def set_python_build(): |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 32 | """Set the python_build flag to true. |
| 33 | |
| 34 | This means that we're building Python itself. Only called from |
| 35 | the setup.py script shipped with Python. |
Andrew M. Kuchling | c14fa30 | 2001-01-17 15:16:52 +0000 | [diff] [blame] | 36 | """ |
Andrew M. Kuchling | c14fa30 | 2001-01-17 15:16:52 +0000 | [diff] [blame] | 37 | global python_build |
| 38 | python_build = 1 |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 39 | |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 40 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 41 | def get_python_inc(plat_specific=0, prefix=None): |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 42 | """Return the directory containing installed Python header files. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 43 | |
| 44 | If 'plat_specific' is false (the default), this is the path to the |
| 45 | non-platform-specific header files, i.e. Python.h and so on; |
| 46 | otherwise, this is the path to platform-specific header files |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 47 | (namely pyconfig.h). |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 48 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 49 | If 'prefix' is supplied, use it instead of sys.prefix or |
| 50 | sys.exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 51 | """ |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 52 | if prefix is None: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 53 | prefix = plat_specific and EXEC_PREFIX or PREFIX |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 54 | if os.name == "posix": |
Andrew M. Kuchling | c14fa30 | 2001-01-17 15:16:52 +0000 | [diff] [blame] | 55 | if python_build: |
| 56 | return "Include/" |
Greg Ward | cf6bea3 | 2000-04-10 01:15:06 +0000 | [diff] [blame] | 57 | return os.path.join(prefix, "include", "python" + sys.version[:3]) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 58 | elif os.name == "nt": |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 59 | return os.path.join(prefix, "include") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 60 | elif os.name == "mac": |
Greg Ward | cf6bea3 | 2000-04-10 01:15:06 +0000 | [diff] [blame] | 61 | return os.path.join(prefix, "Include") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 62 | else: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 63 | raise DistutilsPlatformError( |
| 64 | "I don't know where Python installs its C header files " |
| 65 | "on platform '%s'" % os.name) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 66 | |
| 67 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 68 | def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 69 | """Return the directory containing the Python library (standard or |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 70 | site additions). |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 71 | |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 72 | If 'plat_specific' is true, return the directory containing |
| 73 | platform-specific modules, i.e. any module from a non-pure-Python |
| 74 | module distribution; otherwise, return the platform-shared library |
| 75 | directory. If 'standard_lib' is true, return the directory |
| 76 | containing standard Python library modules; otherwise, return the |
| 77 | directory for site-specific modules. |
| 78 | |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 79 | If 'prefix' is supplied, use it instead of sys.prefix or |
| 80 | sys.exec_prefix -- i.e., ignore 'plat_specific'. |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 81 | """ |
Greg Ward | d38e6f7 | 2000-04-10 01:17:49 +0000 | [diff] [blame] | 82 | if prefix is None: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 83 | prefix = plat_specific and EXEC_PREFIX or PREFIX |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 84 | |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 85 | if os.name == "posix": |
Greg Ward | cf6bea3 | 2000-04-10 01:15:06 +0000 | [diff] [blame] | 86 | libpython = os.path.join(prefix, |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 87 | "lib", "python" + sys.version[:3]) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 88 | if standard_lib: |
| 89 | return libpython |
| 90 | else: |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 91 | return os.path.join(libpython, "site-packages") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 92 | |
| 93 | elif os.name == "nt": |
| 94 | if standard_lib: |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 95 | return os.path.join(prefix, "Lib") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 96 | else: |
Greg Ward | cf6bea3 | 2000-04-10 01:15:06 +0000 | [diff] [blame] | 97 | return prefix |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 98 | |
| 99 | elif os.name == "mac": |
Greg Ward | dc9fe8a | 2000-08-02 01:49:40 +0000 | [diff] [blame] | 100 | if plat_specific: |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 101 | if standard_lib: |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 102 | return os.path.join(prefix, "Mac", "Plugins") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 103 | else: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 104 | raise DistutilsPlatformError( |
| 105 | "OK, where DO site-specific extensions go on the Mac?") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 106 | else: |
| 107 | if standard_lib: |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 108 | return os.path.join(prefix, "Lib") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 109 | else: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 110 | raise DistutilsPlatformError( |
| 111 | "OK, where DO site-specific modules go on the Mac?") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 112 | else: |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 113 | raise DistutilsPlatformError( |
| 114 | "I don't know where Python installs its library " |
| 115 | "on platform '%s'" % os.name) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 116 | |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 117 | |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 118 | def customize_compiler(compiler): |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 119 | """Do any platform-specific customization of a CCompiler instance. |
| 120 | |
| 121 | Mainly needed on Unix, so we can plug in the information that |
| 122 | varies across Unices and is stored in Python's Makefile. |
Greg Ward | bb7baa7 | 2000-06-25 02:09:14 +0000 | [diff] [blame] | 123 | """ |
| 124 | if compiler.compiler_type == "unix": |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 125 | (cc, opt, ccshared, ldshared, so_ext) = \ |
| 126 | get_config_vars('CC', 'OPT', 'CCSHARED', 'LDSHARED', 'SO') |
Greg Ward | bb7baa7 | 2000-06-25 02:09:14 +0000 | [diff] [blame] | 127 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 128 | cc_cmd = cc + ' ' + opt |
| 129 | compiler.set_executables( |
| 130 | preprocessor=cc + " -E", # not always! |
| 131 | compiler=cc_cmd, |
| 132 | compiler_so=cc_cmd + ' ' + ccshared, |
| 133 | linker_so=ldshared, |
| 134 | linker_exe=cc) |
| 135 | |
| 136 | compiler.shared_lib_extension = so_ext |
Greg Ward | bb7baa7 | 2000-06-25 02:09:14 +0000 | [diff] [blame] | 137 | |
| 138 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 139 | def get_config_h_filename(): |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 140 | """Return full pathname of installed pyconfig.h file.""" |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 141 | if python_build: |
| 142 | inc_dir = os.curdir |
| 143 | else: |
| 144 | inc_dir = get_python_inc(plat_specific=1) |
Marc-André Lemburg | 7cf92fa | 2001-07-26 18:06:58 +0000 | [diff] [blame] | 145 | if sys.version < '2.2': |
| 146 | config_h = 'config.h' |
| 147 | else: |
| 148 | # The name of the config.h file changed in 2.2 |
| 149 | config_h = 'pyconfig.h' |
| 150 | return os.path.join(inc_dir, config_h) |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 151 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 152 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 153 | def get_makefile_filename(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 154 | """Return full pathname of installed Makefile from the Python build.""" |
Andrew M. Kuchling | c14fa30 | 2001-01-17 15:16:52 +0000 | [diff] [blame] | 155 | if python_build: |
Neil Schemenauer | 84d14ba | 2001-01-24 17:17:20 +0000 | [diff] [blame] | 156 | return './Makefile' |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 157 | lib_dir = get_python_lib(plat_specific=1, standard_lib=1) |
| 158 | return os.path.join(lib_dir, "config", "Makefile") |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 159 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 160 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 161 | def parse_config_h(fp, g=None): |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 162 | """Parse a config.h-style file. |
| 163 | |
| 164 | A dictionary containing name/value pairs is returned. If an |
| 165 | optional dictionary is passed in as the second argument, it is |
| 166 | used instead of a new dictionary. |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 167 | """ |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 168 | if g is None: |
| 169 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 170 | define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n") |
| 171 | undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n") |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 172 | # |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 173 | while 1: |
| 174 | line = fp.readline() |
| 175 | if not line: |
| 176 | break |
| 177 | m = define_rx.match(line) |
| 178 | if m: |
| 179 | n, v = m.group(1, 2) |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 180 | try: v = string.atoi(v) |
| 181 | except ValueError: pass |
| 182 | g[n] = v |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 183 | else: |
| 184 | m = undef_rx.match(line) |
| 185 | if m: |
| 186 | g[m.group(1)] = 0 |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 187 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 188 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 189 | |
| 190 | # Regexes needed for parsing Makefile (and similar syntaxes, |
| 191 | # like old-style Setup files). |
| 192 | _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") |
| 193 | _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") |
| 194 | _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") |
| 195 | |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 196 | def parse_makefile(fn, g=None): |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 197 | """Parse a Makefile-style file. |
| 198 | |
| 199 | A dictionary containing name/value pairs is returned. If an |
| 200 | optional dictionary is passed in as the second argument, it is |
| 201 | used instead of a new dictionary. |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 202 | """ |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 203 | from distutils.text_file import TextFile |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 204 | fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1) |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 205 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 206 | if g is None: |
| 207 | g = {} |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 208 | done = {} |
| 209 | notdone = {} |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 210 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 211 | while 1: |
| 212 | line = fp.readline() |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 213 | if line is None: # eof |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 214 | break |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 215 | m = _variable_rx.match(line) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 216 | if m: |
| 217 | n, v = m.group(1, 2) |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 218 | v = string.strip(v) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 219 | if "$" in v: |
| 220 | notdone[n] = v |
| 221 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 222 | try: v = string.atoi(v) |
| 223 | except ValueError: pass |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 224 | done[n] = v |
| 225 | |
| 226 | # do variable interpolation here |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 227 | while notdone: |
| 228 | for name in notdone.keys(): |
| 229 | value = notdone[name] |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 230 | m = _findvar1_rx.search(value) or _findvar2_rx.search(value) |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 231 | if m: |
| 232 | n = m.group(1) |
| 233 | if done.has_key(n): |
| 234 | after = value[m.end():] |
Andrew M. Kuchling | b11bd03 | 2001-01-16 16:33:28 +0000 | [diff] [blame] | 235 | value = value[:m.start()] + str(done[n]) + after |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 236 | if "$" in after: |
| 237 | notdone[name] = value |
| 238 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 239 | try: value = string.atoi(value) |
Andrew M. Kuchling | b11bd03 | 2001-01-16 16:33:28 +0000 | [diff] [blame] | 240 | except ValueError: |
| 241 | done[name] = string.strip(value) |
| 242 | else: |
| 243 | done[name] = value |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 244 | del notdone[name] |
| 245 | elif notdone.has_key(n): |
| 246 | # get it on a subsequent round |
| 247 | pass |
| 248 | else: |
| 249 | done[n] = "" |
| 250 | after = value[m.end():] |
| 251 | value = value[:m.start()] + after |
| 252 | if "$" in after: |
| 253 | notdone[name] = value |
| 254 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 255 | try: value = string.atoi(value) |
Andrew M. Kuchling | b11bd03 | 2001-01-16 16:33:28 +0000 | [diff] [blame] | 256 | except ValueError: |
| 257 | done[name] = string.strip(value) |
| 258 | else: |
| 259 | done[name] = value |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 260 | del notdone[name] |
| 261 | else: |
Greg Ward | 3c8e54b | 1998-12-22 12:42:04 +0000 | [diff] [blame] | 262 | # bogus variable reference; just drop it since we can't deal |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 263 | del notdone[name] |
| 264 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 265 | fp.close() |
| 266 | |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 267 | # save the results in the global dictionary |
| 268 | g.update(done) |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 269 | return g |
Greg Ward | 1190ee3 | 1998-12-18 23:46:33 +0000 | [diff] [blame] | 270 | |
| 271 | |
Greg Ward | d283ce7 | 2000-09-17 00:53:02 +0000 | [diff] [blame] | 272 | def expand_makefile_vars(s, vars): |
| 273 | """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in |
| 274 | 'string' according to 'vars' (a dictionary mapping variable names to |
| 275 | values). Variables not present in 'vars' are silently expanded to the |
| 276 | empty string. The variable values in 'vars' should not contain further |
| 277 | variable expansions; if 'vars' is the output of 'parse_makefile()', |
| 278 | you're fine. Returns a variable-expanded version of 's'. |
| 279 | """ |
| 280 | |
| 281 | # This algorithm does multiple expansion, so if vars['foo'] contains |
| 282 | # "${bar}", it will expand ${foo} to ${bar}, and then expand |
| 283 | # ${bar}... and so forth. This is fine as long as 'vars' comes from |
| 284 | # 'parse_makefile()', which takes care of such expansions eagerly, |
| 285 | # according to make's variable expansion semantics. |
| 286 | |
| 287 | while 1: |
| 288 | m = _findvar1_rx.search(s) or _findvar2_rx.search(s) |
| 289 | if m: |
| 290 | name = m.group(1) |
| 291 | (beg, end) = m.span() |
| 292 | s = s[0:beg] + vars.get(m.group(1)) + s[end:] |
| 293 | else: |
| 294 | break |
| 295 | return s |
| 296 | |
| 297 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 298 | _config_vars = None |
| 299 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 300 | def _init_posix(): |
Fred Drake | 522af3a | 1999-01-06 16:28:34 +0000 | [diff] [blame] | 301 | """Initialize the module as appropriate for POSIX systems.""" |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 302 | g = {} |
Greg Ward | a0ca3f2 | 2000-02-02 00:05:14 +0000 | [diff] [blame] | 303 | # load the installed Makefile: |
Greg Ward | a570c05 | 2000-05-23 23:14:00 +0000 | [diff] [blame] | 304 | try: |
| 305 | filename = get_makefile_filename() |
Greg Ward | 3fff8d2 | 2000-09-15 00:03:13 +0000 | [diff] [blame] | 306 | parse_makefile(filename, g) |
Greg Ward | a570c05 | 2000-05-23 23:14:00 +0000 | [diff] [blame] | 307 | except IOError, msg: |
| 308 | my_msg = "invalid Python installation: unable to open %s" % filename |
| 309 | if hasattr(msg, "strerror"): |
| 310 | my_msg = my_msg + " (%s)" % msg.strerror |
| 311 | |
Fred Drake | 70b014d | 2001-07-18 18:39:56 +0000 | [diff] [blame] | 312 | raise DistutilsPlatformError(my_msg) |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 313 | |
| 314 | |
Greg Ward | 4f88028 | 2000-06-27 01:59:06 +0000 | [diff] [blame] | 315 | # On AIX, there are wrong paths to the linker scripts in the Makefile |
| 316 | # -- these paths are relative to the Python source, but when installed |
| 317 | # the scripts are in another directory. |
Neil Schemenauer | 1a02086 | 2001-02-16 03:31:13 +0000 | [diff] [blame] | 318 | if python_build: |
Andrew M. Kuchling | 6335773 | 2001-02-28 19:40:27 +0000 | [diff] [blame] | 319 | g['LDSHARED'] = g['BLDSHARED'] |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 320 | |
| 321 | global _config_vars |
| 322 | _config_vars = g |
Greg Ward | 66e966f | 2000-09-01 01:23:26 +0000 | [diff] [blame] | 323 | |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 324 | |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 325 | def _init_nt(): |
| 326 | """Initialize the module as appropriate for NT""" |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 327 | g = {} |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 328 | # set basic install directories |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 329 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 330 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
Greg Ward | 4d74d73 | 1999-06-08 01:58:36 +0000 | [diff] [blame] | 331 | |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 332 | # XXX hmmm.. a normal install puts include files here |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 333 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
Greg Ward | 32162e8 | 1999-08-29 18:22:13 +0000 | [diff] [blame] | 334 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 335 | g['SO'] = '.pyd' |
Greg Ward | 82d71ca | 2000-06-03 00:44:30 +0000 | [diff] [blame] | 336 | g['EXE'] = ".exe" |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 337 | |
| 338 | global _config_vars |
| 339 | _config_vars = g |
Greg Ward | 82d71ca | 2000-06-03 00:44:30 +0000 | [diff] [blame] | 340 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 341 | |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 342 | def _init_mac(): |
| 343 | """Initialize the module as appropriate for Macintosh systems""" |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 344 | g = {} |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 345 | # set basic install directories |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 346 | g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1) |
| 347 | g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1) |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 348 | |
| 349 | # XXX hmmm.. a normal install puts include files here |
Fred Drake | c1ee39a | 2000-03-09 15:54:52 +0000 | [diff] [blame] | 350 | g['INCLUDEPY'] = get_python_inc(plat_specific=0) |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 351 | |
Jack Jansen | dd13a20 | 2001-05-17 12:52:01 +0000 | [diff] [blame] | 352 | import MacOS |
| 353 | if not hasattr(MacOS, 'runtimemodel'): |
Guido van Rossum | 99f9baa | 2001-05-17 15:03:14 +0000 | [diff] [blame] | 354 | g['SO'] = '.ppc.slb' |
Jack Jansen | dd13a20 | 2001-05-17 12:52:01 +0000 | [diff] [blame] | 355 | else: |
| 356 | g['SO'] = '.%s.slb' % MacOS.runtimemodel |
Greg Ward | 7d73b9e | 2000-03-09 03:16:05 +0000 | [diff] [blame] | 357 | |
| 358 | # XXX are these used anywhere? |
Greg Ward | cf6bea3 | 2000-04-10 01:15:06 +0000 | [diff] [blame] | 359 | g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib") |
| 360 | g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib") |
Greg Ward | 0eff87a | 2000-03-07 03:30:09 +0000 | [diff] [blame] | 361 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 362 | global _config_vars |
| 363 | _config_vars = g |
Greg Ward | 9ddaaa1 | 1999-01-06 14:46:06 +0000 | [diff] [blame] | 364 | |
Fred Drake | 69e2c6e | 2000-02-08 15:55:42 +0000 | [diff] [blame] | 365 | |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 366 | def get_config_vars(*args): |
| 367 | """With no arguments, return a dictionary of all configuration |
| 368 | variables relevant for the current platform. Generally this includes |
| 369 | everything needed to build extensions and install both pure modules and |
| 370 | extensions. On Unix, this means every variable defined in Python's |
| 371 | installed Makefile; on Windows and Mac OS it's a much smaller set. |
| 372 | |
| 373 | With arguments, return a list of values that result from looking up |
| 374 | each argument in the configuration variable dictionary. |
| 375 | """ |
| 376 | global _config_vars |
| 377 | if _config_vars is None: |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 378 | func = globals().get("_init_" + os.name) |
| 379 | if func: |
| 380 | func() |
| 381 | else: |
| 382 | _config_vars = {} |
| 383 | |
| 384 | # Normalized versions of prefix and exec_prefix are handy to have; |
| 385 | # in fact, these are the standard versions used most places in the |
| 386 | # Distutils. |
| 387 | _config_vars['prefix'] = PREFIX |
| 388 | _config_vars['exec_prefix'] = EXEC_PREFIX |
| 389 | |
| 390 | if args: |
| 391 | vals = [] |
| 392 | for name in args: |
| 393 | vals.append(_config_vars.get(name)) |
| 394 | return vals |
| 395 | else: |
| 396 | return _config_vars |
| 397 | |
| 398 | def get_config_var(name): |
| 399 | """Return the value of a single variable using the dictionary |
| 400 | returned by 'get_config_vars()'. Equivalent to |
Fred Drake | c916cdc | 2001-08-02 20:03:12 +0000 | [diff] [blame^] | 401 | get_config_vars().get(name) |
Greg Ward | 879f0f1 | 2000-09-15 01:15:08 +0000 | [diff] [blame] | 402 | """ |
| 403 | return get_config_vars().get(name) |