authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-05 08:27:53-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-02-05 08:27:53-05:00
loga5b34a61ab61882bf55d87e4cbc8186215ecf320
tree7b436f99dc6063e380813a0a357351f301fdbaad
parent11cc1c16fa36a7eb13cba1c43fb153ee6aca7b58
parentb29e3fa2cd667cc967b4c7dfb5023e5ac0224d96
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14562: std.Build: enhancements to ConfigHeaderStep


5 files changed, 165 insertions(+), 76 deletions(-)

lib/std/Build.zig+7-3
...@@ -598,13 +598,17 @@ pub fn addSystemCommand(self: *Build, argv: []const []const u8) *RunStep {...@@ -598,13 +598,17 @@ pub fn addSystemCommand(self: *Build, argv: []const []const u8) *RunStep {
598 return run_step;598 return run_step;
599}599}
600600
601/// Using the `values` provided, produces a C header file, possibly based on a
602/// template input file (e.g. config.h.in).
603/// When an input template file is provided, this function will fail the build
604/// when an option not found in the input file is provided in `values`, and
605/// when an option found in the input file is missing from `values`.
601pub fn addConfigHeader(606pub fn addConfigHeader(
602 b: *Build,607 b: *Build,
603 source: FileSource,608 options: ConfigHeaderStep.Options,
604 style: ConfigHeaderStep.Style,
605 values: anytype,609 values: anytype,
606) *ConfigHeaderStep {610) *ConfigHeaderStep {
607 const config_header_step = ConfigHeaderStep.create(b, source, style);611 const config_header_step = ConfigHeaderStep.create(b, options);
608 config_header_step.addValues(values);612 config_header_step.addValues(values);
609 return config_header_step;613 return config_header_step;
610}614}
lib/std/Build/CompileStep.zig+23-2
...@@ -442,6 +442,26 @@ pub fn installHeader(a: *CompileStep, src_path: []const u8, dest_rel_path: []con...@@ -442,6 +442,26 @@ pub fn installHeader(a: *CompileStep, src_path: []const u8, dest_rel_path: []con
442 a.installed_headers.append(&install_file.step) catch @panic("OOM");442 a.installed_headers.append(&install_file.step) catch @panic("OOM");
443}443}
444444
445pub const InstallConfigHeaderOptions = struct {
446 install_dir: InstallDir = .header,
447 dest_rel_path: ?[]const u8 = null,
448};
449
450pub fn installConfigHeader(
451 cs: *CompileStep,
452 config_header: *ConfigHeaderStep,
453 options: InstallConfigHeaderOptions,
454) void {
455 const dest_rel_path = options.dest_rel_path orelse config_header.include_path;
456 const install_file = cs.builder.addInstallFileWithDir(
457 .{ .generated = &config_header.output_file },
458 options.install_dir,
459 dest_rel_path,
460 );
461 cs.builder.getInstallStep().dependOn(&install_file.step);
462 cs.installed_headers.append(&install_file.step) catch @panic("OOM");
463}
464
445pub fn installHeadersDirectory(465pub fn installHeadersDirectory(
446 a: *CompileStep,466 a: *CompileStep,
447 src_dir_path: []const u8,467 src_dir_path: []const u8,
...@@ -1622,8 +1642,9 @@ fn make(step: *Step) !void {...@@ -1622,8 +1642,9 @@ fn make(step: *Step) !void {
1622 }1642 }
1623 },1643 },
1624 .config_header_step => |config_header| {1644 .config_header_step => |config_header| {
1625 try zig_args.append("-I");1645 const full_file_path = config_header.output_file.path.?;
1626 try zig_args.append(config_header.output_dir);1646 const header_dir_path = full_file_path[0 .. full_file_path.len - config_header.include_path.len];
1647 try zig_args.appendSlice(&.{ "-I", header_dir_path });
1627 },1648 },
1628 }1649 }
1629 }1650 }
lib/std/Build/ConfigHeaderStep.zig+128-55
...@@ -4,13 +4,22 @@ const Step = std.Build.Step;...@@ -4,13 +4,22 @@ const Step = std.Build.Step;
44
5pub const base_id: Step.Id = .config_header;5pub const base_id: Step.Id = .config_header;
66
7pub const Style = enum {7pub const Style = union(enum) {
8 /// The configure format supported by autotools. It uses `#undef foo` to8 /// The configure format supported by autotools. It uses `#undef foo` to
9 /// mark lines that can be substituted with different values.9 /// mark lines that can be substituted with different values.
10 autoconf,10 autoconf: std.Build.FileSource,
11 /// The configure format supported by CMake. It uses `@@FOO@@` and11 /// The configure format supported by CMake. It uses `@@FOO@@` and
12 /// `#cmakedefine` for template substitution.12 /// `#cmakedefine` for template substitution.
13 cmake,13 cmake: std.Build.FileSource,
14 /// Instead of starting with an input file, start with nothing.
15 blank,
16
17 pub fn getFileSource(style: Style) ?std.Build.FileSource {
18 switch (style) {
19 .autoconf, .cmake => |s| return s,
20 .blank => return null,
21 }
22 }
14};23};
1524
16pub const Value = union(enum) {25pub const Value = union(enum) {
...@@ -24,34 +33,50 @@ pub const Value = union(enum) {...@@ -24,34 +33,50 @@ pub const Value = union(enum) {
2433
25step: Step,34step: Step,
26builder: *std.Build,35builder: *std.Build,
27source: std.Build.FileSource,36values: std.StringArrayHashMap(Value),
37output_file: std.Build.GeneratedFile,
38
28style: Style,39style: Style,
29values: std.StringHashMap(Value),40max_bytes: usize,
30max_bytes: usize = 2 * 1024 * 1024,41include_path: []const u8,
31output_dir: []const u8,
32output_basename: []const u8,
3342
34pub fn create(builder: *std.Build, source: std.Build.FileSource, style: Style) *ConfigHeaderStep {43pub const Options = struct {
44 style: Style = .blank,
45 max_bytes: usize = 2 * 1024 * 1024,
46 include_path: ?[]const u8 = null,
47};
48
49pub fn create(builder: *std.Build, options: Options) *ConfigHeaderStep {
35 const self = builder.allocator.create(ConfigHeaderStep) catch @panic("OOM");50 const self = builder.allocator.create(ConfigHeaderStep) catch @panic("OOM");
36 const name = builder.fmt("configure header {s}", .{source.getDisplayName()});51 const name = if (options.style.getFileSource()) |s|
52 builder.fmt("configure {s} header {s}", .{ @tagName(options.style), s.getDisplayName() })
53 else
54 builder.fmt("configure {s} header", .{@tagName(options.style)});
37 self.* = .{55 self.* = .{
38 .builder = builder,56 .builder = builder,
39 .step = Step.init(base_id, name, builder.allocator, make),57 .step = Step.init(base_id, name, builder.allocator, make),
40 .source = source,58 .style = options.style,
41 .style = style,59 .values = std.StringArrayHashMap(Value).init(builder.allocator),
42 .values = std.StringHashMap(Value).init(builder.allocator),60
43 .output_dir = undefined,61 .max_bytes = options.max_bytes,
44 .output_basename = "config.h",62 .include_path = "config.h",
63 .output_file = .{ .step = &self.step },
45 };64 };
46 switch (source) {65
66 if (options.style.getFileSource()) |s| switch (s) {
47 .path => |p| {67 .path => |p| {
48 const basename = std.fs.path.basename(p);68 const basename = std.fs.path.basename(p);
49 if (std.mem.endsWith(u8, basename, ".h.in")) {69 if (std.mem.endsWith(u8, basename, ".h.in")) {
50 self.output_basename = basename[0 .. basename.len - 3];70 self.include_path = basename[0 .. basename.len - 3];
51 }71 }
52 },72 },
53 else => {},73 else => {},
74 };
75
76 if (options.include_path) |include_path| {
77 self.include_path = include_path;
54 }78 }
79
55 return self;80 return self;
56}81}
5782
...@@ -112,8 +137,6 @@ fn putValue(self: *ConfigHeaderStep, field_name: []const u8, comptime T: type, v...@@ -112,8 +137,6 @@ fn putValue(self: *ConfigHeaderStep, field_name: []const u8, comptime T: type, v
112fn make(step: *Step) !void {137fn make(step: *Step) !void {
113 const self = @fieldParentPtr(ConfigHeaderStep, "step", step);138 const self = @fieldParentPtr(ConfigHeaderStep, "step", step);
114 const gpa = self.builder.allocator;139 const gpa = self.builder.allocator;
115 const src_path = self.source.getPath(self.builder);
116 const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes);
117140
118 // The cache is used here not really as a way to speed things up - because writing141 // The cache is used here not really as a way to speed things up - because writing
119 // the data to a file would probably be very fast - but as a way to find a canonical142 // the data to a file would probably be very fast - but as a way to find a canonical
...@@ -130,9 +153,30 @@ fn make(step: *Step) !void {...@@ -130,9 +153,30 @@ fn make(step: *Step) !void {
130 // Random bytes to make ConfigHeaderStep unique. Refresh this with new153 // Random bytes to make ConfigHeaderStep unique. Refresh this with new
131 // random bytes when ConfigHeaderStep implementation is modified in a154 // random bytes when ConfigHeaderStep implementation is modified in a
132 // non-backwards-compatible way.155 // non-backwards-compatible way.
133 var hash = Hasher.init("X1pQzdDt91Zlh7Eh");156 var hash = Hasher.init("PGuDTpidxyMqnkGM");
134 hash.update(self.source.getDisplayName());157
135 hash.update(contents);158 var output = std.ArrayList(u8).init(gpa);
159 defer output.deinit();
160
161 try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n");
162
163 switch (self.style) {
164 .autoconf => |file_source| {
165 const src_path = file_source.getPath(self.builder);
166 const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes);
167 try render_autoconf(contents, &output, self.values, src_path);
168 },
169 .cmake => |file_source| {
170 const src_path = file_source.getPath(self.builder);
171 const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes);
172 try render_cmake(contents, &output, self.values, src_path);
173 },
174 .blank => {
175 try render_blank(&output, self.values, self.include_path);
176 },
177 }
178
179 hash.update(output.items);
136180
137 var digest: [16]u8 = undefined;181 var digest: [16]u8 = undefined;
138 hash.final(&digest);182 hash.final(&digest);
...@@ -143,38 +187,42 @@ fn make(step: *Step) !void {...@@ -143,38 +187,42 @@ fn make(step: *Step) !void {
143 .{std.fmt.fmtSliceHexLower(&digest)},187 .{std.fmt.fmtSliceHexLower(&digest)},
144 ) catch unreachable;188 ) catch unreachable;
145189
146 self.output_dir = try std.fs.path.join(gpa, &[_][]const u8{190 const output_dir = try std.fs.path.join(gpa, &[_][]const u8{
147 self.builder.cache_root, "o", &hash_basename,191 self.builder.cache_root, "o", &hash_basename,
148 });192 });
149 var dir = std.fs.cwd().makeOpenPath(self.output_dir, .{}) catch |err| {193
150 std.debug.print("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) });194 // If output_path has directory parts, deal with them. Example:
195 // output_dir is zig-cache/o/HASH
196 // output_path is libavutil/avconfig.h
197 // We want to open directory zig-cache/o/HASH/libavutil/
198 // but keep output_dir as zig-cache/o/HASH for -I include
199 const sub_dir_path = if (std.fs.path.dirname(self.include_path)) |d|
200 try std.fs.path.join(gpa, &.{ output_dir, d })
201 else
202 output_dir;
203
204 var dir = std.fs.cwd().makeOpenPath(sub_dir_path, .{}) catch |err| {
205 std.debug.print("unable to make path {s}: {s}\n", .{ output_dir, @errorName(err) });
151 return err;206 return err;
152 };207 };
153 defer dir.close();208 defer dir.close();
154209
155 var values_copy = try self.values.clone();210 try dir.writeFile(std.fs.path.basename(self.include_path), output.items);
156 defer values_copy.deinit();
157211
158 var output = std.ArrayList(u8).init(gpa);212 self.output_file.path = try std.fs.path.join(self.builder.allocator, &.{
159 defer output.deinit();213 output_dir, self.include_path,
160 try output.ensureTotalCapacity(contents.len);214 });
161
162 try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n");
163
164 switch (self.style) {
165 .autoconf => try render_autoconf(contents, &output, &values_copy, src_path),
166 .cmake => try render_cmake(contents, &output, &values_copy, src_path),
167 }
168
169 try dir.writeFile(self.output_basename, output.items);
170}215}
171216
172fn render_autoconf(217fn render_autoconf(
173 contents: []const u8,218 contents: []const u8,
174 output: *std.ArrayList(u8),219 output: *std.ArrayList(u8),
175 values_copy: *std.StringHashMap(Value),220 values: std.StringArrayHashMap(Value),
176 src_path: []const u8,221 src_path: []const u8,
177) !void {222) !void {
223 var values_copy = try values.clone();
224 defer values_copy.deinit();
225
178 var any_errors = false;226 var any_errors = false;
179 var line_index: u32 = 0;227 var line_index: u32 = 0;
180 var line_it = std.mem.split(u8, contents, "\n");228 var line_it = std.mem.split(u8, contents, "\n");
...@@ -192,7 +240,7 @@ fn render_autoconf(...@@ -192,7 +240,7 @@ fn render_autoconf(
192 continue;240 continue;
193 }241 }
194 const name = it.rest();242 const name = it.rest();
195 const kv = values_copy.fetchRemove(name) orelse {243 const kv = values_copy.fetchSwapRemove(name) orelse {
196 std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{244 std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{
197 src_path, line_index + 1, name,245 src_path, line_index + 1, name,
198 });246 });
...@@ -202,12 +250,8 @@ fn render_autoconf(...@@ -202,12 +250,8 @@ fn render_autoconf(
202 try renderValue(output, name, kv.value);250 try renderValue(output, name, kv.value);
203 }251 }
204252
205 {253 for (values_copy.keys()) |name| {
206 var it = values_copy.iterator();254 std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
207 while (it.next()) |entry| {
208 const name = entry.key_ptr.*;
209 std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
210 }
211 }255 }
212256
213 if (any_errors) {257 if (any_errors) {
...@@ -218,9 +262,12 @@ fn render_autoconf(...@@ -218,9 +262,12 @@ fn render_autoconf(
218fn render_cmake(262fn render_cmake(
219 contents: []const u8,263 contents: []const u8,
220 output: *std.ArrayList(u8),264 output: *std.ArrayList(u8),
221 values_copy: *std.StringHashMap(Value),265 values: std.StringArrayHashMap(Value),
222 src_path: []const u8,266 src_path: []const u8,
223) !void {267) !void {
268 var values_copy = try values.clone();
269 defer values_copy.deinit();
270
224 var any_errors = false;271 var any_errors = false;
225 var line_index: u32 = 0;272 var line_index: u32 = 0;
226 var line_it = std.mem.split(u8, contents, "\n");273 var line_it = std.mem.split(u8, contents, "\n");
...@@ -244,7 +291,7 @@ fn render_cmake(...@@ -244,7 +291,7 @@ fn render_cmake(
244 any_errors = true;291 any_errors = true;
245 continue;292 continue;
246 };293 };
247 const kv = values_copy.fetchRemove(name) orelse {294 const kv = values_copy.fetchSwapRemove(name) orelse {
248 std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{295 std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{
249 src_path, line_index + 1, name,296 src_path, line_index + 1, name,
250 });297 });
...@@ -254,12 +301,8 @@ fn render_cmake(...@@ -254,12 +301,8 @@ fn render_cmake(
254 try renderValue(output, name, kv.value);301 try renderValue(output, name, kv.value);
255 }302 }
256303
257 {304 for (values_copy.keys()) |name| {
258 var it = values_copy.iterator();305 std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
259 while (it.next()) |entry| {
260 const name = entry.key_ptr.*;
261 std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
262 }
263 }306 }
264307
265 if (any_errors) {308 if (any_errors) {
...@@ -267,6 +310,36 @@ fn render_cmake(...@@ -267,6 +310,36 @@ fn render_cmake(
267 }310 }
268}311}
269312
313fn render_blank(
314 output: *std.ArrayList(u8),
315 defines: std.StringArrayHashMap(Value),
316 include_path: []const u8,
317) !void {
318 const include_guard_name = try output.allocator.dupe(u8, include_path);
319 for (include_guard_name) |*byte| {
320 switch (byte.*) {
321 'a'...'z' => byte.* = byte.* - 'a' + 'A',
322 'A'...'Z', '0'...'9' => continue,
323 else => byte.* = '_',
324 }
325 }
326
327 try output.appendSlice("#ifndef ");
328 try output.appendSlice(include_guard_name);
329 try output.appendSlice("\n#define ");
330 try output.appendSlice(include_guard_name);
331 try output.appendSlice("\n");
332
333 const values = defines.values();
334 for (defines.keys()) |name, i| {
335 try renderValue(output, name, values[i]);
336 }
337
338 try output.appendSlice("#endif /* ");
339 try output.appendSlice(include_guard_name);
340 try output.appendSlice(" */\n");
341}
342
270fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void {343fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void {
271 switch (value) {344 switch (value) {
272 .undef => {345 .undef => {
lib/std/Build/TranslateCStep.zig+2-9
...@@ -15,7 +15,6 @@ builder: *std.Build,...@@ -15,7 +15,6 @@ builder: *std.Build,
15source: std.Build.FileSource,15source: std.Build.FileSource,
16include_dirs: std.ArrayList([]const u8),16include_dirs: std.ArrayList([]const u8),
17c_macros: std.ArrayList([]const u8),17c_macros: std.ArrayList([]const u8),
18output_dir: ?[]const u8,
19out_basename: []const u8,18out_basename: []const u8,
20target: CrossTarget,19target: CrossTarget,
21optimize: std.builtin.OptimizeMode,20optimize: std.builtin.OptimizeMode,
...@@ -36,7 +35,6 @@ pub fn create(builder: *std.Build, options: Options) *TranslateCStep {...@@ -36,7 +35,6 @@ pub fn create(builder: *std.Build, options: Options) *TranslateCStep {
36 .source = source,35 .source = source,
37 .include_dirs = std.ArrayList([]const u8).init(builder.allocator),36 .include_dirs = std.ArrayList([]const u8).init(builder.allocator),
38 .c_macros = std.ArrayList([]const u8).init(builder.allocator),37 .c_macros = std.ArrayList([]const u8).init(builder.allocator),
39 .output_dir = null,
40 .out_basename = undefined,38 .out_basename = undefined,
41 .target = options.target,39 .target = options.target,
42 .optimize = options.optimize,40 .optimize = options.optimize,
...@@ -122,15 +120,10 @@ fn make(step: *Step) !void {...@@ -122,15 +120,10 @@ fn make(step: *Step) !void {
122 const output_path = mem.trimRight(u8, output_path_nl, "\r\n");120 const output_path = mem.trimRight(u8, output_path_nl, "\r\n");
123121
124 self.out_basename = fs.path.basename(output_path);122 self.out_basename = fs.path.basename(output_path);
125 if (self.output_dir) |output_dir| {123 const output_dir = fs.path.dirname(output_path).?;
126 const full_dest = try fs.path.join(self.builder.allocator, &[_][]const u8{ output_dir, self.out_basename });
127 try self.builder.updateFile(output_path, full_dest);
128 } else {
129 self.output_dir = fs.path.dirname(output_path).?;
130 }
131124
132 self.output_file.path = try fs.path.join(125 self.output_file.path = try fs.path.join(
133 self.builder.allocator,126 self.builder.allocator,
134 &[_][]const u8{ self.output_dir.?, self.out_basename },127 &[_][]const u8{ output_dir, self.out_basename },
135 );128 );
136}129}
lib/std/Build/WriteFileStep.zig+5-7
...@@ -9,7 +9,6 @@ pub const base_id = .write_file;...@@ -9,7 +9,6 @@ pub const base_id = .write_file;
99
10step: Step,10step: Step,
11builder: *std.Build,11builder: *std.Build,
12output_dir: []const u8,
13files: std.TailQueue(File),12files: std.TailQueue(File),
1413
15pub const File = struct {14pub const File = struct {
...@@ -23,7 +22,6 @@ pub fn init(builder: *std.Build) WriteFileStep {...@@ -23,7 +22,6 @@ pub fn init(builder: *std.Build) WriteFileStep {
23 .builder = builder,22 .builder = builder,
24 .step = Step.init(.write_file, "writefile", builder.allocator, make),23 .step = Step.init(.write_file, "writefile", builder.allocator, make),
25 .files = .{},24 .files = .{},
26 .output_dir = undefined,
27 };25 };
28}26}
2927
...@@ -87,11 +85,11 @@ fn make(step: *Step) !void {...@@ -87,11 +85,11 @@ fn make(step: *Step) !void {
87 .{std.fmt.fmtSliceHexLower(&digest)},85 .{std.fmt.fmtSliceHexLower(&digest)},
88 ) catch unreachable;86 ) catch unreachable;
8987
90 self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{88 const output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{
91 self.builder.cache_root, "o", &hash_basename,89 self.builder.cache_root, "o", &hash_basename,
92 });90 });
93 var dir = fs.cwd().makeOpenPath(self.output_dir, .{}) catch |err| {91 var dir = fs.cwd().makeOpenPath(output_dir, .{}) catch |err| {
94 std.debug.print("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) });92 std.debug.print("unable to make path {s}: {s}\n", .{ output_dir, @errorName(err) });
95 return err;93 return err;
96 };94 };
97 defer dir.close();95 defer dir.close();
...@@ -101,14 +99,14 @@ fn make(step: *Step) !void {...@@ -101,14 +99,14 @@ fn make(step: *Step) !void {
101 dir.writeFile(node.data.basename, node.data.bytes) catch |err| {99 dir.writeFile(node.data.basename, node.data.bytes) catch |err| {
102 std.debug.print("unable to write {s} into {s}: {s}\n", .{100 std.debug.print("unable to write {s} into {s}: {s}\n", .{
103 node.data.basename,101 node.data.basename,
104 self.output_dir,102 output_dir,
105 @errorName(err),103 @errorName(err),
106 });104 });
107 return err;105 return err;
108 };106 };
109 node.data.source.path = try fs.path.join(107 node.data.source.path = try fs.path.join(
110 self.builder.allocator,108 self.builder.allocator,
111 &[_][]const u8{ self.output_dir, node.data.basename },109 &[_][]const u8{ output_dir, node.data.basename },
112 );110 );
113 }111 }
114 }112 }