Commit 40f5e5dfc6

Andrew Kelley <andrew@ziglang.org>
2021-12-01 03:19:13
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
1 parent 89afd4b
Changed files (3)
lib/std/build.zig
@@ -1442,6 +1442,7 @@ pub const LibExeObjStep = struct {
     emit_docs: bool = false,
     emit_h: bool = false,
     bundle_compiler_rt: ?bool = null,
+    single_threaded: ?bool = null,
     disable_stack_probing: bool,
     disable_sanitize_c: bool,
     sanitize_thread: bool,
@@ -1456,7 +1457,6 @@ pub const LibExeObjStep = struct {
     exec_cmd_args: ?[]const ?[]const u8,
     name_prefix: []const u8,
     filter: ?[]const u8,
-    single_threaded: bool,
     test_evented_io: bool = false,
     code_model: std.builtin.CodeModel = .default,
     wasi_exec_model: ?std.builtin.WasiExecModel = null,
@@ -1649,7 +1649,6 @@ pub const LibExeObjStep = struct {
             .sanitize_thread = false,
             .rdynamic = false,
             .output_dir = null,
-            .single_threaded = false,
             .override_dest_dir = null,
             .installed_path = null,
             .install_step = null,
@@ -2376,9 +2375,6 @@ pub const LibExeObjStep = struct {
             try zig_args.append("-z");
             try zig_args.append("notext");
         }
-        if (self.single_threaded) {
-            try zig_args.append("--single-threaded");
-        }
 
         if (self.libc_file) |libc_file| {
             try zig_args.append("--libc");
@@ -2420,6 +2416,13 @@ pub const LibExeObjStep = struct {
                 try zig_args.append("-fno-compiler-rt");
             }
         }
+        if (self.single_threaded) |single_threaded| {
+            if (single_threaded) {
+                try zig_args.append("-fsingle-threaded");
+            } else {
+                try zig_args.append("-fno-single-threaded");
+            }
+        }
         if (self.disable_stack_probing) {
             try zig_args.append("-fno-stack-check");
         }
src/Compilation.zig
@@ -705,9 +705,9 @@ pub const InitOptions = struct {
     use_lld: ?bool = null,
     use_clang: ?bool = null,
     use_stage1: ?bool = null,
+    single_threaded: ?bool = null,
     rdynamic: bool = false,
     strip: bool = false,
-    single_threaded: bool = false,
     function_sections: bool = false,
     is_native_os: bool,
     is_native_abi: bool,
@@ -1116,7 +1116,11 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
 
         const include_compiler_rt = options.want_compiler_rt orelse needs_c_symbols;
 
-        const single_threaded = options.single_threaded or target_util.isSingleThreaded(options.target);
+        const must_single_thread = target_util.isSingleThreaded(options.target);
+        const single_threaded = options.single_threaded orelse must_single_thread;
+        if (must_single_thread and !single_threaded) {
+            return error.TargetRequiresSingleThreaded;
+        }
 
         const llvm_cpu_features: ?[*:0]const u8 = if (build_options.have_llvm and use_llvm) blk: {
             var buf = std.ArrayList(u8).init(arena);
src/main.zig
@@ -369,8 +369,9 @@ const usage_build_generic =
     \\  -fno-Clang                Prevent using Clang as the C/C++ compilation backend
     \\  -fstage1                  Force using bootstrap compiler as the codegen backend
     \\  -fno-stage1               Prevent using bootstrap compiler as the codegen backend
+    \\  -fsingle-threaded         Code assumes there is only one thread
+    \\  -fno-single-threaded      Code may not assume there is only one thread
     \\  --strip                   Omit debug symbols
-    \\  --single-threaded         Code assumes it is only used single-threaded
     \\  -ofmt=[mode]              Override target object format
     \\    elf                     Executable and Linking Format
     \\    c                       C source code
@@ -564,12 +565,12 @@ fn buildOutputType(
     var provided_name: ?[]const u8 = null;
     var link_mode: ?std.builtin.LinkMode = null;
     var dll_export_fns: ?bool = null;
+    var single_threaded: ?bool = null;
     var root_src_file: ?[]const u8 = null;
     var version: std.builtin.Version = .{ .major = 0, .minor = 0, .patch = 0 };
     var have_version = false;
     var compatibility_version: ?std.builtin.Version = null;
     var strip = false;
-    var single_threaded = false;
     var function_sections = false;
     var watch = false;
     var debug_compile_errors = false;
@@ -1129,8 +1130,10 @@ fn buildOutputType(
                         emit_bin = .no;
                     } else if (mem.eql(u8, arg, "--strip")) {
                         strip = true;
-                    } else if (mem.eql(u8, arg, "--single-threaded")) {
+                    } else if (mem.eql(u8, arg, "-fsingle-threaded")) {
                         single_threaded = true;
+                    } else if (mem.eql(u8, arg, "-fno-single-threaded")) {
+                        single_threaded = false;
                     } else if (mem.eql(u8, arg, "-ffunction-sections")) {
                         function_sections = true;
                     } else if (mem.eql(u8, arg, "--eh-frame-hdr")) {