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