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