From b200313b212099c966db6babae39a2a7268530ef Mon Sep 17 00:00:00 2001 From: Daniel Brondani Date: Tue, 23 Jun 2026 10:09:43 +0200 Subject: [PATCH] [projmgr] Adjust relative paths in `misc` nodes --- tools/projmgr/include/ProjMgrUtils.h | 8 ++ tools/projmgr/src/ProjMgrUtils.cpp | 13 +++ tools/projmgr/src/ProjMgrWorker.cpp | 15 ++-- .../layers/misc-relative.clayer.yml | 11 +++ .../test/data/TestMiscRelativePaths/main.c | 3 + .../misc-relative.csolution.yml | 19 ++++ .../project/misc-relative.cproject.yml | 13 +++ ...misc-relative.Debug+TEST_TARGET.cbuild.yml | 89 +++++++++++++++++++ tools/projmgr/test/src/ProjMgrUnitTests.cpp | 16 ++++ .../test/src/ProjMgrUtilsUnitTests.cpp | 22 +++++ 10 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 tools/projmgr/test/data/TestMiscRelativePaths/layers/misc-relative.clayer.yml create mode 100644 tools/projmgr/test/data/TestMiscRelativePaths/main.c create mode 100644 tools/projmgr/test/data/TestMiscRelativePaths/misc-relative.csolution.yml create mode 100644 tools/projmgr/test/data/TestMiscRelativePaths/project/misc-relative.cproject.yml create mode 100644 tools/projmgr/test/data/TestMiscRelativePaths/ref/misc-relative.Debug+TEST_TARGET.cbuild.yml diff --git a/tools/projmgr/include/ProjMgrUtils.h b/tools/projmgr/include/ProjMgrUtils.h index dc121e131..2b2c62eee 100644 --- a/tools/projmgr/include/ProjMgrUtils.h +++ b/tools/projmgr/include/ProjMgrUtils.h @@ -284,6 +284,14 @@ class ProjMgrUtils { */ static const std::string FormatPath(const std::string& original, const std::string& directory, bool useAbsolutePaths = false); + /** + * @brief adjust relative path fragments in a list of values + * @param vec list of strings that may contain relative paths + * @param ref reference path for formatting + * @param outDir output directory for path formatting + */ + static void AdjustRelativePaths(std::vector& vec, const std::string& ref, const std::string& outDir); + /** * @brief check if list contains incompatible version of pack requirement * @param list of packs diff --git a/tools/projmgr/src/ProjMgrUtils.cpp b/tools/projmgr/src/ProjMgrUtils.cpp index 4b39f7507..67a3df26c 100644 --- a/tools/projmgr/src/ProjMgrUtils.cpp +++ b/tools/projmgr/src/ProjMgrUtils.cpp @@ -493,3 +493,16 @@ const std::string ProjMgrUtils::GetWestBoard(const std::string& board) { std::replace(westBoard.begin(), westBoard.end(), '-', '_'); return westBoard; } + +void ProjMgrUtils::AdjustRelativePaths(vector& vec, const string& ref, const string& outDir) { + static const regex pattern(R"((\.{1,2}/.*))"); + for (string& value : vec) { + if (!value.empty()) { + smatch match; + if (regex_search(value, match, pattern)) { + const auto& replacement = RteFsUtils::RelativePath(ref + "/" + match.str(), outDir, true); + value.replace(match.position(), match.length(), replacement); + } + } + } +} diff --git a/tools/projmgr/src/ProjMgrWorker.cpp b/tools/projmgr/src/ProjMgrWorker.cpp index f92121a7f..403966a33 100644 --- a/tools/projmgr/src/ProjMgrWorker.cpp +++ b/tools/projmgr/src/ProjMgrWorker.cpp @@ -4986,16 +4986,11 @@ bool ProjMgrWorker::ProcessSequencesRelatives(ContextItem& context, BuildType& b return false; } for (auto& misc : build.misc) { - if (!ProcessSequencesRelatives(context, misc.as, "", "", true) || - !ProcessSequencesRelatives(context, misc.c, "", "", true) || - !ProcessSequencesRelatives(context, misc.cpp, "", "", true) || - !ProcessSequencesRelatives(context, misc.c_cpp, "", "", true) || - !ProcessSequencesRelatives(context, misc.lib, "", "", true) || - !ProcessSequencesRelatives(context, misc.library, "", "", true) || - !ProcessSequencesRelatives(context, misc.link, "", "", true) || - !ProcessSequencesRelatives(context, misc.link_c, "", "", true) || - !ProcessSequencesRelatives(context, misc.link_cpp, "", "", true)) { - return false; + for (auto* vec : { &misc.as, &misc.c, &misc.cpp, &misc.c_cpp, &misc.lib, &misc.library, &misc.link, &misc.link_c, &misc.link_cpp }) { + ProjMgrUtils::AdjustRelativePaths(*vec, ref, context.directories.cprj); + if (!ProcessSequencesRelatives(context, *vec, "", "", true)) { + return false; + } } } return true; diff --git a/tools/projmgr/test/data/TestMiscRelativePaths/layers/misc-relative.clayer.yml b/tools/projmgr/test/data/TestMiscRelativePaths/layers/misc-relative.clayer.yml new file mode 100644 index 000000000..f91f330cf --- /dev/null +++ b/tools/projmgr/test/data/TestMiscRelativePaths/layers/misc-relative.clayer.yml @@ -0,0 +1,11 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/Open-CMSIS-Pack/devtools/main/tools/projmgr/schemas/clayer.schema.json + +layer: + misc: + - C-CPP: + - --misc-layer=./relative/from-layer + + groups: + - group: Sources + files: + - file: ../main.c diff --git a/tools/projmgr/test/data/TestMiscRelativePaths/main.c b/tools/projmgr/test/data/TestMiscRelativePaths/main.c new file mode 100644 index 000000000..03b2213bb --- /dev/null +++ b/tools/projmgr/test/data/TestMiscRelativePaths/main.c @@ -0,0 +1,3 @@ +int main(void) { + return 0; +} diff --git a/tools/projmgr/test/data/TestMiscRelativePaths/misc-relative.csolution.yml b/tools/projmgr/test/data/TestMiscRelativePaths/misc-relative.csolution.yml new file mode 100644 index 000000000..8b9f2f95f --- /dev/null +++ b/tools/projmgr/test/data/TestMiscRelativePaths/misc-relative.csolution.yml @@ -0,0 +1,19 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/Open-CMSIS-Pack/devtools/main/tools/projmgr/schemas/csolution.schema.json + +solution: + target-types: + - type: TEST_TARGET + device: RteTest_ARMCM0 + misc: + - C-CPP: + - --misc-sol=./relative/from-solution + + build-types: + - type: Debug + + projects: + - project: ./project/misc-relative.cproject.yml + + packs: + - pack: ARM::RteTest + - pack: ARM::RteTest_DFP diff --git a/tools/projmgr/test/data/TestMiscRelativePaths/project/misc-relative.cproject.yml b/tools/projmgr/test/data/TestMiscRelativePaths/project/misc-relative.cproject.yml new file mode 100644 index 000000000..ddb47f825 --- /dev/null +++ b/tools/projmgr/test/data/TestMiscRelativePaths/project/misc-relative.cproject.yml @@ -0,0 +1,13 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/Open-CMSIS-Pack/devtools/main/tools/projmgr/schemas/cproject.schema.json + +project: + compiler: AC6 + components: + - component: CORE + + misc: + - C-CPP: + - --misc-proj=./relative/from-project + + layers: + - layer: ../layers/misc-relative.clayer.yml diff --git a/tools/projmgr/test/data/TestMiscRelativePaths/ref/misc-relative.Debug+TEST_TARGET.cbuild.yml b/tools/projmgr/test/data/TestMiscRelativePaths/ref/misc-relative.Debug+TEST_TARGET.cbuild.yml new file mode 100644 index 000000000..f4a7e3af8 --- /dev/null +++ b/tools/projmgr/test/data/TestMiscRelativePaths/ref/misc-relative.Debug+TEST_TARGET.cbuild.yml @@ -0,0 +1,89 @@ +build: + generated-by: csolution version 0.0.0 + solution: ../../../../../../data/TestMiscRelativePaths/misc-relative.csolution.yml + project: ../../../../../../data/TestMiscRelativePaths/project/misc-relative.cproject.yml + context: misc-relative.Debug+TEST_TARGET + compiler: AC6 + device: ARM::RteTest_ARMCM0 + device-pack: ARM::RteTest_DFP@0.2.0 + device-books: + - name: http://infocenter.arm.com/help/topic/com.arm.doc.dui0497a/index.html + title: Cortex-M0 Device Generic Users Guide + dbgconf: + - file: ../../../../../../data/TestMiscRelativePaths/.cmsis/misc-relative+TEST_TARGET.dbgconf + version: 0.0.2 + processor: + fpu: off + core: Cortex-M0 + packs: + - pack: ARM::RteTest_DFP@0.2.0 + path: ${CMSIS_PACK_ROOT}/ARM/RteTest_DFP/0.2.0 + misc: + C: + - --misc-proj=../../../../../../data/TestMiscRelativePaths/project/relative/from-project + - --misc-sol=../../../../../../data/TestMiscRelativePaths/relative/from-solution + - --misc-layer=../../../../../../data/TestMiscRelativePaths/layers/relative/from-layer + CPP: + - --misc-proj=../../../../../../data/TestMiscRelativePaths/project/relative/from-project + - --misc-sol=../../../../../../data/TestMiscRelativePaths/relative/from-solution + - --misc-layer=../../../../../../data/TestMiscRelativePaths/layers/relative/from-layer + define: + - ARMCM0 + - _RTE_ + define-asm: + - ARMCM0 + - _RTE_ + add-path: + - ../../../../../../data/TestMiscRelativePaths/project/RTE/_Debug_TEST_TARGET + - ${CMSIS_PACK_ROOT}/ARM/RteTest_DFP/0.2.0/Device/ARM/ARMCM0/Include + add-path-asm: + - ../../../../../../data/TestMiscRelativePaths/project/RTE/_Debug_TEST_TARGET + - ${CMSIS_PACK_ROOT}/ARM/RteTest_DFP/0.2.0/Device/ARM/ARMCM0/Include + output-dirs: + intdir: ../../../../tmp + outdir: . + rtedir: ../../../../../../data/TestMiscRelativePaths/project/RTE + output: + - type: elf + file: misc-relative.axf + - type: comp-db + file: compile_commands.json + - type: comp-db + file: compile_macros.h + components: + - component: ARM::RteTest:CORE@0.1.1 + condition: Cortex-M Device + from-pack: ARM::RteTest_DFP@0.2.0 + selected-by: CORE + implements: RteTest:CORE@1.1.2 + files: + - file: ${CMSIS_PACK_ROOT}/ARM/RteTest_DFP/0.2.0/Doc/html/index.html + category: doc + version: 0.1.1 + apis: + - api: RteTest:CORE@1.1.2 + from-pack: ARM::RteTest_DFP@0.2.0 + implemented-by: ARM::RteTest:CORE@0.1.1 + files: + - file: https://arm-software.github.io/CMSIS_5/Pack/html/pdsc_apis_pg.html + category: doc + version: 1.1.2 + linker: + script: ../../../../../../data/TestMiscRelativePaths/project/RTE/Device/RteTest_ARMCM0/ac6_linker_script.sct.src + regions: ../../../../../../data/TestMiscRelativePaths/project/RTE/Device/RteTest_ARMCM0/regions_RteTest_ARMCM0.h + groups: + - group: Sources + files: + - file: ../../../../../../data/TestMiscRelativePaths/main.c + category: sourceC + constructed-files: + - file: ../../../../../../data/TestMiscRelativePaths/project/RTE/_Debug_TEST_TARGET/RTE_Components.h + category: header + licenses: + - license: + license-agreement: ${CMSIS_PACK_ROOT}/ARM/RteTest_DFP/0.2.0/Doc/license.txt + packs: + - pack: ARM::RteTest_DFP@0.2.0 + components: + - component: ARM::RteTest:CORE@0.1.1 + - component: RteTest:CORE(API) diff --git a/tools/projmgr/test/src/ProjMgrUnitTests.cpp b/tools/projmgr/test/src/ProjMgrUnitTests.cpp index 2d008d687..401934d99 100644 --- a/tools/projmgr/test/src/ProjMgrUnitTests.cpp +++ b/tools/projmgr/test/src/ProjMgrUnitTests.cpp @@ -921,6 +921,22 @@ TEST_F(ProjMgrUnitTests, RunProjMgrLayers) { EXPECT_TRUE(RteFsUtils::Exists(testinput_folder + "/TestLayers/Layer3/RTE/RteTest/MyDir")); } +TEST_F(ProjMgrUnitTests, MiscRelativePathsAtAllLevels) { + char* argv[5]; + + const string csolution = testinput_folder + "/TestMiscRelativePaths/misc-relative.csolution.yml"; + const string output = testoutput_folder + "/misc-relative"; + argv[1] = (char*)"convert"; + argv[2] = (char*)csolution.c_str(); + argv[3] = (char*)"-o"; + argv[4] = (char*)output.c_str(); + EXPECT_EQ(0, RunProjMgr(5, argv, m_envp)); + + const string cbuildFile = output + "/out/misc-relative/TEST_TARGET/Debug/misc-relative.Debug+TEST_TARGET.cbuild.yml"; + ProjMgrTestEnv::CompareFile(cbuildFile, + testinput_folder + "/TestMiscRelativePaths/ref/misc-relative.Debug+TEST_TARGET.cbuild.yml"); +} + TEST_F(ProjMgrUnitTests, RunProjMgrSolution_CbuildFailedToCreate) { char* argv[7]; StdStreamRedirect streamRedirect; diff --git a/tools/projmgr/test/src/ProjMgrUtilsUnitTests.cpp b/tools/projmgr/test/src/ProjMgrUtilsUnitTests.cpp index 29b47030d..617d3eea2 100644 --- a/tools/projmgr/test/src/ProjMgrUtilsUnitTests.cpp +++ b/tools/projmgr/test/src/ProjMgrUtilsUnitTests.cpp @@ -459,6 +459,28 @@ TEST_F(ProjMgrUtilsUnitTests, ReplaceDelimiters) { EXPECT_EQ("path_with_spaces", ProjMgrUtils::ReplaceDelimiters("path/with spaces")); } +TEST_F(ProjMgrUtilsUnitTests, AdjustRelativePaths) { + const string baseDir = testoutput_folder + "/AdjustRelativePaths"; + const string refDir = baseDir + "/ref"; + const string outDir = baseDir + "/out/sub"; + + RteFsUtils::CreateDirectories(refDir + "/assets"); + RteFsUtils::CreateDirectories(baseDir + "/shared"); + RteFsUtils::CreateDirectories(outDir); + + vector paths = { + "prefix ./assets/file.txt suffix", + "copy ../shared/config.yml", + "absolute/path/unchanged", + }; + + ProjMgrUtils::AdjustRelativePaths(paths, refDir, outDir); + + EXPECT_EQ("prefix ../../ref/assets/file.txt suffix", paths[0]); + EXPECT_EQ("copy ../../shared/config.yml", paths[1]); + EXPECT_EQ("absolute/path/unchanged", paths[2]); +} + TEST_F(ProjMgrUtilsUnitTests, FindReferencedContext) { const vector selectedContexts = { "Project1.Debug+Target",