Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/cmake-toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions include/project_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
5 changes: 2 additions & 3 deletions src/cmake_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "}");
Expand Down
1 change: 1 addition & 0 deletions src/project_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading