-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
53 lines (40 loc) · 1.73 KB
/
Copy pathsetup.py
File metadata and controls
53 lines (40 loc) · 1.73 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
"""
Setup script for pascal1981.
A custom ``build_py`` command compiles the C runtime static library
(libpascalrt.a) via ``make -C runtime`` and copies it into the package
directory so that it travels with the wheel.
All project metadata lives in pyproject.toml; this file only exists to
register the custom build command.
"""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
from setuptools import setup
from setuptools.command.build_py import build_py as _build_py
HERE = os.path.dirname(os.path.abspath(__file__))
RUNTIME_DIR = os.path.join(HERE, "runtime")
PACKAGE_DIR = os.path.join(HERE, "src", "pascal1981")
class BuildPyWithRuntime(_build_py):
"""Custom build_py that compiles the C runtime before installing."""
def run(self) -> None:
# -- Build the C runtime static library ------------------------------
make_cmd = ["make", "-C", RUNTIME_DIR]
print(f"* building C runtime: {' '.join(make_cmd)}", file=sys.stderr)
try:
subprocess.run(make_cmd, check=True, stdout=sys.stderr, stderr=sys.stderr)
except subprocess.CalledProcessError:
print(
"error: failed to compile the C runtime. Is clang installed?",
file=sys.stderr,
)
sys.exit(1)
# -- Copy the archive into the package directory ---------------------
src = os.path.join(RUNTIME_DIR, "build", "libpascalrt.a")
dst = os.path.join(PACKAGE_DIR, "libpascalrt.a")
print(f"* copying {src} -> {dst}", file=sys.stderr)
shutil.copy2(src, dst)
# -- Proceed with normal Python build --------------------------------
super().run()
setup(cmdclass={"build_py": BuildPyWithRuntime})