xref: /llvm-project/compiler-rt/test/memprof/lit.cfg.py (revision 0af6c304e48e0484672b53be49a15f411d173e59)
1# -*- Python -*-
2
3import os
4import platform
5import re
6
7import lit.formats
8
9
10def get_required_attr(config, attr_name):
11    attr_value = getattr(config, attr_name, None)
12    if attr_value is None:
13        lit_config.fatal(
14            "No attribute %r in test configuration! You may need to run "
15            "tests from your build directory or add this attribute "
16            "to lit.site.cfg.py " % attr_name
17        )
18    return attr_value
19
20
21# Setup config name.
22config.name = "MemProfiler" + config.name_suffix
23
24# Platform-specific default MEMPROF_OPTIONS for lit tests.
25default_memprof_opts = list(config.default_sanitizer_opts)
26
27default_memprof_opts_str = ":".join(default_memprof_opts)
28if default_memprof_opts_str:
29    config.environment["MEMPROF_OPTIONS"] = default_memprof_opts_str
30    default_memprof_opts_str += ":"
31config.substitutions.append(
32    ("%env_memprof_opts=", "env MEMPROF_OPTIONS=" + default_memprof_opts_str)
33)
34
35# Setup source root.
36config.test_source_root = os.path.dirname(__file__)
37
38libdl_flag = "-ldl"
39
40# Setup default compiler flags used with -fmemory-profile option.
41# FIXME: Review the set of required flags and check if it can be reduced.
42target_cflags = [get_required_attr(config, "target_cflags")]
43target_cxxflags = config.cxx_mode_flags + target_cflags
44clang_memprof_static_cflags = (
45    [
46        "-fmemory-profile",
47        "-mno-omit-leaf-frame-pointer",
48        "-fno-omit-frame-pointer",
49        "-fno-optimize-sibling-calls",
50    ]
51    + config.debug_info_flags
52    + target_cflags
53)
54clang_memprof_static_cxxflags = config.cxx_mode_flags + clang_memprof_static_cflags
55
56memprof_dynamic_flags = []
57if config.memprof_dynamic:
58    memprof_dynamic_flags = ["-shared-libsan"]
59    config.available_features.add("memprof-dynamic-runtime")
60else:
61    config.available_features.add("memprof-static-runtime")
62clang_memprof_cflags = clang_memprof_static_cflags + memprof_dynamic_flags
63clang_memprof_cxxflags = clang_memprof_static_cxxflags + memprof_dynamic_flags
64
65
66def build_invocation(compile_flags):
67    return " " + " ".join([config.clang] + compile_flags) + " "
68
69
70config.substitutions.append(("%clang ", build_invocation(target_cflags)))
71config.substitutions.append(("%clangxx ", build_invocation(target_cxxflags)))
72config.substitutions.append(("%clang_memprof ", build_invocation(clang_memprof_cflags)))
73config.substitutions.append(
74    ("%clangxx_memprof ", build_invocation(clang_memprof_cxxflags))
75)
76if config.memprof_dynamic:
77    shared_libmemprof_path = os.path.join(
78        config.compiler_rt_libdir,
79        "libclang_rt.memprof{}.so".format(config.target_suffix),
80    )
81    config.substitutions.append(("%shared_libmemprof", shared_libmemprof_path))
82    config.substitutions.append(
83        ("%clang_memprof_static ", build_invocation(clang_memprof_static_cflags))
84    )
85    config.substitutions.append(
86        ("%clangxx_memprof_static ", build_invocation(clang_memprof_static_cxxflags))
87    )
88
89config.substitutions.append(("%libdl", libdl_flag))
90
91config.available_features.add("memprof-" + config.bits + "-bits")
92
93config.available_features.add("fast-unwinder-works")
94
95# Set LD_LIBRARY_PATH to pick dynamic runtime up properly.
96new_ld_library_path = os.path.pathsep.join(
97    (config.compiler_rt_libdir, config.environment.get("LD_LIBRARY_PATH", ""))
98)
99config.environment["LD_LIBRARY_PATH"] = new_ld_library_path
100
101# Default test suffixes.
102config.suffixes = [".c", ".cpp"]
103
104config.substitutions.append(("%fPIC", "-fPIC"))
105config.substitutions.append(("%fPIE", "-fPIE"))
106config.substitutions.append(("%pie", "-pie"))
107
108# Only run the tests on supported OSs.
109if config.host_os not in ["Linux"]:
110    config.unsupported = True
111
112if not config.parallelism_group:
113    config.parallelism_group = "shadow-memory"
114