xref: /linux/tools/perf/util/setup.py (revision 4cb5dd0379999af455941ab87d0b30c2ba7d9d66)
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
77# switch off several checks (need to be at the end of cflags list)
78extra_cflags = ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls' ]
79if cc_is_clang:
80    extra_cflags += ["-Wno-unused-command-line-argument" ]
81    if clang_has_option("-Wno-cast-function-type-mismatch"):
82        extra_cflags += ["-Wno-cast-function-type-mismatch" ]
83else:
84    extra_cflags += ['-Wno-cast-function-type' ]
85
86# The python headers have mixed code with declarations (decls after asserts, for instance)
87extra_cflags += [ "-Wno-declaration-after-statement" ]
88
89src_perf  = f'{srctree}/tools/perf'
90build_lib = getenv('PYTHON_EXTBUILD_LIB')
91build_tmp = getenv('PYTHON_EXTBUILD_TMP')
92
93perf = Extension('perf',
94                 sources = [ src_perf + '/util/python.c' ],
95		         include_dirs = ['util/include'],
96		         extra_compile_args = extra_cflags,
97                 )
98
99setup(name='perf',
100      version='0.1',
101      description='Interface with the Linux profiling infrastructure',
102      author='Arnaldo Carvalho de Melo',
103      author_email='acme@redhat.com',
104      license='GPLv2',
105      url='http://perf.wiki.kernel.org',
106      ext_modules=[perf],
107      cmdclass={'build_ext': build_ext, 'install_lib': install_lib})
108