From 1c343d892331b40c79ece93353d7bbd25d2a437e Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 9 Aug 2024 14:45:58 +0200 Subject: [PATCH] change(build_system): refactor reproducible build handling - remove generate_debug_prefix_map.py, move its logic into CMake - move all reproducible builds logic into a new file, prefix_map.cmake --- CMakeLists.txt | 42 ++---------------------- tools/ci/exclude_check_tools_files.txt | 1 - tools/cmake/idf.cmake | 1 + tools/cmake/prefix_map.cmake | 41 +++++++++++++++++++++++ tools/generate_debug_prefix_map.py | 45 -------------------------- 5 files changed, 44 insertions(+), 86 deletions(-) create mode 100644 tools/cmake/prefix_map.cmake delete mode 100644 tools/generate_debug_prefix_map.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cedae7de3..5501c1e4cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,46 +152,8 @@ if(CONFIG_COMPILER_DUMP_RTL_FILES) list(APPEND compile_options "-fdump-rtl-expand") endif() -if(NOT ${CMAKE_C_COMPILER_VERSION} VERSION_LESS 8.0.0) - if(CONFIG_COMPILER_HIDE_PATHS_MACROS) - list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.") - list(APPEND compile_options "-fmacro-prefix-map=${IDF_PATH}=/IDF") - endif() - - if(CONFIG_APP_REPRODUCIBLE_BUILD) - idf_build_set_property(DEBUG_PREFIX_MAP_GDBINIT "${BUILD_DIR}/prefix_map_gdbinit") - - list(APPEND compile_options "-fdebug-prefix-map=${IDF_PATH}=/IDF") - list(APPEND compile_options "-fdebug-prefix-map=${PROJECT_DIR}=/IDF_PROJECT") - list(APPEND compile_options "-fdebug-prefix-map=${BUILD_DIR}=/IDF_BUILD") - - # component dirs - idf_build_get_property(python PYTHON) - idf_build_get_property(idf_path IDF_PATH) - idf_build_get_property(component_dirs BUILD_COMPONENT_DIRS) - - execute_process( - COMMAND ${python} - "${idf_path}/tools/generate_debug_prefix_map.py" - "${BUILD_DIR}" - "${component_dirs}" - OUTPUT_VARIABLE result - RESULT_VARIABLE ret - ) - if(NOT ret EQUAL 0) - message(FATAL_ERROR "This is a bug. Please report to https://github.com/espressif/esp-idf/issues") - endif() - - spaces2list(result) - list(LENGTH component_dirs length) - math(EXPR max_index "${length} - 1") - foreach(index RANGE ${max_index}) - list(GET component_dirs ${index} folder) - list(GET result ${index} after) - list(APPEND compile_options "-fdebug-prefix-map=${folder}=${after}") - endforeach() - endif() -endif() +__generate_prefix_map(prefix_map_compile_options) +list(APPEND compile_options ${prefix_map_compile_options}) if(CONFIG_COMPILER_DISABLE_GCC12_WARNINGS) list(APPEND compile_options "-Wno-address" diff --git a/tools/ci/exclude_check_tools_files.txt b/tools/ci/exclude_check_tools_files.txt index c5d24295e0..b5326f7e41 100644 --- a/tools/ci/exclude_check_tools_files.txt +++ b/tools/ci/exclude_check_tools_files.txt @@ -8,7 +8,6 @@ tools/ci/get_all_test_results.py tools/gdb_panic_server.py tools/check_term.py tools/python_version_checker.py -tools/generate_debug_prefix_map.py tools/ci/astyle-rules.yml tools/ci/checkout_project_ref.py tools/ci/ci_fetch_submodule.py diff --git a/tools/cmake/idf.cmake b/tools/cmake/idf.cmake index 3d2e96af1b..28c2b1d060 100644 --- a/tools/cmake/idf.cmake +++ b/tools/cmake/idf.cmake @@ -48,6 +48,7 @@ if(NOT __idf_env_set) include(ldgen) include(dfu) include(version) + include(prefix_map) __build_init("${idf_path}") diff --git a/tools/cmake/prefix_map.cmake b/tools/cmake/prefix_map.cmake new file mode 100644 index 0000000000..440d412b43 --- /dev/null +++ b/tools/cmake/prefix_map.cmake @@ -0,0 +1,41 @@ +# Utilities for remapping path prefixes +# +# __generate_prefix_map +# Prepares the list of compiler flags for remapping various paths +# to fixed names. This is used when reproducible builds are required. +# This function also creates a gdbinit file for the debugger to +# remap the substituted paths back to the real paths in the filesystem. +function(__generate_prefix_map compile_options_var) + set(compile_options) + idf_build_get_property(idf_path IDF_PATH) + idf_build_get_property(build_components BUILD_COMPONENTS) + + if(CONFIG_COMPILER_HIDE_PATHS_MACROS) + list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.") + list(APPEND compile_options "-fmacro-prefix-map=${idf_path}=/IDF") + endif() + + if(CONFIG_APP_REPRODUCIBLE_BUILD) + list(APPEND compile_options "-fdebug-prefix-map=${idf_path}=/IDF") + list(APPEND compile_options "-fdebug-prefix-map=${PROJECT_DIR}=/IDF_PROJECT") + list(APPEND compile_options "-fdebug-prefix-map=${BUILD_DIR}=/IDF_BUILD") + + # Generate mapping for component paths + set(gdbinit_file_lines) + foreach(component_name ${build_components}) + idf_component_get_property(component_dir ${component_name} COMPONENT_DIR) + + string(TOUPPER ${component_name} component_name_uppercase) + set(substituted_path "/COMPONENT_${component_name_uppercase}_DIR") + list(APPEND compile_options "-fdebug-prefix-map=${component_dir}=${substituted_path}") + string(APPEND gdbinit_file_lines "set substitute-path ${substituted_path} ${component_dir}\n") + endforeach() + + # Write the final gdbinit file + set(gdbinit_path "${BUILD_DIR}/prefix_map_gdbinit") + file(WRITE "${gdbinit_path}" "${gdbinit_file_lines}") + idf_build_set_property(DEBUG_PREFIX_MAP_GDBINIT "${gdbinit_path}") + endif() + + set(${compile_options_var} ${compile_options} PARENT_SCOPE) +endfunction() diff --git a/tools/generate_debug_prefix_map.py b/tools/generate_debug_prefix_map.py deleted file mode 100644 index d9d46b4e2f..0000000000 --- a/tools/generate_debug_prefix_map.py +++ /dev/null @@ -1,45 +0,0 @@ -# SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD -# SPDX-License-Identifier: Apache-2.0 - -# General Workflow: -# 1. read all components dirs, a semicolon-separated string (cmake list) -# 2. map the component dir with a unique prefix /COMPONENT__DIR -# 2. write the prefix mapping file to $BUILD_DIR/prefix_map_gdbinit -# 3. print the unique prefix out, a space-separated string, will be used by the build system to add compile options. - -import argparse -import os -from typing import List - - -def component_name(component_dir: str) -> str: - return '/COMPONENT_{}_DIR'.format(os.path.basename(component_dir).upper()) - - -GDB_SUBSTITUTE_PATH_FMT = 'set substitute-path {} {}\n' - - -def write_gdbinit(build_dir: str, folders: List[str]) -> None: - gdb_init_filepath = os.path.join(build_dir, 'prefix_map_gdbinit') - - with open(gdb_init_filepath, 'w') as fw: - for folder in folders: - fw.write(f'{GDB_SUBSTITUTE_PATH_FMT.format(component_name(folder), folder)}') - - -def main(build_dir: str, folders: List[str]) -> None: - write_gdbinit(build_dir, folders) - print(' '.join([component_name(folder) for folder in folders]), end='') - - -if __name__ == '__main__': - parser = argparse.ArgumentParser(description='print the debug-prefix-map and write to ' - '$BUILD_DIR/prefix_map_gdbinit file') - - parser.add_argument('build_dir', - help='build dir') - parser.add_argument('folders', - help='component folders, semicolon separated string') - args = parser.parse_args() - - main(args.build_dir, args.folders.split(';'))