add zig reduce subcommand

Also adds `-Donly-reduce` flag in build.zig which disables LLVM and
irrelevant code for faster iteration cycles.
This commit is contained in:
Andrew Kelley 2023-11-02 13:55:24 -07:00
parent 1ccc68f307
commit f8c24c2cd0
3 changed files with 60 additions and 0 deletions

View file

@ -33,6 +33,7 @@ pub fn build(b: *std.Build) !void {
const skip_install_langref = b.option(bool, "no-langref", "skip copying of langref to the installation prefix") orelse skip_install_lib_files; const skip_install_langref = b.option(bool, "no-langref", "skip copying of langref to the installation prefix") orelse skip_install_lib_files;
const skip_install_autodocs = b.option(bool, "no-autodocs", "skip copying of standard library autodocs to the installation prefix") orelse skip_install_lib_files; const skip_install_autodocs = b.option(bool, "no-autodocs", "skip copying of standard library autodocs to the installation prefix") orelse skip_install_lib_files;
const no_bin = b.option(bool, "no-bin", "skip emitting compiler binary") orelse false; const no_bin = b.option(bool, "no-bin", "skip emitting compiler binary") orelse false;
const only_reduce = b.option(bool, "only-reduce", "only build zig reduce") orelse false;
const docgen_exe = b.addExecutable(.{ const docgen_exe = b.addExecutable(.{
.name = "docgen", .name = "docgen",
@ -193,6 +194,10 @@ pub fn build(b: *std.Build) !void {
}; };
const exe = addCompilerStep(b, optimize, target); const exe = addCompilerStep(b, optimize, target);
if (only_reduce) {
exe.use_llvm = false;
exe.use_lld = false;
}
exe.strip = strip; exe.strip = strip;
exe.pie = pie; exe.pie = pie;
exe.sanitize_thread = sanitize_thread; exe.sanitize_thread = sanitize_thread;
@ -236,6 +241,7 @@ pub fn build(b: *std.Build) !void {
exe_options.addOption(bool, "force_gpa", force_gpa); exe_options.addOption(bool, "force_gpa", force_gpa);
exe_options.addOption(bool, "only_c", only_c); exe_options.addOption(bool, "only_c", only_c);
exe_options.addOption(bool, "only_core_functionality", only_c); exe_options.addOption(bool, "only_core_functionality", only_c);
exe_options.addOption(bool, "only_reduce", only_reduce);
if (link_libc) { if (link_libc) {
exe.linkLibC(); exe.linkLibC();
@ -391,6 +397,7 @@ pub fn build(b: *std.Build) !void {
test_cases_options.addOption(bool, "force_gpa", force_gpa); test_cases_options.addOption(bool, "force_gpa", force_gpa);
test_cases_options.addOption(bool, "only_c", only_c); test_cases_options.addOption(bool, "only_c", only_c);
test_cases_options.addOption(bool, "only_core_functionality", true); test_cases_options.addOption(bool, "only_core_functionality", true);
test_cases_options.addOption(bool, "only_reduce", false);
test_cases_options.addOption(bool, "enable_qemu", b.enable_qemu); test_cases_options.addOption(bool, "enable_qemu", b.enable_qemu);
test_cases_options.addOption(bool, "enable_wine", b.enable_wine); test_cases_options.addOption(bool, "enable_wine", b.enable_wine);
test_cases_options.addOption(bool, "enable_wasmtime", b.enable_wasmtime); test_cases_options.addOption(bool, "enable_wasmtime", b.enable_wasmtime);
@ -549,6 +556,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
exe_options.addOption(bool, "enable_tracy_allocation", false); exe_options.addOption(bool, "enable_tracy_allocation", false);
exe_options.addOption(bool, "value_tracing", false); exe_options.addOption(bool, "value_tracing", false);
exe_options.addOption(bool, "only_core_functionality", true); exe_options.addOption(bool, "only_core_functionality", true);
exe_options.addOption(bool, "only_reduce", false);
const run_opt = b.addSystemCommand(&.{ const run_opt = b.addSystemCommand(&.{
"wasm-opt", "wasm-opt",

View file

@ -212,6 +212,14 @@ pub fn main() anyerror!void {
} }
} }
if (build_options.only_reduce) {
if (mem.eql(u8, args[1], "reduce")) {
return @import("reduce.zig").main(gpa, arena, args);
} else {
@panic("only reduce is supported in a -Donly-reduce build");
}
}
return mainArgs(gpa, arena, args); return mainArgs(gpa, arena, args);
} }

44
src/reduce.zig Normal file
View file

@ -0,0 +1,44 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const usage =
\\zig reduce [source_file] [transformation]
\\
;
const Transformation = enum {
none,
};
pub fn main(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
const file_path = args[2];
const transformation = std.meta.stringToEnum(Transformation, args[3]);
assert(transformation == .none);
const source_code = try std.fs.cwd().readFileAllocOptions(
arena,
file_path,
std.math.maxInt(u32),
null,
1,
0,
);
var tree = try std.zig.Ast.parse(gpa, source_code, .zig);
defer tree.deinit(gpa);
if (tree.errors.len != 0) {
@panic("syntax errors occurred");
}
var rendered = std.ArrayList(u8).init(gpa);
defer rendered.deinit();
rendered.clearRetainingCapacity();
try tree.renderToArrayList(&rendered);
const stdout = std.io.getStdOut();
try stdout.writeAll(rendered.items);
return std.process.cleanExit();
}