Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1 | from os import getenv, path |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | from subprocess import Popen, PIPE |
| 3 | from re import sub |
| 4 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5 | cc = getenv("CC") |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 6 | cc_is_clang = b"clang version" in Popen([cc.split()[0], "-v"], stderr=PIPE).stderr.readline() |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 7 | src_feature_tests = getenv('srctree') + '/tools/build/feature' |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 8 | |
| 9 | def clang_has_option(option): |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 10 | cc_output = Popen([cc, option, path.join(src_feature_tests, "test-hello.c") ], stderr=PIPE).stderr.readlines() |
| 11 | return [o for o in cc_output if ((b"unknown argument" in o) or (b"is not supported" in o))] == [ ] |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 12 | |
| 13 | if cc_is_clang: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 14 | from distutils.sysconfig import get_config_vars |
| 15 | vars = get_config_vars() |
| 16 | for var in ('CFLAGS', 'OPT'): |
| 17 | vars[var] = sub("-specs=[^ ]+", "", vars[var]) |
| 18 | if not clang_has_option("-mcet"): |
| 19 | vars[var] = sub("-mcet", "", vars[var]) |
| 20 | if not clang_has_option("-fcf-protection"): |
| 21 | vars[var] = sub("-fcf-protection", "", vars[var]) |
| 22 | if not clang_has_option("-fstack-clash-protection"): |
| 23 | vars[var] = sub("-fstack-clash-protection", "", vars[var]) |
| 24 | if not clang_has_option("-fstack-protector-strong"): |
| 25 | vars[var] = sub("-fstack-protector-strong", "", vars[var]) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 26 | if not clang_has_option("-fno-semantic-interposition"): |
| 27 | vars[var] = sub("-fno-semantic-interposition", "", vars[var]) |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 28 | if not clang_has_option("-ffat-lto-objects"): |
| 29 | vars[var] = sub("-ffat-lto-objects", "", vars[var]) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 30 | |
| 31 | from distutils.core import setup, Extension |
| 32 | |
| 33 | from distutils.command.build_ext import build_ext as _build_ext |
| 34 | from distutils.command.install_lib import install_lib as _install_lib |
| 35 | |
| 36 | class build_ext(_build_ext): |
| 37 | def finalize_options(self): |
| 38 | _build_ext.finalize_options(self) |
| 39 | self.build_lib = build_lib |
| 40 | self.build_temp = build_tmp |
| 41 | |
| 42 | class install_lib(_install_lib): |
| 43 | def finalize_options(self): |
| 44 | _install_lib.finalize_options(self) |
| 45 | self.build_dir = build_lib |
| 46 | |
| 47 | |
| 48 | cflags = getenv('CFLAGS', '').split() |
| 49 | # switch off several checks (need to be at the end of cflags list) |
| 50 | cflags += ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls' ] |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 51 | if not cc_is_clang: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 52 | cflags += ['-Wno-cast-function-type' ] |
| 53 | |
| 54 | src_perf = getenv('srctree') + '/tools/perf' |
| 55 | build_lib = getenv('PYTHON_EXTBUILD_LIB') |
| 56 | build_tmp = getenv('PYTHON_EXTBUILD_TMP') |
| 57 | libtraceevent = getenv('LIBTRACEEVENT') |
| 58 | libapikfs = getenv('LIBAPI') |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 59 | libperf = getenv('LIBPERF') |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | |
| 61 | ext_sources = [f.strip() for f in open('util/python-ext-sources') |
| 62 | if len(f.strip()) > 0 and f[0] != '#'] |
| 63 | |
| 64 | # use full paths with source files |
| 65 | ext_sources = list(map(lambda x: '%s/%s' % (src_perf, x) , ext_sources)) |
| 66 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 67 | extra_libraries = [] |
| 68 | if '-DHAVE_LIBNUMA_SUPPORT' in cflags: |
| 69 | extra_libraries = [ 'numa' ] |
| 70 | if '-DHAVE_LIBCAP_SUPPORT' in cflags: |
| 71 | extra_libraries += [ 'cap' ] |
| 72 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 73 | perf = Extension('perf', |
| 74 | sources = ext_sources, |
| 75 | include_dirs = ['util/include'], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 76 | libraries = extra_libraries, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 77 | extra_compile_args = cflags, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 78 | extra_objects = [libtraceevent, libapikfs, libperf], |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 79 | ) |
| 80 | |
| 81 | setup(name='perf', |
| 82 | version='0.1', |
| 83 | description='Interface with the Linux profiling infrastructure', |
| 84 | author='Arnaldo Carvalho de Melo', |
| 85 | author_email='acme@redhat.com', |
| 86 | license='GPLv2', |
| 87 | url='http://perf.wiki.kernel.org', |
| 88 | ext_modules=[perf], |
| 89 | cmdclass={'build_ext': build_ext, 'install_lib': install_lib}) |