| 44 | super().run() |
| 45 | |
| 46 | def build_extension(self, ext: Extension) -> None: |
| 47 | # This is needed to build the Cython extension. |
| 48 | if not isinstance(ext, CMakeExtension): |
| 49 | super().build_extension(ext) |
| 50 | return |
| 51 | |
| 52 | # Must be in this form due to bug in .resolve() only fixed in Python 3.10+ |
| 53 | ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name) |
| 54 | extdir = ext_fullpath.parent.resolve() |
| 55 | |
| 56 | # Using this requires trailing slash for auto-detection & inclusion of |
| 57 | # auxiliary "native" libs |
| 58 | |
| 59 | debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug |
| 60 | cfg = "Debug" if debug else "Release" |
| 61 | |
| 62 | # CMake lets you override the generator - we need to check this. |
| 63 | # Can be set with Conda-Build, for example. |
| 64 | cmake_generator = os.environ.get("CMAKE_GENERATOR", "") |
| 65 | |
| 66 | # EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code |
| 67 | # from Python. |
| 68 | cmake_args = [ |
| 69 | f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}", |
| 70 | f"-DPython_EXECUTABLE={sys.executable}", |
| 71 | f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm |
| 72 | ] |
| 73 | build_args = [] |
| 74 | # Adding CMake arguments set as environment variable |
| 75 | # (needed e.g. to build for ARM OSx on conda-forge) |
| 76 | if "CMAKE_ARGS" in os.environ: |
| 77 | cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item] |
| 78 | |
| 79 | # In this example, we pass in the version to C++. You might not need to. |
| 80 | cmake_args += [f"-DPACKAGE_VERSION_INFO={self.distribution.get_version()}"] |
| 81 | |
| 82 | if self.compiler.compiler_type != "msvc": |
| 83 | # Using Ninja-build since it a) is available as a wheel and b) |
| 84 | # multithreads automatically. MSVC would require all variables be |
| 85 | # exported for Ninja to pick it up, which is a little tricky to do. |
| 86 | # Users can override the generator with CMAKE_GENERATOR in CMake |
| 87 | # 3.15+. |
| 88 | if not cmake_generator or cmake_generator == "Ninja": |
| 89 | try: |
| 90 | import ninja |
| 91 | |
| 92 | ninja_executable_path = Path(ninja.BIN_DIR) / "ninja" |
| 93 | cmake_args += [ |
| 94 | "-GNinja", |
| 95 | f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}", |
| 96 | ] |
| 97 | except ImportError: |
| 98 | pass |
| 99 | |
| 100 | else: |
| 101 | # Single config generators are handled "normally" |
| 102 | single_config = any(x in cmake_generator for x in {"NMake", "Ninja"}) |
| 103 | |