Debian Codegraph @

setup.py

raw
1 import os
2 import sys
3 import shutil
4 import textwrap
5 import platform
6 from pathlib import Path
7 from subprocess import run
8
9 from setuptools import setup, Extension, Command
10 from setuptools.command import build_ext as _build_ext
11
12
13 curdir = Path(__file__).resolve().parent
14 ecodes_c_path = curdir / "src/evdev/ecodes.c"
15
16
17 def create_ecodes(headers=None, reproducible=False):
18 if not headers:
19 include_paths = set()
20 cpath = os.environ.get("CPATH", "").strip()
21 c_inc_path = os.environ.get("C_INCLUDE_PATH", "").strip()
22
23 if cpath:
24 include_paths.update(cpath.split(":"))
25 if c_inc_path:
26 include_paths.update(c_inc_path.split(":"))
27
28 include_paths.add("/usr/include")
29 if platform.system().lower() == "freebsd":
30 files = ["dev/evdev/input.h", "dev/evdev/input-event-codes.h", "dev/evdev/uinput.h"]
31 else:
32 files = ["linux/input.h", "linux/input-event-codes.h", "linux/uinput.h"]
33
34 headers = [os.path.join(path, file) for path in include_paths for file in files]
35
36 headers = [header for header in headers if os.path.isfile(header)]
37 if not headers:
38 msg = """\
39 The 'linux/input.h' and 'linux/input-event-codes.h' include files
40 are missing. You will have to install the kernel header files in
41 order to continue:
42
43 dnf install kernel-headers-$(uname -r)
44 apt-get install linux-headers-$(uname -r)
45 emerge sys-kernel/linux-headers
46 pacman -S kernel-headers
47
48 In case they are installed in a non-standard location, you may use
49 the '--evdev-headers' option to specify one or more colon-separated
50 paths. For example:
51
52 python setup.py \\
53 build \\
54 build_ecodes --evdev-headers path/input.h:path/input-event-codes.h \\
55 build_ext --include-dirs path/ \\
56 install
57
58 If you want to avoid building this package from source, then please consider
59 installing the `evdev-binary` package instead. Keep in mind that it may not be
60 fully compatible with, or support all the features of your current kernel.
61 """
62
63 sys.stderr.write(textwrap.dedent(msg))
64 sys.exit(1)
65
66 print("writing %s (using %s)" % (ecodes_c_path, " ".join(headers)))
67 with ecodes_c_path.open("w") as fh:
68 cmd = [sys.executable, "src/evdev/genecodes_c.py"]
69 if reproducible:
70 cmd.append("--reproducible")
71 cmd.extend(["--ecodes", *headers])
72 run(cmd, check=True, stdout=fh)
73
74
75 class build_ecodes(Command):
76 description = "generate ecodes.c"
77
78 user_options = [
79 ("evdev-headers=", None, "colon-separated paths to input subsystem headers"),
80 ("reproducible", None, "hide host details (host/paths) to create a reproducible output"),
81 ]
82
83 def initialize_options(self):
84 self.evdev_headers = None
85 self.reproducible = False
86
87 def finalize_options(self):
88 if self.evdev_headers:
89 self.evdev_headers = self.evdev_headers.split(":")
90 if self.reproducible is None:
91 self.reproducible = False
92
93 def run(self):
94 create_ecodes(self.evdev_headers, reproducible=self.reproducible)
95
96
97 class build_ext(_build_ext.build_ext):
98 def has_ecodes(self):
99 if ecodes_c_path.exists():
100 print("ecodes.c already exists ... skipping build_ecodes")
101 return False
102 return True
103
104 def generate_ecodes_py(self):
105 ecodes_py = Path(self.build_lib) / "evdev/ecodes.py"
106 print(f"writing {ecodes_py}")
107 with ecodes_py.open("w") as fh:
108 cmd = [sys.executable, "-B", "src/evdev/genecodes_py.py"]
109 res = run(cmd, env={"PYTHONPATH": self.build_lib}, stdout=fh)
110
111 if res.returncode != 0:
112 print(f"failed to generate static {ecodes_py} - will use ecodes_runtime.py")
113 shutil.copy("src/evdev/ecodes_runtime.py", ecodes_py)
114
115 def run(self):
116 for cmd_name in self.get_sub_commands():
117 self.run_command(cmd_name)
118 _build_ext.build_ext.run(self)
119 self.generate_ecodes_py()
120
121 sub_commands = [("build_ecodes", has_ecodes)] + _build_ext.build_ext.sub_commands
122
123
124 cflags = ["-std=c99", "-Wno-error=declaration-after-statement"]
125 setup(
126 ext_modules=[
127 Extension("evdev._input", sources=["src/evdev/input.c"], extra_compile_args=cflags),
128 Extension("evdev._uinput", sources=["src/evdev/uinput.c"], extra_compile_args=cflags),
129 Extension("evdev._ecodes", sources=["src/evdev/ecodes.c"], extra_compile_args=cflags),
130 ],
131 cmdclass={
132 "build_ext": build_ext,
133 "build_ecodes": build_ecodes,
134 },
135 )