1from os import getenv, path 2from subprocess import Popen, PIPE 3from re import sub 4import shlex 5 6cc = getenv("CC") 7assert cc, "Environment variable CC not set" 8 9# Check if CC has options, as is the case in yocto, where it uses CC="cc --sysroot..." 10cc_tokens = cc.split() 11if len(cc_tokens) > 1: 12 cc = cc_tokens[0] 13 cc_options = " ".join([str(e) for e in cc_tokens[1:]]) + " " 14else: 15 cc_options = "" 16 17# ignore optional stderr could be None as it is set to PIPE to avoid that. 18# mypy: disable-error-code="union-attr" 19cc_is_clang = b"clang version" in Popen([cc, "-v"], stderr=PIPE).stderr.readline() 20 21srctree = getenv('srctree') 22assert srctree, "Environment variable srctree, for the Linux sources, not set" 23src_feature_tests = f'{srctree}/tools/build/feature' 24 25def clang_has_option(option): 26 error_substrings = ( 27 b"unknown argument", 28 b"is not supported", 29 b"unknown warning option" 30 ) 31 cmd = shlex.split(f"{cc} {cc_options} {option}") + [ 32 "-o", "/dev/null", 33 path.join(src_feature_tests, "test-hello.c") 34 ] 35 cc_output = Popen(cmd, stderr=PIPE).stderr.readlines() 36 return not any(any(error in line for error in error_substrings) for line in cc_output) 37 38if cc_is_clang: 39 from sysconfig import get_config_vars 40 vars = get_config_vars() 41 for var in ('CFLAGS', 'OPT'): 42 vars[var] = sub("-specs=[^ ]+", "", vars[var]) 43 if not clang_has_option("-mcet"): 44 vars[var] = sub("-mcet", "", vars[var]) 45 if not clang_has_option("-fcf-protection"): 46 vars[var] = sub("-fcf-protection", "", vars[var]) 47 if not clang_has_option("-fstack-clash-protection"): 48 vars[var] = sub("-fstack-clash-protection", "", vars[var]) 49 if not clang_has_option("-fstack-protector-strong"): 50 vars[var] = sub("-fstack-protector-strong", "", vars[var]) 51 if not clang_has_option("-fno-semantic-interposition"): 52 vars[var] = sub("-fno-semantic-interposition", "", vars[var]) 53 if not clang_has_option("-ffat-lto-objects"): 54 vars[var] = sub("-ffat-lto-objects", "", vars[var]) 55 if not clang_has_option("-ftree-loop-distribute-patterns"): 56 vars[var] = sub("-ftree-loop-distribute-patterns", "", vars[var]) 57 if not clang_has_option("-gno-variable-location-views"): 58 vars[var] = sub("-gno-variable-location-views", "", vars[var]) 59 60from setuptools import setup, Extension 61 62from setuptools.command.build_ext import build_ext as _build_ext 63from setuptools.command.install_lib import install_lib as _install_lib 64 65class build_ext(_build_ext): 66 def finalize_options(self): 67 _build_ext.finalize_options(self) 68 self.build_lib = build_lib 69 self.build_temp = build_tmp 70 71class install_lib(_install_lib): 72 def finalize_options(self): 73 _install_lib.finalize_options(self) 74 self.build_dir = build_lib 75 76 77cflags = getenv('CFLAGS', '').split() 78# switch off several checks (need to be at the end of cflags list) 79cflags += ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls' ] 80if cc_is_clang: 81 cflags += ["-Wno-unused-command-line-argument" ] 82 if clang_has_option("-Wno-cast-function-type-mismatch"): 83 cflags += ["-Wno-cast-function-type-mismatch" ] 84else: 85 cflags += ['-Wno-cast-function-type' ] 86 87# The python headers have mixed code with declarations (decls after asserts, for instance) 88cflags += [ "-Wno-declaration-after-statement" ] 89 90src_perf = f'{srctree}/tools/perf' 91build_lib = getenv('PYTHON_EXTBUILD_LIB') 92build_tmp = getenv('PYTHON_EXTBUILD_TMP') 93 94perf = Extension('perf', 95 sources = [ src_perf + '/util/python.c' ], 96 include_dirs = ['util/include'], 97 extra_compile_args = cflags, 98 ) 99 100setup(name='perf', 101 version='0.1', 102 description='Interface with the Linux profiling infrastructure', 103 author='Arnaldo Carvalho de Melo', 104 author_email='acme@redhat.com', 105 license='GPLv2', 106 url='http://perf.wiki.kernel.org', 107 ext_modules=[perf], 108 cmdclass={'build_ext': build_ext, 'install_lib': install_lib}) 109