From c41e712c6ff7c312d7034556a4e3c1f5a0bd8804 Mon Sep 17 00:00:00 2001 From: Danny Oerndrup Date: Thu, 18 Jul 2019 15:16:39 +0200 Subject: [PATCH] cmake: Toolchain abstraction: Abstraction of print memory usage. The method for getting a memory usage report printed during build, is based on a GNU linker (ld) option flag, and thus is not necessarily supported by other toolchain binary tools. The introduced cmake macro allows for a given toolchain to specify how the memory usage report is to be generated, and whether the command for generation, if any, is to be added to the post_build_commands and the post_build_byproducts lists of the top level CMakeLists.txt 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 | 35 ++++++++++++------------ cmake/bintools/gnu/target.cmake | 3 ++ cmake/bintools/gnu/target_memusage.cmake | 32 ++++++++++++++++++++++ cmake/bintools/host-gnu/target.cmake | 3 ++ cmake/bintools/llvm/target.cmake | 3 ++ 5 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 cmake/bintools/gnu/target_memusage.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index a145738a6c3..b4abd30f0fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1219,6 +1219,24 @@ if(NOT CONFIG_BUILD_NO_GAP_FILL) set(GAP_FILL "--gap-fill;0xff") endif() +if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE) + # @Intent: Use the toolchain bintools method for printing memory usage + set(memUsageCmd "") + set(memUsageByProd "") + bintools_print_mem_usage( + RESULT_CMD_LIST memUsageCmd + RESULT_BYPROD_LIST memUsageByProd + ) + list(APPEND + post_build_commands + ${memUsageCmd} + ) + list(APPEND + post_build_byproducts + ${memUsageByProd} + ) +endif() + if(CONFIG_BUILD_OUTPUT_HEX) list(APPEND post_build_commands @@ -1358,23 +1376,6 @@ if(HEX_FILES_TO_MERGE) list(APPEND FLASH_DEPS mergehex) endif() -if(CONFIG_OUTPUT_PRINT_MEMORY_USAGE) - # Use --print-memory-usage with the first link. - # - # Don't use this option with the second link because seeing it twice - # could confuse users and using it on the second link would suppress - # it when the first link has a ram/flash-usage issue. - set(option ${LINKERFLAGPREFIX},--print-memory-usage) - string(MAKE_C_IDENTIFIER check${option} check) - - set(SAVED_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${option}") - zephyr_check_compiler_flag(C "" ${check}) - set(CMAKE_REQUIRED_FLAGS ${SAVED_CMAKE_REQUIRED_FLAGS}) - - target_link_libraries_ifdef(${check} ${ZEPHYR_PREBUILT_EXECUTABLE} ${option}) -endif() - if(EMU_PLATFORM) include(${ZEPHYR_BASE}/cmake/emu/${EMU_PLATFORM}.cmake) else() diff --git a/cmake/bintools/gnu/target.cmake b/cmake/bintools/gnu/target.cmake index db4195be260..7359abcb187 100644 --- a/cmake/bintools/gnu/target.cmake +++ b/cmake/bintools/gnu/target.cmake @@ -12,3 +12,6 @@ find_program(CMAKE_NM ${CROSS_COMPILE}nm PATH ${TOOLCHAIN_HOME} NO_DEF find_program(CMAKE_GDB ${CROSS_COMPILE}gdb PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) find_program(CMAKE_GDB gdb-multiarch PATH ${TOOLCHAIN_HOME} ) + +# Include bin tool abstraction macros +include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_memusage.cmake) diff --git a/cmake/bintools/gnu/target_memusage.cmake b/cmake/bintools/gnu/target_memusage.cmake new file mode 100644 index 00000000000..3e3408021a7 --- /dev/null +++ b/cmake/bintools/gnu/target_memusage.cmake @@ -0,0 +1,32 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Add and/or prepare print of memory usage report +# +# Usage: +# bintools_print_mem_usage( +# RESULT_CMD_LIST +# RESULT_BYPROD_LIST +# ) +# +macro(bintools_print_mem_usage) + + # Here we make use of the linkers ability to produce memory usage output + # and thus we have no need for the above provided arguments, but another + # toolchain with a different set of binary tools, most likely will... + # + # Use --print-memory-usage with the first link. + # + # Don't use this option with the second link because seeing it twice + # could confuse users and using it on the second link would suppress + # it when the first link has a ram/flash-usage issue. + set(option ${LINKERFLAGPREFIX},--print-memory-usage) + string(MAKE_C_IDENTIFIER check${option} check) + + set(SAVED_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${option}") + zephyr_check_compiler_flag(C "" ${check}) + set(CMAKE_REQUIRED_FLAGS ${SAVED_CMAKE_REQUIRED_FLAGS}) + + target_link_libraries_ifdef(${check} ${ZEPHYR_PREBUILT_EXECUTABLE} ${option}) + +endmacro(bintools_print_mem_usage) diff --git a/cmake/bintools/host-gnu/target.cmake b/cmake/bintools/host-gnu/target.cmake index 5b79e5b6fc1..9f31b392316 100644 --- a/cmake/bintools/host-gnu/target.cmake +++ b/cmake/bintools/host-gnu/target.cmake @@ -9,3 +9,6 @@ find_program(CMAKE_RANLILB ranlib ) find_program(CMAKE_READELF readelf) find_program(CMAKE_GDB gdb ) + +# Use the gnu binutil abstraction macros +include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_memusage.cmake) diff --git a/cmake/bintools/llvm/target.cmake b/cmake/bintools/llvm/target.cmake index d0c81e3b090..f973a6d02c1 100644 --- a/cmake/bintools/llvm/target.cmake +++ b/cmake/bintools/llvm/target.cmake @@ -13,3 +13,6 @@ find_program(CMAKE_OBJDUMP llvm-objdump ${find_program_clang_args} ) find_program(CMAKE_RANLIB llvm-ranlib ${find_program_clang_args} ) find_program(CMAKE_OBJCOPY objcopy ${find_program_binutils_args}) find_program(CMAKE_READELF readelf ${find_program_binutils_args}) + +# Use the gnu binutil abstraction macros +include(${ZEPHYR_BASE}/cmake/bintools/gnu/target_memusage.cmake)