From 359b0ceeeae1da1bbe3e62bb2189dec234c78e47 Mon Sep 17 00:00:00 2001 From: Neil Dhar Date: Mon, 4 Apr 2022 20:32:58 -0700 Subject: [PATCH] Clean up setting up GC sections Summary: 1. Use target_link_options instead of supplying the linker argument as a dependency 2. Enable GC sections for executables 3. Enable ffunction-sections and fdata-sections 4. Change the setup so it is easier to include other linker flags (like ICF in the next diff) Reviewed By: jpporto Differential Revision: D35347443 fbshipit-source-id: 2c255e584d30f95504c8c9c0142f44561c47be3f --- API/hermes/CMakeLists.txt | 3 +-- CMakeLists.txt | 8 ++++---- cmake/modules/Hermes.cmake | 1 + 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/API/hermes/CMakeLists.txt b/API/hermes/CMakeLists.txt index 2aa1f7ed275..d2a5d0c56f9 100644 --- a/API/hermes/CMakeLists.txt +++ b/API/hermes/CMakeLists.txt @@ -58,9 +58,8 @@ endif() target_link_libraries(libhermes jsi ${LIBHERMES_VM_DEP} - # Linker flags - ${OPTIONAL_GC_SECTIONS} ) +target_link_options(libhermes PRIVATE ${HERMES_EXTRA_LINKER_FLAGS}) # Export the required header directory target_include_directories(libhermes PUBLIC ..) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b36f217dd6..8007b243d7b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.13.0) # Set the VERSION variables based on the project command if (POLICY CMP0048) @@ -301,9 +301,9 @@ CHECK_CXX_COMPILER_FLAG("" HAVE_GC_SECTIONS) set(CMAKE_EXE_LINKER_FLAGS "${OLD_CMAKE_EXE_LINKER_FLAGS}") if(HAVE_GC_SECTIONS) - set(OPTIONAL_GC_SECTIONS "-Wl,--gc-sections") -else() - set(OPTIONAL_GC_SECTIONS "") + add_flag_if_supported("-ffunction-sections" FUNCTION_SECTIONS) + add_flag_if_supported("-fdata-sections" DATA_SECTIONS) + list(APPEND HERMES_EXTRA_LINKER_FLAGS "LINKER:--gc-sections") endif() # Make the HERMES_RELEASE_VERSION accessible for version printing in C++. diff --git a/cmake/modules/Hermes.cmake b/cmake/modules/Hermes.cmake index 7b679ff2849..deb00df9ba1 100644 --- a/cmake/modules/Hermes.cmake +++ b/cmake/modules/Hermes.cmake @@ -133,6 +133,7 @@ function(add_hermes_executable name) cmake_parse_arguments(ARG "" "" "LINK_LIBS" ${ARGN}) add_executable(${name} ${ARG_UNPARSED_ARGUMENTS}) target_link_libraries(${name} ${ARG_LINK_LIBS} ${HERMES_LINK_COMPONENTS}) + target_link_options(${name} PRIVATE ${HERMES_EXTRA_LINKER_FLAGS}) hermes_update_compile_flags(${name}) endfunction(add_hermes_executable)