authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-30 19:19:13-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-30 19:21:29-07:00
log40f5e5dfc60ef94e61cc6a83547f3e82b401054c
treeffe5c86a062964c660741eb41c0cfd67179f66d7
parent89afd4bd33c72c2e608974d182f4624078da3a7f

CLI: introduce -fsingle-threaded/-fno-single-threaded

Previously there was only `--single-threaded`. This flag now matches other boolean flags, instead of only being able to opt in to single-threaded builds, you can now force multi-threaded builds. Currently this only has the possibility to emit an error message, but it is a better user experience to understand why one cannot choose to enable threads in some cases. This is breaking change to the CLI. Related: #10143

3 files changed, 20 insertions(+), 10 deletions(-)

lib/std/build.zig+8-5
......@@ -1442,6 +1442,7 @@ pub const LibExeObjStep = struct {
14421442 emit_docs: bool = false,
14431443 emit_h: bool = false,
14441444 bundle_compiler_rt: ?bool = null,
1445 single_threaded: ?bool = null,
14451446 disable_stack_probing: bool,
14461447 disable_sanitize_c: bool,
14471448 sanitize_thread: bool,
......@@ -1456,7 +1457,6 @@ pub const LibExeObjStep = struct {
14561457 exec_cmd_args: ?[]const ?[]const u8,
14571458 name_prefix: []const u8,
14581459 filter: ?[]const u8,
1459 single_threaded: bool,
14601460 test_evented_io: bool = false,
14611461 code_model: std.builtin.CodeModel = .default,
14621462 wasi_exec_model: ?std.builtin.WasiExecModel = null,
......@@ -1649,7 +1649,6 @@ pub const LibExeObjStep = struct {
16491649 .sanitize_thread = false,
16501650 .rdynamic = false,
16511651 .output_dir = null,
1652 .single_threaded = false,
16531652 .override_dest_dir = null,
16541653 .installed_path = null,
16551654 .install_step = null,
......@@ -2376,9 +2375,6 @@ pub const LibExeObjStep = struct {
23762375 try zig_args.append("-z");
23772376 try zig_args.append("notext");
23782377 }
2379 if (self.single_threaded) {
2380 try zig_args.append("--single-threaded");
2381 }
23822378
23832379 if (self.libc_file) |libc_file| {
23842380 try zig_args.append("--libc");
......@@ -2420,6 +2416,13 @@ pub const LibExeObjStep = struct {
24202416 try zig_args.append("-fno-compiler-rt");
24212417 }
24222418 }
2419 if (self.single_threaded) |single_threaded| {
2420 if (single_threaded) {
2421 try zig_args.append("-fsingle-threaded");
2422 } else {
2423 try zig_args.append("-fno-single-threaded");
2424 }
2425 }
24232426 if (self.disable_stack_probing) {
24242427 try zig_args.append("-fno-stack-check");
24252428 }
src/Compilation.zig+6-2
......@@ -705,9 +705,9 @@ pub const InitOptions = struct {
705705 use_lld: ?bool = null,
706706 use_clang: ?bool = null,
707707 use_stage1: ?bool = null,
708 single_threaded: ?bool = null,
708709 rdynamic: bool = false,
709710 strip: bool = false,
710 single_threaded: bool = false,
711711 function_sections: bool = false,
712712 is_native_os: bool,
713713 is_native_abi: bool,
......@@ -1116,7 +1116,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11161116
11171117 const include_compiler_rt = options.want_compiler_rt orelse needs_c_symbols;
11181118
1119 const single_threaded = options.single_threaded or target_util.isSingleThreaded(options.target);
1119 const must_single_thread = target_util.isSingleThreaded(options.target);
1120 const single_threaded = options.single_threaded orelse must_single_thread;
1121 if (must_single_thread and !single_threaded) {
1122 return error.TargetRequiresSingleThreaded;
1123 }
11201124
11211125 const llvm_cpu_features: ?[*:0]const u8 = if (build_options.have_llvm and use_llvm) blk: {
11221126 var buf = std.ArrayList(u8).init(arena);
src/main.zig+6-3
......@@ -369,8 +369,9 @@ const usage_build_generic =
369369 \\ -fno-Clang Prevent using Clang as the C/C++ compilation backend
370370 \\ -fstage1 Force using bootstrap compiler as the codegen backend
371371 \\ -fno-stage1 Prevent using bootstrap compiler as the codegen backend
372 \\ -fsingle-threaded Code assumes there is only one thread
373 \\ -fno-single-threaded Code may not assume there is only one thread
372374 \\ --strip Omit debug symbols
373 \\ --single-threaded Code assumes it is only used single-threaded
374375 \\ -ofmt=[mode] Override target object format
375376 \\ elf Executable and Linking Format
376377 \\ c C source code
......@@ -564,12 +565,12 @@ fn buildOutputType(
564565 var provided_name: ?[]const u8 = null;
565566 var link_mode: ?std.builtin.LinkMode = null;
566567 var dll_export_fns: ?bool = null;
568 var single_threaded: ?bool = null;
567569 var root_src_file: ?[]const u8 = null;
568570 var version: std.builtin.Version = .{ .major = 0, .minor = 0, .patch = 0 };
569571 var have_version = false;
570572 var compatibility_version: ?std.builtin.Version = null;
571573 var strip = false;
572 var single_threaded = false;
573574 var function_sections = false;
574575 var watch = false;
575576 var debug_compile_errors = false;
......@@ -1129,8 +1130,10 @@ fn buildOutputType(
11291130 emit_bin = .no;
11301131 } else if (mem.eql(u8, arg, "--strip")) {
11311132 strip = true;
1132 } else if (mem.eql(u8, arg, "--single-threaded")) {
1133 } else if (mem.eql(u8, arg, "-fsingle-threaded")) {
11331134 single_threaded = true;
1135 } else if (mem.eql(u8, arg, "-fno-single-threaded")) {
1136 single_threaded = false;
11341137 } else if (mem.eql(u8, arg, "-ffunction-sections")) {
11351138 function_sections = true;
11361139 } else if (mem.eql(u8, arg, "--eh-frame-hdr")) {