Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace distutils imports #1557

Merged
merged 6 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions python/amici/custom_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib
from setuptools.command.sdist import sdist
from distutils import log

# typehints
Library = Tuple[str, Dict[str, List[str]]]
Expand All @@ -24,7 +23,7 @@
class AmiciInstall(install):
"""Custom install to handle extra arguments"""

log.debug("running AmiciInstall")
print("running AmiciInstall")

# Passing --no-clibs allows to install the Python-only part of AMICI
user_options = install.user_options + [
Expand Down Expand Up @@ -81,7 +80,7 @@ class AmiciBuildCLib(build_clib):
"""Custom build_clib"""

def run(self):
log.debug("running AmiciBuildCLib")
print("running AmiciBuildCLib")

# Always force recompilation. The way setuptools/distutils check for
# whether sources require recompilation is not reliable and may lead
Expand All @@ -91,7 +90,7 @@ def run(self):
build_clib.run(self)

def build_libraries(self, libraries: List[Library]):
log.debug("running AmiciBuildCLib.build_libraries")
print("running AmiciBuildCLib.build_libraries")

no_clibs = 'develop' in self.distribution.command_obj \
and self.get_finalized_command('develop').no_clibs
Expand Down Expand Up @@ -139,7 +138,7 @@ def finalize_options(self):
develop.finalize_options(self)

def run(self):
log.debug("running AmiciDevelop")
print("running AmiciDevelop")

if not self.no_clibs:
self.get_finalized_command('build_clib').run()
Expand All @@ -156,7 +155,7 @@ def run(self):
Returns:

"""
log.debug("running AmiciInstallLib")
print("running AmiciInstallLib")

if 'ENABLE_AMICI_DEBUGGING' in os.environ \
and os.environ['ENABLE_AMICI_DEBUGGING'] == 'TRUE' \
Expand Down Expand Up @@ -187,7 +186,7 @@ def run(self):
the wheel
"""

log.debug("running AmiciBuildExt")
print("running AmiciBuildExt")

no_clibs = 'develop' in self.distribution.command_obj \
and self.get_finalized_command('develop').no_clibs
Expand Down Expand Up @@ -220,13 +219,13 @@ def run(self):
f"Found unexpected number of files: {libfilenames}"
src = libfilenames[0]
dest = os.path.join(target_dir, os.path.basename(src))
log.info(f"copying {src} -> {dest}")
print(f"copying {src} -> {dest}")
copyfile(src, dest)

swig_outdir = os.path.join(os.path.abspath(build_dir), "amici")
generate_swig_interface_files(swig_outdir=swig_outdir)
swig_py_module_path = os.path.join(swig_outdir, 'amici.py')
log.debug("updating typehints")
print("updating typehints")
fix_typehints(swig_py_module_path, swig_py_module_path)

# Always force recompilation. The way setuptools/distutils check for
Expand All @@ -244,7 +243,7 @@ class AmiciSDist(sdist):
def run(self):
"""Setuptools entry-point"""

log.debug("running AmiciSDist")
print("running AmiciSDist")

save_git_version()

Expand All @@ -266,7 +265,7 @@ def save_git_version():
'--always', '--tags']
subprocess.run(cmd, stdout=f)
except Exception as e:
log.warn(e)
print(e)


