Merge pull request #189 from iamtimmy/main

template add-function arguments
This commit is contained in:
Duncan Ogilvie
2025-08-26 17:00:55 +02:00
committed by GitHub
4 changed files with 6 additions and 3 deletions
+2
View File
@@ -302,6 +302,7 @@ condition = "MYPROJECT_BUILD_EXAMPLES"
type = "executable"
link-libraries = ["myproject::mylib"]
add-function = ""
add-arguments = ["myoption"]
pass-sources = false
# Properties from the template are merged with the ones here
@@ -313,6 +314,7 @@ sources = ["src/myexample.cpp"]
The properties declared on a `template` are the same as the ones you use for targets. The only exceptions are:
- `add-function`: Specifies a custom add function. Projects like [pybind11](https://pybind11.readthedocs.io/en/stable/cmake/index.html#new-findpython-mode) have their own `add_xxx` function, which you can specify here.
- `add-arguments`: Arguments to pass to the `add-function` before the list of sources. See [cmake_parse_arguments](https://cmake.org/cmake/help/latest/command/cmake_parse_arguments.html) for more details.
- `pass-sources`: Pass sources directly to the add function instead of using `target_sources`.
## Tests and installation (unfinished)
+1
View File
@@ -118,6 +118,7 @@ struct Target {
struct Template {
Target outline;
std::string add_function;
std::vector<std::string> add_arguments;
bool pass_sources_to_add_function = false;
};
+2 -3
View File
@@ -1372,12 +1372,11 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
// Handle custom add commands from templates.
if (tmplate != nullptr && !tmplate->add_function.empty()) {
add_command = tmplate->add_function;
target_type_string = ""; // TODO: let templates supply options to the add_command here?
if (tmplate->pass_sources_to_add_function) {
cmd(add_command)(target.name, target_type_string, "${" + sources_var + "}");
cmd(add_command)(target.name, tmplate->add_arguments, "${" + sources_var + "}");
} else {
cmd(add_command)(target.name, target_type_string).endl();
cmd(add_command)(target.name, tmplate->add_arguments).endl();
if (has_sources) {
cmd("target_sources")(target.name, target_type == parser::target_interface ? "INTERFACE" : "PRIVATE",
"${" + sources_var + "}");
+1
View File
@@ -785,6 +785,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
tmplate.outline = parse_target(name, t, true);
t.optional("add-function", tmplate.add_function);
t.optional("add-arguments", tmplate.add_arguments);
t.optional("pass-sources-to-add-function", tmplate.pass_sources_to_add_function);
t.optional("pass-sources", tmplate.pass_sources_to_add_function);