Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
79 lines (66 loc) · 2.56 KB
/
Copy pathsetup.py
File metadata and controls
79 lines (66 loc) · 2.56 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
from __future__ import annotations
import importlib.util
import sys
from pathlib import Path
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
_ROOT = Path(__file__).resolve().parent
def _generate_native_tables() -> None:
"""Regenerate native/latex_command_tables.inc from core.latex_commands.
The C++ accelerator cannot import Python, so its command tables are
generated from the single Python source of truth. Regenerating here keeps
every build (pip, setup.sh, CI, packaging) in sync instead of compiling a
stale include.
"""
generator = _ROOT / "native" / "generate_command_tables.py"
if not generator.is_file():
return
spec = importlib.util.spec_from_file_location("_npq_generate_tables", generator)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.main([])
class OptionalBuildExt(build_ext):
"""Build the accelerator when possible without making it mandatory."""
def run(self):
try:
super().run()
except Exception as exc: # pragma: no cover - compiler/platform dependent
self.announce(
f"NotePadPQ: native accelerators unavailable ({exc}); using Python fallback",
level=3,
)
def build_extension(self, extension):
try:
if extension.name == "core._latex_native":
_generate_native_tables()
except Exception as exc: # pragma: no cover - keep the committed include
self.announce(
f"NotePadPQ: could not regenerate native command tables ({exc}); "
"using the committed include",
level=3,
)
try:
super().build_extension(extension)
except Exception as exc: # pragma: no cover - compiler/platform dependent
self.announce(
f"NotePadPQ: could not compile {extension.name} ({exc}); using Python fallback",
level=3,
)
compile_args = ["/O2"] if sys.platform == "win32" else ["-O3"]
setup(
ext_modules=[
Extension(
"core._latex_native",
["native/latex_native.cpp"],
language="c++",
extra_compile_args=compile_args,
),
Extension(
"core._code_native",
["native/code_native.cpp"],
language="c++",
extra_compile_args=compile_args + (["/std:c++17"] if sys.platform == "win32" else ["-std=c++17"]),
),
],
cmdclass={"build_ext": OptionalBuildExt},
)