std.Build.Step.ConfigHeader: handle empty keys more permissively

${} is never used in a cmake config header but still needs to be
substituted
instead of erroring because of an empty key simply omit the value
This commit is contained in:
Jan200101 2025-06-01 20:48:44 +02:00
parent 0386730777
commit 9d6750935e
No known key found for this signature in database
GPG key ID: 5B71B1D78B882E05
3 changed files with 14 additions and 6 deletions

View file

@ -748,10 +748,12 @@ fn expand_variables_cmake(
const key_start = open_pos.target + open_var.len; const key_start = open_pos.target + open_var.len;
const key = result.items[key_start..]; const key = result.items[key_start..];
if (key.len == 0) { const value = values.get(key) orelse
return error.MissingKey; if (key.len == 0)
} .undef
const value = values.get(key) orelse return error.MissingValue; else
return error.MissingValue;
result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len); result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
switch (value) { switch (value) {
.undef, .defined => {}, .undef, .defined => {},
@ -952,8 +954,8 @@ test "expand_variables_cmake simple cases" {
// line with misc content is preserved // line with misc content is preserved
try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values); try testReplaceVariablesCMake(allocator, "no substitution", "no substitution", values);
// empty ${} wrapper leads to an error // empty ${} wrapper is removed
try std.testing.expectError(error.MissingKey, testReplaceVariablesCMake(allocator, "${}", "", values)); try testReplaceVariablesCMake(allocator, "${}", "", values);
// empty @ sigils are preserved // empty @ sigils are preserved
try testReplaceVariablesCMake(allocator, "@", "@", values); try testReplaceVariablesCMake(allocator, "@", "@", values);

View file

@ -94,6 +94,9 @@
// test10 // test10
// @noval@@stringval@@trueval@@zeroval@ // @noval@@stringval@@trueval@@zeroval@
// empty key, removed
// ${}
// no substition // no substition
// ${noval} // ${noval}

View file

@ -94,6 +94,9 @@
// test10 // test10
// test10 // test10
// empty key, removed
//
// no substition // no substition
// //