Skip to content

Commit

Permalink
Refactor de_export.py, extract model compilation (#2315)
Browse files Browse the repository at this point in the history
Move model compilation to a free function in a separate module.
Easier to test and more reusable for recompilation after initial import.

Related to #2306
  • Loading branch information
dweindl authored Feb 26, 2024
1 parent 780d767 commit 4b1e73b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 66 deletions.
80 changes: 80 additions & 0 deletions python/sdist/amici/compile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Functionality for building the C++ extensions of an amici-created model
package.
"""
import subprocess
import sys
from typing import Optional, Union
from pathlib import Path
import os


def build_model_extension(
package_dir: Union[str, Path],
verbose: Optional[Union[bool, int]] = False,
compiler: Optional[str] = None,
extra_msg: Optional[str] = None,
) -> None:
"""
Compile the model extension of an amici-created model package.
:param package_dir:
Directory of the model package to be compiled. I.e., the directory
containing the `setup.py` file.
:param verbose:
Make model compilation verbose.
:param compiler:
Absolute path to the compiler executable to be used to build the Python
extension, e.g. ``/usr/bin/clang``.
:param extra_msg:
Additional message to be printed in case of a failed build.
"""
# setup.py assumes it is run from within the model directory
package_dir = Path(package_dir)
script_args = [sys.executable, package_dir / "setup.py"]

if verbose:
script_args.append("--verbose")
else:
script_args.append("--quiet")

script_args.extend(
[
"build_ext",
f"--build-lib={package_dir}",
# This is generally not required, but helps to reduce the path
# length of intermediate build files, that may easily become
# problematic on Windows, due to its ridiculous 255-character path
# length limit.
f'--build-temp={package_dir / "build"}',
]
)

env = os.environ.copy()
if compiler is not None:
# CMake will use the compiler specified in the CXX environment variable
env["CXX"] = compiler

# distutils.core.run_setup looks nicer, but does not let us check the
# result easily
try:
result = subprocess.run(
script_args,
cwd=str(package_dir),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
env=env,
)
except subprocess.CalledProcessError as e:
print(e.output.decode("utf-8"))
print("Failed building the model extension.")
if extra_msg:
print(f"Note: {extra_msg}")
raise

if verbose:
print(result.stdout.decode("utf-8"))
73 changes: 7 additions & 66 deletions python/sdist/amici/de_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import os
import re
import shutil
import subprocess
import sys
from dataclasses import dataclass
from itertools import chain
from pathlib import Path
Expand Down Expand Up @@ -61,6 +59,7 @@
_default_simplify,
)
from .logging import get_logger, log_execution_time, set_log_level
from .compile import build_model_extension
from .sympy_utils import (
_custom_pow_eval_derivative,
_monkeypatched,
Expand Down Expand Up @@ -2893,7 +2892,12 @@ def compile_model(self) -> None:
"""
Compiles the generated code it into a simulatable module
"""
self._compile_c_code(compiler=self.compiler, verbose=self.verbose)
build_model_extension(
package_dir=self.model_path,
compiler=self.compiler,
verbose=self.verbose,
extra_msg="\n".join(self._build_hints),
)

def _prepare_model_folder(self) -> None:
"""
Expand Down Expand Up @@ -2950,69 +2954,6 @@ def _generate_c_code(self) -> None:
CXX_MAIN_TEMPLATE_FILE, os.path.join(self.model_path, "main.cpp")
)

def _compile_c_code(
self,
verbose: Optional[Union[bool, int]] = False,
compiler: Optional[str] = None,
) -> None:
"""
Compile the generated model code
:param verbose:
Make model compilation verbose
:param compiler:
Absolute path to the compiler executable to be used to build the Python
extension, e.g. ``/usr/bin/clang``.
"""
# setup.py assumes it is run from within the model directory
module_dir = self.model_path
script_args = [sys.executable, os.path.join(module_dir, "setup.py")]

if verbose:
script_args.append("--verbose")
else:
script_args.append("--quiet")

script_args.extend(
[
"build_ext",
f"--build-lib={module_dir}",
# This is generally not required, but helps to reduce the path
# length of intermediate build files, that may easily become
# problematic on Windows, due to its ridiculous 255-character path
# length limit.
f'--build-temp={Path(module_dir, "build")}',
]
)

env = os.environ.copy()
if compiler is not None:
# CMake will use the compiler specified in the CXX environment variable
env["CXX"] = compiler

# distutils.core.run_setup looks nicer, but does not let us check the
# result easily
try:
result = subprocess.run(
script_args,
cwd=module_dir,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
env=env,
)
except subprocess.CalledProcessError as e:
print(e.output.decode("utf-8"))
print("Failed building the model extension.")
if self._build_hints:
print("Note:")
print("\n".join(self._build_hints))
raise

if verbose:
print(result.stdout.decode("utf-8"))

def _generate_m_code(self) -> None:
"""
Create a Matlab script for compiling code files to a mex file
Expand Down

0 comments on commit 4b1e73b

Please sign in to comment.