Skip to content

Commit e0d8a08

Browse files
authored
Merge pull request #121 from anthonyprintup/relative-paths
Added support for relative paths in `link-libraries`
2 parents 56da414 + a7ca9f0 commit e0d8a08

File tree

7 files changed

+82
-1
lines changed

7 files changed

+82
-1
lines changed

src/project_parser.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,38 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
597597
t.optional("link-libraries", target.link_libraries);
598598
t.optional("private-link-libraries", target.private_link_libraries);
599599

600+
// Add support for relative paths for (private-)link-libraries
601+
const auto fix_relative_paths = [&name, &path](ConditionVector &libraries, const char *key) {
602+
for (const auto &library_entries : libraries) {
603+
for (auto &library_path : libraries[library_entries.first]) {
604+
// Skip processing paths with potential CMake macros in them (this check isn't perfect)
605+
// https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#variable-references
606+
if ((library_path.find("${") != std::string::npos || library_path.find("$ENV{") != std::string::npos ||
607+
library_path.find("$CACHE{") != std::string::npos) &&
608+
library_path.find('}') != std::string::npos) {
609+
continue;
610+
}
611+
612+
// Skip paths that don't contain backwards or forwards slashes
613+
if (library_path.find_first_of(R"(\/)") == std::string::npos) {
614+
continue;
615+
}
616+
617+
// Check if the new file path exists, otherwise emit an error
618+
const auto expected_library_file_path = fs::path{path} / library_path;
619+
if (!fs::exists(expected_library_file_path)) {
620+
throw std::runtime_error("Attempted to link against a library file that doesn't exist for target \"" + name + "\" in \"" +
621+
key + "\": " + library_path);
622+
}
623+
624+
// Prepend ${CMAKE_CURRENT_SOURCE_DIR} to the path
625+
library_path.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/");
626+
}
627+
}
628+
};
629+
fix_relative_paths(target.link_libraries, "link-libraries");
630+
fix_relative_paths(target.private_link_libraries, "private-link-libraries");
631+
600632
t.optional("link-options", target.link_options);
601633
t.optional("private-link-options", target.private_link_options);
602634

tests/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cmake.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@ arguments = ["build"]
5757
name = "compile-options"
5858
working-directory = "compile-options"
5959
command = "$<TARGET_FILE:cmkr>"
60-
arguments = ["build"]
60+
arguments = ["build"]
61+
62+
[[test]]
63+
condition = "windows"
64+
name = "relative-paths"
65+
working-directory = "relative-paths"
66+
command = "$<TARGET_FILE:cmkr>"
67+
arguments = ["build"]

tests/relative-paths/cmake.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[project]
2+
name = "relative-paths"
3+
4+
[target.test-library]
5+
type = "static"
6+
sources = ["src/library-code.cpp"]
7+
8+
[target.example]
9+
type = "executable"
10+
sources = ["src/main.cpp"]
11+
windows.link-libraries = ["libs/test-library-x64-Release.lib"]
1.42 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Created by Anthony Printup on 9/18/2023.
2+
#include <cstdio>
3+
4+
extern "C" void library_function() {
5+
std::puts("Hello from library_function!");
6+
}

tests/relative-paths/src/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Created by Anthony Printup on 9/18/2023.
2+
#include <cstdio>
3+
4+
#ifdef WIN32
5+
extern "C" void library_function();
6+
#endif
7+
int main() {
8+
puts("Hello from cmkr(relative-paths)!");
9+
#ifdef WIN32
10+
library_function();
11+
#endif
12+
}

0 commit comments

Comments
 (0)