stage2: eliminate the "compiler id" concept

Instead, append a "dirty suffix" to the version string when there are
dirty git changes and use the version string as the compiler id.

This avoids a dependency on the cache hash system, and saves time on
first invocation of the compiler since it does not have to compute its
compiler id. It also saves time by not having to check the cache for a
saved compiler id.
This commit is contained in:
Andrew Kelley 2020-09-08 11:15:32 -07:00
parent 35f334ae0f
commit c99e34a00e
5 changed files with 87 additions and 80 deletions

View File

@ -56,7 +56,22 @@ pub fn build(b: *Builder) !void {
const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse false;
const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h");
if (!only_install_lib_files) {
b.installDirectory(InstallDirectoryOptions{
.source_dir = "lib",
.install_dir = .Lib,
.install_subdir = "zig",
.exclude_extensions = &[_][]const u8{
"test.zig",
"README.md",
".z.0",
".z.9",
"rfc1951.txt",
},
});
if (only_install_lib_files)
return;
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
exe.install();
exe.setBuildMode(mode);
@ -88,19 +103,40 @@ pub fn build(b: *Builder) !void {
const opt_version_string = b.option([]const u8, "version-string", "Override Zig version string. Default is to find out with git.");
const version = if (opt_version_string) |version| version else v: {
const version_string = b.fmt("{}.{}.{}", .{ zig_version.major, zig_version.minor, zig_version.patch });
var code: u8 = undefined;
const version_untrimmed = b.execAllowFail(&[_][]const u8{
const git_sha_untrimmed = b.execAllowFail(&[_][]const u8{
"git", "-C", b.build_root, "name-rev", "HEAD",
"--tags", "--name-only", "--no-undefined", "--always",
}, &code, .Ignore) catch {
break :v version_string;
};
const git_sha_trimmed = mem.trim(u8, git_sha_untrimmed, " \n\r");
// Detect dirty changes.
const diff_untrimmed = b.execAllowFail(&[_][]const u8{
"git", "-C", b.build_root, "diff", "HEAD",
}, &code, .Ignore) catch |err| {
std.debug.print(
\\Unable to determine zig version string: {}
\\Provide the zig version string explicitly using the `version-string` build option.
, .{err});
std.debug.print("Error executing git diff: {}", .{err});
std.process.exit(1);
};
const trimmed = mem.trim(u8, version_untrimmed, " \n\r");
break :v b.fmt("{}.{}.{}+{}", .{ zig_version.major, zig_version.minor, zig_version.patch, trimmed });
const trimmed_diff = mem.trim(u8, diff_untrimmed, " \n\r");
const dirty_suffix = if (trimmed_diff.len == 0) "" else s: {
const dirty_hash = std.hash.Wyhash.hash(0, trimmed_diff);
break :s b.fmt("dirty{x}", .{@truncate(u32, dirty_hash)});
};
// This will look like e.g. "0.6.0^0" for a tag commit.
if (mem.endsWith(u8, git_sha_trimmed, "^0")) {
const git_ver_string = git_sha_trimmed[0 .. git_sha_trimmed.len - 2];
if (!mem.eql(u8, git_ver_string, version_string)) {
std.debug.print("Expected git tag '{}', found '{}'", .{ version_string, git_ver_string });
std.process.exit(1);
}
break :v b.fmt("{}{}", .{ version_string, dirty_suffix });
} else {
break :v b.fmt("{}+{}{}", .{ version_string, git_sha_trimmed, dirty_suffix });
}
};
exe.addBuildOption([]const u8, "version", version);
@ -117,20 +153,6 @@ pub fn build(b: *Builder) !void {
exe.linkSystemLibraryName("c++");
exe.linkLibC();
}
}
b.installDirectory(InstallDirectoryOptions{
.source_dir = "lib",
.install_dir = .Lib,
.install_subdir = "zig",
.exclude_extensions = &[_][]const u8{
"test.zig",
"README.md",
".z.0",
".z.9",
"rfc1951.txt",
},
});
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
@ -144,6 +166,7 @@ pub fn build(b: *Builder) !void {
test_stage2.addBuildOption(bool, "enable_wine", is_wine_enabled);
test_stage2.addBuildOption(bool, "enable_wasmtime", is_wasmtime_enabled);
test_stage2.addBuildOption(?[]const u8, "glibc_multi_install_dir", glibc_multi_dir);
test_stage2.addBuildOption([]const u8, "version", version);
const test_stage2_step = b.step("test-stage2", "Run the stage2 compiler tests");
test_stage2_step.dependOn(&test_stage2.step);

View File

@ -952,7 +952,6 @@ pub const InitOptions = struct {
linker_z_nodelete: bool = false,
linker_z_defs: bool = false,
stack_size_override: u64 = 0,
compiler_id: [16]u8,
};
pub fn init(gpa: *Allocator, options: InitOptions) !Module {
@ -1056,7 +1055,7 @@ pub fn init(gpa: *Allocator, options: InitOptions) !Module {
// Now we will prepare hash state initializations to avoid redundantly computing hashes.
// First we add common things between things that apply to zig source and all c source files.
cache.add(options.compiler_id);
cache.addBytes(build_options.version);
cache.add(options.optimize_mode);
cache.add(options.target.cpu.arch);
cache.addBytes(options.target.cpu.model.name);

View File

@ -960,9 +960,6 @@ pub fn buildOutputType(
.yes_default_path => try std.fmt.allocPrint(arena, "{}.h", .{root_name}),
};
// TODO look into implementing compiler_id at build time so we don't have to compute it at runtime.
const compiler_id = try introspect.resolveCompilerId(gpa);
var module = Module.init(gpa, .{
.root_name = root_name,
.target = target_info.target,
@ -1002,7 +999,6 @@ pub fn buildOutputType(
.linker_z_nodelete = linker_z_nodelete,
.linker_z_defs = linker_z_defs,
.stack_size_override = stack_size_override,
.compiler_id = compiler_id,
.strip = strip,
}) catch |err| {
fatal("unable to initialize module: {}", .{@errorName(err)});

View File

@ -16,10 +16,6 @@ pub fn cmdEnv(gpa: *Allocator, args: []const []const u8, stdout: anytype) !void
const global_cache_dir = try introspect.resolveGlobalCacheDir(gpa);
defer gpa.free(global_cache_dir);
const compiler_id_digest = try introspect.resolveCompilerId(gpa);
var compiler_id_buf: [compiler_id_digest.len * 2]u8 = undefined;
const compiler_id = std.fmt.bufPrint(&compiler_id_buf, "{x}", .{compiler_id_digest}) catch unreachable;
var bos = std.io.bufferedOutStream(stdout);
const bos_stream = bos.outStream();
@ -32,9 +28,6 @@ pub fn cmdEnv(gpa: *Allocator, args: []const []const u8, stdout: anytype) !void
try jws.objectField("std_dir");
try jws.emitString(zig_std_dir);
try jws.objectField("id");
try jws.emitString(compiler_id);
try jws.objectField("global_cache_dir");
try jws.emitString(global_cache_dir);

View File

@ -9,7 +9,6 @@ const enable_qemu: bool = build_options.enable_qemu;
const enable_wine: bool = build_options.enable_wine;
const enable_wasmtime: bool = build_options.enable_wasmtime;
const glibc_multi_install_dir: ?[]const u8 = build_options.glibc_multi_install_dir;
const introspect = @import("introspect.zig");
const cheader = @embedFile("link/cbe.h");
@ -439,8 +438,6 @@ pub const TestContext = struct {
const ofmt: ?std.builtin.ObjectFormat = if (case.cbe) .c else null;
const bin_name = try std.zig.binNameAlloc(arena, "test_case", target, case.output_mode, null, ofmt);
const compiler_id = try introspect.resolveCompilerId(arena);
var module = try Module.init(allocator, .{
.root_name = "test_case",
.target = target,
@ -455,7 +452,6 @@ pub const TestContext = struct {
.root_pkg = root_pkg,
.keep_source_files_loaded = true,
.object_format = ofmt,
.compiler_id = compiler_id,
});
defer module.deinit();