From 36790074c3aaed4e8f7845293c2c7773c1d4c21b Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Thu, 17 Oct 2019 21:53:15 +0000 Subject: [PATCH] cmake: fix linking to libssp and linking bin2c Only add the macro `-D_FORTIFY_SOURCE=2` if we are linking to libssp with gcc, do not use it at all on clang. For `HostCompile.cmake` add the macro `-Dmain=main` to redefine the macro added by SDL2 to compile flags `-Dmain=SDL_main`. Fix #548. Signed-off-by: Rafael Kitover --- CMakeLists.txt | 25 +++++++++++-------------- cmake/HostCompile.cmake | 14 +++++++++----- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 509755a4..4142a45e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -507,8 +507,17 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang) # common flags set(MY_C_FLAGS -pipe -Wformat -Wformat-security -feliminate-unused-debug-types) - if(NOT CMAKE_BUILD_TYPE MATCHES Debug) - list(APPEND MY_C_FLAGS -D_FORTIFY_SOURCE=2) + if(ENABLE_SSP AND CMAKE_BUILD_TYPE STREQUAL Debug) + if(WIN32) + set(SSP_STATIC ON) + endif() + + find_package(SSP) + + if(SSP_LIBRARY) + list(APPEND MY_C_FLAGS -D_FORTIFY_SOURCE=2) + list(APPEND MY_C_LINKER_FLAGS ${SSP_LIBRARY}) + endif() endif() if(NOT (WIN32 OR X86_32)) # inline asm is not allowed with -fPIC @@ -603,18 +612,6 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang) include(UseGCCBinUtilsWrappers) endif() - if(WIN32) - set(SSP_STATIC ON) - endif() - - if(ENABLE_SSP) - find_package(SSP) - - if(SSP_LIBRARY) - set(MY_C_LINKER_FLAGS ${MY_C_LINKER_FLAGS} ${SSP_LIBRARY}) - endif() - endif() - set(MY_C_LINKER_FLAGS ${MY_C_LINKER_FLAGS} -Wl,-allow-multiple-definition) if(CMAKE_PREFIX_PATH) diff --git a/cmake/HostCompile.cmake b/cmake/HostCompile.cmake index 2777e5c3..ef0b17ae 100644 --- a/cmake/HostCompile.cmake +++ b/cmake/HostCompile.cmake @@ -9,17 +9,21 @@ function(host_compile src dst_cmd) set(link_flags -Wl,--subsystem,console) endif() else() - set(dst "${dst_cmd}") + set(dst ${dst_cmd}) endif() # assume cc foo.c -o foo # will work on most hosts add_custom_command( - OUTPUT "${dst}" + OUTPUT ${dst} COMMAND cc ${src} -o ${dst} ${link_flags} - DEPENDS "${src}" + DEPENDS ${src} ) else() - get_filename_component(dst "${dst_cmd}" NAME) - add_executable("${dst}" "${src}") + get_filename_component(dst ${dst_cmd} NAME) + + add_executable(${dst} ${src}) + + # this is necessary because we override main with SDL_main + target_compile_definitions(${dst} PRIVATE -Dmain=main) endif() endfunction()