cmake: strip file full path to save the code size

Cmake build provide absolute paths to compile files. If __FILE__
macros are used in the source code(ASSERT), the binary will be occupied
by many invalid paths.
This saves some memory, stops leaking user locations in binaries, makes
failure logs more deterministic and most importantly makes builds more
deterministic.
Debuggers usually have a path mapping feature to ensure the files are
still found.

Test config sabre-6quad/citest:

Before:
$ size build/nuttx
   text	   data	    bss	    dec	    hex	filename
 279309	    908	  13652	 293869	  47bed	build/nuttx

After:
$ size build/nuttx
   text	   data	    bss	    dec	    hex	filename
 269313	    908	  13652	 283873	  454e1	build/nuttx

Signed-off-by: chao an <anchao@lixiang.com>
This commit is contained in:
chao an 2024-07-11 16:50:31 +08:00 committed by Xiang Xiao
parent 95a9facf3a
commit 7b4f554f39
2 changed files with 31 additions and 0 deletions

View File

@ -173,6 +173,7 @@ if(NOT EXISTS "${NUTTX_APPS_DIR}")
message(FATAL_ERROR "Application directory ${NUTTX_APPS_DIR} is not found")
endif()
get_filename_component(NUTTX_APPS_DIR ${NUTTX_APPS_DIR} ABSOLUTE)
get_filename_component(apps_dir ${NUTTX_APPS_DIR} NAME)
set(NUTTX_APPS_BINDIR "${CMAKE_BINARY_DIR}/${apps_dir}")
@ -513,6 +514,20 @@ if(CONFIG_NDEBUG)
add_compile_options(-DNDEBUG)
endif()
# Cmake build provide absolute paths to compile files. If __FILE__ macros are
# used in the source code(ASSERT), the binary will contain many invalid paths.
# This saves some memory, stops exposing build systems locations in binaries,
# make failure logs more deterministic and most importantly makes builds more
# failure logs more deterministic and most importantly makes builds more
# deterministic. Debuggers usually have a path mapping feature to ensure the
# files are still found.
if(CONFIG_OUTPUT_STRIP_PATHS)
add_compile_options(-fmacro-prefix-map=${NUTTX_DIR}=)
add_compile_options(-fmacro-prefix-map=${NUTTX_APPS_DIR}=)
add_compile_options(-fmacro-prefix-map=${NUTTX_BOARD_ABS_DIR}=)
add_compile_options(-fmacro-prefix-map=${NUTTX_CHIP_ABS_DIR}=)
endif()
add_definitions(-D__NuttX__)
set_property(

16
Kconfig
View File

@ -447,6 +447,22 @@ config DFU_PID
hex "PID to use for DFU image"
endif # DFU_BINARY
config OUTPUT_STRIP_PATHS
bool "Strip absolute paths from binaries"
default y
depends on ARCH_TOOLCHAIN_GNU
---help---
Cmake build provide absolute paths to compile files. If __FILE__
macros are used in the source code(ASSERT), the binary will contain
many invalid paths.
This saves some memory, stops exposing build systems locations in binaries,
make failure logs more deterministic and most importantly makes builds more
failure logs more deterministic and most importantly makes builds more
deterministic.
Debuggers usually have a path mapping feature to ensure the files are
still found.
endmenu # Binary Output Formats
menu "Customize Header Files"