Skip to content

Commit 579cc9a

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.
1 parent 2eb7e5f commit 579cc9a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

llvm/lib/Support/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
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+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=global-constructors")
11+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=global-constructors")
12+
CHECK_CXX_SOURCE_COMPILES("
13+
#include <mutex>
14+
static std::mutex TestGlobalCtorDtor;
15+
static std::recursive_mutex TestGlobalCtorDtor2;
16+
int main() { (void)TestGlobalCtorDtor; (void)TestGlobalCtorDtor2; return 0;}
17+
" LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
18+
if (NOT LLVM_HAS_NOGLOBAL_CTOR_MUTEX)
19+
string(REPLACE "-Werror=global-constructors" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
20+
string(REPLACE "-Werror=global-constructors" "" CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS})
21+
endif()
22+
323
if(LLVM_ENABLE_ZLIB)
424
set(imported_libs ZLIB::ZLIB)
525
endif()

0 commit comments

Comments
 (0)