2017-11-08 16:19:53 -05:00
|
|
|
"""Cython-based Python Module"""
|
2021-04-15 11:52:16 -04:00
|
|
|
import re
|
|
|
|
|
from os.path import join as pjoin
|
|
|
|
|
from os.path import normpath
|
|
|
|
|
from pkg_resources import parse_version
|
2012-09-06 19:58:03 +00:00
|
|
|
from buildutils import *
|
|
|
|
|
|
|
|
|
|
Import('env', 'build', 'install')
|
|
|
|
|
|
|
|
|
|
localenv = env.Clone()
|
|
|
|
|
|
2018-09-09 22:44:46 -04:00
|
|
|
cythonized = localenv.Command(
|
|
|
|
|
'cantera/_cantera.cpp',
|
|
|
|
|
'cantera/_cantera.pyx',
|
|
|
|
|
'''${python_cmd} -c "import Cython.Build; Cython.Build.cythonize('${SOURCE}')"''')
|
2014-01-31 23:15:17 +00:00
|
|
|
|
|
|
|
|
for f in mglob(localenv, 'cantera', 'pyx', 'pxd'):
|
|
|
|
|
localenv.Depends(cythonized, f)
|
|
|
|
|
|
2014-10-29 15:36:28 +00:00
|
|
|
for line in open('cantera/_cantera.pxd'):
|
|
|
|
|
m = re.search(r'from "(cantera.*?)"', line)
|
|
|
|
|
if m:
|
|
|
|
|
localenv.Depends('cantera/_cantera.cpp', '#include/' + m.group(1))
|
|
|
|
|
|
2012-09-06 19:58:03 +00:00
|
|
|
dataFiles = localenv.RecursiveInstall('#interfaces/cython/cantera/data',
|
2012-09-06 19:58:32 +00:00
|
|
|
'#build/data')
|
2012-09-06 19:58:03 +00:00
|
|
|
build(dataFiles)
|
|
|
|
|
|
2012-09-06 19:58:32 +00:00
|
|
|
testFiles = localenv.RecursiveInstall('#interfaces/cython/cantera/test/data',
|
|
|
|
|
'#test/data')
|
|
|
|
|
build(testFiles)
|
|
|
|
|
|
2018-09-09 22:19:34 -04:00
|
|
|
# Get information needed to build the Python module
|
|
|
|
|
script = '\n'.join(("from distutils.sysconfig import *",
|
|
|
|
|
"import numpy",
|
|
|
|
|
"print(get_config_var('EXT_SUFFIX') or get_config_var('SO'))",
|
|
|
|
|
"print(get_config_var('INCLUDEPY'))",
|
|
|
|
|
"print(get_config_var('LDLIBRARY'))",
|
|
|
|
|
"print(get_config_var('prefix'))",
|
|
|
|
|
"print(get_python_version())",
|
|
|
|
|
"print(numpy.get_include())"))
|
2021-04-13 20:03:30 -04:00
|
|
|
info = get_command_output(localenv["python_cmd"], "-c", script)
|
2018-09-09 22:19:34 -04:00
|
|
|
module_ext, inc, pylib, prefix, py_version, numpy_include = info.splitlines()[-6:]
|
|
|
|
|
localenv.Prepend(CPPPATH=[Dir('#include'), inc, numpy_include])
|
|
|
|
|
localenv.Prepend(LIBS=localenv['cantera_libs'])
|
2021-02-04 17:31:51 -05:00
|
|
|
|
|
|
|
|
# Don't print deprecation warnings for internal Python changes.
|
|
|
|
|
# Only applies to Python 3.8. The field that is deprecated in Python 3.8
|
|
|
|
|
# and causes the warnings to appear will be removed in Python 3.9 so no
|
|
|
|
|
# further warnings should be issued.
|
|
|
|
|
if localenv['HAS_CLANG'] and parse_version(py_version) == parse_version('3.8'):
|
|
|
|
|
localenv.Append(CXXFLAGS='-Wno-deprecated-declarations')
|
|
|
|
|
|
2018-09-09 22:19:34 -04:00
|
|
|
if localenv['OS'] == 'Darwin':
|
|
|
|
|
localenv.Append(LINKFLAGS='-undefined dynamic_lookup')
|
|
|
|
|
elif localenv['OS'] == 'Windows':
|
|
|
|
|
localenv.Append(LIBPATH=prefix+'/libs')
|
|
|
|
|
if localenv['toolchain'] == 'mingw':
|
|
|
|
|
localenv.Append(LIBS='python{}'.format(py_version.replace('.','')))
|
|
|
|
|
if localenv['OS_BITS'] == 64:
|
|
|
|
|
localenv.Append(CPPDEFINES='MS_WIN64')
|
|
|
|
|
# fix for http://bugs.python.org/issue11566
|
|
|
|
|
localenv.Append(CPPDEFINES={'_hypot':'hypot'})
|
|
|
|
|
elif localenv['OS'] == 'Cygwin':
|
|
|
|
|
# extract 'pythonX.Y' from 'libpythonX.Y.dll.a'
|
|
|
|
|
localenv.Append(LIBS=pylib[3:-6])
|
|
|
|
|
|
|
|
|
|
# Build the Python module
|
|
|
|
|
obj = localenv.SharedObject('#build/temp-py/_cantera', 'cantera/_cantera.cpp')
|
|
|
|
|
ext = localenv.LoadableModule('#build/python/cantera/_cantera{}'.format(module_ext),
|
|
|
|
|
obj, LIBPREFIX='', SHLIBSUFFIX=module_ext,
|
|
|
|
|
SHLIBPREFIX='', LIBSUFFIXES=[module_ext])
|
|
|
|
|
localenv['py_extension'] = ext[0].name
|
|
|
|
|
|
|
|
|
|
localenv.SubstFile('setup.py', 'setup.py.in')
|
|
|
|
|
build_cmd = ('cd interfaces/cython &&'
|
|
|
|
|
' $python_cmd_esc setup.py build --build-lib=../../build/python')
|
|
|
|
|
mod = build(localenv.Command('#build/python/cantera/__init__.py', 'setup.py',
|
|
|
|
|
build_cmd))
|
|
|
|
|
env['python_module'] = mod
|
|
|
|
|
env['python_extension'] = ext
|
|
|
|
|
|
|
|
|
|
localenv.Depends(mod, ext)
|
|
|
|
|
localenv.Depends(mod, dataFiles + testFiles)
|
|
|
|
|
localenv.Depends(ext, localenv['cantera_staticlib'])
|
|
|
|
|
|
|
|
|
|
for f in (mglob(localenv, 'cantera', 'py') +
|
|
|
|
|
mglob(localenv, 'cantera/test', 'py') +
|
|
|
|
|
mglob(localenv, 'cantera/examples/kinetics', 'py') +
|
2020-07-04 12:43:22 -05:00
|
|
|
mglob(localenv, 'cantera/examples/multiphase', 'py') +
|
2018-09-09 22:19:34 -04:00
|
|
|
mglob(localenv, 'cantera/examples/onedim', 'py') +
|
2020-07-04 12:43:22 -05:00
|
|
|
mglob(localenv, 'cantera/examples/reactors', 'py') +
|
2018-09-09 22:19:34 -04:00
|
|
|
mglob(localenv, 'cantera/examples/surface_chemistry', 'py') +
|
2020-07-04 12:43:22 -05:00
|
|
|
mglob(localenv, 'cantera/examples/thermo', 'py') +
|
|
|
|
|
mglob(localenv, 'cantera/examples/transport', 'py')):
|
2018-09-09 22:19:34 -04:00
|
|
|
localenv.Depends(mod, f)
|
|
|
|
|
|
|
|
|
|
# Determine installation path and install the Python module
|
|
|
|
|
if localenv['python_prefix'] == 'USER':
|
|
|
|
|
# Install to the OS-dependent user site-packages directory
|
|
|
|
|
extra = '--user'
|
|
|
|
|
if localenv['OS'] == 'Darwin':
|
|
|
|
|
extra += ' --prefix=""'
|
|
|
|
|
elif localenv['python_prefix']:
|
|
|
|
|
# A specific location for the Cantera python module has been given
|
|
|
|
|
if localenv['debian'] and localenv.subst('${python_prefix}') == '/usr/local':
|
|
|
|
|
# Installation to /usr/local is the default on Debian-based distributions
|
|
|
|
|
extra = ''
|
|
|
|
|
elif localenv['OS'] == 'Darwin':
|
|
|
|
|
extra = localenv.subst(' --prefix=${python_prefix}')
|
2019-07-25 23:55:21 +03:00
|
|
|
elif localenv['libdirname'] != 'lib':
|
|
|
|
|
# 64-bit RHEL / Fedora etc. or e.g. x32 Gentoo profile
|
2018-09-09 22:19:34 -04:00
|
|
|
extra = localenv.subst(
|
2020-03-22 03:09:15 +03:00
|
|
|
' --prefix=${{python_prefix}}'
|
2019-07-25 23:55:21 +03:00
|
|
|
' --install-lib=${{python_prefix}}/${{libdirname}}/python{}/site-packages'.format(py_version))
|
2018-09-09 22:19:34 -04:00
|
|
|
else:
|
|
|
|
|
extra = '--user'
|
|
|
|
|
localenv.AppendENVPath(
|
|
|
|
|
'PYTHONUSERBASE',
|
|
|
|
|
normpath(localenv.subst('$python_prefix')))
|
|
|
|
|
else:
|
|
|
|
|
# Install Python module in the default location
|
|
|
|
|
extra = ''
|
|
|
|
|
|
|
|
|
|
env['python_module_loc'] = '<unspecified>'
|
|
|
|
|
if localenv['PYTHON_INSTALLER'] == 'direct':
|
|
|
|
|
mod_inst = install(localenv.Command, 'dummy', mod,
|
|
|
|
|
build_cmd + ' install ' + extra +
|
|
|
|
|
' --record=../../build/python-installed-files.txt' +
|
|
|
|
|
' --single-version-externally-managed')
|
|
|
|
|
global_env = env
|
|
|
|
|
def find_module_dir(target, source, env):
|
|
|
|
|
check = pjoin('cantera', '__init__.py')
|
|
|
|
|
for filename in open('build/python-installed-files.txt').readlines():
|
|
|
|
|
filename = filename.strip()
|
|
|
|
|
if filename.endswith(check):
|
|
|
|
|
filename = filename.replace(check, '')
|
|
|
|
|
global_env['python_module_loc'] = normpath(filename)
|
|
|
|
|
break
|
|
|
|
|
localenv.AlwaysBuild(localenv.AddPostAction(mod_inst, find_module_dir))
|
|
|
|
|
env['install_python_action'] = mod_inst
|
|
|
|
|
elif localenv['PYTHON_INSTALLER'] == 'debian':
|
|
|
|
|
extra = localenv.subst(' --root=${python_prefix}')
|
|
|
|
|
install(localenv.Command, 'dummy', mod,
|
|
|
|
|
build_cmd + ' install --install-layout=deb --no-compile ' + extra)
|
|
|
|
|
elif localenv['PYTHON_INSTALLER'] == 'binary':
|
|
|
|
|
install(localenv.Command, 'dummy', mod,
|
|
|
|
|
build_cmd + ' bdist_msi --dist-dir=../..' +
|
|
|
|
|
' --target-version=' + py_version)
|