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