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