0947521e6241bafa049c8f1c741bfbd0293d12ec
[babeltrace.git] / src / bindings / python / bt2 / setup.py.in
1 # SPDX-License-Identifier: MIT
2 #
3 # Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com>
4 # Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5
6 import sys
7 import os
8
9 # Distutils was removed in Python 3.12, use setuptools as an alternative.
10 if sys.version_info >= (3, 12):
11 from setuptools import setup, Extension
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):
24 import sysconfig
25
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
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
50 original_get_config_vars = sysconfig.get_config_vars
51
52
53 def get_cflags():
54 cflags = os.environ.get("CFLAGS")
55
56 if cflags is None:
57 [cflags] = original_get_config_vars("CFLAGS")
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():
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")
75
76 if not py_ldshared.startswith(py_cc):
77 return py_ldshared
78
79 if cc and ldflags:
80 return "{} -shared {}".format(cc, ldflags)
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 = {
91 "CFLAGS": get_cflags(),
92 "LDSHARED": get_ldshared(),
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
113 sysconfig.get_config_vars = our_get_config_vars
114
115
116 def main():
117 babeltrace_ext = Extension(
118 "bt2._native_bt",
119 sources=["bt2/native_bt.c", "@srcdir@/bt2/logging.c"],
120 libraries=["babeltrace2", "glib-2.0"],
121 extra_objects=[
122 "@top_builddir@/src/autodisc/.libs/libautodisc.a",
123 "@top_builddir@/src/logging/.libs/liblogging.a",
124 "@top_builddir@/src/common/.libs/libbabeltrace2-common.a",
125 "@top_builddir@/src/py-common/.libs/libpy-common.a",
126 "@top_builddir@/src/string-format/.libs/libstring-format.a",
127 ],
128 )
129
130 dist = setup(
131 name="bt2",
132 version="@PACKAGE_VERSION@",
133 description="Babeltrace 2 Python Bindings",
134 packages=["bt2"],
135 package_dir={"bt2": "bt2"},
136 options={
137 "build": {"build_base": "build", "build_lib": "build/build_lib"},
138 "build_ext": {"build_lib": "build/build_lib"},
139 },
140 url="https://babeltrace.org/",
141 ext_modules=[babeltrace_ext],
142 license="MIT",
143 classifiers=[
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",
148 ],
149 )
150
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.
157
158 if "install" in dist.command_obj:
159 install_dir = dist.command_obj["install"].install_libbase
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
166
167 if __name__ == "__main__":
168 main()
This page took 0.035099 seconds and 3 git commands to generate.