Skip to content

Commit da4ec26

Browse files
committed
Fix latest custom target review issues
1 parent 31f77ab commit da4ec26

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

docs/examples/custom-command.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ command = [
126126
]
127127
comment = "Create a stamp file for the template-based custom target"
128128
verbatim = true
129+
130+
# A target-level all = false should override the template's all = true.
131+
# If this target is incorrectly included in the default build, the build fails.
132+
[target.custom-codegen-template-disabled]
133+
type = "generated-custom-target"
134+
all = false
135+
command = ["${CMAKE_COMMAND}", "-E", "false"]
136+
comment = "This target must not run as part of the default build"
129137
```
130138

131139

src/cmake_generator.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,9 +1280,9 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
12801280
auto custom_target = target.custom_target;
12811281
if (tmplate != nullptr) {
12821282
const auto &custom = tmplate->outline.custom_target;
1283-
if (custom.has_all) {
1283+
if (!custom_target.has_all && custom.has_all) {
12841284
custom_target.has_all = true;
1285-
custom_target.all = custom_target.all || custom.all;
1285+
custom_target.all = custom.all;
12861286
}
12871287
if (custom.has_command) {
12881288
custom_target.has_command = true;
@@ -1336,7 +1336,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
13361336
throw_target_error("job-pool cannot be used with uses-terminal");
13371337
}
13381338

1339-
auto normalize_generated_output_source = [](const std::string &output) {
1339+
auto has_cmake_path_reference = [](const std::string &value) {
1340+
return value.find("${") != std::string::npos || value.find("$ENV{") != std::string::npos ||
1341+
value.find("$CACHE{") != std::string::npos;
1342+
};
1343+
1344+
auto normalize_generated_output_source = [&has_cmake_path_reference](const std::string &output) {
13401345
auto starts_with = [](const std::string &value, const std::string &prefix) {
13411346
return value.rfind(prefix, 0) == 0;
13421347
};
@@ -1347,7 +1352,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
13471352
starts_with(output, "${PROJECT_BINARY_DIR}") || starts_with(output, "${CMAKE_CURRENT_SOURCE_DIR}") ||
13481353
starts_with(output, "${CMAKE_SOURCE_DIR}") || starts_with(output, "${PROJECT_SOURCE_DIR}") ||
13491354
starts_with(output, "${CMAKE_CURRENT_LIST_DIR}") || starts_with(output, "$ENV{") || starts_with(output, "$CACHE{") ||
1350-
starts_with(output, "${")) {
1355+
has_cmake_path_reference(output)) {
13511356
return output;
13521357
}
13531358
return "${CMAKE_CURRENT_BINARY_DIR}/" + output;
@@ -1446,8 +1451,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
14461451

14471452
// Make sure relative source files exist
14481453
for (const auto &source : sources) {
1449-
auto var_index = source.find("${");
1450-
if (var_index != std::string::npos)
1454+
if (has_cmake_path_reference(source))
14511455
continue;
14521456
const auto &source_path = fs::path(path) / source;
14531457
if (!fs::exists(source_path)) {

tests/custom-command/cmake.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,11 @@ command = [
113113
]
114114
comment = "Create a stamp file for the template-based custom target"
115115
verbatim = true
116+
117+
# A target-level all = false should override the template's all = true.
118+
# If this target is incorrectly included in the default build, the build fails.
119+
[target.custom-codegen-template-disabled]
120+
type = "generated-custom-target"
121+
all = false
122+
command = ["${CMAKE_COMMAND}", "-E", "false"]
123+
comment = "This target must not run as part of the default build"

0 commit comments

Comments
 (0)