From 4362b3547fe28cd3533823ccb9e6068f53dc43c5 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Mon, 18 Sep 2023 15:43:46 +0200 Subject: [PATCH 1/9] test(relative-paths): Added tests for #116 --- tests/relative-paths/cmake.toml | 24 +++++++++++++++++++++++ tests/relative-paths/src/library-code.cpp | 6 ++++++ tests/relative-paths/src/main.cpp | 8 ++++++++ 3 files changed, 38 insertions(+) create mode 100644 tests/relative-paths/cmake.toml create mode 100644 tests/relative-paths/src/library-code.cpp create mode 100644 tests/relative-paths/src/main.cpp diff --git a/tests/relative-paths/cmake.toml b/tests/relative-paths/cmake.toml new file mode 100644 index 0000000..d88d23d --- /dev/null +++ b/tests/relative-paths/cmake.toml @@ -0,0 +1,24 @@ +[project] +name = "relative-paths" + +[target.test-library] +type = "static" +sources = ["src/library-code.cpp"] +cmake-after = """ +set(TEST_LIBRARY_TARGET_NAME ${CMKR_TARGET}) +set(LIBRARIES_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/libs") +add_custom_command( + TARGET ${CMKR_TARGET} POST_BUILD + COMMAND mkdir "${LIBRARIES_OUTPUT_DIRECTORY}" + COMMAND ${CMAKE_COMMAND} -E copy "$" "${LIBRARIES_OUTPUT_DIRECTORY}/test-library-file.lib" + VERBATIM +) +""" + +[target.example] +type = "executable" +sources = ["src/main.cpp"] +link-libraries = ["libs/test-library-file.lib"] +cmake-after = """ +add_dependencies(${CMKR_TARGET} ${TEST_LIBRARY_TARGET_NAME}) +""" diff --git a/tests/relative-paths/src/library-code.cpp b/tests/relative-paths/src/library-code.cpp new file mode 100644 index 0000000..7e49a69 --- /dev/null +++ b/tests/relative-paths/src/library-code.cpp @@ -0,0 +1,6 @@ +// Created by Anthony Printup on 9/18/2023. +#include + +extern "C" void library_function() { + std::puts("Hello from library_function!"); +} diff --git a/tests/relative-paths/src/main.cpp b/tests/relative-paths/src/main.cpp new file mode 100644 index 0000000..0779a21 --- /dev/null +++ b/tests/relative-paths/src/main.cpp @@ -0,0 +1,8 @@ +// Created by Anthony Printup on 9/18/2023. +#include + +extern "C" void library_function(); +int main() { + puts("Hello from cmkr(relative-paths)!"); + library_function(); +} From 56bd78f7ad4b346b3e584477581777265634f2d2 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Mon, 18 Sep 2023 15:44:44 +0200 Subject: [PATCH 2/9] feat(project-parser): Added support for relative paths in `link-libraries` Fixes #116 --- src/project_parser.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/project_parser.cpp b/src/project_parser.cpp index 22d9e7f..37d0d1c 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -597,6 +597,23 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p t.optional("link-libraries", target.link_libraries); t.optional("private-link-libraries", target.private_link_libraries); + // Add support for relative paths for (private-)link-libraries + const auto fix_relative_paths = [](ConditionVector &libraries) { + for (const auto &library_entries : libraries) { + for (auto &path : libraries[library_entries.first]) { + // Skip absolute paths and paths which do not include a parent path + const fs::path library_file_path {path}; + if (library_file_path.is_absolute() || !library_file_path.has_parent_path()) + continue; + + // Prepend ${CMAKE_CURRENT_SOURCE_DIR} to the path + path.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/"); + } + } + }; + fix_relative_paths(target.link_libraries); + fix_relative_paths(target.private_link_libraries); + t.optional("link-options", target.link_options); t.optional("private-link-options", target.private_link_options); From bfa1e1fe0d5b99cc4399f73a418b376d3908e777 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Mon, 18 Sep 2023 15:49:17 +0200 Subject: [PATCH 3/9] test(relative-paths): Added the latest `relative-paths` test to the tests workflow --- tests/CMakeLists.txt | 10 ++++++++++ tests/cmake.toml | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 448180a..161abca 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -109,3 +109,13 @@ add_test( "$" build ) + +add_test( + NAME + relative-paths + WORKING_DIRECTORY + "${CMAKE_CURRENT_LIST_DIR}/relative-paths" + COMMAND + "$" + build +) diff --git a/tests/cmake.toml b/tests/cmake.toml index 6d20721..994f5b8 100644 --- a/tests/cmake.toml +++ b/tests/cmake.toml @@ -57,4 +57,10 @@ arguments = ["build"] name = "compile-options" working-directory = "compile-options" command = "$" -arguments = ["build"] \ No newline at end of file +arguments = ["build"] + +[[test]] +name = "relative-paths" +working-directory = "relative-paths" +command = "$" +arguments = ["build"] From 19e61aa1aa6effb5cd57b713a33f1669d9502941 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Mon, 18 Sep 2023 16:41:37 +0200 Subject: [PATCH 4/9] test(relative-paths): Attempt to fix the tests workflow --- tests/CMakeLists.txt | 12 +++++++++++- tests/cmake.toml | 8 +++++++- tests/relative-paths/cmake.toml | 3 --- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 161abca..39b4fcf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -110,6 +110,16 @@ add_test( build ) +add_test( + NAME + relative-paths-setup + WORKING_DIRECTORY + "${CMAKE_CURRENT_LIST_DIR}/relative-paths" + COMMAND + "$" + "build --target test-library" +) + add_test( NAME relative-paths @@ -117,5 +127,5 @@ add_test( "${CMAKE_CURRENT_LIST_DIR}/relative-paths" COMMAND "$" - build + "build --target example" ) diff --git a/tests/cmake.toml b/tests/cmake.toml index 994f5b8..109ba8c 100644 --- a/tests/cmake.toml +++ b/tests/cmake.toml @@ -59,8 +59,14 @@ working-directory = "compile-options" command = "$" arguments = ["build"] +[[test]] +name = "relative-paths-setup" +working-directory = "relative-paths" +command = "$" +arguments = ["build --target test-library"] + [[test]] name = "relative-paths" working-directory = "relative-paths" command = "$" -arguments = ["build"] +arguments = ["build --target example"] diff --git a/tests/relative-paths/cmake.toml b/tests/relative-paths/cmake.toml index d88d23d..c308e7d 100644 --- a/tests/relative-paths/cmake.toml +++ b/tests/relative-paths/cmake.toml @@ -19,6 +19,3 @@ add_custom_command( type = "executable" sources = ["src/main.cpp"] link-libraries = ["libs/test-library-file.lib"] -cmake-after = """ -add_dependencies(${CMKR_TARGET} ${TEST_LIBRARY_TARGET_NAME}) -""" From 1c2947b2fc1d7a7f89d4a489712d255b3b2219df Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Mon, 18 Sep 2023 17:35:46 +0200 Subject: [PATCH 5/9] fix(relative-paths): Prevent processing paths which may contain CMake macros, added a check to see if the library that's being linked exists on disk --- src/project_parser.cpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/project_parser.cpp b/src/project_parser.cpp index 37d0d1c..1119538 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -598,21 +598,37 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p t.optional("private-link-libraries", target.private_link_libraries); // Add support for relative paths for (private-)link-libraries - const auto fix_relative_paths = [](ConditionVector &libraries) { + const auto fix_relative_paths = [&name, &path](ConditionVector &libraries, const char *key) { for (const auto &library_entries : libraries) { - for (auto &path : libraries[library_entries.first]) { + for (auto &library_path : libraries[library_entries.first]) { + // Skip processing paths with potential CMake macros in them (this check isn't perfect) + // https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#variable-references + if ((library_path.find("${") != std::string::npos || library_path.find("$ENV{") != std::string::npos || + library_path.find("$CACHE{") != std::string::npos) && + library_path.find('}') != std::string::npos) { + continue; + } + // Skip absolute paths and paths which do not include a parent path - const fs::path library_file_path {path}; - if (library_file_path.is_absolute() || !library_file_path.has_parent_path()) + const fs::path library_file_path{library_path}; + if (library_file_path.is_absolute() || !library_file_path.has_parent_path()) { continue; + } + + // Check if the new file path exists, otherwise emit an error + const auto expected_library_file_path = fs::path{path} / library_file_path; + if (!fs::exists(expected_library_file_path)) { + throw std::runtime_error("Attempted to link against a library file that doesn't exist for target \"" + name + "\" in \"" + + key + "\": " + library_path); + } // Prepend ${CMAKE_CURRENT_SOURCE_DIR} to the path - path.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/"); + library_path.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/"); } } }; - fix_relative_paths(target.link_libraries); - fix_relative_paths(target.private_link_libraries); + fix_relative_paths(target.link_libraries, "link-libraries"); + fix_relative_paths(target.private_link_libraries, "private-link-libraries"); t.optional("link-options", target.link_options); t.optional("private-link-options", target.private_link_options); From 854e8817c4b542fea33ef2bcbabe5f54c965f566 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Mon, 18 Sep 2023 17:46:08 +0200 Subject: [PATCH 6/9] test(relative-paths): Remove inlined CMake from cmake.toml --- tests/relative-paths/cmake.toml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/relative-paths/cmake.toml b/tests/relative-paths/cmake.toml index c308e7d..ac0bd4d 100644 --- a/tests/relative-paths/cmake.toml +++ b/tests/relative-paths/cmake.toml @@ -4,16 +4,6 @@ name = "relative-paths" [target.test-library] type = "static" sources = ["src/library-code.cpp"] -cmake-after = """ -set(TEST_LIBRARY_TARGET_NAME ${CMKR_TARGET}) -set(LIBRARIES_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/libs") -add_custom_command( - TARGET ${CMKR_TARGET} POST_BUILD - COMMAND mkdir "${LIBRARIES_OUTPUT_DIRECTORY}" - COMMAND ${CMAKE_COMMAND} -E copy "$" "${LIBRARIES_OUTPUT_DIRECTORY}/test-library-file.lib" - VERBATIM -) -""" [target.example] type = "executable" From b24a0a2fdca70288789d764a20c45693e0cfe97f Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Mon, 18 Sep 2023 17:48:17 +0200 Subject: [PATCH 7/9] test(relative-paths): Make `link-libraries` conditional to the Windows platform --- tests/CMakeLists.txt | 29 +++++++++++------------------ tests/cmake.toml | 9 ++------- tests/relative-paths/cmake.toml | 2 +- tests/relative-paths/src/main.cpp | 4 ++++ 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 39b4fcf..22aa4b4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -110,22 +110,15 @@ add_test( build ) -add_test( - NAME - relative-paths-setup - WORKING_DIRECTORY - "${CMAKE_CURRENT_LIST_DIR}/relative-paths" - COMMAND - "$" - "build --target test-library" -) +if(WIN32) # windows + add_test( + NAME + relative-paths + WORKING_DIRECTORY + "${CMAKE_CURRENT_LIST_DIR}/relative-paths" + COMMAND + "$" + build + ) -add_test( - NAME - relative-paths - WORKING_DIRECTORY - "${CMAKE_CURRENT_LIST_DIR}/relative-paths" - COMMAND - "$" - "build --target example" -) +endif() diff --git a/tests/cmake.toml b/tests/cmake.toml index 109ba8c..338b37d 100644 --- a/tests/cmake.toml +++ b/tests/cmake.toml @@ -60,13 +60,8 @@ command = "$" arguments = ["build"] [[test]] -name = "relative-paths-setup" -working-directory = "relative-paths" -command = "$" -arguments = ["build --target test-library"] - -[[test]] +condition = "windows" name = "relative-paths" working-directory = "relative-paths" command = "$" -arguments = ["build --target example"] +arguments = ["build"] diff --git a/tests/relative-paths/cmake.toml b/tests/relative-paths/cmake.toml index ac0bd4d..d029bbe 100644 --- a/tests/relative-paths/cmake.toml +++ b/tests/relative-paths/cmake.toml @@ -8,4 +8,4 @@ sources = ["src/library-code.cpp"] [target.example] type = "executable" sources = ["src/main.cpp"] -link-libraries = ["libs/test-library-file.lib"] +windows.link-libraries = ["libs/test-library-x64-Release.lib"] diff --git a/tests/relative-paths/src/main.cpp b/tests/relative-paths/src/main.cpp index 0779a21..3bef1f9 100644 --- a/tests/relative-paths/src/main.cpp +++ b/tests/relative-paths/src/main.cpp @@ -1,8 +1,12 @@ // Created by Anthony Printup on 9/18/2023. #include +#ifdef WIN32 extern "C" void library_function(); +#endif int main() { puts("Hello from cmkr(relative-paths)!"); +#ifdef WIN32 library_function(); +#endif } From c573c9a76d3a25b19face3ccb7fb56e9edcc37a1 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Mon, 18 Sep 2023 17:49:01 +0200 Subject: [PATCH 8/9] test(relative-paths): Add the test-library file to libs --- .../libs/test-library-x64-Release.lib | Bin 0 -> 1450 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/relative-paths/libs/test-library-x64-Release.lib diff --git a/tests/relative-paths/libs/test-library-x64-Release.lib b/tests/relative-paths/libs/test-library-x64-Release.lib new file mode 100644 index 0000000000000000000000000000000000000000..cc5017a8c0553a7015eca8c9479af50612f2f014 GIT binary patch literal 1450 zcmb_cOK;Oa5FX=Jl!!+v&LB5xrE;j_LKE7kZJaoXVwy)4r{!YFP3%qU)`^4dRKO_$ zi4&Ee;>w*f+);l*xpPDyaY0-_9H7iPF-?OI5Aj+1n9sA`nb~hWu~>8Tt>sXZJBiu& zLQIIw#uj2Hd7*F4Ukmt+5CEJ4VD@QCCN)Xagmg)i)1{TdN~R>{iDlWzSi`ZK$(Uu< zotkq;Yjj$=XWFggq?8t6n1#QO=lXFx3vqKL0JIp(6dRMsrt>WAcdW&Gggc z4bxF8#3D79sC{lmw++JE^=*g-CGIc&)h@Gw5Rb<>Zec!1eIsN%7=;ykj1WvcjM-E0 zhC_63m-H-vI!;~(MlgoL=tt_E%}FJo4sfzeOE~}$!fDKC^3EeADp7 z&|?N=NL9tsG>0%KL8<_jIT-RlISrpPi>fYkf#)3L|&!uLQAGXQ-HaG`Iqz@h)3 zJgG0sP=Qjs>E7z}zDO;Ob_nW&s|Lh6iVVw8xt_#2nwn-d+giKhxj+Bg{_S@Z`VJJo BH5dQ@ literal 0 HcmV?d00001 From a7ca9f04c09f802f8567593c798df697e869c4bf Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Wed, 20 Sep 2023 00:35:41 +0200 Subject: [PATCH 9/9] fix(relative-paths): Replaced `has_parent_path` with a simpler path check --- src/project_parser.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/project_parser.cpp b/src/project_parser.cpp index 1119538..218b44c 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -609,14 +609,13 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p continue; } - // Skip absolute paths and paths which do not include a parent path - const fs::path library_file_path{library_path}; - if (library_file_path.is_absolute() || !library_file_path.has_parent_path()) { + // Skip paths that don't contain backwards or forwards slashes + if (library_path.find_first_of(R"(\/)") == std::string::npos) { continue; } // Check if the new file path exists, otherwise emit an error - const auto expected_library_file_path = fs::path{path} / library_file_path; + const auto expected_library_file_path = fs::path{path} / library_path; if (!fs::exists(expected_library_file_path)) { throw std::runtime_error("Attempted to link against a library file that doesn't exist for target \"" + name + "\" in \"" + key + "\": " + library_path);