| ... | ... | @@ -4,13 +4,22 @@ const Step = std.Build.Step; |
| 4 | 4 | |
| 5 | 5 | pub const base_id: Step.Id = .config_header; |
| 6 | 6 | |
| 7 | | pub const Style = enum { |
| 7 | pub const Style = union(enum) { |
| 8 | 8 | /// The configure format supported by autotools. It uses `#undef foo` to |
| 9 | 9 | /// mark lines that can be substituted with different values. |
| 10 | | autoconf, |
| 10 | autoconf: std.Build.FileSource, |
| 11 | 11 | /// The configure format supported by CMake. It uses `@@FOO@@` and |
| 12 | 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 | 25 | pub const Value = union(enum) { |
| ... | ... | @@ -24,34 +33,50 @@ pub const Value = union(enum) { |
| 24 | 33 | |
| 25 | 34 | step: Step, |
| 26 | 35 | builder: *std.Build, |
| 27 | | source: std.Build.FileSource, |
| 36 | values: std.StringArrayHashMap(Value), |
| 37 | output_file: std.Build.GeneratedFile, |
| 38 | |
| 28 | 39 | style: Style, |
| 29 | | values: std.StringHashMap(Value), |
| 30 | | max_bytes: usize = 2 * 1024 * 1024, |
| 31 | | output_dir: []const u8, |
| 32 | | output_basename: []const u8, |
| 40 | max_bytes: usize, |
| 41 | include_path: []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 | 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 | 55 | self.* = .{ |
| 38 | 56 | .builder = builder, |
| 39 | 57 | .step = Step.init(base_id, name, builder.allocator, make), |
| 40 | | .source = source, |
| 41 | | .style = style, |
| 42 | | .values = std.StringHashMap(Value).init(builder.allocator), |
| 43 | | .output_dir = undefined, |
| 44 | | .output_basename = "config.h", |
| 58 | .style = options.style, |
| 59 | .values = std.StringArrayHashMap(Value).init(builder.allocator), |
| 60 | |
| 61 | .max_bytes = options.max_bytes, |
| 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 | 67 | .path => |p| { |
| 48 | 68 | const basename = std.fs.path.basename(p); |
| 49 | 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 | 73 | else => {}, |
| 74 | }; |
| 75 | |
| 76 | if (options.include_path) |include_path| { |
| 77 | self.include_path = include_path; |
| 54 | 78 | } |
| 79 | |
| 55 | 80 | return self; |
| 56 | 81 | } |
| 57 | 82 | |
| ... | ... | @@ -112,8 +137,6 @@ fn putValue(self: *ConfigHeaderStep, field_name: []const u8, comptime T: type, v |
| 112 | 137 | fn make(step: *Step) !void { |
| 113 | 138 | const self = @fieldParentPtr(ConfigHeaderStep, "step", step); |
| 114 | 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 | 141 | // The cache is used here not really as a way to speed things up - because writing |
| 119 | 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 | 153 | // Random bytes to make ConfigHeaderStep unique. Refresh this with new |
| 131 | 154 | // random bytes when ConfigHeaderStep implementation is modified in a |
| 132 | 155 | // non-backwards-compatible way. |
| 133 | | var hash = Hasher.init("X1pQzdDt91Zlh7Eh"); |
| 134 | | hash.update(self.source.getDisplayName()); |
| 135 | | hash.update(contents); |
| 156 | var hash = Hasher.init("PGuDTpidxyMqnkGM"); |
| 157 | |
| 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 | 181 | var digest: [16]u8 = undefined; |
| 138 | 182 | hash.final(&digest); |
| ... | ... | @@ -143,38 +187,42 @@ fn make(step: *Step) !void { |
| 143 | 187 | .{std.fmt.fmtSliceHexLower(&digest)}, |
| 144 | 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 | 191 | self.builder.cache_root, "o", &hash_basename, |
| 148 | 192 | }); |
| 149 | | var dir = std.fs.cwd().makeOpenPath(self.output_dir, .{}) catch |err| { |
| 150 | | std.debug.print("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) }); |
| 193 | |
| 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 | 206 | return err; |
| 152 | 207 | }; |
| 153 | 208 | defer dir.close(); |
| 154 | 209 | |
| 155 | | var values_copy = try self.values.clone(); |
| 156 | | defer values_copy.deinit(); |
| 210 | try dir.writeFile(std.fs.path.basename(self.include_path), output.items); |
| 157 | 211 | |
| 158 | | var output = std.ArrayList(u8).init(gpa); |
| 159 | | defer output.deinit(); |
| 160 | | try output.ensureTotalCapacity(contents.len); |
| 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); |
| 212 | self.output_file.path = try std.fs.path.join(self.builder.allocator, &.{ |
| 213 | output_dir, self.include_path, |
| 214 | }); |
| 170 | 215 | } |
| 171 | 216 | |
| 172 | 217 | fn render_autoconf( |
| 173 | 218 | contents: []const u8, |
| 174 | 219 | output: *std.ArrayList(u8), |
| 175 | | values_copy: *std.StringHashMap(Value), |
| 220 | values: std.StringArrayHashMap(Value), |
| 176 | 221 | src_path: []const u8, |
| 177 | 222 | ) !void { |
| 223 | var values_copy = try values.clone(); |
| 224 | defer values_copy.deinit(); |
| 225 | |
| 178 | 226 | var any_errors = false; |
| 179 | 227 | var line_index: u32 = 0; |
| 180 | 228 | var line_it = std.mem.split(u8, contents, "\n"); |
| ... | ... | @@ -192,7 +240,7 @@ fn render_autoconf( |
| 192 | 240 | continue; |
| 193 | 241 | } |
| 194 | 242 | const name = it.rest(); |
| 195 | | const kv = values_copy.fetchRemove(name) orelse { |
| 243 | const kv = values_copy.fetchSwapRemove(name) orelse { |
| 196 | 244 | std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{ |
| 197 | 245 | src_path, line_index + 1, name, |
| 198 | 246 | }); |
| ... | ... | @@ -202,12 +250,8 @@ fn render_autoconf( |
| 202 | 250 | try renderValue(output, name, kv.value); |
| 203 | 251 | } |
| 204 | 252 | |
| 205 | | { |
| 206 | | var it = values_copy.iterator(); |
| 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 | | } |
| 253 | for (values_copy.keys()) |name| { |
| 254 | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); |
| 211 | 255 | } |
| 212 | 256 | |
| 213 | 257 | if (any_errors) { |
| ... | ... | @@ -218,9 +262,12 @@ fn render_autoconf( |
| 218 | 262 | fn render_cmake( |
| 219 | 263 | contents: []const u8, |
| 220 | 264 | output: *std.ArrayList(u8), |
| 221 | | values_copy: *std.StringHashMap(Value), |
| 265 | values: std.StringArrayHashMap(Value), |
| 222 | 266 | src_path: []const u8, |
| 223 | 267 | ) !void { |
| 268 | var values_copy = try values.clone(); |
| 269 | defer values_copy.deinit(); |
| 270 | |
| 224 | 271 | var any_errors = false; |
| 225 | 272 | var line_index: u32 = 0; |
| 226 | 273 | var line_it = std.mem.split(u8, contents, "\n"); |
| ... | ... | @@ -244,7 +291,7 @@ fn render_cmake( |
| 244 | 291 | any_errors = true; |
| 245 | 292 | continue; |
| 246 | 293 | }; |
| 247 | | const kv = values_copy.fetchRemove(name) orelse { |
| 294 | const kv = values_copy.fetchSwapRemove(name) orelse { |
| 248 | 295 | std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{ |
| 249 | 296 | src_path, line_index + 1, name, |
| 250 | 297 | }); |
| ... | ... | @@ -254,12 +301,8 @@ fn render_cmake( |
| 254 | 301 | try renderValue(output, name, kv.value); |
| 255 | 302 | } |
| 256 | 303 | |
| 257 | | { |
| 258 | | var it = values_copy.iterator(); |
| 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 | | } |
| 304 | for (values_copy.keys()) |name| { |
| 305 | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); |
| 263 | 306 | } |
| 264 | 307 | |
| 265 | 308 | if (any_errors) { |
| ... | ... | @@ -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 | 343 | fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { |
| 271 | 344 | switch (value) { |
| 272 | 345 | .undef => { |