diff --git a/CMakeLists.txt b/CMakeLists.txt index a4567f9c89e..1ba96e4bfa9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1377,14 +1377,23 @@ if(CONFIG_OUTPUT_STAT) endif() if(CONFIG_BUILD_OUTPUT_STRIPPED) + set(out_stripped_cmd "") + set(out_stripped_byprod "") + bintools_strip( + RESULT_CMD_LIST out_stripped_cmd + RESULT_BYPROD_LIST out_stripped_byprod + STRIP_ALL + FILE_INPUT ${KERNEL_ELF_NAME} + FILE_OUTPUT ${KERNEL_STRIP_NAME} + ) list(APPEND post_build_commands - COMMAND - ${CMAKE_STRIP} --strip-all ${KERNEL_ELF_NAME} -o ${KERNEL_STRIP_NAME} + ${out_stripped_cmd} ) list(APPEND post_build_byproducts ${KERNEL_STRIP_NAME} + ${out_stripped_byprod} ) endif() diff --git a/cmake/bintools/gnu/target.cmake b/cmake/bintools/gnu/target.cmake index 0831515af8a..d02889893a3 100644 --- a/cmake/bintools/gnu/target.cmake +++ b/cmake/bintools/gnu/target.cmake @@ -18,3 +18,4 @@ include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_memusage.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_objcopy.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_objdump.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_readelf.cmake) +include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_strip.cmake) diff --git a/cmake/bintools/gnu/target_strip.cmake b/cmake/bintools/gnu/target_strip.cmake new file mode 100644 index 00000000000..08244f17f89 --- /dev/null +++ b/cmake/bintools/gnu/target_strip.cmake @@ -0,0 +1,62 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Construct a commandline suitable for calling the toolchain binary tools +# version of strip. +# +# Usage: +# bintools_strip( +# RESULT_CMD_LIST +# RESULT_BYPROD_LIST +# +# STRIP_ALL +# STRIP_DEBUG +# STRIP_DWO +# +# FILE_INPUT +# FILE_OUTPUT +# ) +function(bintools_strip) + cmake_parse_arguments( + # Prefix of output variables + BINTOOLS_STRIP + # List of argument names without values, hence boolean + "STRIP_ALL;STRIP_DEBUG;STRIP_DWO" + # List of argument names with one value + "RESULT_CMD_LIST;RESULT_BYPROD_LIST;FILE_INPUT;FILE_OUTPUT" + # List of argument names with multible values + "" + # Parser input + ${ARGN} + ) + + if(NOT DEFINED BINTOOLS_STRIP_RESULT_CMD_LIST OR NOT DEFINED ${BINTOOLS_STRIP_RESULT_CMD_LIST}) + message(FATAL_ERROR "RESULT_CMD_LIST is required.") + elseif(NOT DEFINED BINTOOLS_STRIP_FILE_INPUT OR NOT DEFINED BINTOOLS_STRIP_FILE_OUTPUT) + message(FATAL_ERROR "Both FILE_INPUT and FILE_OUTPUT are required.") + endif() + + # Handle stripping + set(strip_what "") + if(${BINTOOLS_STRIP_STRIP_ALL}) + set(strip_what "--strip-all") + elseif(${BINTOOLS_STRIP_STRIP_DEBUG}) + set(strip_what "--strip-debug") + elseif(${BINTOOLS_STRIP_STRIP_DWO}) + set(strip_what "--strip-dwo") + endif() + + # Handle output + set(strip_output -o ${BINTOOLS_STRIP_FILE_OUTPUT}) + + # Construct the command + set(strip_cmd + # Base command + COMMAND ${CMAKE_STRIP} ${strip_what} + # Input and Output + ${BINTOOLS_STRIP_FILE_INPUT} ${strip_output} + ) + + # Place command in the parent provided variable + set(${BINTOOLS_STRIP_RESULT_CMD_LIST} ${strip_cmd} PARENT_SCOPE) + +endfunction(bintools_strip) diff --git a/cmake/bintools/host-gnu/target.cmake b/cmake/bintools/host-gnu/target.cmake index b9f70a0ba84..82da57ccb3f 100644 --- a/cmake/bintools/host-gnu/target.cmake +++ b/cmake/bintools/host-gnu/target.cmake @@ -15,3 +15,4 @@ include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_memusage.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_objcopy.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_objdump.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_readelf.cmake) +include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_strip.cmake) diff --git a/cmake/bintools/llvm/target.cmake b/cmake/bintools/llvm/target.cmake index f475d5d43b1..e52b2be8712 100644 --- a/cmake/bintools/llvm/target.cmake +++ b/cmake/bintools/llvm/target.cmake @@ -19,3 +19,4 @@ include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_memusage.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_objcopy.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_objdump.cmake) include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_readelf.cmake) +include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_strip.cmake)