Index: compiler-rt/CMakeLists.txt =================================================================== --- compiler-rt/CMakeLists.txt +++ compiler-rt/CMakeLists.txt @@ -570,6 +570,14 @@ # Unittests need access to C++ standard library. string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " ${stdlib_flag}") +# CMAKE_SYSROOT instructs cmake to pass `--sysroot=${CMAKE_SYSROOT}` to compile +# and link steps. For normal cmake targets we don't need to do anything, but for +# lit tests we need to manually add this flag to the compilation options. +# This isn't done here for Apple platforms where sysroot handling is more +# nuanced. See `find_darwin_sdk_dir` and `darwin_add_builtin_library`. +if (NOT APPLE AND (COMPILER_RT_HAS_SYSROOT_FLAG AND CMAKE_SYSROOT)) + string(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS " --sysroot=${CMAKE_SYSROOT}") +endif() # When cross-compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help in compilation # and linking of unittests. Index: compiler-rt/lib/tsan/go/buildgo.sh =================================================================== --- compiler-rt/lib/tsan/go/buildgo.sh +++ compiler-rt/lib/tsan/go/buildgo.sh @@ -220,7 +220,7 @@ fi $CC $DIR/gotsan.cpp -c -o $DIR/race_$SUFFIX.syso $FLAGS $CFLAGS -$CC $OSCFLAGS $ARCHCFLAGS test.c $DIR/race_$SUFFIX.syso -g -o $DIR/test $OSLDFLAGS $LDFLAGS +$CC $OSCFLAGS $ARCHCFLAGS test.c $DIR/race_$SUFFIX.syso -g -o $DIR/test $OSLDFLAGS $LDFLAGS $EXTRA_CFLAGS # Verify that no libc specific code is present. if [ "$DEPENDS_ON_LIBC" != "1" ]; then Index: compiler-rt/lib/tsan/rtl/CMakeLists.txt =================================================================== --- compiler-rt/lib/tsan/rtl/CMakeLists.txt +++ compiler-rt/lib/tsan/rtl/CMakeLists.txt @@ -114,6 +114,8 @@ if("${CMAKE_C_FLAGS}" MATCHES "-Wno-(error=)?unused-command-line-argument") set(EXTRA_CFLAGS "-Wno-error=unused-command-line-argument ${EXTRA_CFLAGS}") endif() +# Make sure the flags in COMPILER_RT_TEST_COMPILER_CFLAGS are also used by buildgo.sh. +set(EXTRA_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS} ${EXTRA_CFLAGS}") if(APPLE) # Ideally we would check the SDK version for the actual platform we are Index: compiler-rt/test/fuzzer/lit.cfg.py =================================================================== --- compiler-rt/test/fuzzer/lit.cfg.py +++ compiler-rt/test/fuzzer/lit.cfg.py @@ -75,7 +75,7 @@ def generate_compiler_cmd(is_cpp=True, fuzzer_enabled=True, msan_enabled=False): compiler_cmd = config.clang - extra_cmd = config.target_flags + extra_cmd = config.target_flags + config.target_cflags if is_cpp: std_cmd = "--driver-mode=g++" Index: compiler-rt/test/lit.common.configured.in =================================================================== --- compiler-rt/test/lit.common.configured.in +++ compiler-rt/test/lit.common.configured.in @@ -7,7 +7,6 @@ # Generic config options for all compiler-rt lit tests. set_default("target_triple", "@COMPILER_RT_DEFAULT_TARGET_TRIPLE@") -set_default("target_cflags", "@COMPILER_RT_TEST_COMPILER_CFLAGS@") set_default("host_arch", "@HOST_ARCH@") set_default("target_arch", "@COMPILER_RT_DEFAULT_TARGET_ARCH@") set_default("host_os", "@HOST_OS@") @@ -68,6 +67,9 @@ set_default("have_zlib", @ZLIB_FOUND_PYBOOL@) set_default("libcxx_used", "@LLVM_LIBCXX_USED@") +target_cflags = getattr(config, "target_cflags", "") +setattr(config, "target_cflags", target_cflags + " @COMPILER_RT_TEST_COMPILER_CFLAGS@") + # LLVM tools dir can be passed in lit parameters, so try to # apply substitution. config.llvm_tools_dir = lit_config.substitute(config.llvm_tools_dir) Index: compiler-rt/test/safestack/lit.cfg.py =================================================================== --- compiler-rt/test/safestack/lit.cfg.py +++ compiler-rt/test/safestack/lit.cfg.py @@ -13,10 +13,16 @@ # Add clang substitutions. config.substitutions.append( - ("%clang_nosafestack ", config.clang + " -O0 -fno-sanitize=safe-stack ") + ( + "%clang_nosafestack ", + config.clang + config.target_cflags + " -O0 -fno-sanitize=safe-stack ", + ) ) config.substitutions.append( - ("%clang_safestack ", config.clang + " -O0 -fsanitize=safe-stack ") + ( + "%clang_safestack ", + config.clang + config.target_cflags + " -O0 -fsanitize=safe-stack ", + ) ) if config.lto_supported: