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