Skip to content

Commit

Permalink
cmake: modernize cmake code and remove unnecessary arguments from too…
Browse files Browse the repository at this point in the history
…lchain files.
  • Loading branch information
oltolm authored and jrfonseca committed Apr 23, 2023
1 parent a85f4eb commit 123a9ee
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 52 deletions.
15 changes: 4 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ project (drmingw


option (ENABLE_COVERAGE "Enable code coverage." OFF)
option (POSIX_THREADS "Allow posix threads." OFF)


##############################################################################
Expand Down Expand Up @@ -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 <thread>\n#ifdef _GLIBCXX_HAS_GTHREADS\n#error _GLIBCXX_HAS_GTHREADS\n#endif"
COMMAND "${CMAKE_CXX_COMPILER}" -x c++ -E -
Expand All @@ -61,23 +61,16 @@ 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
# XXX: Broken on https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86832
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)
Expand Down
4 changes: 0 additions & 4 deletions ci/toolchain/aarch64-w64-mingw32-clang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
4 changes: 0 additions & 4 deletions ci/toolchain/i686-w64-mingw32-clang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
4 changes: 0 additions & 4 deletions ci/toolchain/x86_64-w64-mingw32-clang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
30 changes: 2 additions & 28 deletions cmake/StaticCRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 <thread>\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 ()

Expand Down
2 changes: 1 addition & 1 deletion tests/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 123a9ee

Please sign in to comment.