def set_compiler_specific_library_options(
Expand Down Expand Up @@ -298,7 +297,7 @@ def set_compiler_specific_library_options(
for field in ['cflags', 'sources', 'macros']:
try:
lib[1][field] += lib[1][f'{field}_{compiler_type}']
log.info(f"Changed {field} for {lib[0]} with {compiler_type} "
print(f"Changed {field} for {lib[0]} with {compiler_type} "
f"to {lib[1][field]}")
except KeyError:
# No compiler-specific options set
Expand All @@ -322,7 +321,7 @@ def set_compiler_specific_extension_options(
new_value = getattr(ext, attr) + \
getattr(ext, f'{attr}_{compiler_type}')
setattr(ext, attr, new_value)
log.info(f"Changed {attr} for {compiler_type} to {new_value}")
print(f"Changed {attr} for {compiler_type} to {new_value}")
except AttributeError:
# No compiler-specific options set
pass
Expand Down
6 changes: 3 additions & 3 deletions python/amici/setup.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def build_extension(self, ext):
# except for Windows, where this seems to be incompatible with
# providing swig files. Not investigated further...
if sys.platform != 'win32':
import distutils.ccompiler
import setuptools._distutils.ccompiler
self.compiler.compile = compile_parallel.__get__(
self.compiler, distutils.ccompiler.CCompiler)
self.compiler, setuptools._distutils.ccompiler.CCompiler)

build_ext.build_extension(self, ext)

Expand Down Expand Up @@ -64,7 +64,7 @@ def get_amici_libs() -> List[str]:


def get_extension() -> Extension:
"""Get distutils extension object for this AMICI model package"""
"""Get setuptools extension object for this AMICI model package"""

cxx_flags = []
linker_flags = []
Expand Down
27 changes: 13 additions & 14 deletions python/amici/setuptools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import shlex
import subprocess

from distutils import log
from .swig import find_swig, get_swig_version

try:
Expand Down Expand Up @@ -143,7 +142,7 @@ def get_hdf5_config() -> PackageInfo:
hdf5_include_dir_found = os.path.isfile(
os.path.join(hdf5_include_dir_hint, 'hdf5.h'))
if hdf5_include_dir_found:
log.info('hdf5.h found in %s' % hdf5_include_dir_hint)
print(f"hdf5.h found in {hdf5_include_dir_hint}")
h5pkgcfg['include_dirs'] = [hdf5_include_dir_hint]
break

Expand All @@ -153,7 +152,7 @@ def get_hdf5_config() -> PackageInfo:
hdf5_library_dir_found = os.path.isfile(
os.path.join(hdf5_library_dir_hint, lib_filename))
if hdf5_library_dir_found:
log.info(f'{lib_filename} found in {hdf5_library_dir_hint}')
print(f'{lib_filename} found in {hdf5_library_dir_hint}')
h5pkgcfg['library_dirs'] = [hdf5_library_dir_hint]
break
if hdf5_library_dir_found:
Expand Down Expand Up @@ -192,8 +191,8 @@ def add_coverage_flags_if_required(cxx_flags: List[str],
"""
if 'ENABLE_GCOV_COVERAGE' in os.environ and \
os.environ['ENABLE_GCOV_COVERAGE'].upper() == 'TRUE':
log.info("ENABLE_GCOV_COVERAGE was set to TRUE."
" Building AMICI with coverage symbols.")
print("ENABLE_GCOV_COVERAGE was set to TRUE."
" Building AMICI with coverage symbols.")
cxx_flags.extend(['-g', '-O0', '--coverage'])
linker_flags.extend(['--coverage', '-g'])

Expand All @@ -212,8 +211,8 @@ def add_debug_flags_if_required(cxx_flags: List[str],
"""
if 'ENABLE_AMICI_DEBUGGING' in os.environ \
and os.environ['ENABLE_AMICI_DEBUGGING'] == 'TRUE':
log.info("ENABLE_AMICI_DEBUGGING was set to TRUE."
" Building AMICI with debug symbols.")
print("ENABLE_AMICI_DEBUGGING was set to TRUE."
" Building AMICI with debug symbols.")
cxx_flags.extend(['-g', '-O0', '-UNDEBUG'])
linker_flags.extend(['-g'])

Expand All @@ -237,7 +236,7 @@ def generate_swig_interface_files(swig_outdir: str = None,
f'-Iamici{os.sep}include',
]

log.info(f"Found SWIG version {swig_version}")
print(f"Found SWIG version {swig_version}")

# Are HDF5 includes available to generate the wrapper?
if with_hdf5 is None:
Expand All @@ -258,7 +257,7 @@ def generate_swig_interface_files(swig_outdir: str = None,
'-o', os.path.join("amici", "amici_wrap.cxx"),
os.path.join("amici", "swig", "amici.i")]

log.info(f"Running SWIG: {' '.join(swig_cmd)}")
print(f"Running SWIG: {' '.join(swig_cmd)}")
sp = subprocess.run(swig_cmd, stdout=subprocess.PIPE,
stderr=sys.stdout.buffer)
if not sp.returncode == 0:
Expand All @@ -271,15 +270,15 @@ def add_openmp_flags(cxx_flags: List, ldflags: List) -> None:

# Enable OpenMP support for Linux / OSX:
if sys.platform == 'linux':
log.info("Adding OpenMP flags...")
print("Adding OpenMP flags...")
cxx_flags.insert(0, "-fopenmp")
ldflags.insert(0, "-fopenmp")
elif sys.platform == 'darwin':
if os.path.exists('/usr/local/lib/libomp.a'):
log.info("Adding OpenMP flags...")
print("Adding OpenMP flags...")
cxx_flags[0:0] = ["-Xpreprocessor", "-fopenmp"]
ldflags[0:0] = ["-Xpreprocessor", "-fopenmp", "-lomp"]
else:
log.info("Not adding OpenMP flags, because /usr/local/lib/libomp.a"
" does not exist. To enable, run `brew install libomp` "
"or add flags manually.")
print("Not adding OpenMP flags, because /usr/local/lib/libomp.a"
" does not exist. To enable, run `brew install libomp` "
"or add flags manually.")
1 change: 1 addition & 0 deletions python/sdist/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ install_requires =
pkgconfig
wurlitzer
toposort
setuptools>=48
include_package_data = True
zip_safe = False

Expand Down