18c2ecf20Sopenharmony_cifrom os import getenv, path
28c2ecf20Sopenharmony_cifrom subprocess import Popen, PIPE
38c2ecf20Sopenharmony_cifrom re import sub
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_cicc = getenv("CC")
68c2ecf20Sopenharmony_cicc_is_clang = b"clang version" in Popen([cc.split()[0], "-v"], stderr=PIPE).stderr.readline()
78c2ecf20Sopenharmony_cisrc_feature_tests  = getenv('srctree') + '/tools/build/feature'
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cidef clang_has_option(option):
108c2ecf20Sopenharmony_ci    cc_output = Popen([cc, option, path.join(src_feature_tests, "test-hello.c") ], stderr=PIPE).stderr.readlines()
118c2ecf20Sopenharmony_ci    return [o for o in cc_output if ((b"unknown argument" in o) or (b"is not supported" in o))] == [ ]
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciif cc_is_clang:
148c2ecf20Sopenharmony_ci    from distutils.sysconfig import get_config_vars
158c2ecf20Sopenharmony_ci    vars = get_config_vars()
168c2ecf20Sopenharmony_ci    for var in ('CFLAGS', 'OPT'):
178c2ecf20Sopenharmony_ci        vars[var] = sub("-specs=[^ ]+", "", vars[var])
188c2ecf20Sopenharmony_ci        if not clang_has_option("-mcet"):
198c2ecf20Sopenharmony_ci            vars[var] = sub("-mcet", "", vars[var])
208c2ecf20Sopenharmony_ci        if not clang_has_option("-fcf-protection"):
218c2ecf20Sopenharmony_ci            vars[var] = sub("-fcf-protection", "", vars[var])
228c2ecf20Sopenharmony_ci        if not clang_has_option("-fstack-clash-protection"):
238c2ecf20Sopenharmony_ci            vars[var] = sub("-fstack-clash-protection", "", vars[var])
248c2ecf20Sopenharmony_ci        if not clang_has_option("-fstack-protector-strong"):
258c2ecf20Sopenharmony_ci            vars[var] = sub("-fstack-protector-strong", "", vars[var])
268c2ecf20Sopenharmony_ci        if not clang_has_option("-fno-semantic-interposition"):
278c2ecf20Sopenharmony_ci            vars[var] = sub("-fno-semantic-interposition", "", vars[var])
288c2ecf20Sopenharmony_ci        if not clang_has_option("-ffat-lto-objects"):
298c2ecf20Sopenharmony_ci            vars[var] = sub("-ffat-lto-objects", "", vars[var])
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cifrom distutils.core import setup, Extension
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cifrom distutils.command.build_ext   import build_ext   as _build_ext
348c2ecf20Sopenharmony_cifrom distutils.command.install_lib import install_lib as _install_lib
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciclass build_ext(_build_ext):
378c2ecf20Sopenharmony_ci    def finalize_options(self):
388c2ecf20Sopenharmony_ci        _build_ext.finalize_options(self)
398c2ecf20Sopenharmony_ci        self.build_lib  = build_lib
408c2ecf20Sopenharmony_ci        self.build_temp = build_tmp
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ciclass install_lib(_install_lib):
438c2ecf20Sopenharmony_ci    def finalize_options(self):
448c2ecf20Sopenharmony_ci        _install_lib.finalize_options(self)
458c2ecf20Sopenharmony_ci        self.build_dir = build_lib
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cicflags = getenv('CFLAGS', '').split()
498c2ecf20Sopenharmony_ci# switch off several checks (need to be at the end of cflags list)
508c2ecf20Sopenharmony_cicflags += ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls' ]
518c2ecf20Sopenharmony_ciif not cc_is_clang:
528c2ecf20Sopenharmony_ci    cflags += ['-Wno-cast-function-type' ]
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cisrc_perf  = getenv('srctree') + '/tools/perf'
558c2ecf20Sopenharmony_cibuild_lib = getenv('PYTHON_EXTBUILD_LIB')
568c2ecf20Sopenharmony_cibuild_tmp = getenv('PYTHON_EXTBUILD_TMP')
578c2ecf20Sopenharmony_cilibtraceevent = getenv('LIBTRACEEVENT')
588c2ecf20Sopenharmony_cilibapikfs = getenv('LIBAPI')
598c2ecf20Sopenharmony_cilibperf = getenv('LIBPERF')
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciext_sources = [f.strip() for f in open('util/python-ext-sources')
628c2ecf20Sopenharmony_ci				if len(f.strip()) > 0 and f[0] != '#']
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci# use full paths with source files
658c2ecf20Sopenharmony_ciext_sources = list(map(lambda x: '%s/%s' % (src_perf, x) , ext_sources))
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciextra_libraries = []
688c2ecf20Sopenharmony_ciif '-DHAVE_LIBNUMA_SUPPORT' in cflags:
698c2ecf20Sopenharmony_ci    extra_libraries = [ 'numa' ]
708c2ecf20Sopenharmony_ciif '-DHAVE_LIBCAP_SUPPORT' in cflags:
718c2ecf20Sopenharmony_ci    extra_libraries += [ 'cap' ]
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciperf = Extension('perf',
748c2ecf20Sopenharmony_ci		  sources = ext_sources,
758c2ecf20Sopenharmony_ci		  include_dirs = ['util/include'],
768c2ecf20Sopenharmony_ci		  libraries = extra_libraries,
778c2ecf20Sopenharmony_ci		  extra_compile_args = cflags,
788c2ecf20Sopenharmony_ci		  extra_objects = [libtraceevent, libapikfs, libperf],
798c2ecf20Sopenharmony_ci                 )
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cisetup(name='perf',
828c2ecf20Sopenharmony_ci      version='0.1',
838c2ecf20Sopenharmony_ci      description='Interface with the Linux profiling infrastructure',
848c2ecf20Sopenharmony_ci      author='Arnaldo Carvalho de Melo',
858c2ecf20Sopenharmony_ci      author_email='acme@redhat.com',
868c2ecf20Sopenharmony_ci      license='GPLv2',
878c2ecf20Sopenharmony_ci      url='http://perf.wiki.kernel.org',
888c2ecf20Sopenharmony_ci      ext_modules=[perf],
898c2ecf20Sopenharmony_ci      cmdclass={'build_ext': build_ext, 'install_lib': install_lib})
90