-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_cpp.py
61 lines (46 loc) · 1.68 KB
/
build_cpp.py
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
import argparse
import subprocess
import os
import shutil
from nanobind.stubgen import StubGen
def green_print(msg):
print("\033[92m" + msg + "\033[0m")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, default="Release", help="Configuration type")
args = parser.parse_args()
# Build nanobind module
# Remove build directory
shutil.rmtree("build", ignore_errors=True)
# Run cmake and build
ret1 = subprocess.run(["cmake", "-S", ".", "-B", "build"], shell=True)
ret2 = subprocess.run(["cmake", "--build", "build", "--config", args.config], shell=True)
if ret1.returncode != 0 or ret2.returncode != 0:
raise Exception("Failed to build nanobind module")
green_print("Finished building nanobind module")
# In windows, copy the built module
# and rename Debug to _pyglimshow_impl
if os.name == "nt":
src = f"build/{args.config}"
dst = "pyglimshow/_pyglimshow_impl"
if os.path.exists(dst):
shutil.rmtree(dst)
shutil.copytree(src, dst)
else:
raise
# Generate __init__.py
with open("pyglimshow/_pyglimshow_impl/__init__.py", "w") as f:
f.write("from ._pyglimshow_impl import *")
green_print(f"Moved {src} to {dst}, and generated __init__.py")
# Generate pyi file
import pyglimshow
module = pyglimshow._pyglimshow_impl._pyglimshow_impl
sg = StubGen(module)
sg.put(module)
pyi = sg.get()
with open("pyglimshow/_pyglimshow_impl/__init__.pyi", "w") as f:
f.write(pyi)
green_print("Generated pyi file")
green_print("Successfully built nanobind module")
if __name__ == "__main__":
main()