Skip to content

Commit

Permalink
cmake: strip file full path to save the code size
Browse files Browse the repository at this point in the history
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.

If the compiler support preprocessing files and expand the "__FILE__"
and "__BASE_FILE__" macros as if the files resided in directory new instead.
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 <[email protected]>
  • Loading branch information
anchao committed Jul 11, 2024
1 parent 95a9fac commit 15d0568
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down Expand Up @@ -513,6 +514,21 @@ 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 be occupied by many invalid
# paths. If the compiler support preprocessing files and expand the "__FILE__"
# and "__BASE_FILE__" macros as if the files resided in directory new instead.
# 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.
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(
Expand Down
17 changes: 17 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,23 @@ 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 be occupied
by many invalid paths.
If the compiler support preprocessing files and expand the "__FILE__"
and "__BASE_FILE__" macros as if the files resided in directory new instead.
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.

endmenu # Binary Output Formats

menu "Customize Header Files"
Expand Down

0 comments on commit 15d0568

Please sign in to comment.