xref: /linux/tools/perf/util/setup.py (revision e7e86d7697c6ed1dbbde18d7185c35b6967945ed)
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    cmd = shlex.split(f"{cc} {cc_options} {option}")
27    cmd.append(path.join(src_feature_tests, "test-hello.c"))
28    cc_output = Popen(cmd, stderr=PIPE).stderr.readlines()
29    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))] == [ ]
30
31if cc_is_clang:
32    from sysconfig import get_config_vars
33    vars = get_config_vars()
34    for var in ('CFLAGS', 'OPT'):
35        vars[var] = sub("-specs=[^ ]+", "", vars[var])
36        if not clang_has_option("-mcet"):
37            vars[var] = sub("-mcet", "", vars[var])
38        if not clang_has_option("-fcf-protection"):
39            vars[var] = sub("-fcf-protection", "", vars[var])
40        if not clang_has_option("-fstack-clash-protection"):
41            vars[var] = sub("-fstack-clash-protection", "", vars[var])
42        if not clang_has_option("-fstack-protector-strong"):
43            vars[var] = sub("-fstack-protector-strong", "", vars[var])
44        if not clang_has_option("-fno-semantic-interposition"):
45            vars[var] = sub("-fno-semantic-interposition", "", vars[var])
46        if not clang_has_option("-ffat-lto-objects"):
47            vars[var] = sub("-ffat-lto-objects", "", vars[var])
48        if not clang_has_option("-ftree-loop-distribute-patterns"):
49            vars[var] = sub("-ftree-loop-distribute-patterns", "", vars[var])
50        if not clang_has_option("-gno-variable-location-views"):
51            vars[var] = sub("-gno-variable-location-views", "", vars[var])
52
53from setuptools import setup, Extension
54
55from setuptools.command.build_ext   import build_ext   as _build_ext
56from setuptools.command.install_lib import install_lib as _install_lib
57
58class build_ext(_build_ext):
59    def finalize_options(self):
60        _build_ext.finalize_options(self)
61        self.build_lib  = build_lib
62        self.build_temp = build_tmp
63
64class install_lib(_install_lib):
65    def finalize_options(self):
66        _install_lib.finalize_options(self)
67        self.build_dir = build_lib
68
69
70cflags = getenv('CFLAGS', '').split()
71# switch off several checks (need to be at the end of cflags list)
72cflags += ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls' ]
73if cc_is_clang:
74    cflags += ["-Wno-unused-command-line-argument" ]
75    if clang_has_option("-Wno-cast-function-type-mismatch"):
76        cflags += ["-Wno-cast-function-type-mismatch" ]
77else:
78    cflags += ['-Wno-cast-function-type' ]
79
80# The python headers have mixed code with declarations (decls after asserts, for instance)
81cflags += [ "-Wno-declaration-after-statement" ]
82
83src_perf  = f'{srctree}/tools/perf'
84build_lib = getenv('PYTHON_EXTBUILD_LIB')
85build_tmp = getenv('PYTHON_EXTBUILD_TMP')
86
87perf = Extension('perf',
88                 sources = [ src_perf + '/util/python.c' ],
89		         include_dirs = ['util/include'],
90		         extra_compile_args = cflags,
91                 )
92
93setup(name='perf',
94      version='0.1',
95      description='Interface with the Linux profiling infrastructure',
96      author='Arnaldo Carvalho de Melo',
97      author_email='acme@redhat.com',
98      license='GPLv2',
99      url='http://perf.wiki.kernel.org',
100      ext_modules=[perf],
101      cmdclass={'build_ext': build_ext, 'install_lib': install_lib})
102