authorgravatar for sentrycraft123@gmail.comJan200101 <sentrycraft123@gmail.com> 2023-06-21 21:51:06+02:00
committergravatar for sentrycraft123@gmail.comJan200101 <sentrycraft123@gmail.com> 2023-06-21 21:51:06+02:00
log5177068c885cb2377bc6058c81418318b9cfdb9c
treea0c66eb5f6d89e664d6709f519c3dcbb2b6670fc
parenta257e33fff8dd9efa7f43ea14e05eef65a7f3354
signaturelock-open Commit is signed but in an unrecognized format.

std.Build: correctly implement cmakedefine and cmakedefine01


1 files changed, 67 insertions(+), 13 deletions(-)

lib/std/Build/Step/ConfigHeader.zig+67-13
...@@ -299,6 +299,11 @@ fn render_cmake(...@@ -299,6 +299,11 @@ fn render_cmake(
299 var line_index: u32 = 0;299 var line_index: u32 = 0;
300 var line_it = std.mem.splitScalar(u8, contents, '\n');300 var line_it = std.mem.splitScalar(u8, contents, '\n');
301 while (line_it.next()) |line| : (line_index += 1) {301 while (line_it.next()) |line| : (line_index += 1) {
302 // if we reached the end of the buffer there is nothing worth doing anymore
303 if (line_it.index == line_it.buffer.len) {
304 continue;
305 }
306
302 if (!std.mem.startsWith(u8, line, "#")) {307 if (!std.mem.startsWith(u8, line, "#")) {
303 try output.appendSlice(line);308 try output.appendSlice(line);
304 try output.appendSlice("\n");309 try output.appendSlice("\n");
...@@ -313,6 +318,9 @@ fn render_cmake(...@@ -313,6 +318,9 @@ fn render_cmake(
313 try output.appendSlice("\n");318 try output.appendSlice("\n");
314 continue;319 continue;
315 }320 }
321
322 const booldefine = std.mem.eql(u8, cmakedefine, "cmakedefine01");
323
316 const name = it.next() orelse {324 const name = it.next() orelse {
317 try step.addError("{s}:{d}: error: missing define name", .{325 try step.addError("{s}:{d}: error: missing define name", .{
318 src_path, line_index + 1,326 src_path, line_index + 1,
...@@ -320,19 +328,66 @@ fn render_cmake(...@@ -320,19 +328,66 @@ fn render_cmake(
320 any_errors = true;328 any_errors = true;
321 continue;329 continue;
322 };330 };
323 const kv = values_copy.fetchSwapRemove(name) orelse {331 var value = values_copy.get(name) orelse blk: {
324 try step.addError("{s}:{d}: error: unspecified config header value: '{s}'", .{332 if (booldefine) {
325 src_path, line_index + 1, name,333 break :blk Value{ .int = 0 };
326 });334 }
327 any_errors = true;335 break :blk Value.undef;
328 continue;
329 };336 };
330 try renderValueC(output, name, kv.value);
331 }
332337
333 for (values_copy.keys()) |name| {338 value = blk: {
334 try step.addError("{s}: error: config header value unused: '{s}'", .{ src_path, name });339 switch (value) {
335 any_errors = true;340 .boolean => |b| {
341 if (!b) {
342 break :blk Value.undef;
343 }
344 },
345 .int => |i| {
346 if (i == 0) {
347 break :blk Value.undef;
348 }
349 },
350 .string => |string| {
351 if (string.len == 0) {
352 break :blk Value.undef;
353 }
354 },
355
356 else => {
357 break :blk value;
358 },
359 }
360 };
361
362 if (booldefine) {
363 value = blk: {
364 switch (value) {
365 .undef => {
366 break :blk Value{ .boolean = false };
367 },
368 .defined => {
369 break :blk Value{ .boolean = false };
370 },
371 .boolean => |b| {
372 break :blk Value{ .boolean = b };
373 },
374 .int => |i| {
375 break :blk Value{ .boolean = i != 0 };
376 },
377 .string => |string| {
378 break :blk Value{ .boolean = string.len != 0 };
379 },
380
381 else => {
382 break :blk Value{ .boolean = false };
383 },
384 }
385 };
386 } else if (value != Value.undef) {
387 value = Value{ .ident = it.rest() };
388 }
389
390 try renderValueC(output, name, value);
336 }391 }
337392
338 if (any_errors) {393 if (any_errors) {
...@@ -392,8 +447,7 @@ fn renderValueC(output: *std.ArrayList(u8), name: []const u8, value: Value) !voi...@@ -392,8 +447,7 @@ fn renderValueC(output: *std.ArrayList(u8), name: []const u8, value: Value) !voi
392 .boolean => |b| {447 .boolean => |b| {
393 try output.appendSlice("#define ");448 try output.appendSlice("#define ");
394 try output.appendSlice(name);449 try output.appendSlice(name);
395 try output.appendSlice(" ");450 try output.appendSlice(if (b) " 1\n" else " 0\n");
396 try output.appendSlice(if (b) "true\n" else "false\n");
397 },451 },
398 .int => |i| {452 .int => |i| {
399 try output.writer().print("#define {s} {d}\n", .{ name, i });453 try output.writer().print("#define {s} {d}\n", .{ name, i });