Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
1b8fb862 | 2 | # |
0235b0db MJ |
3 | # Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com> |
4 | # Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
1b8fb862 MJ |
5 | |
6 | import sys | |
afdd1f82 | 7 | import os |
d5689f24 SM |
8 | import shutil |
9 | import subprocess | |
1b8fb862 | 10 | |
d617eb92 MJ |
11 | # Distutils was removed in Python 3.12, use setuptools as an alternative. |
12 | if sys.version_info >= (3, 12): | |
6ec97181 | 13 | from setuptools import setup, Extension |
d617eb92 MJ |
14 | else: |
15 | from distutils.core import setup, Extension | |
16 | ||
17 | # Starting with Debian's Python 3.10, the default install scheme is | |
18 | # 'posix_local' which is a Debian specific scheme based on 'posix_prefix' but | |
19 | # with an added 'local' prefix. This is the default so users doing system wide | |
20 | # manual installations of python modules end up in '/usr/local'. This | |
21 | # interferes with our autotools based install which already defaults to | |
22 | # '/usr/local' and expect a provided prefix to be used verbatim. | |
23 | # | |
24 | # Monkeypatch sysconfig to override this scheme and use 'posix_prefix' instead. | |
25 | if sys.version_info >= (3, 10): | |
927263e4 | 26 | import sysconfig |
1b8fb862 | 27 | |
d617eb92 MJ |
28 | original_get_preferred_scheme = sysconfig.get_preferred_scheme |
29 | ||
30 | def our_get_preferred_scheme(key): | |
31 | scheme = original_get_preferred_scheme(key) | |
32 | if scheme == "posix_local": | |
33 | return "posix_prefix" | |
34 | else: | |
35 | return scheme | |
36 | ||
37 | sysconfig.get_preferred_scheme = our_get_preferred_scheme | |
38 | ||
39 | else: | |
40 | import distutils.sysconfig as sysconfig | |
41 | ||
1b8fb862 MJ |
42 | PY_PATH_WARN_MSG = """ |
43 | -------------------------------------WARNING------------------------------------ | |
44 | The install directory used:\n ({})\nis not included in your PYTHONPATH. | |
45 | ||
46 | To add this directory to your Python search path permanently you can add the | |
47 | following command to your .bashrc/.zshrc: | |
48 | export PYTHONPATH="${{PYTHONPATH}}:{}" | |
49 | -------------------------------------------------------------------------------- | |
50 | """ | |
51 | ||
452480eb | 52 | original_get_config_vars = sysconfig.get_config_vars |
afdd1f82 JG |
53 | |
54 | ||
55 | def get_cflags(): | |
f5567ea8 | 56 | cflags = os.environ.get("CFLAGS") |
afdd1f82 JG |
57 | |
58 | if cflags is None: | |
f5567ea8 | 59 | [cflags] = original_get_config_vars("CFLAGS") |
afdd1f82 JG |
60 | |
61 | return cflags | |
62 | ||
63 | ||
64 | # distutils performs a similar transformation step on LDSHARED on | |
e7401568 | 65 | # darwin to use the overridden CC as the default command for LDSHARED |
afdd1f82 JG |
66 | # (see distutils' customize_compiler() step in the sysconfig module). |
67 | # | |
68 | # This takes it a step further by using our own LDFLAGS (when available) | |
e7401568 SM |
69 | # along with the overridden compiler and ensure that flags that are unsupported |
70 | # by either the Python interprter's CC or the overridden CC don't cause a | |
afdd1f82 JG |
71 | # build failure. |
72 | def get_ldshared(): | |
f5567ea8 FD |
73 | cc = os.environ.get("CC") |
74 | ldflags = os.environ.get("LDFLAGS") | |
75 | [py_cc] = original_get_config_vars("CC") | |
76 | [py_ldshared] = original_get_config_vars("LDSHARED") | |
afdd1f82 JG |
77 | |
78 | if not py_ldshared.startswith(py_cc): | |
79 | return py_ldshared | |
80 | ||
81 | if cc and ldflags: | |
f5567ea8 | 82 | return "{} -shared {}".format(cc, ldflags) |
afdd1f82 JG |
83 | elif cc: |
84 | return cc + py_ldshared[len(py_cc) :] | |
85 | elif ldflags: | |
86 | return py_cc + py_ldshared[len(py_cc) :] | |
87 | else: | |
88 | return py_ldshared | |
89 | ||
90 | ||
91 | def our_get_config_vars(*args): | |
1a543dc4 SM |
92 | overridden_config_vars_funcs = { |
93 | "CFLAGS": get_cflags, | |
94 | "LDSHARED": get_ldshared, | |
afdd1f82 JG |
95 | } |
96 | ||
97 | if len(args) == 0: | |
abc2ea2d | 98 | # Return a dict with all config vars. |
afdd1f82 | 99 | all_config_vars = original_get_config_vars() |
1a543dc4 SM |
100 | for name in overridden_config_vars_funcs: |
101 | all_config_vars[name] = overridden_config_vars_funcs[name]() | |
afdd1f82 | 102 | |
abc2ea2d SM |
103 | return all_config_vars |
104 | else: | |
105 | # Return a list with the requested config vars. | |
106 | subset_config_vars = [] | |
107 | for name in args: | |
1a543dc4 SM |
108 | if name in overridden_config_vars_funcs: |
109 | subset_config_vars.append(overridden_config_vars_funcs[name]()) | |
abc2ea2d SM |
110 | else: |
111 | subset_config_vars.append(original_get_config_vars(name)[0]) | |
112 | ||
113 | return subset_config_vars | |
afdd1f82 JG |
114 | |
115 | ||
452480eb | 116 | sysconfig.get_config_vars = our_get_config_vars |
afdd1f82 | 117 | |
cfbd7cf3 | 118 | |
d5689f24 SM |
119 | # Returns 'True' when running on a MinGW system. |
120 | def is_mingw(): | |
121 | return sys.platform == "win32" and shutil.which("cygpath") != None | |
122 | ||
123 | ||
124 | # On MinGW systems run 'cygpath -m' on 'path', on other systems return 'path' as-is. | |
125 | def cygpath_m(path: str): | |
126 | if is_mingw(): | |
127 | return subprocess.check_output( | |
128 | 'cygpath -m "{}"'.format(path), shell=True, encoding="utf-8" | |
129 | ).strip("\n") | |
130 | ||
131 | return path | |
132 | ||
133 | ||
134 | # On MinGW systems, check CFLAGS and CPPFLAGS for absolute include paths | |
135 | # (starts with '-I/') and convert them to valid Windows paths using cygpath. | |
136 | if is_mingw(): | |
137 | for flagvar in ["CFLAGS", "CPPFLAGS"]: | |
138 | cur_flags = os.getenv(flagvar) | |
139 | if cur_flags != None: | |
140 | new_flags = "" | |
141 | for flag in cur_flags.split(): | |
142 | if flag.startswith("-I/"): | |
143 | flag = "-I{}".format(cygpath_m(flag[2:])) | |
144 | ||
145 | new_flags += " {}".format(flag) | |
146 | ||
147 | os.environ[flagvar] = new_flags | |
148 | ||
149 | ||
1b8fb862 | 150 | def main(): |
cfbd7cf3 | 151 | babeltrace_ext = Extension( |
f5567ea8 | 152 | "bt2._native_bt", |
d5689f24 SM |
153 | sources=[ |
154 | "bt2/native_bt.c", | |
155 | cygpath_m("@srcdir@/bt2/logging.c"), | |
156 | ], | |
f5567ea8 | 157 | libraries=["babeltrace2", "glib-2.0"], |
cfbd7cf3 | 158 | extra_objects=[ |
86ef6105 MJ |
159 | "@top_builddir@/src/autodisc/.libs/libautodisc.a", |
160 | "@top_builddir@/src/logging/.libs/liblogging.a", | |
ed615696 | 161 | "@top_builddir@/src/common/.libs/libcommon.a", |
86ef6105 MJ |
162 | "@top_builddir@/src/py-common/.libs/libpy-common.a", |
163 | "@top_builddir@/src/string-format/.libs/libstring-format.a", | |
cfbd7cf3 FD |
164 | ], |
165 | ) | |
1b8fb862 | 166 | |
cfbd7cf3 | 167 | dist = setup( |
f5567ea8 FD |
168 | name="bt2", |
169 | version="@PACKAGE_VERSION@", | |
170 | description="Babeltrace 2 Python Bindings", | |
171 | packages=["bt2"], | |
172 | package_dir={"bt2": "bt2"}, | |
cfbd7cf3 | 173 | options={ |
f5567ea8 FD |
174 | "build": {"build_base": "build", "build_lib": "build/build_lib"}, |
175 | "build_ext": {"build_lib": "build/build_lib"}, | |
cfbd7cf3 | 176 | }, |
f5567ea8 | 177 | url="https://babeltrace.org/", |
cfbd7cf3 | 178 | ext_modules=[babeltrace_ext], |
f5567ea8 | 179 | license="MIT", |
cfbd7cf3 | 180 | classifiers=[ |
f5567ea8 FD |
181 | "Development Status :: 5 - Production/Stable", |
182 | "Intended Audience :: Developers", | |
183 | "License :: OSI Approved :: The MIT License", | |
184 | "Programming Language :: Python :: 3" "Topic :: System :: Logging", | |
cfbd7cf3 FD |
185 | ], |
186 | ) | |
1b8fb862 | 187 | |
cfbd7cf3 FD |
188 | # After the installation, we check that the install directory is included in |
189 | # the Python search path and we print a warning message when it's not. | |
190 | # We need to do this because Python search path differs depending on the distro | |
191 | # and some distros don't include any /usr/local/ in the search path. This is | |
192 | # also useful for out-of-tree installs and tests. | |
193 | # It's only relevant to make this check on the `install` command. | |
1b8fb862 | 194 | |
f5567ea8 FD |
195 | if "install" in dist.command_obj: |
196 | install_dir = dist.command_obj["install"].install_libbase | |
1b8fb862 MJ |
197 | if install_dir not in sys.path: |
198 | # We can't consider this an error because if affects every | |
199 | # distro differently. We only warn the user that some | |
200 | # extra configuration is needed to use the bindings | |
201 | print(PY_PATH_WARN_MSG.format(install_dir, install_dir)) | |
202 | ||
cfbd7cf3 | 203 | |
1b8fb862 MJ |
204 | if __name__ == "__main__": |
205 | main() |