CMake: rework compilation/link flags
- Default compiler and linker flags were added silently. They are now set in the cache if no previous values are present. - Debug flags and regular flags have been merged. - Position-independent code (doesn't appear in the cache because CMake lacks variables like CMAKE_EXE_COMPILE_FLAGS): - Shared libraries are compiled with -fPIC. - Executables and static libraries are compiled with -fPIE. - Executables are linked with -pie -fPIE.
This commit is contained in:
parent
51cb1ab08a
commit
1cb5742ef9
|
@ -58,9 +58,40 @@ endif()
|
|||
|
||||
### Flags ###
|
||||
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -Wextra -Wstrict-prototypes")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
|
||||
# Specific compiler flags
|
||||
# These variables are to be used (manually) when compiling a shared
|
||||
# library or an executable (or static library).
|
||||
set(SHARED_FLAGS "-fPIC")
|
||||
set(EXE_FLAGS "-fPIE")
|
||||
|
||||
# Common compiler flags
|
||||
set(C_CXX_COMMON_FLAGS "-Wall -Wextra")
|
||||
set(C_FLAGS "${C_CXX_COMMON_FLAGS} -Wstrict-prototypes")
|
||||
set(CXX_FLAGS "${C_CXX_COMMON_FLAGS}")
|
||||
|
||||
if (NOT CMAKE_C_FLAGS)
|
||||
set(CMAKE_C_FLAGS ${C_FLAGS}
|
||||
CACHE STRING "Set default C flags" FORCE)
|
||||
endif()
|
||||
|
||||
if (NOT CMAKE_CXX_FLAGS)
|
||||
set(CMAKE_CXX_FLAGS ${CXX_FLAGS}
|
||||
CACHE STRING "Set default C++ flags" FORCE)
|
||||
endif()
|
||||
|
||||
# Linker flags
|
||||
# Question: why the hell does CMAKE_SHARED_LINKER_FLAGS contain " "
|
||||
# by default?!
|
||||
if ("${CMAKE_SHARED_LINKER_FLAGS}" STREQUAL " ")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined"
|
||||
CACHE STRING "Set default shared linker flags" FORCE)
|
||||
endif()
|
||||
|
||||
# Linker flags when linking an executable
|
||||
if ("${CMAKE_EXE_LINKER_FLAGS}" STREQUAL " ")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-pie -fPIE"
|
||||
CACHE STRING "Set default shared linker flags" FORCE)
|
||||
endif()
|
||||
|
||||
|
||||
### Libraries ###
|
||||
|
|
|
@ -1,17 +1,30 @@
|
|||
# Explicitly link to libc
|
||||
set(EXTRA_LIBS ${EXTRA_LIBS} c)
|
||||
|
||||
add_library(libowlps-client.a STATIC libowlps-client.c)
|
||||
# Targets
|
||||
add_library(libowlps-client.so SHARED libowlps-client.c)
|
||||
add_library(libowlps-client.a STATIC libowlps-client.c)
|
||||
|
||||
# Fix targets' names
|
||||
set_target_properties(
|
||||
libowlps-client.a libowlps-client.so
|
||||
libowlps-client.so libowlps-client.a
|
||||
PROPERTIES
|
||||
OUTPUT_NAME owlps-client
|
||||
SOVERSION 1
|
||||
VERSION 1.0
|
||||
)
|
||||
|
||||
# Add compile flags
|
||||
set_target_properties(libowlps-client.so
|
||||
PROPERTIES COMPILE_FLAGS ${SHARED_FLAGS})
|
||||
set_target_properties(libowlps-client.a
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
||||
# Link libraries
|
||||
target_link_libraries(libowlps-client.so libowlps.so ${EXTRA_LIBS})
|
||||
target_link_libraries(libowlps-client.a libowlps.a ${EXTRA_LIBS})
|
||||
|
||||
# Install targets
|
||||
install(
|
||||
TARGETS libowlps-client.so libowlps-client.a
|
||||
DESTINATION lib)
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
# Explicitly link to libc
|
||||
set(EXTRA_LIBS ${EXTRA_LIBS} c)
|
||||
|
||||
# Targets
|
||||
add_library(libowlps-resultreader.a STATIC libowlps-resultreader.c)
|
||||
add_library(libowlps-resultreader.so SHARED libowlps-resultreader.c)
|
||||
|
||||
# Fix targets' names
|
||||
set_target_properties(
|
||||
libowlps-resultreader.a libowlps-resultreader.so
|
||||
PROPERTIES
|
||||
|
@ -10,11 +13,28 @@ set_target_properties(
|
|||
SOVERSION 1
|
||||
VERSION 1.0
|
||||
)
|
||||
|
||||
# Add compile flags
|
||||
set_target_properties(libowlps-resultreader.so
|
||||
PROPERTIES COMPILE_FLAGS ${SHARED_FLAGS})
|
||||
set_target_properties(libowlps-resultreader.a
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
||||
# Link libraries
|
||||
target_link_libraries(libowlps-resultreader.so libowlps.so ${EXTRA_LIBS})
|
||||
target_link_libraries(libowlps-resultreader.a libowlps.a ${EXTRA_LIBS})
|
||||
|
||||
# Install targets
|
||||
install(
|
||||
TARGETS libowlps-resultreader.so libowlps-resultreader.a
|
||||
DESTINATION lib)
|
||||
install(
|
||||
FILES owlps-resultreader.h
|
||||
DESTINATION include)
|
||||
|
||||
|
||||
|
||||
### Demo program ###
|
||||
|
||||
add_executable(owlps-resultreader-udp owlps-resultreader-udp.c)
|
||||
target_link_libraries(owlps-resultreader-udp
|
||||
|
@ -23,12 +43,6 @@ target_link_libraries(owlps-resultreader-udp
|
|||
install(
|
||||
TARGETS owlps-resultreader-udp
|
||||
DESTINATION bin)
|
||||
install(
|
||||
FILES owlps-resultreader.h
|
||||
DESTINATION include)
|
||||
|
||||
|
||||
### Demo program ###
|
||||
|
||||
add_executable(owlps-resultreader-udp.semistatic EXCLUDE_FROM_ALL
|
||||
owlps-resultreader-udp.c)
|
||||
|
@ -49,3 +63,10 @@ target_link_libraries(owlps-resultreader-udp.static
|
|||
# Add the static target to the global list of static targets
|
||||
list(APPEND OWLPS_STATIC_TARGETS owlps-resultreader-udp.static)
|
||||
set(OWLPS_STATIC_TARGETS ${OWLPS_STATIC_TARGETS} PARENT_SCOPE)
|
||||
|
||||
# Add compile flags for the above targets
|
||||
set_target_properties(
|
||||
owlps-resultreader-udp
|
||||
owlps-resultreader-udp.semistatic
|
||||
owlps-resultreader-udp.static
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
|
|
@ -8,8 +8,11 @@ endif()
|
|||
# Explicitly link to libc
|
||||
set(EXTRA_LIBS ${EXTRA_LIBS} c)
|
||||
|
||||
add_library(libowlps.a STATIC libowlps.c)
|
||||
# Targets
|
||||
add_library(libowlps.so SHARED libowlps.c)
|
||||
add_library(libowlps.a STATIC libowlps.c)
|
||||
|
||||
# Fix targets' names
|
||||
set_target_properties(
|
||||
libowlps.a libowlps.so
|
||||
PROPERTIES
|
||||
|
@ -17,8 +20,18 @@ set_target_properties(
|
|||
SOVERSION 3
|
||||
VERSION 3.1
|
||||
)
|
||||
|
||||
# Add compile flags
|
||||
set_target_properties(libowlps.so
|
||||
PROPERTIES COMPILE_FLAGS ${SHARED_FLAGS})
|
||||
set_target_properties(libowlps.a
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
||||
# Link libraries
|
||||
target_link_libraries(libowlps.so ${EXTRA_LIBS})
|
||||
target_link_libraries(libowlps.a ${EXTRA_LIBS})
|
||||
|
||||
# Install targets
|
||||
install(
|
||||
TARGETS libowlps.so libowlps.a
|
||||
DESTINATION lib)
|
||||
|
|
|
@ -27,3 +27,10 @@ target_link_libraries(owlps-aggregatord.static
|
|||
# Add the static target to the global list of static targets
|
||||
list(APPEND OWLPS_STATIC_TARGETS owlps-aggregatord.static)
|
||||
set(OWLPS_STATIC_TARGETS ${OWLPS_STATIC_TARGETS} PARENT_SCOPE)
|
||||
|
||||
# Add compile flags for the above targets
|
||||
set_target_properties(
|
||||
owlps-aggregatord
|
||||
owlps-aggregatord.semistatic
|
||||
owlps-aggregatord.static
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
|
|
@ -50,3 +50,10 @@ endif()
|
|||
# Add the static target to the global list of static targets
|
||||
list(APPEND OWLPS_STATIC_TARGETS owlps-client.static)
|
||||
set(OWLPS_STATIC_TARGETS ${OWLPS_STATIC_TARGETS} PARENT_SCOPE)
|
||||
|
||||
# Add compile flags for the above targets
|
||||
set_target_properties(
|
||||
owlps-client
|
||||
owlps-client.semistatic
|
||||
owlps-client.static
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
|
|
@ -62,3 +62,10 @@ target_link_libraries(owlps-listenerd.static
|
|||
# Add the static target to the global list of static targets
|
||||
list(APPEND OWLPS_STATIC_TARGETS owlps-listenerd.static)
|
||||
set(OWLPS_STATIC_TARGETS ${OWLPS_STATIC_TARGETS} PARENT_SCOPE)
|
||||
|
||||
# Add compile flags for the above targets
|
||||
set_target_properties(
|
||||
owlps-listenerd
|
||||
owlps-listenerd.semistatic
|
||||
owlps-listenerd.static
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
|
|
@ -91,6 +91,13 @@ target_link_libraries(owlps-positionerd.static
|
|||
list(APPEND OWLPS_STATIC_TARGETS owlps-positionerd.static)
|
||||
set(OWLPS_STATIC_TARGETS ${OWLPS_STATIC_TARGETS} PARENT_SCOPE)
|
||||
|
||||
# Add compile flags for the above targets
|
||||
set_target_properties(
|
||||
owlps-positionerd
|
||||
owlps-positionerd.semistatic
|
||||
owlps-positionerd.static
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
||||
|
||||
### Extra targets ###
|
||||
|
||||
|
|
|
@ -30,3 +30,10 @@ target_link_libraries(owlps-udp-to-http.static
|
|||
# Add the static target to the global list of static targets
|
||||
list(APPEND OWLPS_STATIC_TARGETS owlps-udp-to-http.static)
|
||||
set(OWLPS_STATIC_TARGETS ${OWLPS_STATIC_TARGETS} PARENT_SCOPE)
|
||||
|
||||
# Add compile flags for the above targets
|
||||
set_target_properties(
|
||||
owlps-udp-to-http
|
||||
owlps-udp-to-http.semistatic
|
||||
owlps-udp-to-http.static
|
||||
PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
|
||||
|
|
Loading…
Reference in New Issue