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