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 |
1b8fb862 | 8 | |
d617eb92 MJ |
9 | # Distutils was removed in Python 3.12, use setuptools as an alternative. |
10 | if sys.version_info >= (3, 12): | |
6ec97181 | 11 | from setuptools import setup, Extension |
d617eb92 MJ |
12 | else: |
13 | from distutils.core import setup, Extension | |
14 | ||
15 | # Starting with Debian's Python 3.10, the default install scheme is | |
16 | # 'posix_local' which is a Debian specific scheme based on 'posix_prefix' but | |
17 | # with an added 'local' prefix. This is the default so users doing system wide | |
18 | # manual installations of python modules end up in '/usr/local'. This | |
19 | # interferes with our autotools based install which already defaults to | |
20 | # '/usr/local' and expect a provided prefix to be used verbatim. | |
21 | # | |
22 | # Monkeypatch sysconfig to override this scheme and use 'posix_prefix' instead. | |
23 | if sys.version_info >= (3, 10): | |
927263e4 | 24 | import sysconfig |
1b8fb862 | 25 | |
d617eb92 MJ |
26 | original_get_preferred_scheme = sysconfig.get_preferred_scheme |
27 | ||
28 | def our_get_preferred_scheme(key): | |
29 | scheme = original_get_preferred_scheme(key) | |
30 | if scheme == "posix_local": | |
31 | return "posix_prefix" | |
32 | else: | |
33 | return scheme | |
34 | ||
35 | sysconfig.get_preferred_scheme = our_get_preferred_scheme | |
36 | ||
37 | else: | |
38 | import distutils.sysconfig as sysconfig | |
39 | ||
1b8fb862 MJ |
40 | PY_PATH_WARN_MSG = """ |
41 | -------------------------------------WARNING------------------------------------ | |
42 | The install directory used:\n ({})\nis not included in your PYTHONPATH. | |
43 | ||
44 | To add this directory to your Python search path permanently you can add the | |
45 | following command to your .bashrc/.zshrc: | |
46 | export PYTHONPATH="${{PYTHONPATH}}:{}" | |
47 | -------------------------------------------------------------------------------- | |
48 | """ | |
49 | ||
452480eb | 50 | original_get_config_vars = sysconfig.get_config_vars |
afdd1f82 JG |
51 | |
52 | ||
53 | def get_cflags(): | |
f5567ea8 | 54 | cflags = os.environ.get("CFLAGS") |
afdd1f82 JG |
55 | |
56 | if cflags is None: | |
f5567ea8 | 57 | [cflags] = original_get_config_vars("CFLAGS") |
afdd1f82 JG |
58 | |
59 | return cflags | |
60 | ||
61 | ||
62 | # distutils performs a similar transformation step on LDSHARED on | |
63 | # darwin to use the overriden CC as the default command for LDSHARED | |
64 | # (see distutils' customize_compiler() step in the sysconfig module). | |
65 | # | |
66 | # This takes it a step further by using our own LDFLAGS (when available) | |
67 | # along with the overriden compiler and ensure that flags that are unsupported | |
68 | # by either the Python interprter's CC or the overriden CC don't cause a | |
69 | # build failure. | |
70 | def get_ldshared(): | |
f5567ea8 FD |
71 | cc = os.environ.get("CC") |
72 | ldflags = os.environ.get("LDFLAGS") | |
73 | [py_cc] = original_get_config_vars("CC") | |
74 | [py_ldshared] = original_get_config_vars("LDSHARED") | |
afdd1f82 JG |
75 | |
76 | if not py_ldshared.startswith(py_cc): | |
77 | return py_ldshared | |
78 | ||
79 | if cc and ldflags: | |
f5567ea8 | 80 | return "{} -shared {}".format(cc, ldflags) |
afdd1f82 JG |
81 | elif cc: |
82 | return cc + py_ldshared[len(py_cc) :] | |
83 | elif ldflags: | |
84 | return py_cc + py_ldshared[len(py_cc) :] | |
85 | else: | |
86 | return py_ldshared | |
87 | ||
88 | ||
89 | def our_get_config_vars(*args): | |
90 | overridden_config_vars = { | |
f5567ea8 FD |
91 | "CFLAGS": get_cflags(), |
92 | "LDSHARED": get_ldshared(), | |
afdd1f82 JG |
93 | } |
94 | ||
95 | if len(args) == 0: | |
96 | all_config_vars = original_get_config_vars() | |
97 | for name in overridden_config_vars: | |
98 | all_config_vars[name] = overridden_config_vars[name] | |
99 | return all_config_vars | |
100 | ||
101 | subset_config_vars = [] | |
102 | for name in args: | |
103 | if name not in overridden_config_vars: | |
104 | [var] = original_get_config_vars(name) | |
105 | subset_config_vars.append(var) | |
106 | continue | |
107 | ||
108 | subset_config_vars.append(overridden_config_vars[name]) | |
109 | ||
110 | return subset_config_vars | |
111 | ||
112 | ||
452480eb | 113 | sysconfig.get_config_vars = our_get_config_vars |
afdd1f82 | 114 | |
cfbd7cf3 | 115 | |
1b8fb862 | 116 | def main(): |
cfbd7cf3 | 117 | babeltrace_ext = Extension( |
f5567ea8 FD |
118 | "bt2._native_bt", |
119 | sources=["bt2/native_bt.c", "@srcdir@/bt2/logging.c"], | |
120 | libraries=["babeltrace2", "glib-2.0"], | |
cfbd7cf3 | 121 | extra_objects=[ |
86ef6105 MJ |
122 | "@top_builddir@/src/autodisc/.libs/libautodisc.a", |
123 | "@top_builddir@/src/logging/.libs/liblogging.a", | |
f5567ea8 | 124 | "@top_builddir@/src/common/.libs/libbabeltrace2-common.a", |
86ef6105 MJ |
125 | "@top_builddir@/src/py-common/.libs/libpy-common.a", |
126 | "@top_builddir@/src/string-format/.libs/libstring-format.a", | |
cfbd7cf3 FD |
127 | ], |
128 | ) | |
1b8fb862 | 129 | |
cfbd7cf3 | 130 | dist = setup( |
f5567ea8 FD |
131 | name="bt2", |
132 | version="@PACKAGE_VERSION@", | |
133 | description="Babeltrace 2 Python Bindings", | |
134 | packages=["bt2"], | |
135 | package_dir={"bt2": "bt2"}, | |
cfbd7cf3 | 136 | options={ |
f5567ea8 FD |
137 | "build": {"build_base": "build", "build_lib": "build/build_lib"}, |
138 | "build_ext": {"build_lib": "build/build_lib"}, | |
cfbd7cf3 | 139 | }, |
f5567ea8 | 140 | url="https://babeltrace.org/", |
cfbd7cf3 | 141 | ext_modules=[babeltrace_ext], |
f5567ea8 | 142 | license="MIT", |
cfbd7cf3 | 143 | classifiers=[ |
f5567ea8 FD |
144 | "Development Status :: 5 - Production/Stable", |
145 | "Intended Audience :: Developers", | |
146 | "License :: OSI Approved :: The MIT License", | |
147 | "Programming Language :: Python :: 3" "Topic :: System :: Logging", | |
cfbd7cf3 FD |
148 | ], |
149 | ) | |
1b8fb862 | 150 | |
cfbd7cf3 FD |
151 | # After the installation, we check that the install directory is included in |
152 | # the Python search path and we print a warning message when it's not. | |
153 | # We need to do this because Python search path differs depending on the distro | |
154 | # and some distros don't include any /usr/local/ in the search path. This is | |
155 | # also useful for out-of-tree installs and tests. | |
156 | # It's only relevant to make this check on the `install` command. | |
1b8fb862 | 157 | |
f5567ea8 FD |
158 | if "install" in dist.command_obj: |
159 | install_dir = dist.command_obj["install"].install_libbase | |
1b8fb862 MJ |
160 | if install_dir not in sys.path: |
161 | # We can't consider this an error because if affects every | |
162 | # distro differently. We only warn the user that some | |
163 | # extra configuration is needed to use the bindings | |
164 | print(PY_PATH_WARN_MSG.format(install_dir, install_dir)) | |
165 | ||
cfbd7cf3 | 166 | |
1b8fb862 MJ |
167 | if __name__ == "__main__": |
168 | main() |