Skip to content

Commit 49cdab3

Browse files
authored
Merge pull request #169 from build-cpp/namespace-targets
Support `::mylib` which that target that exists at configure-time
2 parents 3caf593 + e318139 commit 49cdab3

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ AllowShortCaseLabelsOnASingleLine: false
1313
AllowShortFunctionsOnASingleLine: false
1414
AllowShortIfStatementsOnASingleLine: false
1515
AllowShortLoopsOnASingleLine: false
16+
AllowShortLambdasOnASingleLine: None
1617
AlwaysBreakAfterDefinitionReturnType: None
1718
AlwaysBreakAfterReturnType: None
1819
AlwaysBreakBeforeMultilineStrings: false

docs/cmake-toml.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ A table mapping the cmkr features to the relevant CMake construct and the releva
272272
| `compile-options` | [`target_compile_options`](https://cmake.org/cmake/help/latest/command/target_compile_options.html) | Adds compiler flags. |
273273
| `include-directories` | [`target_include_directories`](https://cmake.org/cmake/help/latest/command/target_include_directories.html) | Adds include directories. |
274274
| `link-directories` | [`target_link_directories`](https://cmake.org/cmake/help/latest/command/target_link_directories.html) | Adds library directories. |
275-
| `link-libraries` | [`target_link_libraries`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) | Adds library dependencies. |
275+
| `link-libraries` | [`target_link_libraries`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) | Adds library dependencies. Use `::mylib` to make sure a target exists. |
276276
| `link-options` | [`target_link_options`](https://cmake.org/cmake/help/latest/command/target_link_options.html) | Adds linker flags. |
277277
| `precompile-headers` | [`target_precompile_headers`](https://cmake.org/cmake/help/latest/command/target_precompile_headers.html) | Specifies precompiled headers. |
278278
| `properties` | [`set_target_properties`](https://cmake.org/cmake/help/latest/command/set_target_properties.html) | See [properties on targets](https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets) for more information. |

src/cmake_generator.cpp

+43-10
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,15 @@ struct Generator {
558558
}
559559

560560
void conditional_includes(const parser::ConditionVector &include) {
561-
handle_condition(include, [this](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
561+
handle_condition(include, [this](const std::string &, const std::vector<std::string> &includes) {
562+
inject_includes(includes);
563+
});
562564
}
563565

564566
void conditional_cmake(const parser::Condition<std::string> &cmake) {
565-
handle_condition(cmake, [this](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
567+
handle_condition(cmake, [this](const std::string &, const std::string &cmake) {
568+
inject_cmake(cmake);
569+
});
566570
}
567571

568572
bool if_condition(const std::string &condition) {
@@ -719,9 +723,15 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
719723

720724
// Helper lambdas for more convenient CMake generation
721725
auto &ss = gen.ss;
722-
auto cmd = [&gen](const std::string &command) { return gen.cmd(command); };
723-
auto comment = [&gen](const std::string &comment) { return gen.comment(comment); };
724-
auto endl = [&gen]() { gen.endl(); };
726+
auto cmd = [&gen](const std::string &command) {
727+
return gen.cmd(command);
728+
};
729+
auto comment = [&gen](const std::string &comment) {
730+
return gen.comment(comment);
731+
};
732+
auto endl = [&gen]() {
733+
gen.endl();
734+
};
725735

726736
std::string cmkr_url = "https://github.com/build-cpp/cmkr";
727737
comment("This file is automatically generated from cmake.toml - DO NOT EDIT");
@@ -1145,7 +1155,9 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
11451155
for (size_t i = 0; i < project.targets.size(); i++) {
11461156
const auto &target = project.targets[i];
11471157

1148-
auto throw_target_error = [&target](const std::string &message) { throw std::runtime_error("[target." + target.name + "] " + message); };
1158+
auto throw_target_error = [&target](const std::string &message) {
1159+
throw std::runtime_error("[target." + target.name + "] " + message);
1160+
};
11491161

11501162
const parser::Template *tmplate = nullptr;
11511163
std::unique_ptr<ConditionScope> tmplate_cs{};
@@ -1384,8 +1396,29 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
13841396
}
13851397

13861398
auto target_cmd = [&](const char *command, const parser::ConditionVector &cargs, const std::string &scope) {
1387-
gen.handle_condition(cargs,
1388-
[&](const std::string &, const std::vector<std::string> &args) { cmd(command)(target.name, scope, args); });
1399+
gen.handle_condition(cargs, [&](const std::string &, const std::vector<std::string> &args) {
1400+
cmd(command)(target.name, scope, args);
1401+
});
1402+
};
1403+
1404+
auto link_libraries = [&](const parser::ConditionVector &cargs, const std::string &scope) {
1405+
gen.handle_condition(cargs, [&](const std::string &, const std::vector<std::string> &args) {
1406+
std::vector<std::string> targs;
1407+
for (const std::string &arg : args) {
1408+
if (arg.find("::") == 0) {
1409+
auto library = arg.substr(2);
1410+
// clang-format off
1411+
cmd("if")("NOT", "TARGET", library);
1412+
cmd("message")("FATAL_ERROR", "Target \"" + library + "\" referenced by \"" + target.name + "\" does not exist!");
1413+
cmd("endif")().endl();
1414+
// clang-format on
1415+
targs.push_back(std::move(library));
1416+
} else {
1417+
targs.push_back(arg);
1418+
}
1419+
}
1420+
cmd("target_link_libraries")(target.name, scope, targs);
1421+
});
13891422
};
13901423

13911424
auto gen_target_cmds = [&](const parser::Target &t) {
@@ -1404,8 +1437,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
14041437
target_cmd("target_link_directories", t.link_directories, target_scope);
14051438
target_cmd("target_link_directories", t.private_link_directories, "PRIVATE");
14061439

1407-
target_cmd("target_link_libraries", t.link_libraries, target_scope);
1408-
target_cmd("target_link_libraries", t.private_link_libraries, "PRIVATE");
1440+
link_libraries(t.link_libraries, target_scope);
1441+
link_libraries(t.private_link_libraries, "PRIVATE");
14091442

14101443
target_cmd("target_link_options", t.link_options, target_scope);
14111444
target_cmd("target_link_options", t.private_link_options, "PRIVATE");

0 commit comments

Comments
 (0)