| ... | @@ -4,13 +4,22 @@ const Step = std.Build.Step; | ... | @@ -4,13 +4,22 @@ const Step = std.Build.Step; |
| 4 | | 4 | |
| 5 | pub const base_id: Step.Id = .config_header; | 5 | pub const base_id: Step.Id = .config_header; |
| 6 | | 6 | |
| 7 | pub const Style = enum { | 7 | pub const Style = union(enum) { |
| 8 | /// The configure format supported by autotools. It uses `#undef foo` to | 8 | /// 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@@` and | 11 | /// 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 | }; |
| 15 | | 24 | |
| 16 | pub const Value = union(enum) { | 25 | pub const Value = union(enum) { |
| ... | @@ -24,34 +33,50 @@ pub const Value = union(enum) { | ... | @@ -24,34 +33,50 @@ pub const Value = union(enum) { |
| 24 | | 33 | |
| 25 | step: Step, | 34 | step: Step, |
| 26 | builder: *std.Build, | 35 | builder: *std.Build, |
| 27 | source: std.Build.FileSource, | 36 | values: std.StringArrayHashMap(Value), |
| | 37 | output_file: std.Build.GeneratedFile, |
| | 38 | |
| 28 | style: Style, | 39 | style: Style, |
| 29 | values: std.StringHashMap(Value), | 40 | max_bytes: usize, |
| 30 | max_bytes: usize = 2 * 1024 * 1024, | 41 | include_path: []const u8, |
| 31 | output_dir: []const u8, | | |
| 32 | output_basename: []const u8, | | |
| 33 | | 42 | |
| 34 | pub fn create(builder: *std.Build, source: std.Build.FileSource, style: Style) *ConfigHeaderStep { | 43 | pub const Options = struct { |
| | 44 | style: Style = .blank, |
| | 45 | max_bytes: usize = 2 * 1024 * 1024, |
| | 46 | include_path: ?[]const u8 = null, |
| | 47 | }; |
| | 48 | |
| | 49 | pub 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 | } |
| 57 | | 82 | |
| ... | @@ -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 |
| 112 | fn make(step: *Step) !void { | 137 | fn 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); | | |
| 117 | | 140 | |
| 118 | // The cache is used here not really as a way to speed things up - because writing | 141 | // 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 canonical | 142 | // 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 new | 153 | // Random bytes to make ConfigHeaderStep unique. Refresh this with new |
| 131 | // random bytes when ConfigHeaderStep implementation is modified in a | 154 | // 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); |
| 136 | | 180 | |
| 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; |
| 145 | | 189 | |
| 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(); |
| 154 | | 209 | |
| 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(); | | |
| 157 | | 211 | |
| 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 | } |
| 171 | | 216 | |
| 172 | fn render_autoconf( | 217 | fn 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 | } |
| 204 | | 252 | |
| 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 | } |
| 212 | | 256 | |
| 213 | if (any_errors) { | 257 | if (any_errors) { |
| ... | @@ -218,9 +262,12 @@ fn render_autoconf( | ... | @@ -218,9 +262,12 @@ fn render_autoconf( |
| 218 | fn render_cmake( | 262 | fn 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 | } |
| 256 | | 303 | |
| 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 | } |
| 264 | | 307 | |
| 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 | } |
| 269 | | 312 | |
| | 313 | fn 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 | |
| 270 | fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { | 343 | fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { |
| 271 | switch (value) { | 344 | switch (value) { |
| 272 | .undef => { | 345 | .undef => { |