[llvm] 579cc9a - Build libSupport with -Werror=global-constructors (NFC)

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 25 20:04:50 PDT 2021


Author: Mehdi Amini
Date: 2021-07-26T03:04:31Z
New Revision: 579cc9ad2e2db6c3f1670b9f42c2cfe67bc5722c

URL: https://github.com/llvm/llvm-project/commit/579cc9ad2e2db6c3f1670b9f42c2cfe67bc5722c
DIFF: https://github.com/llvm/llvm-project/commit/579cc9ad2e2db6c3f1670b9f42c2cfe67bc5722c.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.

Added: 
    

Modified: 
    llvm/lib/Support/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index 2242b0ec60ab..94d1b02076b0 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -1,5 +1,25 @@
 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).
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=global-constructors")
+SET(CMAKE_C_FLAGS "${CMAKE_C_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})
+  string(REPLACE "-Werror=global-constructors" "" CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS})
+endif()
+
 if(LLVM_ENABLE_ZLIB)
   set(imported_libs ZLIB::ZLIB)
 endif()


        


More information about the llvm-commits mailing list