|
15 | 15 | import os |
16 | 16 | import re |
17 | 17 | import shutil |
18 | | -import subprocess |
19 | | -import sys |
20 | 18 | from dataclasses import dataclass |
21 | 19 | from itertools import chain |
22 | 20 | from pathlib import Path |
|
61 | 59 | _default_simplify, |
62 | 60 | ) |
63 | 61 | from .logging import get_logger, log_execution_time, set_log_level |
| 62 | +from .compile import build_model_extension |
64 | 63 | from .sympy_utils import ( |
65 | 64 | _custom_pow_eval_derivative, |
66 | 65 | _monkeypatched, |
@@ -2893,7 +2892,12 @@ def compile_model(self) -> None: |
2893 | 2892 | """ |
2894 | 2893 | Compiles the generated code it into a simulatable module |
2895 | 2894 | """ |
2896 | | - self._compile_c_code(compiler=self.compiler, verbose=self.verbose) |
| 2895 | + build_model_extension( |
| 2896 | + package_dir=self.model_path, |
| 2897 | + compiler=self.compiler, |
| 2898 | + verbose=self.verbose, |
| 2899 | + extra_msg="\n".join(self._build_hints), |
| 2900 | + ) |
2897 | 2901 |
|
2898 | 2902 | def _prepare_model_folder(self) -> None: |
2899 | 2903 | """ |
@@ -2950,69 +2954,6 @@ def _generate_c_code(self) -> None: |
2950 | 2954 | CXX_MAIN_TEMPLATE_FILE, os.path.join(self.model_path, "main.cpp") |
2951 | 2955 | ) |
2952 | 2956 |
|
2953 | | - def _compile_c_code( |
2954 | | - self, |
2955 | | - verbose: Optional[Union[bool, int]] = False, |
2956 | | - compiler: Optional[str] = None, |
2957 | | - ) -> None: |
2958 | | - """ |
2959 | | - Compile the generated model code |
2960 | | -
|
2961 | | - :param verbose: |
2962 | | - Make model compilation verbose |
2963 | | -
|
2964 | | - :param compiler: |
2965 | | - Absolute path to the compiler executable to be used to build the Python |
2966 | | - extension, e.g. ``/usr/bin/clang``. |
2967 | | - """ |
2968 | | - # setup.py assumes it is run from within the model directory |
2969 | | - module_dir = self.model_path |
2970 | | - script_args = [sys.executable, os.path.join(module_dir, "setup.py")] |
2971 | | - |
2972 | | - if verbose: |
2973 | | - script_args.append("--verbose") |
2974 | | - else: |
2975 | | - script_args.append("--quiet") |
2976 | | - |
2977 | | - script_args.extend( |
2978 | | - [ |
2979 | | - "build_ext", |
2980 | | - f"--build-lib={module_dir}", |
2981 | | - # This is generally not required, but helps to reduce the path |
2982 | | - # length of intermediate build files, that may easily become |
2983 | | - # problematic on Windows, due to its ridiculous 255-character path |
2984 | | - # length limit. |
2985 | | - f'--build-temp={Path(module_dir, "build")}', |
2986 | | - ] |
2987 | | - ) |
2988 | | - |
2989 | | - env = os.environ.copy() |
2990 | | - if compiler is not None: |
2991 | | - # CMake will use the compiler specified in the CXX environment variable |
2992 | | - env["CXX"] = compiler |
2993 | | - |
2994 | | - # distutils.core.run_setup looks nicer, but does not let us check the |
2995 | | - # result easily |
2996 | | - try: |
2997 | | - result = subprocess.run( |
2998 | | - script_args, |
2999 | | - cwd=module_dir, |
3000 | | - stdout=subprocess.PIPE, |
3001 | | - stderr=subprocess.STDOUT, |
3002 | | - check=True, |
3003 | | - env=env, |
3004 | | - ) |
3005 | | - except subprocess.CalledProcessError as e: |
3006 | | - print(e.output.decode("utf-8")) |
3007 | | - print("Failed building the model extension.") |
3008 | | - if self._build_hints: |
3009 | | - print("Note:") |
3010 | | - print("\n".join(self._build_hints)) |
3011 | | - raise |
3012 | | - |
3013 | | - if verbose: |
3014 | | - print(result.stdout.decode("utf-8")) |
3015 | | - |
3016 | 2957 | def _generate_m_code(self) -> None: |
3017 | 2958 | """ |
3018 | 2959 | Create a Matlab script for compiling code files to a mex file |
|
0 commit comments