From b6174350ab9c9c8ed21cf7fb812e1e01a36f2c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Sat, 27 Jan 2024 05:18:03 +0500 Subject: [PATCH] build: CMake: Add option for static runtime Enabled by default on Windows. --- CMakeLists.txt | 12 ++++++++++-- src/CMakeLists.txt | 2 +- src/qt/CMakeLists.txt | 1 + src/qt/test/CMakeLists.txt | 1 + src/test/CMakeLists.txt | 1 + 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 75a021b8f0..8c8ba57859 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,6 @@ if(MSVC) if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") message(FATAL_ERROR "It's not yet possible to build Gridcoin with MSVC") endif() - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) add_compile_options(/U NDEBUG) else() add_compile_options(-UNDEBUG) @@ -85,6 +84,7 @@ option(ENABLE_GUI "Enable Qt-based GUI" OFF) option(ENABLE_DOCS "Build Doxygen documentation" OFF) option(ENABLE_TESTS "Build tests" OFF) option(LUPDATE "Update translation files" OFF) +option(STATIC_RUNTIME "Link runtime statically" ${WIN32}) # CPU-dependent options option(ENABLE_SSE41 "Build code that uses SSE4.1 intrinsics" ${HAS_SSE41}) @@ -245,12 +245,20 @@ endif() set(CMAKE_POSITION_INDEPENDENT_CODE ${ENABLE_PIE}) # Set compiler flags -if (APPLE) +if(APPLE) add_compile_options(-Wno-error=deprecated-declarations) add_compile_options(-Wno-error=thread-safety-analysis) add_compile_options(-Wno-error=thread-safety-reference) endif() +if(STATIC_RUNTIME) + if(CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang)\$") + list(APPEND RUNTIME_LIBS -static-libgcc -static-libstdc++) + elseif(MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() + # Set endianness if(CMAKE_CXX_BYTE_ORDER EQUAL BIG_ENDIAN) set(WORDS_BIGENDIAN 1) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cc1778d82c..d13071850e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -257,7 +257,7 @@ endif() if(ENABLE_DAEMON) add_executable(gridcoinresearchd gridcoinresearchd.cpp) - target_link_libraries(gridcoinresearchd PUBLIC gridcoin_util) + target_link_libraries(gridcoinresearchd PUBLIC ${RUNTIME_LIBS} gridcoin_util) if(WIN32) target_sources(gridcoinresearchd PRIVATE gridcoinresearchd-res.rc) diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index c47c1fa13f..14a7e45b77 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -176,6 +176,7 @@ add_dependencies(gridcoinqt gridcoinqt_l10n) add_executable(gridcoinresearch WIN32 MACOSX_BUNDLE bitcoin.cpp) target_link_libraries(gridcoinresearch PRIVATE + ${RUNTIME_LIBS} Qt5::Widgets gridcoin_util gridcoinqt diff --git a/src/qt/test/CMakeLists.txt b/src/qt/test/CMakeLists.txt index 4717e74597..f6ff90608d 100644 --- a/src/qt/test/CMakeLists.txt +++ b/src/qt/test/CMakeLists.txt @@ -8,6 +8,7 @@ set_target_properties(test_gridcoin-qt PROPERTIES ) target_link_libraries(test_gridcoin-qt PRIVATE + ${RUNTIME_LIBS} Qt5::Test Qt5::Widgets gridcoin_util diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 7bed59e789..1555b347ec 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -56,6 +56,7 @@ add_executable(test_gridcoin add_subdirectory(data) target_link_libraries(test_gridcoin PRIVATE + ${RUNTIME_LIBS} Boost::unit_test_framework gridcoin_util )