Merge pull request #121 from anthonyprintup/relative-paths

Added support for relative paths in `link-libraries`
main
Duncan Ogilvie 7 months ago committed by GitHub
commit e0d8a085db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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);

13
tests/CMakeLists.txt generated

@ -109,3 +109,16 @@ add_test(
"$<TARGET_FILE:cmkr>"
build
)
if(WIN32) # windows
add_test(
NAME
relative-paths
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/relative-paths"
COMMAND
"$<TARGET_FILE:cmkr>"
build
)
endif()

@ -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"]

@ -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"]

@ -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!");
}

@ -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
}
Loading…
Cancel
Save