Skip to content

Commit fcbf937

Browse files
authored
Merge pull request #189 from iamtimmy/main
template add-function arguments
2 parents 735898d + e609c8a commit fcbf937

File tree

4 files changed

+6
-3
lines changed

4 files changed

+6
-3
lines changed

docs/cmake-toml.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ condition = "MYPROJECT_BUILD_EXAMPLES"
302302
type = "executable"
303303
link-libraries = ["myproject::mylib"]
304304
add-function = ""
305+
add-arguments = ["myoption"]
305306
pass-sources = false
306307

307308
# Properties from the template are merged with the ones here
@@ -313,6 +314,7 @@ sources = ["src/myexample.cpp"]
313314
The properties declared on a `template` are the same as the ones you use for targets. The only exceptions are:
314315

315316
- `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.
317+
- `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.
316318
- `pass-sources`: Pass sources directly to the add function instead of using `target_sources`.
317319

318320
## Tests and installation (unfinished)

include/project_parser.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct Target {
118118
struct Template {
119119
Target outline;
120120
std::string add_function;
121+
std::vector<std::string> add_arguments;
121122
bool pass_sources_to_add_function = false;
122123
};
123124

src/cmake_generator.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,12 +1372,11 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
13721372
// Handle custom add commands from templates.
13731373
if (tmplate != nullptr && !tmplate->add_function.empty()) {
13741374
add_command = tmplate->add_function;
1375-
target_type_string = ""; // TODO: let templates supply options to the add_command here?
13761375

13771376
if (tmplate->pass_sources_to_add_function) {
1378-
cmd(add_command)(target.name, target_type_string, "${" + sources_var + "}");
1377+
cmd(add_command)(target.name, tmplate->add_arguments, "${" + sources_var + "}");
13791378
} else {
1380-
cmd(add_command)(target.name, target_type_string).endl();
1379+
cmd(add_command)(target.name, tmplate->add_arguments).endl();
13811380
if (has_sources) {
13821381
cmd("target_sources")(target.name, target_type == parser::target_interface ? "INTERFACE" : "PRIVATE",
13831382
"${" + sources_var + "}");

src/project_parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
785785
tmplate.outline = parse_target(name, t, true);
786786

787787
t.optional("add-function", tmplate.add_function);
788+
t.optional("add-arguments", tmplate.add_arguments);
788789
t.optional("pass-sources-to-add-function", tmplate.pass_sources_to_add_function);
789790
t.optional("pass-sources", tmplate.pass_sources_to_add_function);
790791

0 commit comments

Comments
 (0)