From 81c91735d60f4a0711e23d071977c7433e96b5cd Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Tue, 5 Aug 2025 13:11:18 +0200 Subject: [PATCH] feat(cmakev2/build): set PROJECT_VER build property Initialize the PROJECT_VER build property based on the following precedence. 1. The PROJECT_VER environment or CMake variable. 2. The version.txt file located in the top-level project directory. 3. The VERSION argument, if provided, in the project() macro. 4. The output of git describe if the project is within a Git repository. 5. Defaults to 1 if none of the above conditions are met. Signed-off-by: Frantisek Hrbata --- tools/cmakev2/build.cmake | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tools/cmakev2/build.cmake b/tools/cmakev2/build.cmake index 0318410bf94..c2a981af40a 100644 --- a/tools/cmakev2/build.cmake +++ b/tools/cmakev2/build.cmake @@ -104,6 +104,58 @@ function(__init_project_name) idf_build_set_property(PROJECT_NAME "${project_name}") endfunction() +#[[ + __init_project_version() + + Initialize the PROJECT_VER build property based on the following precedence. + + 1. The PROJECT_VER environment or CMake variable. + 2. The version.txt file located in the top-level project directory. + 3. The VERSION argument, if provided, in the project() macro. + 4. The output of git describe if the project is within a Git repository. + 5. Defaults to 1 if none of the above conditions are met. + + The value of PROJECT_VER will be overridden later if + CONFIG_APP_PROJECT_VER_FROM_CONFIG is defined. For more details, refer to + components/esp_app_format/CMakeLists.txt. +#]] +function(__init_project_version) + idf_build_get_property(project_dir PROJECT_DIR) + + # 1. The PROJECT_VER environment or CMake variable. + __get_default_value(VARIABLE PROJECT_VER + DEFAULT NOTFOUND + OUTPUT project_ver) + if(project_ver) + idf_build_set_property(PROJECT_VER "${project_ver}") + return() + endif() + + # 2. The version.txt file located in the top-level project directory. + if(EXISTS "${project_dir}/version.txt") + file(STRINGS "${project_dir}/version.txt" project_ver) + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${project_dir}/version.txt") + idf_build_set_property(PROJECT_VER "${project_ver}") + return() + endif() + + # 3. The VERSION argument, if provided, in the project() macro. + if(PROJECT_VERSION) + idf_build_set_property(PROJECT_VER "${PROJECT_VERSION}") + return() + endif() + + # 4. The output of git describe if the project is within a Git repository. + git_describe(project_ver "${project_dir}") + if(project_ver) + idf_build_set_property(PROJECT_VER "${project_ver}") + return() + endif() + + # 5. Defaults to 1 if none of the above conditions are met. + idf_build_set_property(PROJECT_VER 1) +endfunction() + #[[ __init_build_configuration() @@ -543,6 +595,9 @@ function(__init_build) # Set PROJECT_NAME build property __init_project_name() + # Set PROJECT_VER build property + __init_project_version() + # Initialize all compilation options and defines. __init_build_configuration()