summaryrefslogtreecommitdiff
path: root/CMakeLists.txt
diff options
context:
space:
mode:
authorMartin Matuška <martin@matuska.org>2022-01-03 12:07:13 +0100
committerGitHub <noreply@github.com>2022-01-03 12:07:13 +0100
commitdd733f5c25efcc727c38949811e597d196a003f7 (patch)
tree89f84668ac88e22d9e732b525bcffa16d79c70e7 /CMakeLists.txt
parent38db08b16698e73c22b131cddd68658b80a43845 (diff)
parentfc0758237548328941fa7a9699510d6f357896ed (diff)
downloadlibarchive-dd733f5c25efcc727c38949811e597d196a003f7.tar.gz
Merge pull request #1625 from evelikov/gc-sections
Instrument gcc/clang to discard unused code during link
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r--CMakeLists.txt44
1 files changed, 25 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c5c22a24..1e1ff575 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,8 @@
#
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12 FATAL_ERROR)
+if(POLICY CMP0065)
+ cmake_policy(SET CMP0065 NEW) #3.4 don't use `-rdynamic` with executables
+endif()
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW) #3.12.0 `find_package()`` uses ``<PackageName>_ROOT`` variables.
endif()
@@ -96,24 +99,8 @@ endif ()
# Especially for early development, we want to be a little
# aggressive about diagnosing build problems; this can get
# relaxed somewhat in final shipping versions.
-IF (CMAKE_C_COMPILER_ID MATCHES "^GNU$")
- SET(CMAKE_REQUIRED_FLAGS "-Wall -Wformat -Wformat-security")
- #################################################################
- # Set compile flags for all build types.
- SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wformat -Wformat-security")
- if (ENABLE_WERROR)
- SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
- endif ()
- #################################################################
- # Set compile flags for debug build.
- # This is added into CMAKE_C_FLAGS when CMAKE_BUILD_TYPE is "Debug"
- SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wextra")
- SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wunused")
- SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wshadow")
- SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wmissing-prototypes")
- SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wcast-qual")
-ENDIF (CMAKE_C_COMPILER_ID MATCHES "^GNU$")
-IF (CMAKE_C_COMPILER_ID MATCHES "^Clang$")
+IF (CMAKE_C_COMPILER_ID MATCHES "^GNU$" OR
+ CMAKE_C_COMPILER_ID MATCHES "^Clang$")
SET(CMAKE_REQUIRED_FLAGS "-Wall -Wformat -Wformat-security")
#################################################################
# Set compile flags for all build types.
@@ -130,7 +117,26 @@ IF (CMAKE_C_COMPILER_ID MATCHES "^Clang$")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wshadow")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wmissing-prototypes")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wcast-qual")
-ENDIF (CMAKE_C_COMPILER_ID MATCHES "^Clang$")
+ # Ideally this will be a compile/link time check, yet there's no obvious way
+ # how considering how old our minimum required cmake version is. The official
+ # cmake.org side does not host the manual pages even. Normally we can use
+ # either of the following two, yet neither is supported as of 3.0.2
+ # - check_linker_flag - does not exist
+ # - try_compile - does not support linker flags
+ #
+ # The CI fails with this on MacOS
+ IF(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin")
+ # Place the functions and data into separate sections, allowing the linker
+ # to garbage collect the unused ones.
+ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
+ SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
+ SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
+ # Printing the discarded section is "too much", so enable on demand.
+ #SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Wl,--print-gc-sections")
+ #SET(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -Wl,--print-gc-sections")
+ ENDIF(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin")
+ENDIF (CMAKE_C_COMPILER_ID MATCHES "^GNU$" OR
+ CMAKE_C_COMPILER_ID MATCHES "^Clang$")
IF (CMAKE_C_COMPILER_ID MATCHES "^XL$")
SET(CMAKE_C_COMPILER "xlc_r")
SET(CMAKE_REQUIRED_FLAGS "-qflag=e:e -qformat=sec")