Skip to content

Commit

Permalink
[Cython] Fix Windows builds
Browse files Browse the repository at this point in the history
For some versions of Python, the DLL module extension included in the
sysconfig module is not correct. In that case, the file extension needs
to be constructed manually.

To make this information easier to get to, the JSON module is used to
parse data returned by sysconfig from the Python which will be building
the module.
  • Loading branch information
bryanwweber authored and speth committed Nov 26, 2021
1 parent 7c5dfa9 commit 7f35015
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
46 changes: 32 additions & 14 deletions interfaces/cython/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from os.path import join as pjoin
from os.path import normpath
from pathlib import Path
from pkg_resources import parse_version
import json
from buildutils import *

Import('env', 'build', 'install')
Expand Down Expand Up @@ -32,37 +33,54 @@ testFiles = localenv.RecursiveInstall('#interfaces/cython/cantera/test/data',
build(testFiles)

# Get information needed to build the Python module
script = '\n'.join(("from sysconfig import *",
"import numpy",
"print(get_config_var('EXT_SUFFIX'))",
"print(get_config_var('INCLUDEPY'))",
"print(get_config_var('LDLIBRARY'))",
"print(get_config_var('prefix'))",
"print(get_python_version())",
"print(numpy.get_include())"))
info = get_command_output(localenv["python_cmd"], "-c", script)
module_ext, inc, pylib, prefix, py_version, numpy_include = info.splitlines()[-6:]
script = """\
from sysconfig import *
import numpy
import json
vars = get_config_vars()
vars["plat"] = get_platform()
vars["numpy_include"] = numpy.get_include()
print(json.dumps(vars))
"""
info = json.loads(get_command_output(localenv["python_cmd"], "-c", script))
module_ext = info["EXT_SUFFIX"]
inc = info["INCLUDEPY"]
pylib = info.get("LDLIBRARY")
prefix = info["prefix"]
py_version_short = parse_version(info["py_version_short"])
py_version_full = parse_version(info["py_version"])
numpy_include = info["numpy_include"]
localenv.Prepend(CPPPATH=[Dir('#include'), inc, numpy_include])
localenv.Prepend(LIBS=localenv['cantera_libs'])

# Fix the module extension for Windows from the sysconfig library.
# See https://github.com/python/cpython/pull/22088 and
# https://bugs.python.org/issue39825
if (
py_version_full < parse_version("3.8.7")
and localenv["OS"] == "Windows"
and module_ext == ".pyd"
):
module_ext = f".cp{info['py_version_nodot']}-{info['plat'].replace('-', '_')}.pyd"

# 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'):
if localenv["HAS_CLANG"] and py_version_short == parse_version("3.8"):
localenv.Append(CXXFLAGS='-Wno-deprecated-declarations')

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('.','')))
localenv.Append(LIBS=f"python{info['py_version_nodot']}")
if localenv['OS_BITS'] == 64:
localenv.Append(CPPDEFINES='MS_WIN64')
# fix for https://bugs.python.org/issue11566. Fixed in 3.7.3 and higher.
# Fix for https://bugs.python.org/issue11566. Fixed in 3.7.3 and higher.
# See https://github.com/python/cpython/pull/11283
if parse_version(py_version) < parse_version("3.7.3"):
if py_version_full < parse_version("3.7.3"):
localenv.Append(CPPDEFINES={"_hypot": "hypot"})
elif localenv['OS'] == 'Cygwin':
# extract 'pythonX.Y' from 'libpythonX.Y.dll.a'
Expand Down
4 changes: 1 addition & 3 deletions interfaces/cython/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

extension = Extension("cantera._cantera", sources=[])

setup(
ext_modules=[extension],
)
setup(ext_modules=[extension])

0 comments on commit 7f35015

Please sign in to comment.