|
8 | 8 |
|
9 | 9 |
|
10 | 10 | class CMakeExtension(Extension):
|
11 |
| - def __init__(self, name: str, sourcedir: Path) -> None: |
| 11 | + def __init__(self, name: str, source_dir: Path) -> None: |
12 | 12 | super().__init__(name, sources=[])
|
13 |
| - self.sourcedir = sourcedir.resolve() |
| 13 | + self.source_dir = source_dir.resolve() |
14 | 14 |
|
15 | 15 |
|
16 | 16 | class CMakeBuild(build_ext):
|
17 |
| - def build_extension(self, ext: CMakeExtension) -> None: |
18 |
| - ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name) |
19 |
| - extdir = ext_fullpath.parent.resolve() |
| 17 | + def conan_profile(self) -> str: |
| 18 | + subprocess.run(["conan", "profile", "detect", "-e"], check=True) |
20 | 19 |
|
21 |
| - build_temp = Path(self.build_temp) / ext.name |
22 |
| - build_temp.mkdir(parents=True, exist_ok=True) |
| 20 | + default_path = Path.home() / ".conan2" / "profiles" / "default" |
23 | 21 |
|
24 |
| - subprocess.run(["conan", "profile", "detect", "-e"], check=True) |
| 22 | + with open(default_path, "r") as f: |
| 23 | + default_profile = f.read() |
| 24 | + default_profile = default_profile.replace("compiler.cppstd=gnu14", "compiler.cppstd=gnu20") |
| 25 | + with open(default_path, "w") as f: |
| 26 | + f.write(default_profile) |
25 | 27 |
|
26 |
| - conan_odr_remote = "https://artifactory.opendocument.app/artifactory/api/conan/conan" |
| 28 | + def conan_remote(self) -> str: |
| 29 | + conan_odr_remote = ( |
| 30 | + "https://artifactory.opendocument.app/artifactory/api/conan/conan" |
| 31 | + ) |
27 | 32 |
|
28 |
| - result = subprocess.run(["conan", "remote", "list"], check=True, capture_output=True, text=True) |
| 33 | + result = subprocess.run( |
| 34 | + ["conan", "remote", "list"], check=True, capture_output=True, text=True |
| 35 | + ) |
29 | 36 | if conan_odr_remote not in result.stdout:
|
30 | 37 | print(f"Adding Conan remote {conan_odr_remote}")
|
31 |
| - subprocess.run([ |
| 38 | + subprocess.run( |
| 39 | + [ |
| 40 | + "conan", |
| 41 | + "remote", |
| 42 | + "add", |
| 43 | + "odr", |
| 44 | + conan_odr_remote, |
| 45 | + ], |
| 46 | + check=True, |
| 47 | + ) |
| 48 | + |
| 49 | + def conan_install(self, source_dir: Path, build_dir: Path) -> None: |
| 50 | + subprocess.run( |
| 51 | + [ |
32 | 52 | "conan",
|
33 |
| - "remote", |
34 |
| - "add", |
35 |
| - "odr", |
36 |
| - conan_odr_remote, |
37 |
| - ], check=True) |
38 |
| - |
39 |
| - conan_args = [ |
40 |
| - f"--output-folder={str(build_temp)}", |
41 |
| - "--build=missing", |
42 |
| - "-s", |
43 |
| - "build_type=Release", |
44 |
| - ] |
45 |
| - |
46 |
| - subprocess.run(["conan", "install", str(ext.sourcedir), *conan_args], check=True) |
47 |
| - |
48 |
| - cmake_args = [ |
49 |
| - f"-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake", |
50 |
| - f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={str(extdir)}{os.sep}", |
51 |
| - f"-DPYTHON_EXECUTABLE={sys.executable}", |
52 |
| - f"-DCMAKE_BUILD_TYPE=Release", |
53 |
| - ] |
54 |
| - |
55 |
| - build_args = [ |
56 |
| - "--config", |
57 |
| - "Release", |
58 |
| - ] |
| 53 | + "install", |
| 54 | + str(source_dir), |
| 55 | + f"--output-folder={str(build_dir)}", |
| 56 | + "--build=missing", |
| 57 | + "-s", |
| 58 | + "build_type=Release", |
| 59 | + ], |
| 60 | + check=True, |
| 61 | + ) |
| 62 | + |
| 63 | + def cmake_configure(self, source_dir: Path, build_dir: Path, ext_dir: Path) -> None: |
| 64 | + subprocess.run( |
| 65 | + [ |
| 66 | + "cmake", |
| 67 | + "-S", |
| 68 | + str(source_dir), |
| 69 | + "-B", |
| 70 | + str(build_dir), |
| 71 | + f"-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake", |
| 72 | + f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={str(ext_dir)}{os.sep}", |
| 73 | + f"-DPYTHON_EXECUTABLE={sys.executable}", |
| 74 | + f"-DCMAKE_BUILD_TYPE=Release", |
| 75 | + ], |
| 76 | + check=True, |
| 77 | + ) |
59 | 78 |
|
| 79 | + def cmake_build(self, build_dir: Path) -> None: |
60 | 80 | subprocess.run(
|
61 |
| - ["cmake", "-S", str(ext.sourcedir), "-B", str(build_temp), *cmake_args], check=True |
| 81 | + [ |
| 82 | + "cmake", |
| 83 | + "--build", |
| 84 | + str(build_dir), |
| 85 | + "--config", |
| 86 | + "Release", |
| 87 | + ], |
| 88 | + check=True, |
62 | 89 | )
|
63 |
| - subprocess.run(["cmake", "--build", str(build_temp), *build_args], check=True) |
| 90 | + |
| 91 | + def cmake_install(self, build_dir: Path) -> None: |
| 92 | + subprocess.run( |
| 93 | + [ |
| 94 | + "cmake", |
| 95 | + "--install", |
| 96 | + str(build_dir), |
| 97 | + "--config", |
| 98 | + "Release", |
| 99 | + ], |
| 100 | + check=True, |
| 101 | + ) |
| 102 | + |
| 103 | + def build_extension(self, ext: CMakeExtension) -> None: |
| 104 | + source_dir = Path(ext.source_dir) |
| 105 | + |
| 106 | + ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name) |
| 107 | + ext_dir = ext_fullpath.parent.resolve() |
| 108 | + |
| 109 | + build_temp = Path(self.build_temp) / ext.name |
| 110 | + build_temp.mkdir(parents=True, exist_ok=True) |
| 111 | + |
| 112 | + self.conan_profile() |
| 113 | + self.conan_remote() |
| 114 | + self.conan_install(source_dir, build_temp) |
| 115 | + |
| 116 | + self.cmake_configure(source_dir, build_temp, ext_dir) |
| 117 | + self.cmake_build(build_temp) |
| 118 | + self.cmake_install(build_temp) |
64 | 119 |
|
65 | 120 |
|
66 | 121 | setup(
|
|
0 commit comments