"""Provide access to Python's configuration information. The specific
configuration variables available depend heavily on the platform and
configuration. The values may be retrieved using
get_config_var(name), and the list of variables is available via
get_config_vars().keys(). Additional convenience functions are also
Written by: Fred L. Drake, Jr.
__revision__
= "$Id: sysconfig.py,v 1.61.2.1 2005/01/06 23:16:03 jackjansen Exp $"
from errors
import DistutilsPlatformError
# These are needed in a couple of spots, so just compute them once.
PREFIX
= os
.path
.normpath(sys
.prefix
)
EXEC_PREFIX
= os
.path
.normpath(sys
.exec_prefix
)
# python_build: (Boolean) if true, we're either building Python or
# building an extension with an un-installed Python, so we use
# different (hard-wired) directories.
argv0_path
= os
.path
.dirname(os
.path
.abspath(sys
.executable
))
landmark
= os
.path
.join(argv0_path
, "Modules", "Setup")
python_build
= os
.path
.isfile(landmark
)
def get_python_version ():
"""Return a string containing the major and minor Python version,
leaving off the patchlevel. Sample return values could be '1.5'
def get_python_inc(plat_specific
=0, prefix
=None):
"""Return the directory containing installed Python header files.
If 'plat_specific' is false (the default), this is the path to the
non-platform-specific header files, i.e. Python.h and so on;
otherwise, this is the path to platform-specific header files
If 'prefix' is supplied, use it instead of sys.prefix or
sys.exec_prefix -- i.e., ignore 'plat_specific'.
prefix
= plat_specific
and EXEC_PREFIX
or PREFIX
base
= os
.path
.dirname(os
.path
.abspath(sys
.executable
))
inc_dir
= os
.path
.join(base
, "Include")
if not os
.path
.exists(inc_dir
):
inc_dir
= os
.path
.join(os
.path
.dirname(base
), "Include")
return os
.path
.join(prefix
, "include", "python" + sys
.version
[:3])
return os
.path
.join(prefix
, "include")
return os
.path
.join(prefix
, "Mac", "Include")
return os
.path
.join(prefix
, "Include")
return os
.path
.join(prefix
, "Include")
raise DistutilsPlatformError(
"I don't know where Python installs its C header files "
"on platform '%s'" % os
.name
)
def get_python_lib(plat_specific
=0, standard_lib
=0, prefix
=None):
"""Return the directory containing the Python library (standard or
If 'plat_specific' is true, return the directory containing
platform-specific modules, i.e. any module from a non-pure-Python
module distribution; otherwise, return the platform-shared library
directory. If 'standard_lib' is true, return the directory
containing standard Python library modules; otherwise, return the
directory for site-specific modules.
If 'prefix' is supplied, use it instead of sys.prefix or
sys.exec_prefix -- i.e., ignore 'plat_specific'.
prefix
= plat_specific
and EXEC_PREFIX
or PREFIX
libpython
= os
.path
.join(prefix
,
"lib", "python" + get_python_version())
return os
.path
.join(libpython
, "site-packages")
return os
.path
.join(prefix
, "Lib")
return os
.path
.join(PREFIX
, "Lib", "site-packages")
return os
.path
.join(prefix
, "Lib", "lib-dynload")
return os
.path
.join(prefix
, "Lib", "site-packages")
return os
.path
.join(prefix
, "Lib")
return os
.path
.join(prefix
, "Lib", "site-packages")
return os
.path
.join(PREFIX
, "Lib")
return os
.path
.join(PREFIX
, "Lib", "site-packages")
raise DistutilsPlatformError(
"I don't know where Python installs its library "
"on platform '%s'" % os
.name
)
def customize_compiler(compiler
):
"""Do any platform-specific customization of a CCompiler instance.
Mainly needed on Unix, so we can plug in the information that
varies across Unices and is stored in Python's Makefile.
if compiler
.compiler_type
== "unix":
(cc
, cxx
, opt
, basecflags
, ccshared
, ldshared
, so_ext
) = \
get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS', 'CCSHARED', 'LDSHARED', 'SO')
if os
.environ
.has_key('CC'):
if os
.environ
.has_key('CXX'):
if os
.environ
.has_key('LDSHARED'):
ldshared
= os
.environ
['LDSHARED']
if os
.environ
.has_key('CPP'):
cpp
= cc
+ " -E" # not always
if os
.environ
.has_key('LDFLAGS'):
ldshared
= ldshared
+ ' ' + os
.environ
['LDFLAGS']
opt
= basecflags
+ ' ' + opt
if os
.environ
.has_key('CFLAGS'):
opt
= opt
+ ' ' + os
.environ
['CFLAGS']
ldshared
= ldshared
+ ' ' + os
.environ
['CFLAGS']
if os
.environ
.has_key('CPPFLAGS'):
cpp
= cpp
+ ' ' + os
.environ
['CPPFLAGS']
opt
= opt
+ ' ' + os
.environ
['CPPFLAGS']
ldshared
= ldshared
+ ' ' + os
.environ
['CPPFLAGS']
compiler
.set_executables(
compiler_so
=cc_cmd
+ ' ' + ccshared
,
compiler
.shared_lib_extension
= so_ext
def get_config_h_filename():
"""Return full pathname of installed pyconfig.h file."""
inc_dir
= get_python_inc(plat_specific
=1)
# The name of the config.h file changed in 2.2
return os
.path
.join(inc_dir
, config_h
)
def get_makefile_filename():
"""Return full pathname of installed Makefile from the Python build."""
return os
.path
.join(os
.path
.dirname(sys
.executable
), "Makefile")
lib_dir
= get_python_lib(plat_specific
=1, standard_lib
=1)
return os
.path
.join(lib_dir
, "config", "Makefile")
def parse_config_h(fp
, g
=None):
"""Parse a config.h-style file.
A dictionary containing name/value pairs is returned. If an
optional dictionary is passed in as the second argument, it is
used instead of a new dictionary.
define_rx
= re
.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
undef_rx
= re
.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
m
= define_rx
.match(line
)
# Regexes needed for parsing Makefile (and similar syntaxes,
# like old-style Setup files).
_variable_rx
= re
.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
_findvar1_rx
= re
.compile(r
"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
_findvar2_rx
= re
.compile(r
"\${([A-Za-z][A-Za-z0-9_]*)}")
def parse_makefile(fn
, g
=None):
"""Parse a Makefile-style file.
A dictionary containing name/value pairs is returned. If an
optional dictionary is passed in as the second argument, it is
used instead of a new dictionary.
from distutils
.text_file
import TextFile
fp
= TextFile(fn
, strip_comments
=1, skip_blanks
=1, join_lines
=1)
m
= _variable_rx
.match(line
)
# do variable interpolation here
for name
in notdone
.keys():
m
= _findvar1_rx
.search(value
) or _findvar2_rx
.search(value
)
value
= value
[:m
.start()] + str(done
[n
]) + after
done
[name
] = string
.strip(value
)
# get it on a subsequent round
value
= value
[:m
.start()] + after
done
[name
] = string
.strip(value
)
# bogus variable reference; just drop it since we can't deal
# save the results in the global dictionary
def expand_makefile_vars(s
, vars):
"""Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
'string' according to 'vars' (a dictionary mapping variable names to
values). Variables not present in 'vars' are silently expanded to the
empty string. The variable values in 'vars' should not contain further
variable expansions; if 'vars' is the output of 'parse_makefile()',
you're fine. Returns a variable-expanded version of 's'.
# This algorithm does multiple expansion, so if vars['foo'] contains
# "${bar}", it will expand ${foo} to ${bar}, and then expand
# ${bar}... and so forth. This is fine as long as 'vars' comes from
# 'parse_makefile()', which takes care of such expansions eagerly,
# according to make's variable expansion semantics.
m
= _findvar1_rx
.search(s
) or _findvar2_rx
.search(s
)
s
= s
[0:beg
] + vars.get(m
.group(1)) + s
[end
:]
"""Initialize the module as appropriate for POSIX systems."""
# load the installed Makefile:
filename
= get_makefile_filename()
parse_makefile(filename
, g
)
my_msg
= "invalid Python installation: unable to open %s" % filename
if hasattr(msg
, "strerror"):
my_msg
= my_msg
+ " (%s)" % msg
.strerror
raise DistutilsPlatformError(my_msg
)
# On MacOSX we need to check the setting of the environment variable
# MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
# it needs to be compatible.
# If it isn't set we set it to the configure-time value
if sys
.platform
== 'darwin' and g
.has_key('CONFIGURE_MACOSX_DEPLOYMENT_TARGET'):
cfg_target
= g
['CONFIGURE_MACOSX_DEPLOYMENT_TARGET']
cur_target
= os
.getenv('MACOSX_DEPLOYMENT_TARGET', '')
os
.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target
)
if cfg_target
!= cur_target
:
my_msg
= ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
% (cur_target
, cfg_target
))
raise DistutilsPlatformError(my_msg
)
# On AIX, there are wrong paths to the linker scripts in the Makefile
# -- these paths are relative to the Python source, but when installed
# the scripts are in another directory.
g
['LDSHARED'] = g
['BLDSHARED']
elif sys
.version
< '2.1':
# The following two branches are for 1.5.2 compatibility.
if sys
.platform
== 'aix4': # what about AIX 3.x ?
# Linker script is in the config directory, not in Modules as the
python_lib
= get_python_lib(standard_lib
=1)
ld_so_aix
= os
.path
.join(python_lib
, 'config', 'ld_so_aix')
python_exp
= os
.path
.join(python_lib
, 'config', 'python.exp')
g
['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix
, g
['CC'], python_exp
)
elif sys
.platform
== 'beos':
# Linker script is in the config directory. In the Makefile it is
# relative to the srcdir, which after installation no longer makes
python_lib
= get_python_lib(standard_lib
=1)
linkerscript_path
= string
.split(g
['LDSHARED'])[0]
linkerscript_name
= os
.path
.basename(linkerscript_path
)
linkerscript
= os
.path
.join(python_lib
, 'config',
# XXX this isn't the right place to do this: adding the Python
# library to the link, if needed, should be in the "build_ext"
# command. (It's also needed for non-MS compilers on Windows, and
# it's taken care of for them by the 'build_ext.get_libraries()'
g
['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
(linkerscript
, PREFIX
, sys
.version
[0:3]))
"""Initialize the module as appropriate for NT"""
# set basic install directories
g
['LIBDEST'] = get_python_lib(plat_specific
=0, standard_lib
=1)
g
['BINLIBDEST'] = get_python_lib(plat_specific
=1, standard_lib
=1)
# XXX hmmm.. a normal install puts include files here
g
['INCLUDEPY'] = get_python_inc(plat_specific
=0)
"""Initialize the module as appropriate for Macintosh systems"""
# set basic install directories
g
['LIBDEST'] = get_python_lib(plat_specific
=0, standard_lib
=1)
g
['BINLIBDEST'] = get_python_lib(plat_specific
=1, standard_lib
=1)
# XXX hmmm.. a normal install puts include files here
g
['INCLUDEPY'] = get_python_inc(plat_specific
=0)
if not hasattr(MacOS
, 'runtimemodel'):
g
['SO'] = '.%s.slb' % MacOS
.runtimemodel
# XXX are these used anywhere?
g
['install_lib'] = os
.path
.join(EXEC_PREFIX
, "Lib")
g
['install_platlib'] = os
.path
.join(EXEC_PREFIX
, "Mac", "Lib")
# These are used by the extension module build
"""Initialize the module as appropriate for OS/2"""
# set basic install directories
g
['LIBDEST'] = get_python_lib(plat_specific
=0, standard_lib
=1)
g
['BINLIBDEST'] = get_python_lib(plat_specific
=1, standard_lib
=1)
# XXX hmmm.. a normal install puts include files here
g
['INCLUDEPY'] = get_python_inc(plat_specific
=0)
def get_config_vars(*args
):
"""With no arguments, return a dictionary of all configuration
variables relevant for the current platform. Generally this includes
everything needed to build extensions and install both pure modules and
extensions. On Unix, this means every variable defined in Python's
installed Makefile; on Windows and Mac OS it's a much smaller set.
With arguments, return a list of values that result from looking up
each argument in the configuration variable dictionary.
func
= globals().get("_init_" + os
.name
)
# Normalized versions of prefix and exec_prefix are handy to have;
# in fact, these are the standard versions used most places in the
_config_vars
['prefix'] = PREFIX
_config_vars
['exec_prefix'] = EXEC_PREFIX
vals
.append(_config_vars
.get(name
))
def get_config_var(name
):
"""Return the value of a single variable using the dictionary
returned by 'get_config_vars()'. Equivalent to
get_config_vars().get(name)
return get_config_vars().get(name
)