Skip to content

Commit e74ced2

Browse files
committed
frontend: fix -fsingle-threaded default detection logic
The logic in 509be7c assumed that `use_llvm` meant that the LLVM backend would be used, however, use_llvm is false when there are no zig files to compile, which is the case for zig cc. This logic resulted in `-fsingle-threaded` which made libc++ fail to compile for C++ code that includes the threading abstractions (such as LLVM).
1 parent 1b0b46a commit e74ced2

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/Compilation.zig

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,13 +1112,22 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
11121112

11131113
const include_compiler_rt = options.want_compiler_rt orelse needs_c_symbols;
11141114

1115-
const must_single_thread = target_util.isSingleThreaded(options.target);
1116-
const single_threaded = options.single_threaded orelse must_single_thread or
1117-
// x86_64 codegen doesn't support TLV for most object formats
1118-
(!use_llvm and options.target.cpu.arch == .x86_64 and options.target.ofmt != .macho);
1119-
if (must_single_thread and !single_threaded) {
1120-
return error.TargetRequiresSingleThreaded;
1121-
}
1115+
const single_threaded = st: {
1116+
if (target_util.isSingleThreaded(options.target)) {
1117+
if (options.single_threaded == false)
1118+
return error.TargetRequiresSingleThreaded;
1119+
break :st true;
1120+
}
1121+
if (options.main_mod != null) {
1122+
const zig_backend = zigBackend(options.target, use_llvm);
1123+
if (!target_util.supportsThreads(options.target, zig_backend)) {
1124+
if (options.single_threaded == false)
1125+
return error.BackendRequiresSingleThreaded;
1126+
break :st true;
1127+
}
1128+
}
1129+
break :st options.single_threaded orelse false;
1130+
};
11221131

11231132
const llvm_cpu_features: ?[*:0]const u8 = if (use_llvm) blk: {
11241133
var buf = std.ArrayList(u8).init(arena);

src/target.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@ pub fn supportsTailCall(target: std.Target, backend: std.builtin.CompilerBackend
641641
}
642642
}
643643

644+
pub fn supportsThreads(target: std.Target, backend: std.builtin.CompilerBackend) bool {
645+
return switch (backend) {
646+
.stage2_x86_64 => target.ofmt == .macho,
647+
else => true,
648+
};
649+
}
650+
644651
pub fn libcFloatPrefix(float_bits: u16) []const u8 {
645652
return switch (float_bits) {
646653
16, 80 => "__",

0 commit comments

Comments
 (0)