Skip to content

Commit

Permalink
Merge pull request #121 from anthonyprintup/relative-paths
Browse files Browse the repository at this point in the history
Added support for relative paths in `link-libraries`
  • Loading branch information
mrexodia authored Sep 22, 2023
2 parents 56da414 + a7ca9f0 commit e0d8a08
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/project_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,38 @@ 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 = [&name, &path](ConditionVector &libraries, const char *key) {
for (const auto &library_entries : libraries) {
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 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_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
library_path.insert(0, "${CMAKE_CURRENT_SOURCE_DIR}/");
}
}
};
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);

Expand Down
13 changes: 13 additions & 0 deletions tests/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion tests/cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ arguments = ["build"]
name = "compile-options"
working-directory = "compile-options"
command = "$<TARGET_FILE:cmkr>"
arguments = ["build"]
arguments = ["build"]

[[test]]
condition = "windows"
name = "relative-paths"
working-directory = "relative-paths"
command = "$<TARGET_FILE:cmkr>"
arguments = ["build"]
11 changes: 11 additions & 0 deletions tests/relative-paths/cmake.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "relative-paths"

[target.test-library]
type = "static"
sources = ["src/library-code.cpp"]

[target.example]
type = "executable"
sources = ["src/main.cpp"]
windows.link-libraries = ["libs/test-library-x64-Release.lib"]
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/relative-paths/src/library-code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Created by Anthony Printup on 9/18/2023.
#include <cstdio>

extern "C" void library_function() {
std::puts("Hello from library_function!");
}
12 changes: 12 additions & 0 deletions tests/relative-paths/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Created by Anthony Printup on 9/18/2023.
#include <cstdio>

#ifdef WIN32
extern "C" void library_function();
#endif
int main() {
puts("Hello from cmkr(relative-paths)!");
#ifdef WIN32
library_function();
#endif
}

0 comments on commit e0d8a08

Please sign in to comment.