[compiler-rt] 402461b - Build libSupport with -Werror=global-constructors (NFC)

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 26 21:27:40 PDT 2021


Author: Mehdi Amini
Date: 2021-07-27T04:27:18Z
New Revision: 402461beb051b6a5c158f1e36d8e2c2b676e8804

URL: https://github.com/llvm/llvm-project/commit/402461beb051b6a5c158f1e36d8e2c2b676e8804
DIFF: https://github.com/llvm/llvm-project/commit/402461beb051b6a5c158f1e36d8e2c2b676e8804.diff

LOG: 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.

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
    llvm/lib/Support/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh b/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
index 5b6433011a098..c793875db0990 100755
--- a/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
+++ b/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
@@ -123,7 +123,7 @@ cd ${LIBCXX_BUILD}
 ninja cxx cxxabi
 
 FLAGS="${FLAGS} -fno-rtti -fno-exceptions"
-LLVM_FLAGS="${FLAGS} -nostdinc++ -I${ZLIB_BUILD} -I${LIBCXX_BUILD}/include/c++/v1"
+LLVM_FLAGS="${FLAGS} -nostdinc++ -I${ZLIB_BUILD} -I${LIBCXX_BUILD}/include/c++/v1 -Wno-error=global-constructors"
 
 # Build LLVM.
 if [[ ! -d ${LLVM_BUILD} ]]; then

diff  --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index 2242b0ec60abc..014b4a2caf156 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -1,5 +1,26 @@
 include(GetLibraryName)
 
+# 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.
+# We don't use `add_flag_if_supported` as instead of compiling an empty file we
+# check if the current platform is able to compile global std::mutex with this
+# flag (Linux can, Darwin can't for example).
+check_cxx_compiler_flag("-Werror=global-constructors" HAS_WERROR_GLOBAL_CTORS)
+if (HAS_WERROR_GLOBAL_CTORS)
+  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=global-constructors")
+  CHECK_CXX_SOURCE_COMPILES("
+  #include <mutex>
+  static std::mutex TestGlobalCtorDtor;
+  static std::recursive_mutex TestGlobalCtorDtor2;
+  int main() { (void)TestGlobalCtorDtor; (void)TestGlobalCtorDtor2; return 0;}
+  " LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
+  if (NOT LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
+    string(REPLACE "-Werror=global-constructors" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
+  endif()
+endif()
+
 if(LLVM_ENABLE_ZLIB)
   set(imported_libs ZLIB::ZLIB)
 endif()


        


More information about the llvm-commits mailing list