aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Build/ConfigHeaderStep.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/Build/ConfigHeaderStep.zig')
-rw-r--r--lib/std/Build/ConfigHeaderStep.zig249
1 files changed, 189 insertions, 60 deletions
diff --git a/lib/std/Build/ConfigHeaderStep.zig b/lib/std/Build/ConfigHeaderStep.zig
index 58a78b939d..f8d6f7bd57 100644
--- a/lib/std/Build/ConfigHeaderStep.zig
+++ b/lib/std/Build/ConfigHeaderStep.zig
@@ -4,13 +4,24 @@ const Step = std.Build.Step;
pub const base_id: Step.Id = .config_header;
-pub const Style = enum {
+pub const Style = union(enum) {
/// The configure format supported by autotools. It uses `#undef foo` to
/// mark lines that can be substituted with different values.
- autoconf,
+ autoconf: std.Build.FileSource,
/// The configure format supported by CMake. It uses `@@FOO@@` and
/// `#cmakedefine` for template substitution.
- cmake,
+ cmake: std.Build.FileSource,
+ /// Instead of starting with an input file, start with nothing.
+ blank,
+ /// Start with nothing, like blank, and output a nasm .asm file.
+ nasm,
+
+ pub fn getFileSource(style: Style) ?std.Build.FileSource {
+ switch (style) {
+ .autoconf, .cmake => |s| return s,
+ .blank, .nasm => return null,
+ }
+ }
};
pub const Value = union(enum) {
@@ -24,34 +35,50 @@ pub const Value = union(enum) {
step: Step,
builder: *std.Build,
-source: std.Build.FileSource,
+values: std.StringArrayHashMap(Value),
+output_file: std.Build.GeneratedFile,
+
style: Style,
-values: std.StringHashMap(Value),
-max_bytes: usize = 2 * 1024 * 1024,
-output_dir: []const u8,
-output_basename: []const u8,
+max_bytes: usize,
+include_path: []const u8,
+
+pub const Options = struct {
+ style: Style = .blank,
+ max_bytes: usize = 2 * 1024 * 1024,
+ include_path: ?[]const u8 = null,
+};
-pub fn create(builder: *std.Build, source: std.Build.FileSource, style: Style) *ConfigHeaderStep {
+pub fn create(builder: *std.Build, options: Options) *ConfigHeaderStep {
const self = builder.allocator.create(ConfigHeaderStep) catch @panic("OOM");
- const name = builder.fmt("configure header {s}", .{source.getDisplayName()});
+ const name = if (options.style.getFileSource()) |s|
+ builder.fmt("configure {s} header {s}", .{ @tagName(options.style), s.getDisplayName() })
+ else
+ builder.fmt("configure {s} header", .{@tagName(options.style)});
self.* = .{
.builder = builder,
.step = Step.init(base_id, name, builder.allocator, make),
- .source = source,
- .style = style,
- .values = std.StringHashMap(Value).init(builder.allocator),
- .output_dir = undefined,
- .output_basename = "config.h",
+ .style = options.style,
+ .values = std.StringArrayHashMap(Value).init(builder.allocator),
+
+ .max_bytes = options.max_bytes,
+ .include_path = "config.h",
+ .output_file = .{ .step = &self.step },
};
- switch (source) {
+
+ if (options.style.getFileSource()) |s| switch (s) {
.path => |p| {
const basename = std.fs.path.basename(p);
if (std.mem.endsWith(u8, basename, ".h.in")) {
- self.output_basename = basename[0 .. basename.len - 3];
+ self.include_path = basename[0 .. basename.len - 3];
}
},
else => {},
+ };
+
+ if (options.include_path) |include_path| {
+ self.include_path = include_path;
}
+
return self;
}
@@ -59,6 +86,10 @@ pub fn addValues(self: *ConfigHeaderStep, values: anytype) void {
return addValuesInner(self, values) catch @panic("OOM");
}
+pub fn getFileSource(self: *ConfigHeaderStep) std.Build.FileSource {
+ return .{ .generated = &self.output_file };
+}
+
fn addValuesInner(self: *ConfigHeaderStep, values: anytype) !void {
inline for (@typeInfo(@TypeOf(values)).Struct.fields) |field| {
try putValue(self, field.name, field.type, @field(values, field.name));
@@ -100,6 +131,12 @@ fn putValue(self: *ConfigHeaderStep, field_name: []const u8, comptime T: type, v
return;
}
},
+ .Int => {
+ if (ptr.size == .Slice and ptr.child == u8) {
+ try self.values.put(field_name, .{ .string = v });
+ return;
+ }
+ },
else => {},
}
@@ -112,8 +149,6 @@ fn putValue(self: *ConfigHeaderStep, field_name: []const u8, comptime T: type, v
fn make(step: *Step) !void {
const self = @fieldParentPtr(ConfigHeaderStep, "step", step);
const gpa = self.builder.allocator;
- const src_path = self.source.getPath(self.builder);
- const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes);
// The cache is used here not really as a way to speed things up - because writing
// the data to a file would probably be very fast - but as a way to find a canonical
@@ -130,9 +165,39 @@ fn make(step: *Step) !void {
// Random bytes to make ConfigHeaderStep unique. Refresh this with new
// random bytes when ConfigHeaderStep implementation is modified in a
// non-backwards-compatible way.
- var hash = Hasher.init("X1pQzdDt91Zlh7Eh");
- hash.update(self.source.getDisplayName());
- hash.update(contents);
+ var hash = Hasher.init("PGuDTpidxyMqnkGM");
+
+ var output = std.ArrayList(u8).init(gpa);
+ defer output.deinit();
+
+ const header_text = "This file was generated by ConfigHeaderStep using the Zig Build System.";
+ const c_generated_line = "/* " ++ header_text ++ " */\n";
+ const asm_generated_line = "; " ++ header_text ++ "\n";
+
+ switch (self.style) {
+ .autoconf => |file_source| {
+ try output.appendSlice(c_generated_line);
+ const src_path = file_source.getPath(self.builder);
+ const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes);
+ try render_autoconf(contents, &output, self.values, src_path);
+ },
+ .cmake => |file_source| {
+ try output.appendSlice(c_generated_line);
+ const src_path = file_source.getPath(self.builder);
+ const contents = try std.fs.cwd().readFileAlloc(gpa, src_path, self.max_bytes);
+ try render_cmake(contents, &output, self.values, src_path);
+ },
+ .blank => {
+ try output.appendSlice(c_generated_line);
+ try render_blank(&output, self.values, self.include_path);
+ },
+ .nasm => {
+ try output.appendSlice(asm_generated_line);
+ try render_nasm(&output, self.values);
+ },
+ }
+
+ hash.update(output.items);
var digest: [16]u8 = undefined;
hash.final(&digest);
@@ -143,38 +208,40 @@ fn make(step: *Step) !void {
.{std.fmt.fmtSliceHexLower(&digest)},
) catch unreachable;
- self.output_dir = try std.fs.path.join(gpa, &[_][]const u8{
- self.builder.cache_root, "o", &hash_basename,
- });
- var dir = std.fs.cwd().makeOpenPath(self.output_dir, .{}) catch |err| {
- std.debug.print("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) });
+ const output_dir = try self.builder.cache_root.join(gpa, &.{ "o", &hash_basename });
+
+ // If output_path has directory parts, deal with them. Example:
+ // output_dir is zig-cache/o/HASH
+ // output_path is libavutil/avconfig.h
+ // We want to open directory zig-cache/o/HASH/libavutil/
+ // but keep output_dir as zig-cache/o/HASH for -I include
+ const sub_dir_path = if (std.fs.path.dirname(self.include_path)) |d|
+ try std.fs.path.join(gpa, &.{ output_dir, d })
+ else
+ output_dir;
+
+ var dir = std.fs.cwd().makeOpenPath(sub_dir_path, .{}) catch |err| {
+ std.debug.print("unable to make path {s}: {s}\n", .{ output_dir, @errorName(err) });
return err;
};
defer dir.close();
- var values_copy = try self.values.clone();
- defer values_copy.deinit();
-
- var output = std.ArrayList(u8).init(gpa);
- defer output.deinit();
- try output.ensureTotalCapacity(contents.len);
-
- try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n");
+ try dir.writeFile(std.fs.path.basename(self.include_path), output.items);
- switch (self.style) {
- .autoconf => try render_autoconf(contents, &output, &values_copy, src_path),
- .cmake => try render_cmake(contents, &output, &values_copy, src_path),
- }
-
- try dir.writeFile(self.output_basename, output.items);
+ self.output_file.path = try std.fs.path.join(self.builder.allocator, &.{
+ output_dir, self.include_path,
+ });
}
fn render_autoconf(
contents: []const u8,
output: *std.ArrayList(u8),
- values_copy: *std.StringHashMap(Value),
+ values: std.StringArrayHashMap(Value),
src_path: []const u8,
) !void {
+ var values_copy = try values.clone();
+ defer values_copy.deinit();
+
var any_errors = false;
var line_index: u32 = 0;
var line_it = std.mem.split(u8, contents, "\n");
@@ -192,22 +259,18 @@ fn render_autoconf(
continue;
}
const name = it.rest();
- const kv = values_copy.fetchRemove(name) orelse {
+ const kv = values_copy.fetchSwapRemove(name) orelse {
std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{
src_path, line_index + 1, name,
});
any_errors = true;
continue;
};
- try renderValue(output, name, kv.value);
+ try renderValueC(output, name, kv.value);
}
- {
- var it = values_copy.iterator();
- while (it.next()) |entry| {
- const name = entry.key_ptr.*;
- std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
- }
+ for (values_copy.keys()) |name| {
+ std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
}
if (any_errors) {
@@ -218,9 +281,12 @@ fn render_autoconf(
fn render_cmake(
contents: []const u8,
output: *std.ArrayList(u8),
- values_copy: *std.StringHashMap(Value),
+ values: std.StringArrayHashMap(Value),
src_path: []const u8,
) !void {
+ var values_copy = try values.clone();
+ defer values_copy.deinit();
+
var any_errors = false;
var line_index: u32 = 0;
var line_it = std.mem.split(u8, contents, "\n");
@@ -244,22 +310,18 @@ fn render_cmake(
any_errors = true;
continue;
};
- const kv = values_copy.fetchRemove(name) orelse {
+ const kv = values_copy.fetchSwapRemove(name) orelse {
std.debug.print("{s}:{d}: error: unspecified config header value: '{s}'\n", .{
src_path, line_index + 1, name,
});
any_errors = true;
continue;
};
- try renderValue(output, name, kv.value);
+ try renderValueC(output, name, kv.value);
}
- {
- var it = values_copy.iterator();
- while (it.next()) |entry| {
- const name = entry.key_ptr.*;
- std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
- }
+ for (values_copy.keys()) |name| {
+ std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
}
if (any_errors) {
@@ -267,7 +329,44 @@ fn render_cmake(
}
}
-fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void {
+fn render_blank(
+ output: *std.ArrayList(u8),
+ defines: std.StringArrayHashMap(Value),
+ include_path: []const u8,
+) !void {
+ const include_guard_name = try output.allocator.dupe(u8, include_path);
+ for (include_guard_name) |*byte| {
+ switch (byte.*) {
+ 'a'...'z' => byte.* = byte.* - 'a' + 'A',
+ 'A'...'Z', '0'...'9' => continue,
+ else => byte.* = '_',
+ }
+ }
+
+ try output.appendSlice("#ifndef ");
+ try output.appendSlice(include_guard_name);
+ try output.appendSlice("\n#define ");
+ try output.appendSlice(include_guard_name);
+ try output.appendSlice("\n");
+
+ const values = defines.values();
+ for (defines.keys()) |name, i| {
+ try renderValueC(output, name, values[i]);
+ }
+
+ try output.appendSlice("#endif /* ");
+ try output.appendSlice(include_guard_name);
+ try output.appendSlice(" */\n");
+}
+
+fn render_nasm(output: *std.ArrayList(u8), defines: std.StringArrayHashMap(Value)) !void {
+ const values = defines.values();
+ for (defines.keys()) |name, i| {
+ try renderValueNasm(output, name, values[i]);
+ }
+}
+
+fn renderValueC(output: *std.ArrayList(u8), name: []const u8, value: Value) !void {
switch (value) {
.undef => {
try output.appendSlice("/* #undef ");
@@ -297,3 +396,33 @@ fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void
},
}
}
+
+fn renderValueNasm(output: *std.ArrayList(u8), name: []const u8, value: Value) !void {
+ switch (value) {
+ .undef => {
+ try output.appendSlice("; %undef ");
+ try output.appendSlice(name);
+ try output.appendSlice("\n");
+ },
+ .defined => {
+ try output.appendSlice("%define ");
+ try output.appendSlice(name);
+ try output.appendSlice("\n");
+ },
+ .boolean => |b| {
+ try output.appendSlice("%define ");
+ try output.appendSlice(name);
+ try output.appendSlice(if (b) " 1\n" else " 0\n");
+ },
+ .int => |i| {
+ try output.writer().print("%define {s} {d}\n", .{ name, i });
+ },
+ .ident => |ident| {
+ try output.writer().print("%define {s} {s}\n", .{ name, ident });
+ },
+ .string => |string| {
+ // TODO: use nasm-specific escaping instead of zig string literals
+ try output.writer().print("%define {s} \"{}\"\n", .{ name, std.zig.fmtEscapes(string) });
+ },
+ }
+}