1from os import getenv, path
2from subprocess import Popen, PIPE
3from re import sub
4
5cc = getenv("CC")
6
7# Check if CC has options, as is the case in yocto, where it uses CC="cc --sysroot..."
8cc_tokens = cc.split()
9if len(cc_tokens) > 1:
10    cc = cc_tokens[0]
11    cc_options = " ".join([str(e) for e in cc_tokens[1:]]) + " "
12else:
13    cc_options = ""
14
15cc_is_clang = b"clang version" in Popen([cc, "-v"], stderr=PIPE).stderr.readline()
16src_feature_tests  = getenv('srctree') + '/tools/build/feature'
17
18def clang_has_option(option):
19    cc_output = Popen([cc, cc_options + option, path.join(src_feature_tests, "test-hello.c") ], stderr=PIPE).stderr.readlines()
20    return [o for o in cc_output if ((b"unknown argument" in o) or (b"is not supported" in o))] == [ ]
21
22if cc_is_clang:
23    from sysconfig import get_config_vars
24    vars = get_config_vars()
25    for var in ('CFLAGS', 'OPT'):
26        vars[var] = sub("-specs=[^ ]+", "", vars[var])
27        if not clang_has_option("-mcet"):
28            vars[var] = sub("-mcet", "", vars[var])
29        if not clang_has_option("-fcf-protection"):
30            vars[var] = sub("-fcf-protection", "", vars[var])
31        if not clang_has_option("-fstack-clash-protection"):
32            vars[var] = sub("-fstack-clash-protection", "", vars[var])
33        if not clang_has_option("-fstack-protector-strong"):
34            vars[var] = sub("-fstack-protector-strong", "", vars[var])
35        if not clang_has_option("-fno-semantic-interposition"):
36            vars[var] = sub("-fno-semantic-interposition", "", vars[var])
37        if not clang_has_option("-ffat-lto-objects"):
38            vars[var] = sub("-ffat-lto-objects", "", vars[var])
39
40from setuptools import setup, Extension
41
42from setuptools.command.build_ext   import build_ext   as _build_ext
43from setuptools.command.install_lib import install_lib as _install_lib
44
45class build_ext(_build_ext):
46    def finalize_options(self):
47        _build_ext.finalize_options(self)
48        self.build_lib  = build_lib
49        self.build_temp = build_tmp
50
51class install_lib(_install_lib):
52    def finalize_options(self):
53        _install_lib.finalize_options(self)
54        self.build_dir = build_lib
55
56
57cflags = getenv('CFLAGS', '').split()
58# switch off several checks (need to be at the end of cflags list)
59cflags += ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls', '-DPYTHON_PERF' ]
60if cc_is_clang:
61    cflags += ["-Wno-unused-command-line-argument" ]
62else:
63    cflags += ['-Wno-cast-function-type' ]
64
65src_perf  = getenv('srctree') + '/tools/perf'
66build_lib = getenv('PYTHON_EXTBUILD_LIB')
67build_tmp = getenv('PYTHON_EXTBUILD_TMP')
68libtraceevent = getenv('LIBTRACEEVENT')
69libapikfs = getenv('LIBAPI')
70libperf = getenv('LIBPERF')
71
72ext_sources = [f.strip() for f in open('util/python-ext-sources')
73				if len(f.strip()) > 0 and f[0] != '#']
74
75extra_libraries = []
76
77if '-DHAVE_LIBTRACEEVENT' in cflags:
78    extra_libraries += [ 'traceevent' ]
79else:
80    ext_sources.remove('util/trace-event.c')
81
82# use full paths with source files
83ext_sources = list(map(lambda x: '%s/%s' % (src_perf, x) , ext_sources))
84
85if '-DHAVE_LIBNUMA_SUPPORT' in cflags:
86    extra_libraries += [ 'numa' ]
87if '-DHAVE_LIBCAP_SUPPORT' in cflags:
88    extra_libraries += [ 'cap' ]
89
90perf = Extension('perf',
91		  sources = ext_sources,
92		  include_dirs = ['util/include'],
93		  libraries = extra_libraries,
94		  extra_compile_args = cflags,
95		  extra_objects = [ x for x in [libtraceevent, libapikfs, libperf]
96                                    if x is not None],
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