mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
Merge pull request #14562: std.Build: enhancements to ConfigHeaderStep
This commit is contained in:
commit
a5b34a61ab
@ -598,13 +598,17 @@ pub fn addSystemCommand(self: *Build, argv: []const []const u8) *RunStep {
|
||||
return run_step;
|
||||
}
|
||||
|
||||
/// Using the `values` provided, produces a C header file, possibly based on a
|
||||
/// template input file (e.g. config.h.in).
|
||||
/// When an input template file is provided, this function will fail the build
|
||||
/// when an option not found in the input file is provided in `values`, and
|
||||
/// when an option found in the input file is missing from `values`.
|
||||
pub fn addConfigHeader(
|
||||
b: *Build,
|
||||
source: FileSource,
|
||||
style: ConfigHeaderStep.Style,
|
||||
options: ConfigHeaderStep.Options,
|
||||
values: anytype,
|
||||
) *ConfigHeaderStep {
|
||||
const config_header_step = ConfigHeaderStep.create(b, source, style);
|
||||
const config_header_step = ConfigHeaderStep.create(b, options);
|
||||
config_header_step.addValues(values);
|
||||
return config_header_step;
|
||||
}
|
||||
|
||||
@ -442,6 +442,26 @@ pub fn installHeader(a: *CompileStep, src_path: []const u8, dest_rel_path: []con
|
||||
a.installed_headers.append(&install_file.step) catch @panic("OOM");
|
||||
}
|
||||
|
||||
pub const InstallConfigHeaderOptions = struct {
|
||||
install_dir: InstallDir = .header,
|
||||
dest_rel_path: ?[]const u8 = null,
|
||||
};
|
||||
|
||||
pub fn installConfigHeader(
|
||||
cs: *CompileStep,
|
||||
config_header: *ConfigHeaderStep,
|
||||
options: InstallConfigHeaderOptions,
|
||||
) void {
|
||||
const dest_rel_path = options.dest_rel_path orelse config_header.include_path;
|
||||
const install_file = cs.builder.addInstallFileWithDir(
|
||||
.{ .generated = &config_header.output_file },
|
||||
options.install_dir,
|
||||
dest_rel_path,
|
||||
);
|
||||
cs.builder.getInstallStep().dependOn(&install_file.step);
|
||||
cs.installed_headers.append(&install_file.step) catch @panic("OOM");
|
||||
}
|
||||
|
||||
pub fn installHeadersDirectory(
|
||||
a: *CompileStep,
|
||||
src_dir_path: []const u8,
|
||||
@ -1622,8 +1642,9 @@ fn make(step: *Step) !void {
|
||||
}
|
||||
},
|
||||
.config_header_step => |config_header| {
|
||||
try zig_args.append("-I");
|
||||
try zig_args.append(config_header.output_dir);
|
||||
const full_file_path = config_header.output_file.path.?;
|
||||
const header_dir_path = full_file_path[0 .. full_file_path.len - config_header.include_path.len];
|
||||
try zig_args.appendSlice(&.{ "-I", header_dir_path });
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,13 +4,22 @@ 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,
|
||||
|
||||
pub fn getFileSource(style: Style) ?std.Build.FileSource {
|
||||
switch (style) {
|
||||
.autoconf, .cmake => |s| return s,
|
||||
.blank => return null,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub const Value = union(enum) {
|
||||
@ -24,34 +33,50 @@ pub const Value = union(enum) {
|
||||
|
||||
step: Step,
|
||||
builder: *std.Build,
|
||||
source: std.Build.FileSource,
|
||||
style: Style,
|
||||
values: std.StringHashMap(Value),
|
||||
max_bytes: usize = 2 * 1024 * 1024,
|
||||
output_dir: []const u8,
|
||||
output_basename: []const u8,
|
||||
values: std.StringArrayHashMap(Value),
|
||||
output_file: std.Build.GeneratedFile,
|
||||
|
||||
pub fn create(builder: *std.Build, source: std.Build.FileSource, style: Style) *ConfigHeaderStep {
|
||||
style: Style,
|
||||
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, 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;
|
||||
}
|
||||
|
||||
@ -112,8 +137,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 +153,30 @@ 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();
|
||||
|
||||
try output.appendSlice("/* This file was generated by ConfigHeaderStep using the Zig Build System. */\n");
|
||||
|
||||
switch (self.style) {
|
||||
.autoconf => |file_source| {
|
||||
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| {
|
||||
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 render_blank(&output, self.values, self.include_path);
|
||||
},
|
||||
}
|
||||
|
||||
hash.update(output.items);
|
||||
|
||||
var digest: [16]u8 = undefined;
|
||||
hash.final(&digest);
|
||||
@ -143,38 +187,42 @@ fn make(step: *Step) !void {
|
||||
.{std.fmt.fmtSliceHexLower(&digest)},
|
||||
) catch unreachable;
|
||||
|
||||
self.output_dir = try std.fs.path.join(gpa, &[_][]const u8{
|
||||
const 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) });
|
||||
|
||||
// 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();
|
||||
try dir.writeFile(std.fs.path.basename(self.include_path), output.items);
|
||||
|
||||
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");
|
||||
|
||||
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,7 +240,7 @@ 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,
|
||||
});
|
||||
@ -202,13 +250,9 @@ fn render_autoconf(
|
||||
try renderValue(output, name, kv.value);
|
||||
}
|
||||
|
||||
{
|
||||
var it = values_copy.iterator();
|
||||
while (it.next()) |entry| {
|
||||
const name = entry.key_ptr.*;
|
||||
for (values_copy.keys()) |name| {
|
||||
std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
|
||||
}
|
||||
}
|
||||
|
||||
if (any_errors) {
|
||||
return error.HeaderConfigFailed;
|
||||
@ -218,9 +262,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,7 +291,7 @@ 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,
|
||||
});
|
||||
@ -254,19 +301,45 @@ fn render_cmake(
|
||||
try renderValue(output, name, kv.value);
|
||||
}
|
||||
|
||||
{
|
||||
var it = values_copy.iterator();
|
||||
while (it.next()) |entry| {
|
||||
const name = entry.key_ptr.*;
|
||||
for (values_copy.keys()) |name| {
|
||||
std.debug.print("{s}: error: config header value unused: '{s}'\n", .{ src_path, name });
|
||||
}
|
||||
}
|
||||
|
||||
if (any_errors) {
|
||||
return error.HeaderConfigFailed;
|
||||
}
|
||||
}
|
||||
|
||||
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 renderValue(output, name, values[i]);
|
||||
}
|
||||
|
||||
try output.appendSlice("#endif /* ");
|
||||
try output.appendSlice(include_guard_name);
|
||||
try output.appendSlice(" */\n");
|
||||
}
|
||||
|
||||
fn renderValue(output: *std.ArrayList(u8), name: []const u8, value: Value) !void {
|
||||
switch (value) {
|
||||
.undef => {
|
||||
|
||||
@ -15,7 +15,6 @@ builder: *std.Build,
|
||||
source: std.Build.FileSource,
|
||||
include_dirs: std.ArrayList([]const u8),
|
||||
c_macros: std.ArrayList([]const u8),
|
||||
output_dir: ?[]const u8,
|
||||
out_basename: []const u8,
|
||||
target: CrossTarget,
|
||||
optimize: std.builtin.OptimizeMode,
|
||||
@ -36,7 +35,6 @@ pub fn create(builder: *std.Build, options: Options) *TranslateCStep {
|
||||
.source = source,
|
||||
.include_dirs = std.ArrayList([]const u8).init(builder.allocator),
|
||||
.c_macros = std.ArrayList([]const u8).init(builder.allocator),
|
||||
.output_dir = null,
|
||||
.out_basename = undefined,
|
||||
.target = options.target,
|
||||
.optimize = options.optimize,
|
||||
@ -122,15 +120,10 @@ fn make(step: *Step) !void {
|
||||
const output_path = mem.trimRight(u8, output_path_nl, "\r\n");
|
||||
|
||||
self.out_basename = fs.path.basename(output_path);
|
||||
if (self.output_dir) |output_dir| {
|
||||
const full_dest = try fs.path.join(self.builder.allocator, &[_][]const u8{ output_dir, self.out_basename });
|
||||
try self.builder.updateFile(output_path, full_dest);
|
||||
} else {
|
||||
self.output_dir = fs.path.dirname(output_path).?;
|
||||
}
|
||||
const output_dir = fs.path.dirname(output_path).?;
|
||||
|
||||
self.output_file.path = try fs.path.join(
|
||||
self.builder.allocator,
|
||||
&[_][]const u8{ self.output_dir.?, self.out_basename },
|
||||
&[_][]const u8{ output_dir, self.out_basename },
|
||||
);
|
||||
}
|
||||
|
||||
@ -9,7 +9,6 @@ pub const base_id = .write_file;
|
||||
|
||||
step: Step,
|
||||
builder: *std.Build,
|
||||
output_dir: []const u8,
|
||||
files: std.TailQueue(File),
|
||||
|
||||
pub const File = struct {
|
||||
@ -23,7 +22,6 @@ pub fn init(builder: *std.Build) WriteFileStep {
|
||||
.builder = builder,
|
||||
.step = Step.init(.write_file, "writefile", builder.allocator, make),
|
||||
.files = .{},
|
||||
.output_dir = undefined,
|
||||
};
|
||||
}
|
||||
|
||||
@ -87,11 +85,11 @@ fn make(step: *Step) !void {
|
||||
.{std.fmt.fmtSliceHexLower(&digest)},
|
||||
) catch unreachable;
|
||||
|
||||
self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{
|
||||
const output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{
|
||||
self.builder.cache_root, "o", &hash_basename,
|
||||
});
|
||||
var dir = fs.cwd().makeOpenPath(self.output_dir, .{}) catch |err| {
|
||||
std.debug.print("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) });
|
||||
var dir = fs.cwd().makeOpenPath(output_dir, .{}) catch |err| {
|
||||
std.debug.print("unable to make path {s}: {s}\n", .{ output_dir, @errorName(err) });
|
||||
return err;
|
||||
};
|
||||
defer dir.close();
|
||||
@ -101,14 +99,14 @@ fn make(step: *Step) !void {
|
||||
dir.writeFile(node.data.basename, node.data.bytes) catch |err| {
|
||||
std.debug.print("unable to write {s} into {s}: {s}\n", .{
|
||||
node.data.basename,
|
||||
self.output_dir,
|
||||
output_dir,
|
||||
@errorName(err),
|
||||
});
|
||||
return err;
|
||||
};
|
||||
node.data.source.path = try fs.path.join(
|
||||
self.builder.allocator,
|
||||
&[_][]const u8{ self.output_dir, node.data.basename },
|
||||
&[_][]const u8{ output_dir, node.data.basename },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user