Skip to content

Commit 402461b

Browse files
committed
Build libSupport with -Werror=global-constructors (NFC)
Ensure that libSupport does not carry any static global initializer. libSupport can be embedded in use cases where we don't want to load all cl::opt unless we want to parse the command line. ManagedStatic can be used to enable lazy-initialization of globals. The -Werror=global-constructors is only added on platform that have support for the flag and for which std::mutex does not have a global destructor. This is ensured by having CMake trying to compile a file with a global mutex before adding the flag to libSupport.
1 parent 531b19a commit 402461b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ cd ${LIBCXX_BUILD}
123123
ninja cxx cxxabi
124124

125125
FLAGS="${FLAGS} -fno-rtti -fno-exceptions"
126-
LLVM_FLAGS="${FLAGS} -nostdinc++ -I${ZLIB_BUILD} -I${LIBCXX_BUILD}/include/c++/v1"
126+
LLVM_FLAGS="${FLAGS} -nostdinc++ -I${ZLIB_BUILD} -I${LIBCXX_BUILD}/include/c++/v1 -Wno-error=global-constructors"
127127

128128
# Build LLVM.
129129
if [[ ! -d ${LLVM_BUILD} ]]; then

llvm/lib/Support/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
include(GetLibraryName)
22

3+
# Ensure that libSupport does not carry any static global initializer.
4+
# libSupport can be embedded in use cases where we don't want to load all
5+
# cl::opt unless we want to parse the command line.
6+
# ManagedStatic can be used to enable lazy-initialization of globals.
7+
# We don't use `add_flag_if_supported` as instead of compiling an empty file we
8+
# check if the current platform is able to compile global std::mutex with this
9+
# flag (Linux can, Darwin can't for example).
10+
check_cxx_compiler_flag("-Werror=global-constructors" HAS_WERROR_GLOBAL_CTORS)
11+
if (HAS_WERROR_GLOBAL_CTORS)
12+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=global-constructors")
13+
CHECK_CXX_SOURCE_COMPILES("
14+
#include <mutex>
15+
static std::mutex TestGlobalCtorDtor;
16+
static std::recursive_mutex TestGlobalCtorDtor2;
17+
int main() { (void)TestGlobalCtorDtor; (void)TestGlobalCtorDtor2; return 0;}
18+
" LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
19+
if (NOT LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
20+
string(REPLACE "-Werror=global-constructors" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
21+
endif()
22+
endif()
23+
324
if(LLVM_ENABLE_ZLIB)
425
set(imported_libs ZLIB::ZLIB)
526
endif()

0 commit comments

Comments
 (0)