bt2/setup.py.in: improve legibility of our_get_config_vars a bit
[babeltrace.git] / src / bindings / python / bt2 / setup.py.in
CommitLineData
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
6import sys
afdd1f82 7import os
1b8fb862 8
d617eb92
MJ
9# Distutils was removed in Python 3.12, use setuptools as an alternative.
10if sys.version_info >= (3, 12):
6ec97181 11 from setuptools import setup, Extension
d617eb92
MJ
12else:
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.
23if 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
37else:
38 import distutils.sysconfig as sysconfig
39
1b8fb862
MJ
40PY_PATH_WARN_MSG = """
41-------------------------------------WARNING------------------------------------
42The install directory used:\n ({})\nis not included in your PYTHONPATH.
43
44To add this directory to your Python search path permanently you can add the
45following command to your .bashrc/.zshrc:
46 export PYTHONPATH="${{PYTHONPATH}}:{}"
47--------------------------------------------------------------------------------
48"""
49
452480eb 50original_get_config_vars = sysconfig.get_config_vars
afdd1f82
JG
51
52
53def 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
e7401568 63# darwin to use the overridden CC as the default command for LDSHARED
afdd1f82
JG
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)
e7401568
SM
67# along with the overridden compiler and ensure that flags that are unsupported
68# by either the Python interprter's CC or the overridden CC don't cause a
afdd1f82
JG
69# build failure.
70def 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
89def 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:
abc2ea2d 96 # Return a dict with all config vars.
afdd1f82
JG
97 all_config_vars = original_get_config_vars()
98 for name in overridden_config_vars:
99 all_config_vars[name] = overridden_config_vars[name]
afdd1f82 100
abc2ea2d
SM
101 return all_config_vars
102 else:
103 # Return a list with the requested config vars.
104 subset_config_vars = []
105 for name in args:
106 if name in overridden_config_vars:
107 subset_config_vars.append(overridden_config_vars[name])
108 else:
109 subset_config_vars.append(original_get_config_vars(name)[0])
110
111 return subset_config_vars
afdd1f82
JG
112
113
452480eb 114sysconfig.get_config_vars = our_get_config_vars
afdd1f82 115
cfbd7cf3 116
1b8fb862 117def main():
cfbd7cf3 118 babeltrace_ext = Extension(
f5567ea8
FD
119 "bt2._native_bt",
120 sources=["bt2/native_bt.c", "@srcdir@/bt2/logging.c"],
121 libraries=["babeltrace2", "glib-2.0"],
cfbd7cf3 122 extra_objects=[
86ef6105
MJ
123 "@top_builddir@/src/autodisc/.libs/libautodisc.a",
124 "@top_builddir@/src/logging/.libs/liblogging.a",
ed615696 125 "@top_builddir@/src/common/.libs/libcommon.a",
86ef6105
MJ
126 "@top_builddir@/src/py-common/.libs/libpy-common.a",
127 "@top_builddir@/src/string-format/.libs/libstring-format.a",
cfbd7cf3
FD
128 ],
129 )
1b8fb862 130
cfbd7cf3 131 dist = setup(
f5567ea8
FD
132 name="bt2",
133 version="@PACKAGE_VERSION@",
134 description="Babeltrace 2 Python Bindings",
135 packages=["bt2"],
136 package_dir={"bt2": "bt2"},
cfbd7cf3 137 options={
f5567ea8
FD
138 "build": {"build_base": "build", "build_lib": "build/build_lib"},
139 "build_ext": {"build_lib": "build/build_lib"},
cfbd7cf3 140 },
f5567ea8 141 url="https://babeltrace.org/",
cfbd7cf3 142 ext_modules=[babeltrace_ext],
f5567ea8 143 license="MIT",
cfbd7cf3 144 classifiers=[
f5567ea8
FD
145 "Development Status :: 5 - Production/Stable",
146 "Intended Audience :: Developers",
147 "License :: OSI Approved :: The MIT License",
148 "Programming Language :: Python :: 3" "Topic :: System :: Logging",
cfbd7cf3
FD
149 ],
150 )
1b8fb862 151
cfbd7cf3
FD
152 # After the installation, we check that the install directory is included in
153 # the Python search path and we print a warning message when it's not.
154 # We need to do this because Python search path differs depending on the distro
155 # and some distros don't include any /usr/local/ in the search path. This is
156 # also useful for out-of-tree installs and tests.
157 # It's only relevant to make this check on the `install` command.
1b8fb862 158
f5567ea8
FD
159 if "install" in dist.command_obj:
160 install_dir = dist.command_obj["install"].install_libbase
1b8fb862
MJ
161 if install_dir not in sys.path:
162 # We can't consider this an error because if affects every
163 # distro differently. We only warn the user that some
164 # extra configuration is needed to use the bindings
165 print(PY_PATH_WARN_MSG.format(install_dir, install_dir))
166
cfbd7cf3 167
1b8fb862
MJ
168if __name__ == "__main__":
169 main()
This page took 0.129318 seconds and 4 git commands to generate.