| 1 | # The MIT License (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 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | # of this software and associated documentation files (the "Software"), to deal |
| 8 | # in the Software without restriction, including without limitation the rights |
| 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | # copies of the Software, and to permit persons to whom the Software is |
| 11 | # furnished to do so, subject to the following conditions: |
| 12 | # |
| 13 | # The above copyright notice and this permission notice shall be included in |
| 14 | # all copies or substantial portions of the Software. |
| 15 | # |
| 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | # THE SOFTWARE. |
| 23 | |
| 24 | import sys |
| 25 | import os |
| 26 | import distutils.sysconfig |
| 27 | |
| 28 | from distutils.core import setup, Extension |
| 29 | |
| 30 | PY_PATH_WARN_MSG = """ |
| 31 | -------------------------------------WARNING------------------------------------ |
| 32 | The install directory used:\n ({})\nis not included in your PYTHONPATH. |
| 33 | |
| 34 | To add this directory to your Python search path permanently you can add the |
| 35 | following command to your .bashrc/.zshrc: |
| 36 | export PYTHONPATH="${{PYTHONPATH}}:{}" |
| 37 | -------------------------------------------------------------------------------- |
| 38 | """ |
| 39 | |
| 40 | original_get_config_vars = distutils.sysconfig.get_config_vars |
| 41 | |
| 42 | |
| 43 | def get_cflags(): |
| 44 | cflags = os.environ.get('CFLAGS') |
| 45 | |
| 46 | if cflags is None: |
| 47 | [cflags] = original_get_config_vars('CFLAGS') |
| 48 | |
| 49 | return cflags |
| 50 | |
| 51 | |
| 52 | # distutils performs a similar transformation step on LDSHARED on |
| 53 | # darwin to use the overriden CC as the default command for LDSHARED |
| 54 | # (see distutils' customize_compiler() step in the sysconfig module). |
| 55 | # |
| 56 | # This takes it a step further by using our own LDFLAGS (when available) |
| 57 | # along with the overriden compiler and ensure that flags that are unsupported |
| 58 | # by either the Python interprter's CC or the overriden CC don't cause a |
| 59 | # build failure. |
| 60 | def get_ldshared(): |
| 61 | cc = os.environ.get('CC') |
| 62 | ldflags = os.environ.get('LDFLAGS') |
| 63 | [py_cc] = original_get_config_vars('CC') |
| 64 | [py_ldshared] = original_get_config_vars('LDSHARED') |
| 65 | |
| 66 | if not py_ldshared.startswith(py_cc): |
| 67 | return py_ldshared |
| 68 | |
| 69 | if cc and ldflags: |
| 70 | return '{} -shared {}'.format(cc, ldflags) |
| 71 | elif cc: |
| 72 | return cc + py_ldshared[len(py_cc) :] |
| 73 | elif ldflags: |
| 74 | return py_cc + py_ldshared[len(py_cc) :] |
| 75 | else: |
| 76 | return py_ldshared |
| 77 | |
| 78 | |
| 79 | def our_get_config_vars(*args): |
| 80 | overridden_config_vars = { |
| 81 | 'CFLAGS': get_cflags(), |
| 82 | 'LDSHARED': get_ldshared(), |
| 83 | } |
| 84 | |
| 85 | if len(args) == 0: |
| 86 | all_config_vars = original_get_config_vars() |
| 87 | for name in overridden_config_vars: |
| 88 | all_config_vars[name] = overridden_config_vars[name] |
| 89 | return all_config_vars |
| 90 | |
| 91 | subset_config_vars = [] |
| 92 | for name in args: |
| 93 | if name not in overridden_config_vars: |
| 94 | [var] = original_get_config_vars(name) |
| 95 | subset_config_vars.append(var) |
| 96 | continue |
| 97 | |
| 98 | subset_config_vars.append(overridden_config_vars[name]) |
| 99 | |
| 100 | return subset_config_vars |
| 101 | |
| 102 | |
| 103 | distutils.sysconfig.get_config_vars = our_get_config_vars |
| 104 | |
| 105 | |
| 106 | def main(): |
| 107 | babeltrace_ext = Extension( |
| 108 | 'bt2._native_bt', |
| 109 | sources=['bt2/native_bt.c', '@srcdir@/bt2/logging.c'], |
| 110 | libraries=['babeltrace2', 'glib-2.0'], |
| 111 | extra_objects=[ |
| 112 | '@top_builddir@/src/autodisc/.libs/libbabeltrace2-autodisc.a', |
| 113 | '@top_builddir@/src/logging/.libs/libbabeltrace2-logging.a', |
| 114 | '@top_builddir@/src/common/.libs/libbabeltrace2-common.a', |
| 115 | '@top_builddir@/src/py-common/.libs/libbabeltrace2-py-common.a', |
| 116 | '@top_builddir@/src/string-format/.libs/libbabeltrace2-string-format.a', |
| 117 | ], |
| 118 | ) |
| 119 | |
| 120 | dist = setup( |
| 121 | name='bt2', |
| 122 | version='@PACKAGE_VERSION@', |
| 123 | description='Babeltrace 2 Python Bindings', |
| 124 | packages=['bt2'], |
| 125 | package_dir={'bt2': 'bt2'}, |
| 126 | options={ |
| 127 | 'build': {'build_base': 'build', 'build_lib': 'build/build_lib'}, |
| 128 | 'build_ext': {'build_lib': 'build/build_lib'}, |
| 129 | }, |
| 130 | url='https://babeltrace.org/', |
| 131 | ext_modules=[babeltrace_ext], |
| 132 | license='MIT', |
| 133 | classifiers=[ |
| 134 | 'Development Status :: 5 - Production/Stable', |
| 135 | 'Intended Audience :: Developers', |
| 136 | 'License :: OSI Approved :: The MIT License', |
| 137 | 'Programming Language :: Python :: 3' 'Topic :: System :: Logging', |
| 138 | ], |
| 139 | ) |
| 140 | |
| 141 | # After the installation, we check that the install directory is included in |
| 142 | # the Python search path and we print a warning message when it's not. |
| 143 | # We need to do this because Python search path differs depending on the distro |
| 144 | # and some distros don't include any /usr/local/ in the search path. This is |
| 145 | # also useful for out-of-tree installs and tests. |
| 146 | # It's only relevant to make this check on the `install` command. |
| 147 | |
| 148 | if 'install' in dist.command_obj: |
| 149 | install_dir = dist.command_obj['install'].install_libbase |
| 150 | if install_dir not in sys.path: |
| 151 | # We can't consider this an error because if affects every |
| 152 | # distro differently. We only warn the user that some |
| 153 | # extra configuration is needed to use the bindings |
| 154 | print(PY_PATH_WARN_MSG.format(install_dir, install_dir)) |
| 155 | |
| 156 | |
| 157 | if __name__ == "__main__": |
| 158 | main() |