Skip to content

Commit

Permalink
compiler: Enable -Wdate-time for C/C++ code in most Zig commands.
Browse files Browse the repository at this point in the history
In particular: `build-obj` (& friends), `test`, `run`, and `translate-c`.

Not `cc`/`c++`, however, as we don't advertise deterministic builds for those
commands. For those, it is expected that people set up the necessary flags
themselves if they want determinism. (See #20811 for one such flag.)
  • Loading branch information
alexrp committed Jul 27, 2024
1 parent 66b7127 commit b360c35
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ native_system_include_paths: []const []const u8,
/// Corresponds to `-u <symbol>` for ELF/MachO and `/include:<symbol>` for COFF/PE.
force_undefined_symbols: std.StringArrayHashMapUnmanaged(void),

warn_nondeterminism: bool,

c_object_table: std.AutoArrayHashMapUnmanaged(*CObject, void) = .{},
win32_resource_table: if (dev.env.supports(.win32_resource)) std.AutoArrayHashMapUnmanaged(*Win32Resource, void) else struct {
pub fn keys(_: @This()) [0]void {
Expand Down Expand Up @@ -1175,6 +1177,7 @@ pub const CreateOptions = struct {
hash_style: link.File.Elf.HashStyle = .both,
entry: Entry = .default,
force_undefined_symbols: std.StringArrayHashMapUnmanaged(void) = .{},
warn_nondeterminism: bool = false,
stack_size: ?u64 = null,
image_base: ?u64 = null,
version: ?std.SemanticVersion = null,
Expand Down Expand Up @@ -1572,6 +1575,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
.native_system_include_paths = options.native_system_include_paths,
.wasi_emulated_libs = options.wasi_emulated_libs,
.force_undefined_symbols = options.force_undefined_symbols,
.warn_nondeterminism = options.warn_nondeterminism,
.link_eh_frame_hdr = link_eh_frame_hdr,
.global_cc_argv = options.global_cc_argv,
.file_system_inputs = options.file_system_inputs,
Expand Down Expand Up @@ -2625,6 +2629,7 @@ fn addNonIncrementalStuffToCacheManifest(
man.hash.add(comp.include_compiler_rt);
man.hash.add(comp.rc_includes);
man.hash.addListOfBytes(comp.force_undefined_symbols.keys());
man.hash.add(comp.warn_nondeterminism);
man.hash.addListOfBytes(comp.framework_dirs);
try link.hashAddSystemLibs(man, comp.system_libs);

Expand Down Expand Up @@ -5731,6 +5736,10 @@ pub fn addCCArgs(
},
}

if (comp.warn_nondeterminism) {
try argv.append("-Wdate-time");
}

if (target_util.supports_fpic(target) and mod.pic) {
try argv.append("-fPIC");
}
Expand Down
8 changes: 6 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ fn buildOutputType(
var test_no_exec = false;
var entry: Compilation.CreateOptions.Entry = .default;
var force_undefined_symbols: std.StringArrayHashMapUnmanaged(void) = .{};
var warn_nondeterminism: bool = false;
var stack_size: ?u64 = null;
var image_base: ?u64 = null;
var link_eh_frame_hdr = false;
Expand Down Expand Up @@ -1150,8 +1151,6 @@ fn buildOutputType(
n_jobs = num;
} else if (mem.eql(u8, arg, "--subsystem")) {
subsystem = try parseSubSystem(args_iter.nextOrFatal());
} else if (mem.eql(u8, arg, "-O")) {
mod_opts.optimize_mode = parseOptimizeMode(args_iter.nextOrFatal());
} else if (mem.startsWith(u8, arg, "-fentry=")) {
entry = .{ .named = arg["-fentry=".len..] };
} else if (mem.eql(u8, arg, "--force_undefined")) {
Expand Down Expand Up @@ -1299,6 +1298,10 @@ fn buildOutputType(
target_mcpu = arg["-mcpu=".len..];
} else if (mem.startsWith(u8, arg, "-O")) {
mod_opts.optimize_mode = parseOptimizeMode(arg["-O".len..]);
warn_nondeterminism = mod_opts.optimize_mode != .Debug;
} else if (mem.eql(u8, arg, "-O")) {
mod_opts.optimize_mode = parseOptimizeMode(args_iter.nextOrFatal());
warn_nondeterminism = mod_opts.optimize_mode != .Debug;
} else if (mem.eql(u8, arg, "--dynamic-linker")) {
create_module.dynamic_linker = args_iter.nextOrFatal();
} else if (mem.eql(u8, arg, "--sysroot")) {
Expand Down Expand Up @@ -3348,6 +3351,7 @@ fn buildOutputType(
.link_emit_relocs = link_emit_relocs,
.entry = entry,
.force_undefined_symbols = force_undefined_symbols,
.warn_nondeterminism = warn_nondeterminism,
.stack_size = stack_size,
.image_base = image_base,
.formatted_panics = formatted_panics,
Expand Down

0 comments on commit b360c35

Please sign in to comment.