aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-06-23 23:56:18 -0700
committerGitHub <noreply@github.com>2023-06-23 23:56:18 -0700
commitb129f1b0462e6c80b4a97851ea3aa8b3f0a4aa37 (patch)
tree33b0a1d8ed1ca572bfb1ff159f3663397fe00aea /lib/std
parentff0a88b133b9c4f27528f39d05ff65a977756bee (diff)
parentf74285b3be58f0282fcdfeab4345660b6f5d35e5 (diff)
downloadzig-b129f1b0462e6c80b4a97851ea3aa8b3f0a4aa37.tar.gz
zig-b129f1b0462e6c80b4a97851ea3aa8b3f0a4aa37.zip
Merge pull request #16037 from Jan200101/PR/cmakedefine-fix
Correct cmakedefine implementation
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/Build/Step/ConfigHeader.zig154
1 files changed, 140 insertions, 14 deletions
diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig
index 4b76e24b26..3d4ed628cc 100644
--- a/lib/std/Build/Step/ConfigHeader.zig
+++ b/lib/std/Build/Step/ConfigHeader.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const ConfigHeader = @This();
const Step = std.Build.Step;
+const Allocator = std.mem.Allocator;
pub const Style = union(enum) {
/// The configure format supported by autotools. It uses `#undef foo` to
@@ -292,13 +293,27 @@ fn render_cmake(
values: std.StringArrayHashMap(Value),
src_path: []const u8,
) !void {
+ var build = step.owner;
+ var allocator = build.allocator;
+
var values_copy = try values.clone();
defer values_copy.deinit();
var any_errors = false;
var line_index: u32 = 0;
var line_it = std.mem.splitScalar(u8, contents, '\n');
- while (line_it.next()) |line| : (line_index += 1) {
+ while (line_it.next()) |raw_line| : (line_index += 1) {
+ // if we reached the end of the buffer there is nothing worth doing anymore
+ if (line_it.index == line_it.buffer.len) {
+ continue;
+ }
+
+ const first_pass = replace_variables(allocator, raw_line, values, "@", "@") catch @panic("Failed to substitute");
+ const line = replace_variables(allocator, first_pass, values, "${", "}") catch @panic("Failed to substitute");
+
+ allocator.free(first_pass);
+ defer allocator.free(line);
+
if (!std.mem.startsWith(u8, line, "#")) {
try output.appendSlice(line);
try output.appendSlice("\n");
@@ -313,6 +328,9 @@ fn render_cmake(
try output.appendSlice("\n");
continue;
}
+
+ const booldefine = std.mem.eql(u8, cmakedefine, "cmakedefine01");
+
const name = it.next() orelse {
try step.addError("{s}:{d}: error: missing define name", .{
src_path, line_index + 1,
@@ -320,19 +338,66 @@ fn render_cmake(
any_errors = true;
continue;
};
- const kv = values_copy.fetchSwapRemove(name) orelse {
- try step.addError("{s}:{d}: error: unspecified config header value: '{s}'", .{
- src_path, line_index + 1, name,
- });
- any_errors = true;
- continue;
+ var value = values_copy.get(name) orelse blk: {
+ if (booldefine) {
+ break :blk Value{ .int = 0 };
+ }
+ break :blk Value.undef;
};
- try renderValueC(output, name, kv.value);
- }
- for (values_copy.keys()) |name| {
- try step.addError("{s}: error: config header value unused: '{s}'", .{ src_path, name });
- any_errors = true;
+ value = blk: {
+ switch (value) {
+ .boolean => |b| {
+ if (!b) {
+ break :blk Value.undef;
+ }
+ },
+ .int => |i| {
+ if (i == 0) {
+ break :blk Value.undef;
+ }
+ },
+ .string => |string| {
+ if (string.len == 0) {
+ break :blk Value.undef;
+ }
+ },
+
+ else => {
+ break :blk value;
+ },
+ }
+ };
+
+ if (booldefine) {
+ value = blk: {
+ switch (value) {
+ .undef => {
+ break :blk Value{ .boolean = false };
+ },
+ .defined => {
+ break :blk Value{ .boolean = false };
+ },
+ .boolean => |b| {
+ break :blk Value{ .boolean = b };
+ },
+ .int => |i| {
+ break :blk Value{ .boolean = i != 0 };
+ },
+ .string => |string| {
+ break :blk Value{ .boolean = string.len != 0 };
+ },
+
+ else => {
+ break :blk Value{ .boolean = false };
+ },
+ }
+ };
+ } else if (value != Value.undef) {
+ value = Value{ .ident = it.rest() };
+ }
+
+ try renderValueC(output, name, value);
}
if (any_errors) {
@@ -392,8 +457,7 @@ fn renderValueC(output: *std.ArrayList(u8), name: []const u8, value: Value) !voi
.boolean => |b| {
try output.appendSlice("#define ");
try output.appendSlice(name);
- try output.appendSlice(" ");
- try output.appendSlice(if (b) "true\n" else "false\n");
+ try output.appendSlice(if (b) " 1\n" else " 0\n");
},
.int => |i| {
try output.writer().print("#define {s} {d}\n", .{ name, i });
@@ -437,3 +501,65 @@ fn renderValueNasm(output: *std.ArrayList(u8), name: []const u8, value: Value) !
},
}
}
+
+fn replace_variables(
+ allocator: Allocator,
+ contents: []const u8,
+ values: std.StringArrayHashMap(Value),
+ prefix: []const u8,
+ suffix: []const u8,
+) ![]const u8 {
+ var content_buf = allocator.dupe(u8, contents) catch @panic("OOM");
+
+ var last_index: usize = 0;
+ while (std.mem.indexOfPos(u8, content_buf, last_index, prefix)) |prefix_index| {
+ const start_index = prefix_index + prefix.len;
+ if (std.mem.indexOfPos(u8, content_buf, start_index, suffix)) |suffix_index| {
+ const end_index = suffix_index + suffix.len;
+
+ const beginline = content_buf[0..prefix_index];
+ const endline = content_buf[end_index..];
+ const key = content_buf[start_index..suffix_index];
+ const value = values.get(key) orelse .undef;
+
+ switch (value) {
+ .boolean => |b| {
+ const buf = try std.fmt.allocPrint(allocator, "{s}{}{s}", .{ beginline, @intFromBool(b), endline });
+ last_index = start_index + 1;
+
+ allocator.free(content_buf);
+ content_buf = buf;
+ },
+ .int => |i| {
+ const buf = try std.fmt.allocPrint(allocator, "{s}{}{s}", .{ beginline, i, endline });
+ const isNegative = i < 0;
+ const digits = (if (0 < i) std.math.log10(std.math.absCast(i)) else 0) + 1;
+ last_index = start_index + @intFromBool(isNegative) + digits + 1;
+
+ allocator.free(content_buf);
+ content_buf = buf;
+ },
+ .string => |string| {
+ const buf = try std.fmt.allocPrint(allocator, "{s}{s}{s}", .{ beginline, string, endline });
+ last_index = start_index + string.len + 1;
+
+ allocator.free(content_buf);
+ content_buf = buf;
+ },
+
+ else => {
+ const buf = try std.fmt.allocPrint(allocator, "{s}{s}", .{ beginline, endline });
+ last_index = start_index + 1;
+
+ allocator.free(content_buf);
+ content_buf = buf;
+ },
+ }
+ continue;
+ }
+
+ last_index = start_index + 1;
+ }
+
+ return content_buf;
+}