authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-07 18:07:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-07 18:07:10-07:00
logea792011d188cff29872c4dd8e732af60765852c
treedfdc63bff691362d988023af1422b20ba7fac059
parente579c88f4ca6deaff53f0d1d226fdfc8715f85ac

add std.build.ConfigHeaderStep

This API converts a config.h.in file into config.h. This is useful when introducing a build.zig file to an existing C/C++ project that is configured with autotools or cmake. The cmake syntax is not implemented yet.

5 files changed, 260 insertions(+), 12 deletions(-)

lib/std/build.zig+13
......@@ -21,6 +21,7 @@ const ThisModule = @This();
2121
2222pub const CheckFileStep = @import("build/CheckFileStep.zig");
2323pub const CheckObjectStep = @import("build/CheckObjectStep.zig");
24pub const ConfigHeaderStep = @import("build/ConfigHeaderStep.zig");
2425pub const EmulatableRunStep = @import("build/EmulatableRunStep.zig");
2526pub const FmtStep = @import("build/FmtStep.zig");
2627pub const InstallArtifactStep = @import("build/InstallArtifactStep.zig");
......@@ -364,6 +365,17 @@ pub const Builder = struct {
364365 return run_step;
365366 }
366367
368 pub fn addConfigHeader(
369 b: *Builder,
370 source: FileSource,
371 style: ConfigHeaderStep.Style,
372 values: anytype,
373 ) *ConfigHeaderStep {
374 const config_header_step = ConfigHeaderStep.create(b, source, style);
375 config_header_step.addValues(values);
376 return config_header_step;
377 }
378
367379 /// Allocator.dupe without the need to handle out of memory.
368380 pub fn dupe(self: *Builder, bytes: []const u8) []u8 {
369381 return self.allocator.dupe(u8, bytes) catch unreachable;
......@@ -1427,6 +1439,7 @@ pub const Step = struct {
14271439 emulatable_run,
14281440 check_file,
14291441 check_object,
1442 config_header,
14301443 install_raw,
14311444 options,
14321445 custom,
lib/std/build/ConfigHeaderStep.zig created+221
......@@ -0,0 +1,221 @@
1const std = @import("../std.zig");
2const ConfigHeaderStep = @This();
3const Step = std.build.Step;
4const Builder = std.build.Builder;
5
6pub const base_id: Step.Id = .config_header;
7
8pub const Style = enum {
9 /// The configure format supported by autotools. It uses `#undef foo` to
10 /// mark lines that can be substituted with different values.
11 autoconf,
12 /// The configure format supported by CMake. It uses `@@FOO@@` and
13 /// `#cmakedefine` for template substitution.
14 cmake,
15};
16
17pub const Value = union(enum) {
18 undef,
19 defined,
20 boolean: bool,
21 int: i64,
22 ident: []const u8,
23 string: []const u8,
24};
25
26step: Step,
27builder: *Builder,
28source: std.build.FileSource,
29style: Style,
30values: std.StringHashMap(Value),
31max_bytes: usize = 2 * 1024 * 1024,
32output_dir: []const u8,
33output_basename: []const u8,
34
35pub fn create(builder: *Builder, source: std.build.FileSource, style: Style) *ConfigHeaderStep {
36 const self = builder.allocator.create(ConfigHeaderStep) catch @panic("OOM");
37 const name = builder.fmt("configure header {s}", .{source.getDisplayName()});
38 self.* = .{
39 .builder = builder,
40 .step = Step.init(base_id, name, builder.allocator, make),
41 .source = source,
42 .style = style,
43 .values = std.StringHashMap(Value).init(builder.allocator),
44 .output_dir = undefined,
45 .output_basename = "config.h",
46 };
47 switch (source) {
48 .path => |p| {
49 const basename = std.fs.path.basename(p);
50 if (std.mem.endsWith(u8, basename, ".h.in")) {
51 self.output_basename = basename[0 .. basename.len - 3];
52 }
53 },
54 else => {},
55 }
56 return self;
57}
58
59pub fn addValues(self: *ConfigHeaderStep, values: anytype) void {
60 return addValuesInner(self, values) catch @panic("OOM");
61}
62
63fn addValuesInner(self: *ConfigHeaderStep, values: anytype) !void {
64 inline for (@typeInfo(@TypeOf(values)).Struct.fields) |field| {
65 switch (@typeInfo(field.type)) {
66 .Null => {
67 try self.values.put(field.name, .undef);
68 },
69 .Void => {
70 try self.values.put(field.name, .defined);
71 },
72 .Bool => {
73 try self.values.put(field.name, .{ .boolean = @field(values, field.name) });
74 },
75 .ComptimeInt => {
76 try self.values.put(field.name, .{ .int = @field(values, field.name) });
77 },
78 .EnumLiteral => {
79 try self.values.put(field.name, .{ .ident = @tagName(@field(values, field.name)) });
80 },
81 .Pointer => |ptr| {
82 switch (@typeInfo(ptr.child)) {
83 .Array => |array| {
84 if (ptr.size == .One and array.child == u8) {
85 try self.values.put(field.name, .{ .string = @field(values, field.name) });
86 continue;
87 }
88 },
89 else => {},
90 }
91
92 @compileError("unsupported ConfigHeaderStep value type: " ++
93 @typeName(field.type));
94 },
95 else => @compileError("unsupported ConfigHeaderStep value type: " ++
96 @typeName(field.type)),
97 }
98 }
99}
100
101fn make(step: *Step) !void {
102 const self = @fieldParentPtr(ConfigHeaderStep, "step", step);
103 const gpa = self.builder.allocator;
104 const src_path = self.source.getPath(self.builder);
105 const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes);
106
107 // The cache is used here not really as a way to speed things up - because writing
108 // the data to a file would probably be very fast - but as a way to find a canonical
109 // location to put build artifacts.
110
111 // If, for example, a hard-coded path was used as the location to put ConfigHeaderStep
112 // files, then two ConfigHeaderStep executing in parallel might clobber each other.
113
114 // TODO port the cache system from the compiler to zig std lib. Until then
115 // we construct the path directly, and no "cache hit" detection happens;
116 // the files are always written.
117 // Note there is very similar code over in WriteFileStep
118 const Hasher = std.crypto.auth.siphash.SipHash128(1, 3);
119 // Random bytes to make ConfigHeaderStep unique. Refresh this with new
120 // random bytes when ConfigHeaderStep implementation is modified in a
121 // non-backwards-compatible way.
122 var hash = Hasher.init("X1pQzdDt91Zlh7Eh");
123 hash.update(self.source.getDisplayName());
124 hash.update(contents);
125
126 var digest: [16]u8 = undefined;
127 hash.final(&digest);
128 var hash_basename: [digest.len * 2]u8 = undefined;
129 _ = std.fmt.bufPrint(
130 &hash_basename,
131 "{s}",
132 .{std.fmt.fmtSliceHexLower(&digest)},
133 ) catch unreachable;
134
135 self.output_dir = try std.fs.path.join(gpa, &[_][]const u8{
136 self.builder.cache_root, "o", &hash_basename,
137 });
138
139 var dir = std.fs.cwd().makeOpenPath(self.output_dir, .{}) catch |err| {
140 std.debug.print("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) });
141 return err;
142 };
143 defer dir.close();
144
145 var values_copy = try self.values.clone();
146 defer values_copy.deinit();
147
148 var output = std.ArrayList(u8).init(gpa);
149 defer output.deinit();
150 try output.ensureTotalCapacity(contents.len);
151
152 try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n");
153
154 var any_errors = false;
155 var line_index: u32 = 0;
156 var line_it = std.mem.split(u8, contents, "\n");
157 while (line_it.next()) |line| : (line_index += 1) {
158 if (!std.mem.startsWith(u8, line, "#")) {
159 try output.appendSlice(line);
160 try output.appendSlice("\n");
161 continue;
162 }
163 var it = std.mem.tokenize(u8, line[1..], " \t\r");
164 const undef = it.next().?;
165 if (!std.mem.eql(u8, undef, "undef")) {
166 try output.appendSlice(line);
167 try output.appendSlice("\n");
168 continue;
169 }
170 const name = it.rest();
171 const kv = values_copy.fetchRemove(name) orelse {
172 std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{
173 src_path, line_index + 1, name,
174 });
175 any_errors = true;
176 continue;
177 };
178 switch (kv.value) {
179 .undef => {
180 try output.appendSlice("/* #undef ");
181 try output.appendSlice(name);
182 try output.appendSlice(" */\n");
183 },
184 .defined => {
185 try output.appendSlice("#define ");
186 try output.appendSlice(name);
187 try output.appendSlice("\n");
188 },
189 .boolean => |b| {
190 try output.appendSlice("#define ");
191 try output.appendSlice(name);
192 try output.appendSlice(" ");
193 try output.appendSlice(if (b) "true\n" else "false\n");
194 },
195 .int => |i| {
196 try output.writer().print("#define {s} {d}\n", .{ name, i });
197 },
198 .ident => |ident| {
199 try output.writer().print("#define {s} {s}\n", .{ name, ident });
200 },
201 .string => |string| {
202 // TODO: use C-specific escaping instead of zig string literals
203 try output.writer().print("#define {s} \"{}\"\n", .{ name, std.zig.fmtEscapes(string) });
204 },
205 }
206 }
207
208 {
209 var it = values_copy.iterator();
210 while (it.next()) |entry| {
211 const name = entry.key_ptr.*;
212 std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
213 }
214 }
215
216 if (any_errors) {
217 return error.HeaderConfigFailed;
218 }
219
220 try dir.writeFile(self.output_basename, output.items);
221}
lib/std/build/LibExeObjStep.zig+11
......@@ -28,6 +28,7 @@ const EmulatableRunStep = std.build.EmulatableRunStep;
2828const CheckObjectStep = std.build.CheckObjectStep;
2929const RunStep = std.build.RunStep;
3030const OptionsStep = std.build.OptionsStep;
31const ConfigHeaderStep = std.build.ConfigHeaderStep;
3132const LibExeObjStep = @This();
3233
3334pub const base_id = .lib_exe_obj;
......@@ -266,6 +267,7 @@ pub const IncludeDir = union(enum) {
266267 raw_path: []const u8,
267268 raw_path_system: []const u8,
268269 other_step: *LibExeObjStep,
270 config_header_step: *ConfigHeaderStep,
269271};
270272
271273pub const Kind = enum {
......@@ -932,6 +934,11 @@ pub fn addIncludePath(self: *LibExeObjStep, path: []const u8) void {
932934 self.include_dirs.append(IncludeDir{ .raw_path = self.builder.dupe(path) }) catch unreachable;
933935}
934936
937pub fn addConfigHeader(self: *LibExeObjStep, config_header: *ConfigHeaderStep) void {
938 self.step.dependOn(&config_header.step);
939 self.include_dirs.append(.{ .config_header_step = config_header }) catch @panic("OOM");
940}
941
935942pub fn addLibraryPath(self: *LibExeObjStep, path: []const u8) void {
936943 self.lib_paths.append(self.builder.dupe(path)) catch unreachable;
937944}
......@@ -1684,6 +1691,10 @@ fn make(step: *Step) !void {
16841691 try zig_args.append("-isystem");
16851692 try zig_args.append(fs.path.dirname(h_path).?);
16861693 },
1694 .config_header_step => |config_header| {
1695 try zig_args.append("-I");
1696 try zig_args.append(config_header.output_dir);
1697 },
16871698 }
16881699 }
16891700
lib/std/build/RunStep.zig+2-2
......@@ -17,7 +17,7 @@ const max_stdout_size = 1 * 1024 * 1024; // 1 MiB
1717
1818const RunStep = @This();
1919
20pub const base_id = .run;
20pub const base_id: Step.Id = .run;
2121
2222step: Step,
2323builder: *Builder,
......@@ -59,7 +59,7 @@ pub fn create(builder: *Builder, name: []const u8) *RunStep {
5959 const self = builder.allocator.create(RunStep) catch unreachable;
6060 self.* = RunStep{
6161 .builder = builder,
62 .step = Step.init(.run, name, builder.allocator, make),
62 .step = Step.init(base_id, name, builder.allocator, make),
6363 .argv = ArrayList(Arg).init(builder.allocator),
6464 .cwd = null,
6565 .env_map = null,
lib/std/build/WriteFileStep.zig+13-10
......@@ -63,14 +63,15 @@ fn make(step: *Step) !void {
6363 // files, then two WriteFileSteps executing in parallel might clobber each other.
6464
6565 // TODO port the cache system from the compiler to zig std lib. Until then
66 // we use blake2b directly and construct the path, and no "cache hit"
67 // detection happens; the files are always written.
68 var hash = std.crypto.hash.blake2.Blake2b384.init(.{});
69
66 // we directly construct the path, and no "cache hit" detection happens;
67 // the files are always written.
68 // Note there is similar code over in ConfigHeaderStep.
69 const Hasher = std.crypto.auth.siphash.SipHash128(1, 3);
7070 // Random bytes to make WriteFileStep unique. Refresh this with
7171 // new random bytes when WriteFileStep implementation is modified
7272 // in a non-backwards-compatible way.
73 hash.update("eagVR1dYXoE7ARDP");
73 var hash = Hasher.init("eagVR1dYXoE7ARDP");
74
7475 {
7576 var it = self.files.first;
7677 while (it) |node| : (it = node.next) {
......@@ -79,21 +80,23 @@ fn make(step: *Step) !void {
7980 hash.update("|");
8081 }
8182 }
82 var digest: [48]u8 = undefined;
83 var digest: [16]u8 = undefined;
8384 hash.final(&digest);
8485 var hash_basename: [64]u8 = undefined;
85 _ = fs.base64_encoder.encode(&hash_basename, &digest);
86 _ = std.fmt.bufPrint(
87 &hash_basename,
88 "{s}",
89 .{std.fmt.fmtSliceHexLower(&digest)},
90 ) catch unreachable;
8691 self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{
8792 self.builder.cache_root,
8893 "o",
8994 &hash_basename,
9095 });
91 // TODO replace with something like fs.makePathAndOpenDir
92 fs.cwd().makePath(self.output_dir) catch |err| {
96 var dir = fs.cwd().makeOpenPath(self.output_dir, .{}) catch |err| {
9397 std.debug.print("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) });
9498 return err;
9599 };
96 var dir = try fs.cwd().openDir(self.output_dir, .{});
97100 defer dir.close();
98101 {
99102 var it = self.files.first;