-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
113 lines (89 loc) · 3.74 KB
/
setup.py
File metadata and controls
113 lines (89 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Setup script for my_project."""
import importlib
import logging
import os
import subprocess
import sys
from typing import List
import packaging.version
import pybind11
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
version_module_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "my_project", "version.py")
spec = importlib.util.spec_from_file_location("version", version_module_path) # type: ignore
version_module = importlib.util.module_from_spec(spec) # type: ignore
sys.modules["version"] = version_module
spec.loader.exec_module(version_module) # type: ignore
version_content = version_module.__version__
package_version = packaging.version.parse(version_content)
setup.version = str(package_version)
class CMakeExtension(Extension):
"""CMake extension for my_project."""
def __init__(self, name: str, sourcedir: str = "") -> None:
"""Initialize the CMake extension.
Args:
name (str): The name of the extension.
sourcedir (str): The source directory of the extension.
"""
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(os.path.dirname(__file__))
class CMakeBuild(build_ext):
"""CMake build for my_project."""
def run_cmake_tests(self, build_temp: str) -> bool:
"""Run CTest and return True if all tests pass.
Args:
build_temp (str): Build directory path.
Returns:
bool: True if all tests pass, False otherwise.
"""
try:
subprocess.check_call(["ctest", "--output-on-failure"], cwd=build_temp)
return True
except subprocess.CalledProcessError:
logging.error("C++ tests failed!")
return False
def build_extension(self, ext: CMakeExtension) -> None:
"""Build the extension.
Args:
ext (CMakeExtension): The extension to build.
Raises:
RuntimeError: If C++ tests fail and REQUIRE_CPP_TESTS is set.
"""
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
build_temp = os.path.join(self.build_temp, "build")
os.makedirs(build_temp, exist_ok=True)
os.makedirs(extdir, exist_ok=True)
cmake_args: List[str] = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}",
"-DCMAKE_BUILD_TYPE=Release",
]
# Add pybind11 path to CMAKE_PREFIX_PATH
pybind11_cmake_dir = pybind11.get_cmake_dir()
cmake_args.append(f"-Dpybind11_DIR={pybind11_cmake_dir}")
# C++ 테스트 실행 여부 결정
run_cpp_tests = os.environ.get("RUN_CPP_TESTS", "").lower() in ("1", "true", "yes", "on")
require_cpp_tests = os.environ.get("REQUIRE_CPP_TESTS", "").lower() in ("1", "true", "yes", "on")
if run_cpp_tests:
cmake_args.append("-DBUILD_TESTS=ON")
else:
cmake_args.append("-DBUILD_TESTS=OFF")
subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=build_temp)
subprocess.check_call(["cmake", "--build", "."], cwd=build_temp)
if run_cpp_tests:
tests_passed = self.run_cmake_tests(build_temp)
if not tests_passed and require_cpp_tests:
raise RuntimeError(
"C++ tests failed and REQUIRE_CPP_TESTS is set. "
"Fix the tests or set REQUIRE_CPP_TESTS=0 to ignore test failures."
)
setup(
name="my_project",
packages=["my_project"],
ext_modules=[CMakeExtension("my_project.lib.my_project_core")],
cmdclass={"build_ext": CMakeBuild},
zip_safe=False,
package_data={
"my_project": ["lib/*.so", "lib/*.pyd", "lib/*.pyi"],
},
include_package_data=True,
)