Skip to content

Commit 9b988ca

Browse files
committed
config: Add new option to replace IDF_PATH and project path with placeholders in macros
Allows building with asserts on and still not finding any actual file paths in the final binary file. Alternative fix for #6306 Progress towards #5873
1 parent 9ae01e4 commit 9b988ca

10 files changed

+53
-8
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ if(CONFIG_COMPILER_DUMP_RTL_FILES)
9090
list(APPEND compile_options "-fdump-rtl-expand")
9191
endif()
9292

93+
if(CONFIG_COMPILER_HIDE_PATHS_MACROS)
94+
list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.")
95+
list(APPEND compile_options "-fmacro-prefix-map=${IDF_PATH}=IDF")
96+
endif()
97+
9398
# GCC-specific options
9499
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
95100
list(APPEND compile_options "-fstrict-volatile-bitfields"

Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,22 @@ mainmenu "Espressif IoT Development Framework Configuration"
279279

280280
endchoice # assertions
281281

282+
menuconfig COMPILER_HIDE_PATHS_MACROS
283+
bool "Replace ESP-IDF and project paths in binaries"
284+
default y
285+
depends on IDF_CMAKE
286+
help
287+
When expanding the __FILE__ and __BASE_FILE__ macros, replace paths inside ESP-IDF
288+
with paths relative to the placeholder string "IDF", and convert paths inside the
289+
project directory to relative paths.
290+
291+
This allows building the project with assertions or other code that embeds file paths,
292+
without the binary containing the exact path to the IDF or project directories.
293+
294+
This option passes -fmacro-prefix-map options to the GCC command line. To replace additional
295+
paths in your binaries, modify the project CMakeLists.txt file to pass custom -fmacro-prefix-map or
296+
-ffile-prefix-map arguments.
297+
282298
menuconfig COMPILER_CXX_EXCEPTIONS
283299
bool "Enable C++ exceptions"
284300
default n

tools/test_apps/system/no_embedded_paths/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ idf_build_get_property(idf_path IDF_PATH)
99
idf_build_get_property(python PYTHON)
1010
idf_build_get_property(elf EXECUTABLE)
1111

12-
# If the configuration is one that doesn't expect any paths to be found then run this build step
12+
# If the configuration is one that doesn't expect the IDF_PATH to be found in binaries then run this build step
1313
# after building the ELF, will fail if it finds any file paths in binary files
14-
if(CONFIG_OPTIMIZATION_ASSERTIONS_SILENT OR CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED)
14+
if(CONFIG_OPTIMIZATION_ASSERTIONS_SILENT OR
15+
CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED OR
16+
CONFIG_COMPILER_HIDE_PATHS_MACROS)
1517
add_custom_command(
1618
TARGET ${elf}
1719
POST_BUILD

tools/test_apps/system/no_embedded_paths/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
This test app exists to verify that paths (like __FILE__) are not compiled into
44
any object files in configurations where this should be avoided.
55

6-
It doubles up as a build-time check that disabling assertions doesn't lead to
7-
any warnings.
6+
Configurations where this is relevant include:
87

9-
(These configurations include: assertions disabled, 'silent' asserts, any reproducible
10-
builds configuration.)
8+
* Assertions disabled (doubles up as a build-time check that disabling assertions doesn't lead to any warnings)
9+
* Silent assertions
10+
* CONFIG_COMPILER_HIDE_PATHS_MACROS is set to replace IDF_PATH and project dir with placeholders when expanding `__FILE__`
1111

1212
Not embedding paths reduces the binary size, avoids leaking information about
13-
the compilation environment, and is a necessary step to supporet reproducible
13+
the compilation environment, and is a necessary step to support reproducible
1414
builds across projects built in different directories.
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
/* This test app only exists for the build stage, so doesn't need to do anything at runtime */
1+
/* This test app only exists for the build stage, so doesn't need to do anything at runtime
2+
apart from embedding an assert to check asserts inside the project dir */
3+
#include <assert.h>
4+
5+
// Declared non-static to avoid the assert being optimized out
6+
int other_function(void)
7+
{
8+
return 3;
9+
}
10+
211
void app_main(void)
312
{
13+
assert(other_function() == 3);
414
}

tools/test_apps/system/no_embedded_paths/sdkconfig.ci.noasserts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y
22
CONFIG_FREERTOS_ASSERT_DISABLE=y
3+
CONFIG_COMPILER_HIDE_PATHS_MACROS=n
34

45
# compiling as many files as possible here (we don't have 100% coverage of course, due to config options, but
56
# try to maximize what we can check

tools/test_apps/system/no_embedded_paths/sdkconfig.ci.noasserts.nimble

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y
22
CONFIG_FREERTOS_ASSERT_DISABLE=y
3+
CONFIG_COMPILER_HIDE_PATHS_MACROS=n
34

45
# the other sdkconfig builds Bluedroid, build NimBLE here
56
#
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# this is the default value, actually
2+
CONFIG_COMPILER_HIDE_PATHS_MACROS=y
3+
4+
# compiling as many files as possible here (we don't have 100% coverage of course, due to config options, but
5+
# try to maximize what we can check
6+
CONFIG_BT_ENABLED=y
7+
CONFIG_BT_BLUEDROID_ENABLED=y
8+
CONFIG_BLE_MESH=y

tools/test_apps/system/no_embedded_paths/sdkconfig.ci.silentasserts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
2+
CONFIG_COMPILER_HIDE_PATHS_MACROS=n
23

34
# compiling as many files as possible here (we don't have 100% coverage of course, due to config options, but
45
# try to maximize what we can check

tools/test_apps/system/no_embedded_paths/sdkconfig.ci.silentasserts.nimble

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
22
CONFIG_FREERTOS_ASSERT_DISABLE=y
3+
CONFIG_COMPILER_HIDE_PATHS_MACROS=n
34

45
# the other sdkconfig builds Bluedroid, build NimBLE here
56
#

0 commit comments

Comments
 (0)