| ... | ... | @@ -4,15 +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, |
| 14 | | /// Generate a c header from scratch with the values passed. |
| 15 | | generated, |
| 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 | } |
| 16 | 23 | }; |
| 17 | 24 | |
| 18 | 25 | pub const Value = union(enum) { |
| ... | ... | @@ -26,91 +33,96 @@ pub const Value = union(enum) { |
| 26 | 33 | |
| 27 | 34 | step: Step, |
| 28 | 35 | builder: *std.Build, |
| 29 | | source: std.Build.FileSource, |
| 36 | values: std.StringArrayHashMap(Value), |
| 37 | output_file: std.Build.GeneratedFile, |
| 38 | |
| 30 | 39 | style: Style, |
| 31 | | values: std.StringHashMap(Value), |
| 32 | | gen_keys: std.ArrayList([]const u8), |
| 33 | | gen_values: std.ArrayList(Value), |
| 34 | | max_bytes: usize = 2 * 1024 * 1024, |
| 35 | | output_dir: []const u8, |
| 36 | | output_path: []const u8, |
| 37 | | output_gen: std.build.GeneratedFile, |
| 38 | | |
| 39 | | pub fn create(builder: *std.Build, source: std.Build.FileSource, style: Style) *ConfigHeaderStep { |
| 40 | max_bytes: usize, |
| 41 | include_path: []const u8, |
| 42 | |
| 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 { |
| 40 | 50 | const self = builder.allocator.create(ConfigHeaderStep) catch @panic("OOM"); |
| 41 | | 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)}); |
| 42 | 55 | self.* = .{ |
| 43 | 56 | .builder = builder, |
| 44 | 57 | .step = Step.init(base_id, name, builder.allocator, make), |
| 45 | | .source = source, |
| 46 | | .style = style, |
| 47 | | .values = std.StringHashMap(Value).init(builder.allocator), |
| 48 | | .gen_keys = std.ArrayList([]const u8).init(builder.allocator), |
| 49 | | .gen_values = std.ArrayList(Value).init(builder.allocator), |
| 50 | | .output_dir = undefined, |
| 51 | | .output_path = "config.h", |
| 52 | | .output_gen = std.build.GeneratedFile{ .step = &self.step }, |
| 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 }, |
| 53 | 64 | }; |
| 54 | 65 | |
| 55 | | switch (source) { |
| 66 | if (options.style.getFileSource()) |s| switch (s) { |
| 56 | 67 | .path => |p| { |
| 57 | | self.output_path = p; |
| 58 | | |
| 59 | | switch (style) { |
| 60 | | .autoconf, .cmake => { |
| 61 | | if (std.mem.endsWith(u8, p, ".h.in")) { |
| 62 | | self.output_path = p[0 .. p.len - 3]; |
| 63 | | } |
| 64 | | }, |
| 65 | | else => {}, |
| 68 | const basename = std.fs.path.basename(p); |
| 69 | if (std.mem.endsWith(u8, basename, ".h.in")) { |
| 70 | self.include_path = basename[0 .. basename.len - 3]; |
| 66 | 71 | } |
| 67 | 72 | }, |
| 68 | 73 | else => {}, |
| 74 | }; |
| 75 | |
| 76 | if (options.include_path) |include_path| { |
| 77 | self.include_path = include_path; |
| 69 | 78 | } |
| 70 | 79 | |
| 71 | 80 | return self; |
| 72 | 81 | } |
| 73 | 82 | |
| 74 | | pub fn getOutputSource(self: *ConfigHeaderStep) std.build.FileSource { |
| 75 | | return std.build.FileSource{ .generated = &self.output_gen }; |
| 76 | | } |
| 77 | | |
| 78 | 83 | pub fn addValues(self: *ConfigHeaderStep, values: anytype) void { |
| 79 | 84 | return addValuesInner(self, values) catch @panic("OOM"); |
| 80 | 85 | } |
| 81 | 86 | |
| 82 | 87 | fn addValuesInner(self: *ConfigHeaderStep, values: anytype) !void { |
| 83 | 88 | inline for (@typeInfo(@TypeOf(values)).Struct.fields) |field| { |
| 84 | | const val = try getValue(self, field.type, @field(values, field.name)); |
| 85 | | switch (self.style) { |
| 86 | | .generated => { |
| 87 | | try self.gen_keys.append(field.name); |
| 88 | | try self.gen_values.append(val); |
| 89 | | }, |
| 90 | | else => try self.values.put(field.name, val), |
| 91 | | } |
| 89 | try putValue(self, field.name, field.type, @field(values, field.name)); |
| 92 | 90 | } |
| 93 | 91 | } |
| 94 | 92 | |
| 95 | | fn getValue(self: *ConfigHeaderStep, comptime T: type, v: T) !Value { |
| 93 | fn putValue(self: *ConfigHeaderStep, field_name: []const u8, comptime T: type, v: T) !void { |
| 96 | 94 | switch (@typeInfo(T)) { |
| 97 | | .Null => return .undef, |
| 98 | | .Void => return .defined, |
| 99 | | .Bool => return .{ .boolean = v }, |
| 100 | | .Int, .ComptimeInt => return .{ .int = v }, |
| 101 | | .EnumLiteral => return .{ .ident = @tagName(v) }, |
| 95 | .Null => { |
| 96 | try self.values.put(field_name, .undef); |
| 97 | }, |
| 98 | .Void => { |
| 99 | try self.values.put(field_name, .defined); |
| 100 | }, |
| 101 | .Bool => { |
| 102 | try self.values.put(field_name, .{ .boolean = v }); |
| 103 | }, |
| 104 | .Int => { |
| 105 | try self.values.put(field_name, .{ .int = v }); |
| 106 | }, |
| 107 | .ComptimeInt => { |
| 108 | try self.values.put(field_name, .{ .int = v }); |
| 109 | }, |
| 110 | .EnumLiteral => { |
| 111 | try self.values.put(field_name, .{ .ident = @tagName(v) }); |
| 112 | }, |
| 102 | 113 | .Optional => { |
| 103 | 114 | if (v) |x| { |
| 104 | | return getValue(self, @TypeOf(x), x); |
| 115 | return putValue(self, field_name, @TypeOf(x), x); |
| 105 | 116 | } else { |
| 106 | | return .undef; |
| 117 | try self.values.put(field_name, .undef); |
| 107 | 118 | } |
| 108 | 119 | }, |
| 109 | 120 | .Pointer => |ptr| { |
| 110 | 121 | switch (@typeInfo(ptr.child)) { |
| 111 | 122 | .Array => |array| { |
| 112 | 123 | if (ptr.size == .One and array.child == u8) { |
| 113 | | return .{ .string = v }; |
| 124 | try self.values.put(field_name, .{ .string = v }); |
| 125 | return; |
| 114 | 126 | } |
| 115 | 127 | }, |
| 116 | 128 | else => {}, |
| ... | ... | @@ -125,11 +137,6 @@ fn getValue(self: *ConfigHeaderStep, comptime T: type, v: T) !Value { |
| 125 | 137 | fn make(step: *Step) !void { |
| 126 | 138 | const self = @fieldParentPtr(ConfigHeaderStep, "step", step); |
| 127 | 139 | const gpa = self.builder.allocator; |
| 128 | | const src_path = self.source.getPath(self.builder); |
| 129 | | const contents = switch (self.style) { |
| 130 | | .generated => src_path, |
| 131 | | else => try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes), |
| 132 | | }; |
| 133 | 140 | |
| 134 | 141 | // The cache is used here not really as a way to speed things up - because writing |
| 135 | 142 | // the data to a file would probably be very fast - but as a way to find a canonical |
| ... | ... | @@ -146,9 +153,30 @@ fn make(step: *Step) !void { |
| 146 | 153 | // Random bytes to make ConfigHeaderStep unique. Refresh this with new |
| 147 | 154 | // random bytes when ConfigHeaderStep implementation is modified in a |
| 148 | 155 | // non-backwards-compatible way. |
| 149 | | var hash = Hasher.init("X1pQzdDt91Zlh7Eh"); |
| 150 | | hash.update(self.source.getDisplayName()); |
| 151 | | 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); |
| 152 | 180 | |
| 153 | 181 | var digest: [16]u8 = undefined; |
| 154 | 182 | hash.final(&digest); |
| ... | ... | @@ -159,7 +187,7 @@ fn make(step: *Step) !void { |
| 159 | 187 | .{std.fmt.fmtSliceHexLower(&digest)}, |
| 160 | 188 | ) catch unreachable; |
| 161 | 189 | |
| 162 | | self.output_dir = try std.fs.path.join(gpa, &[_][]const u8{ |
| 190 | const output_dir = try std.fs.path.join(gpa, &[_][]const u8{ |
| 163 | 191 | self.builder.cache_root, "o", &hash_basename, |
| 164 | 192 | }); |
| 165 | 193 | |
| ... | ... | @@ -168,45 +196,33 @@ fn make(step: *Step) !void { |
| 168 | 196 | // output_path is libavutil/avconfig.h |
| 169 | 197 | // We want to open directory zig-cache/o/HASH/libavutil/ |
| 170 | 198 | // but keep output_dir as zig-cache/o/HASH for -I include |
| 171 | | var outdir = self.output_dir; |
| 172 | | var outpath = self.output_path; |
| 173 | | if (std.fs.path.dirname(self.output_path)) |d| { |
| 174 | | outdir = try std.fs.path.join(gpa, &[_][]const u8{ self.output_dir, d }); |
| 175 | | outpath = std.fs.path.basename(self.output_path); |
| 176 | | } |
| 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; |
| 177 | 203 | |
| 178 | | var dir = std.fs.cwd().makeOpenPath(outdir, .{}) catch |err| { |
| 179 | | std.debug.print("unable to make path {s}: {s}\n", .{ outdir, @errorName(err) }); |
| 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) }); |
| 180 | 206 | return err; |
| 181 | 207 | }; |
| 182 | 208 | defer dir.close(); |
| 183 | 209 | |
| 184 | | var values_copy = try self.values.clone(); |
| 185 | | defer values_copy.deinit(); |
| 186 | | |
| 187 | | var output = std.ArrayList(u8).init(gpa); |
| 188 | | defer output.deinit(); |
| 189 | | try output.ensureTotalCapacity(contents.len); |
| 190 | | |
| 191 | | try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n"); |
| 192 | | |
| 193 | | switch (self.style) { |
| 194 | | .autoconf => try render_autoconf(contents, &output, &values_copy, src_path), |
| 195 | | .cmake => try render_cmake(contents, &output, &values_copy, src_path), |
| 196 | | .generated => try render_generated(gpa, &output, &self.gen_keys, &self.gen_values, self.source.getDisplayName()), |
| 197 | | } |
| 210 | try dir.writeFile(std.fs.path.basename(self.include_path), output.items); |
| 198 | 211 | |
| 199 | | try dir.writeFile(outpath, output.items); |
| 200 | | |
| 201 | | self.output_gen.path = try std.fs.path.join(gpa, &[_][]const u8{ self.output_dir, self.output_path }); |
| 212 | self.output_file.path = try std.fs.path.join(self.builder.allocator, &.{ |
| 213 | output_dir, self.include_path, |
| 214 | }); |
| 202 | 215 | } |
| 203 | 216 | |
| 204 | 217 | fn render_autoconf( |
| 205 | 218 | contents: []const u8, |
| 206 | 219 | output: *std.ArrayList(u8), |
| 207 | | values_copy: *std.StringHashMap(Value), |
| 220 | values: std.StringArrayHashMap(Value), |
| 208 | 221 | src_path: []const u8, |
| 209 | 222 | ) !void { |
| 223 | var values_copy = try values.clone(); |
| 224 | defer values_copy.deinit(); |
| 225 | |
| 210 | 226 | var any_errors = false; |
| 211 | 227 | var line_index: u32 = 0; |
| 212 | 228 | var line_it = std.mem.split(u8, contents, "\n"); |
| ... | ... | @@ -224,7 +240,7 @@ fn render_autoconf( |
| 224 | 240 | continue; |
| 225 | 241 | } |
| 226 | 242 | const name = it.rest(); |
| 227 | | const kv = values_copy.fetchRemove(name) orelse { |
| 243 | const kv = values_copy.fetchSwapRemove(name) orelse { |
| 228 | 244 | std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{ |
| 229 | 245 | src_path, line_index + 1, name, |
| 230 | 246 | }); |
| ... | ... | @@ -234,12 +250,8 @@ fn render_autoconf( |
| 234 | 250 | try renderValue(output, name, kv.value); |
| 235 | 251 | } |
| 236 | 252 | |
| 237 | | { |
| 238 | | var it = values_copy.iterator(); |
| 239 | | while (it.next()) |entry| { |
| 240 | | const name = entry.key_ptr.*; |
| 241 | | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); |
| 242 | | } |
| 253 | for (values_copy.keys()) |name| { |
| 254 | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); |
| 243 | 255 | } |
| 244 | 256 | |
| 245 | 257 | if (any_errors) { |
| ... | ... | @@ -250,9 +262,12 @@ fn render_autoconf( |
| 250 | 262 | fn render_cmake( |
| 251 | 263 | contents: []const u8, |
| 252 | 264 | output: *std.ArrayList(u8), |
| 253 | | values_copy: *std.StringHashMap(Value), |
| 265 | values: std.StringArrayHashMap(Value), |
| 254 | 266 | src_path: []const u8, |
| 255 | 267 | ) !void { |
| 268 | var values_copy = try values.clone(); |
| 269 | defer values_copy.deinit(); |
| 270 | |
| 256 | 271 | var any_errors = false; |
| 257 | 272 | var line_index: u32 = 0; |
| 258 | 273 | var line_it = std.mem.split(u8, contents, "\n"); |
| ... | ... | @@ -276,7 +291,7 @@ fn render_cmake( |
| 276 | 291 | any_errors = true; |
| 277 | 292 | continue; |
| 278 | 293 | }; |
| 279 | | const kv = values_copy.fetchRemove(name) orelse { |
| 294 | const kv = values_copy.fetchSwapRemove(name) orelse { |
| 280 | 295 | std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{ |
| 281 | 296 | src_path, line_index + 1, name, |
| 282 | 297 | }); |
| ... | ... | @@ -286,12 +301,8 @@ fn render_cmake( |
| 286 | 301 | try renderValue(output, name, kv.value); |
| 287 | 302 | } |
| 288 | 303 | |
| 289 | | { |
| 290 | | var it = values_copy.iterator(); |
| 291 | | while (it.next()) |entry| { |
| 292 | | const name = entry.key_ptr.*; |
| 293 | | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); |
| 294 | | } |
| 304 | for (values_copy.keys()) |name| { |
| 305 | std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name }); |
| 295 | 306 | } |
| 296 | 307 | |
| 297 | 308 | if (any_errors) { |
| ... | ... | @@ -299,6 +310,36 @@ fn render_cmake( |
| 299 | 310 | } |
| 300 | 311 | } |
| 301 | 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 | |
| 302 | 343 | fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void { |
| 303 | 344 | switch (value) { |
| 304 | 345 | .undef => { |
| ... | ... | @@ -329,31 +370,3 @@ fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void |
| 329 | 370 | }, |
| 330 | 371 | } |
| 331 | 372 | } |
| 332 | | |
| 333 | | fn render_generated( |
| 334 | | gpa: std.mem.Allocator, |
| 335 | | output: *std.ArrayList(u8), |
| 336 | | keys: *std.ArrayList([]const u8), |
| 337 | | values: *std.ArrayList(Value), |
| 338 | | src_path: []const u8, |
| 339 | | ) !void { |
| 340 | | var include_guard = try gpa.dupe(u8, src_path); |
| 341 | | defer gpa.free(include_guard); |
| 342 | | |
| 343 | | for (include_guard) |*ch| { |
| 344 | | if (ch.* == '.' or std.fs.path.isSep(ch.*)) { |
| 345 | | ch.* = '_'; |
| 346 | | } else { |
| 347 | | ch.* = std.ascii.toUpper(ch.*); |
| 348 | | } |
| 349 | | } |
| 350 | | |
| 351 | | try output.writer().print("#ifndef {s}\n", .{include_guard}); |
| 352 | | try output.writer().print("#define {s}\n", .{include_guard}); |
| 353 | | |
| 354 | | for (keys.items) |k, i| { |
| 355 | | try renderValue(output, k, values.items[i]); |
| 356 | | } |
| 357 | | |
| 358 | | try output.writer().print("#endif /* {s} */\n", .{include_guard}); |
| 359 | | } |