From 919df016f9116eba1f30b312bbd2ad8c77fae267 Mon Sep 17 00:00:00 2001 From: Danny Oerndrup Date: Thu, 1 Aug 2019 08:04:12 +0200 Subject: [PATCH] cmake: Toolchain abstraction: Abstraction of binary tool, readelf. This abstracts the interface for generation of the readelf command line, by naming the desired actions instead of directly setting the command parameters, which then opens up for other binary tool sets which may require different arguments to achieve the desired result. The intent here is to abstract Zephyr's dependence on toolchains, thus allowing for easier porting to other, perhaps commercial, toolchains and/or usecases. No functional change expected. Signed-off-by: Danny Oerndrup --- CMakeLists.txt | 13 +++++- cmake/bintools/gnu/target.cmake | 1 + cmake/bintools/gnu/target_readelf.cmake | 60 +++++++++++++++++++++++++ cmake/bintools/host-gnu/target.cmake | 1 + cmake/bintools/llvm/target.cmake | 1 + 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 cmake/bintools/gnu/target_readelf.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f2a585c2119..a4567f9c89e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1356,14 +1356,23 @@ if(CONFIG_OUTPUT_DISASSEMBLY) endif() if(CONFIG_OUTPUT_STAT) + set(out_stat_cmd "") + set(out_stat_byprod "") + bintools_readelf( + RESULT_CMD_LIST out_stat_cmd + RESULT_BYPROD_LIST out_stat_byprod + HEADERS + FILE_INPUT ${KERNEL_ELF_NAME} + FILE_OUTPUT ${KERNEL_STAT_NAME} + ) list(APPEND post_build_commands - COMMAND - ${CMAKE_READELF} -e ${KERNEL_ELF_NAME} > ${KERNEL_STAT_NAME} + ${out_stat_cmd} ) list(APPEND post_build_byproducts ${KERNEL_STAT_NAME} + ${out_stat_byprod} ) endif() diff --git a/cmake/bintools/gnu/target.cmake b/cmake/bintools/gnu/target.cmake index 4020bfc4bc1..0831515af8a 100644 --- a/cmake/bintools/gnu/target.cmake +++ b/cmake/bintools/gnu/target.cmake @@ -17,3 +17,4 @@ find_program(CMAKE_GDB gdb-multiarch PATH ${TOOLCHAIN_HOME} 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) diff --git a/cmake/bintools/gnu/target_readelf.cmake b/cmake/bintools/gnu/target_readelf.cmake new file mode 100644 index 00000000000..6db3f45c38d --- /dev/null +++ b/cmake/bintools/gnu/target_readelf.cmake @@ -0,0 +1,60 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Construct a commandline suitable for calling the toolchain binary tools +# version of readelf. +# +# Usage: +# bintools_readelf( +# RESULT_CMD_LIST +# RESULT_BYPROD_LIST +# +# HEADERS +# +# FILE_INPUT +# FILE_OUTPUT +# ) +function(bintools_readelf) + cmake_parse_arguments( + # Prefix of output variables + BINTOOLS_READELF + # List of argument names without values, hence boolean + "HEADERS" + # 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} + ) + + # Verify arguments + if(NOT DEFINED BINTOOLS_READELF_RESULT_CMD_LIST OR NOT DEFINED ${BINTOOLS_READELF_RESULT_CMD_LIST}) + message(FATAL_ERROR "RESULT_CMD_LIST is required.") + elseif(NOT DEFINED BINTOOLS_READELF_FILE_INPUT) + message(FATAL_ERROR "FILE_INPUT is required.") + endif() + + # Handle headers + set(readelf_headers "") + if(${BINTOOLS_READELF_HEADERS}) + set(readelf_headers "-e") # --headers + endif() + + # Handle output + set(readelf_output "") + if(DEFINED BINTOOLS_READELF_FILE_OUTPUT) + set(readelf_output > ${BINTOOLS_READELF_FILE_OUTPUT}) + endif() + + # Construct the command + set(readelf_cmd + # Base command + COMMAND ${CMAKE_READELF} ${readelf_headers} + # Input and Output + ${BINTOOLS_READELF_FILE_INPUT} ${readelf_output} + ) + + # Place command in the parent provided variable + set(${BINTOOLS_READELF_RESULT_CMD_LIST} ${readelf_cmd} PARENT_SCOPE) + +endfunction(bintools_readelf) diff --git a/cmake/bintools/host-gnu/target.cmake b/cmake/bintools/host-gnu/target.cmake index 67fb53c7a77..b9f70a0ba84 100644 --- a/cmake/bintools/host-gnu/target.cmake +++ b/cmake/bintools/host-gnu/target.cmake @@ -14,3 +14,4 @@ find_program(CMAKE_GDB gdb ) 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) diff --git a/cmake/bintools/llvm/target.cmake b/cmake/bintools/llvm/target.cmake index f455ece218a..f475d5d43b1 100644 --- a/cmake/bintools/llvm/target.cmake +++ b/cmake/bintools/llvm/target.cmake @@ -18,3 +18,4 @@ find_program(CMAKE_READELF readelf ${find_program_binutils_args}) 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)