[SCons] Refactored configuration/compilation of the Cython module

This commit is contained in:
Ray Speth 2012-12-26 23:55:48 +00:00
parent dafcc9e024
commit 4991ada305

View file

@ -5,20 +5,75 @@ Import('env', 'build', 'install')
localenv = env.Clone()
def configure_numpy(python_command):
script = '\n'.join(("from distutils.sysconfig import *",
"import numpy",
"print(get_config_var('SO'))",
"print(get_python_version())",
"print(numpy.get_include())"))
info = getCommandOutput(python_command, '-c', script)
module_ext, py_version, numpy_include = info.splitlines()
incDirs = (".", "../../include", numpy_include,
localenv['sundials_include'], localenv['boost_inc_dir'])
localenv['py_include_dirs'] = repr([x for x in incDirs if x])
return module_ext, py_version
def add_dependencies(mod, ext):
localenv.Depends(mod, ext)
localenv.Depends(ext, localenv['cantera_staticlib'])
localenv.Depends(mod, dataFiles + testFiles)
for f in mglob(localenv, 'cantera', 'pyx', 'pxd'):
localenv.Depends(ext, f)
for f in (mglob(localenv, 'cantera/test', 'py') +
mglob(localenv, 'cantera/examples/tutorial', 'py') +
mglob(localenv, 'cantera/examples/equilibrium', 'py') +
mglob(localenv, 'cantera/examples/kinetics', 'py') +
mglob(localenv, 'cantera/examples/transport', 'py') +
mglob(localenv, 'cantera/examples/reactors', 'py') +
mglob(localenv, 'cantera/examples/onedim', 'py') +
mglob(localenv, 'cantera/examples/surface_chemistry', 'py') +
mglob(localenv, 'cantera/examples/misc', 'py')):
localenv.Depends(mod, f)
def install_module(prefix, python_version):
if prefix:
# A specific location for the Cantera python module has been specified
extra = '--prefix="%s"' % prefix
else:
# Install Python module in the default location
extra = ''
major = python_version[0]
dummy = 'dummy' + major
if localenv['PYTHON_INSTALLER'] == 'direct':
install(localenv.Command, dummy, mod,
build_cmd + ' install --record ../../build/python%s-installed-files.txt %s' % (major, extra))
elif localenv['PYTHON_INSTALLER'] == 'debian':
install(localenv.Command, dummy, mod,
build_cmd + ' install --install-layout=deb --no-compile %s' % extra)
elif localenv['PYTHON_INSTALLER'] == 'binary':
install(localenv.Command, dummy, mod,
build_cmd + ' bdist_msi --dist-dir=../..' +
' --target-version=%s' % python_version)
libDirs = ('../../build/lib', localenv['sundials_libdir'],
localenv['boost_lib_dir'])
localenv['py_cantera_libs'] = repr(localenv['cantera_libs'])
localenv['py_libdirs'] = repr([x for x in libDirs if x])
if localenv['CC'] == 'cl':
localenv['py_extra_compiler_args'] = repr(['/EHsc'])
else:
localenv['py_extra_compiler_args'] = repr([])
# Compile the Python module with the same compiler as the rest of Cantera
localenv['py_extra_compiler_args'] = repr([])
if localenv['OS'] == 'Windows':
if env['CC'] == 'cl':
compilerOpt = ' --compiler=msvc'
localenv['py_extra_compiler_args'] = repr(['/EHsc'])
elif env['CC'] == 'gcc':
compilerOpt = ' --compiler=mingw32'
else:
@ -32,31 +87,9 @@ testFiles = localenv.RecursiveInstall('#interfaces/cython/cantera/test/data',
'#test/data')
build(testFiles)
moduleDependencies = (mglob(localenv, 'cantera/test', 'py') +
mglob(localenv, 'cantera/examples/tutorial', 'py') +
mglob(localenv, 'cantera/examples/equilibrium', 'py') +
mglob(localenv, 'cantera/examples/kinetics', 'py') +
mglob(localenv, 'cantera/examples/transport', 'py') +
mglob(localenv, 'cantera/examples/reactors', 'py') +
mglob(localenv, 'cantera/examples/onedim', 'py') +
mglob(localenv, 'cantera/examples/surface_chemistry', 'py') +
mglob(localenv, 'cantera/examples/misc', 'py'))
# Cython module for Python 3.x
if localenv['python3_package'] == 'y':
info = getCommandOutput(localenv['python3_cmd'], '-c',
'\n'.join(("from distutils.sysconfig import *",
"import numpy",
"print(get_config_var('SO'))",
"print(get_python_version())",
"print(numpy.get_include())")))
module_ext, py3_version, numpy_include = info.splitlines()
incDirs = (".", "../../include", numpy_include,
localenv['sundials_include'], localenv['boost_inc_dir'])
localenv['py_include_dirs'] = repr([x for x in incDirs if x])
module_ext, py3_version = configure_numpy(localenv['python3_cmd'])
make_setup = localenv.SubstFile('#interfaces/cython/setup3.py',
'#interfaces/cython/setup.py.in')
build(make_setup)
@ -68,11 +101,9 @@ if localenv['python3_package'] == 'y':
+ compilerOpt)
build_cmd = build_base % 'build'
build_ext_cmd = build_base % 'build_ext'
ext = build(localenv.Command('#build/python3/cantera/_cantera%s' % module_ext,
'setup3.py',
build_ext_cmd))
build_base % 'build_ext'))
mod = build(localenv.Command('#build/python3/cantera/__init__.py',
'setup3.py',
build_cmd))
@ -80,49 +111,13 @@ if localenv['python3_package'] == 'y':
localenv.AddPreAction(ext, Delete('interfaces/cython/cantera/_cantera.cpp'))
localenv.Depends(mod, ext)
localenv.Depends(ext, localenv['cantera_staticlib'])
localenv.Depends(mod, dataFiles + testFiles)
for f in mglob(localenv, 'cantera', 'pyx', 'pxd'):
localenv.Depends(ext, f)
for f in moduleDependencies:
localenv.Depends(mod, f)
# Install the Python module
if localenv['python3_prefix']:
# A specific location for the Cantera python module has been specified
extra = '--prefix="%s"' % localenv['python3_prefix']
else:
# Install Python module in the default location
extra = ''
if localenv['PYTHON_INSTALLER'] == 'direct':
install(localenv.Command, 'dummy3', mod,
build_cmd + ' install --record ../../build/python3-installed-files.txt %s' % extra)
elif localenv['PYTHON_INSTALLER'] == 'debian':
install(localenv.Command, 'dummy3', mod,
build_cmd + ' install --install-layout=deb --no-compile %s' % extra)
elif localenv['PYTHON_INSTALLER'] == 'binary':
install(localenv.Command, 'dummy3', mod,
build_cmd + ' bdist_msi --dist-dir=../..' +
' --target-version=%s' + py3_version)
add_dependencies(mod, ext)
install_module(localenv['python3_prefix'], py3_version)
# Cython module for Python 2.x
if localenv['python_package'] == 'new':
info = getCommandOutput(localenv['python_cmd'], '-c',
'\n'.join(("from distutils.sysconfig import *",
"import numpy",
"print get_config_var('SO')",
"print get_python_version()",
"print numpy.get_include()")))
module_ext, py2_version, numpy_include = info.splitlines()
incDirs = (".", "../../include", numpy_include,
localenv['sundials_include'], localenv['boost_inc_dir'])
localenv['py_include_dirs'] = repr([x for x in incDirs if x])
module_ext, py2_version = configure_numpy(localenv['python_cmd'])
make_setup = localenv.SubstFile('#interfaces/cython/setup2.py',
'#interfaces/cython/setup.py.in')
build(make_setup)
@ -134,11 +129,9 @@ if localenv['python_package'] == 'new':
+ compilerOpt)
build_cmd = build_base % 'build'
build_ext_cmd = build_base % 'build_ext'
ext = build(localenv.Command('#build/python2/cantera/_cantera%s' % module_ext,
'setup2.py',
build_ext_cmd))
build_base % 'build_ext'))
mod = build(localenv.Command('#build/python2/cantera/__init__.py',
'setup2.py',
build_cmd))
@ -147,7 +140,6 @@ if localenv['python_package'] == 'new':
localenv.AddPreAction(ext, Delete('interfaces/cython/cantera/_cantera.cpp'))
# Use 3to2 to convert examples from Python 3 syntax
if env['python_convert_examples']:
def convert_example(target, source, env):
shutil.copyfile(source[0].abspath, target[0].abspath)
@ -167,30 +159,5 @@ if localenv['python_package'] == 'new':
convert_example))
localenv.Depends(a, mod)
localenv.Depends(mod, ext)
localenv.Depends(ext, localenv['cantera_staticlib'])
localenv.Depends(mod, dataFiles + testFiles)
for f in mglob(localenv, 'cantera', 'pyx', 'pxd'):
localenv.Depends(ext, f)
for f in moduleDependencies:
localenv.Depends(mod, f)
# Install the Python module
if localenv['python_prefix']:
# A specific location for the Cantera python module has been specified
extra = '--prefix="%s"' % localenv['python_prefix']
else:
# Install Python module in the default location
extra = ''
if localenv['PYTHON_INSTALLER'] == 'direct':
install(localenv.Command, 'dummy2', mod,
build_cmd + ' install --record ../../build/python2-installed-files.txt %s' % extra)
elif localenv['PYTHON_INSTALLER'] == 'debian':
install(localenv.Command, 'dummy2', mod,
build_cmd + ' install --install-layout=deb --no-compile %s' % extra)
elif localenv['PYTHON_INSTALLER'] == 'binary':
install(localenv.Command, 'dummy2', mod,
build_cmd + ' bdist_msi --dist-dir=../..' +
' --target-version=%s' % py2_version)
add_dependencies(mod, ext)
install_module(localenv['python_prefix'], py2_version)