From 123a9eee931133c16223ee061c4e49c787566420 Mon Sep 17 00:00:00 2001 From: oltolm Date: Sat, 22 Apr 2023 12:20:03 +0200 Subject: [PATCH] cmake: modernize cmake code and remove unnecessary arguments from toolchain files. --- CMakeLists.txt | 15 +++------- ci/toolchain/aarch64-w64-mingw32-clang.cmake | 4 --- ci/toolchain/i686-w64-mingw32-clang.cmake | 4 --- ci/toolchain/x86_64-w64-mingw32-clang.cmake | 4 --- cmake/StaticCRT.cmake | 30 ++------------------ tests/apps/CMakeLists.txt | 2 +- 6 files changed, 7 insertions(+), 52 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a5cbbd..614f98b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,6 @@ project (drmingw option (ENABLE_COVERAGE "Enable code coverage." OFF) -option (POSIX_THREADS "Allow posix threads." OFF) ############################################################################## @@ -53,6 +52,7 @@ include (StaticCRT) # Avoid Posix threads. Posix threads is required for support of certain C++11 # multi-threading features, but it introduces a new DLL dependency and we don't # use those features. +# https://github.com/jrfonseca/drmingw/issues/82#issuecomment-1360081041 execute_process ( COMMAND "${CMAKE_COMMAND}" -E echo "#include \n#ifdef _GLIBCXX_HAS_GTHREADS\n#error _GLIBCXX_HAS_GTHREADS\n#endif" COMMAND "${CMAKE_CXX_COMPILER}" -x c++ -E - @@ -61,11 +61,7 @@ execute_process ( ERROR_QUIET ) if (NOT STATUS_CXX11_THREADS EQUAL 0) - if (POSIX_THREADS) - message (WARNING "Win32 threads recommended.") - else () - message (SEND_ERROR "Win32 threads required.") - endif () + message (SEND_ERROR "Win32 threads required.") endif () # Enable stack protection @@ -73,11 +69,8 @@ endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8.0" OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8.3") add_compile_options (-fstack-protector-all) - # MinGW doesn't link against libssp automatically, and furthermore - # we want static linking. - set (SSP_LIBRARY "-Wl,-Bstatic -lssp -Wl,-Bdynamic") - set (CMAKE_C_STANDARD_LIBRARIES "${SSP_LIBRARY} ${CMAKE_C_STANDARD_LIBRARIES}") - set (CMAKE_CXX_STANDARD_LIBRARIES "${SSP_LIBRARY} ${CMAKE_CXX_STANDARD_LIBRARIES}") + # MinGW doesn't link against libssp automatically. + link_libraries (ssp) endif () if (CMAKE_SIZEOF_VOID_P EQUAL 8) diff --git a/ci/toolchain/aarch64-w64-mingw32-clang.cmake b/ci/toolchain/aarch64-w64-mingw32-clang.cmake index e688418..15d98ce 100644 --- a/ci/toolchain/aarch64-w64-mingw32-clang.cmake +++ b/ci/toolchain/aarch64-w64-mingw32-clang.cmake @@ -18,7 +18,3 @@ set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) - -# Statically link CRT -set (CMAKE_C_FLAGS "--start-no-unused-arguments -Wl,-Bstatic -lunwind -Wl,-Bdynamic --end-no-unused-arguments") -set (CMAKE_CXX_FLAGS "--start-no-unused-arguments -static-libstdc++ -Wl,-Bstatic -lunwind -Wl,-Bdynamic --end-no-unused-arguments") diff --git a/ci/toolchain/i686-w64-mingw32-clang.cmake b/ci/toolchain/i686-w64-mingw32-clang.cmake index d4b43ac..a679ece 100644 --- a/ci/toolchain/i686-w64-mingw32-clang.cmake +++ b/ci/toolchain/i686-w64-mingw32-clang.cmake @@ -18,7 +18,3 @@ set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) - -# Statically link CRT -set (CMAKE_C_FLAGS "--start-no-unused-arguments -Wl,-Bstatic -lunwind -Wl,-Bdynamic --end-no-unused-arguments") -set (CMAKE_CXX_FLAGS "--start-no-unused-arguments -static-libstdc++ -Wl,-Bstatic -lunwind -Wl,-Bdynamic --end-no-unused-arguments") diff --git a/ci/toolchain/x86_64-w64-mingw32-clang.cmake b/ci/toolchain/x86_64-w64-mingw32-clang.cmake index dd28070..0369a6d 100644 --- a/ci/toolchain/x86_64-w64-mingw32-clang.cmake +++ b/ci/toolchain/x86_64-w64-mingw32-clang.cmake @@ -18,7 +18,3 @@ set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) - -# Statically link CRT -set (CMAKE_C_FLAGS "--start-no-unused-arguments -Wl,-Bstatic -lunwind -Wl,-Bdynamic --end-no-unused-arguments") -set (CMAKE_CXX_FLAGS "--start-no-unused-arguments -static-libstdc++ -Wl,-Bstatic -lunwind -Wl,-Bdynamic --end-no-unused-arguments") diff --git a/cmake/StaticCRT.cmake b/cmake/StaticCRT.cmake index fb55f84..e91dad3 100644 --- a/cmake/StaticCRT.cmake +++ b/cmake/StaticCRT.cmake @@ -2,35 +2,9 @@ include (CheckCXXCompilerFlag) if (MINGW) # Avoid depending on MinGW runtime DLLs + add_link_options (-static) if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - check_cxx_compiler_flag (-static-libgcc HAVE_STATIC_LIBGCC_FLAG) - if (HAVE_STATIC_LIBGCC_FLAG) - set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc") - set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc") - set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -static-libgcc") - endif () - endif () - if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - check_cxx_compiler_flag (-static-libstdc++ HAVE_STATIC_LIBSTDCXX_FLAG) - if (HAVE_STATIC_LIBSTDCXX_FLAG) - set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++") - set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++") - set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -static-libstdc++") - endif () - - # Statically link Posix threads when detected. - execute_process ( - COMMAND "${CMAKE_COMMAND}" -E echo "#include \n#ifdef _GLIBCXX_HAS_GTHREADS\n#error _GLIBCXX_HAS_GTHREADS\n#endif" - COMMAND "${CMAKE_CXX_COMPILER}" -x c++ -E - - RESULT_VARIABLE STATUS_CXX11_THREADS - OUTPUT_QUIET - ERROR_QUIET - ) - if (NOT STATUS_CXX11_THREADS EQUAL 0) - # https://stackoverflow.com/a/28001271 - # XXX Unfortunately this doesn't work for Release builds, https://github.com/jrfonseca/drmingw/issues/82 - set (CMAKE_CXX_STANDARD_LIBRARIES "-Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic ${CMAKE_CXX_STANDARD_LIBRARIES}") - endif () + add_link_options (-static-libgcc -static-libstdc++) endif () endif () diff --git a/tests/apps/CMakeLists.txt b/tests/apps/CMakeLists.txt index 84a9836..40f5fdb 100644 --- a/tests/apps/CMakeLists.txt +++ b/tests/apps/CMakeLists.txt @@ -134,7 +134,7 @@ add_test_executable (ud2 ud2.c) if (MINGW) target_compile_options (stack_buffer_overflow PRIVATE -fstack-protector-all) target_link_options (stack_buffer_overflow PRIVATE -fstack-protector-all) - target_link_libraries (stack_buffer_overflow PRIVATE -Wl,-Bstatic -lssp -Wl,-Bdynamic) + target_link_libraries (stack_buffer_overflow PRIVATE ssp) endif () if (MSVC) target_compile_options (stack_buffer_overflow PRIVATE /GS)