CMake improvements: Move _FORTIFY_SOURCE=2 definition to targets, do not apply in Debug mode. Make use of add_compile_definitions() instead of add_definitions() when available

This commit is contained in:
Carles Fernandez 2022-12-04 00:05:07 +01:00
commit c504b16568
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
3 changed files with 31 additions and 12 deletions

View File

@ -274,7 +274,11 @@ endif()
# allow 'large' files in 32 bit builds
if(UNIX)
add_definitions(-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES)
if(CMAKE_VERSION VERSION_GREATER 3.12.0)
add_compile_definitions(_LARGEFILE_SOURCE _FILE_OFFSET_BITS=64 _LARGE_FILES)
else()
add_definitions(-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES)
endif()
endif()
# If this is an out-of-tree build, do not pollute the original source directory

View File

@ -9,9 +9,6 @@
# Project setup
########################################################################
cmake_minimum_required(VERSION 2.8.12...3.25)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose build type: None Debug Release RelWithDebInfo MinSizeRel")
project(volk_gnsssdr)
enable_language(CXX)
@ -227,7 +224,11 @@ endif()
set(CROSSCOMPILE_MULTILIB ${CROSSCOMPILE_MULTILIB} CACHE STRING "Define \"true\" if you have and want to use multiple C development libs installed for cross compile")
if(MSVC)
add_definitions(-D_USE_MATH_DEFINES) # enables math constants on all supported versions of MSVC
if(CMAKE_VERSION VERSION_GREATER 3.12.0)
add_compile_definitions(_USE_MATH_DEFINES) # enables math constants on all supported versions of MSVC
else()
add_definitions(-D_USE_MATH_DEFINES) # enables math constants on all supported versions of MSVC
endif()
add_compile_options(/W1) # reduce warnings
add_compile_options(/wo4309)
add_compile_options(/wd4752)
@ -237,11 +238,11 @@ endif()
# allow 'large' files in 32 bit builds
if(UNIX)
add_definitions(-D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64
-D_LARGE_FILES
-D_FORTIFY_SOURCE=2
)
if(CMAKE_VERSION VERSION_GREATER 3.12.0)
add_compile_definitions(_LARGEFILE_SOURCE _FILE_OFFSET_BITS=64 _LARGE_FILES)
else()
add_definitions(-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES)
endif()
endif()
@ -319,7 +320,11 @@ if(MSVC)
endif()
set(BOOST_ALL_DYN_LINK "${BOOST_ALL_DYN_LINK}" CACHE BOOL "boost enable dynamic linking")
if(BOOST_ALL_DYN_LINK)
add_definitions(-DBOOST_ALL_DYN_LINK) # setup boost auto-linking in msvc
if(CMAKE_VERSION VERSION_GREATER 3.12.0)
add_compile_definitions(BOOST_ALL_DYN_LINK) # enables math constants on all supported versions of MSVC
else()
add_definitions(-DBOOST_ALL_DYN_LINK) # setup boost auto-linking in msvc
endif()
else()
unset(BOOST_REQUIRED_COMPONENTS) # empty components list for static link
endif()

View File

@ -590,6 +590,11 @@ if(NOT (ENABLE_STATIC_LIBS AND (CMAKE_GENERATOR STREQUAL Xcode)))
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC $<INSTALL_INTERFACE:include>
)
if(UNIX)
target_compile_definitions(volk_gnsssdr
PUBLIC $<$<CONFIG:Release>:_FORTIFY_SOURCE=2>$<$<CONFIG:RelWithDebInfo>:_FORTIFY_SOURCE=2>
)
endif()
if(USE_CPU_FEATURES)
if(CPUFEATURES_FOUND)
target_include_directories(volk_gnsssdr
@ -603,6 +608,7 @@ if(NOT (ENABLE_STATIC_LIBS AND (CMAKE_GENERATOR STREQUAL Xcode)))
)
endif()
endif()
# Configure target properties
if(ORC_FOUND)
target_link_libraries(volk_gnsssdr PRIVATE ${ORC_LIBRARIES})
@ -659,7 +665,11 @@ if(ENABLE_STATIC_LIBS)
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
)
if(UNIX)
target_compile_definitions(volk_gnsssdr_static
PUBLIC $<$<CONFIG:Release>:_FORTIFY_SOURCE=2>$<$<CONFIG:RelWithDebInfo>:_FORTIFY_SOURCE=2>
)
endif()
set_target_properties(volk_gnsssdr_static PROPERTIES OUTPUT_NAME volk_gnsssdr)
install(TARGETS volk_gnsssdr_static