Skip to content

Commit

Permalink
frontend: fix -fsingle-threaded default detection logic
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
andrewrk committed Nov 6, 2023
1 parent 1b0b46a commit e74ced2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1112,13 +1112,22 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {

const include_compiler_rt = options.want_compiler_rt orelse needs_c_symbols;

const must_single_thread = target_util.isSingleThreaded(options.target);
const single_threaded = options.single_threaded orelse must_single_thread or
// x86_64 codegen doesn't support TLV for most object formats
(!use_llvm and options.target.cpu.arch == .x86_64 and options.target.ofmt != .macho);
if (must_single_thread and !single_threaded) {
return error.TargetRequiresSingleThreaded;
}
const single_threaded = st: {
if (target_util.isSingleThreaded(options.target)) {
if (options.single_threaded == false)
return error.TargetRequiresSingleThreaded;
break :st true;
}
if (options.main_mod != null) {
const zig_backend = zigBackend(options.target, use_llvm);
if (!target_util.supportsThreads(options.target, zig_backend)) {
if (options.single_threaded == false)
return error.BackendRequiresSingleThreaded;
break :st true;
}
}
break :st options.single_threaded orelse false;
};

const llvm_cpu_features: ?[*:0]const u8 = if (use_llvm) blk: {
var buf = std.ArrayList(u8).init(arena);
Expand Down
7 changes: 7 additions & 0 deletions src/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,13 @@ pub fn supportsTailCall(target: std.Target, backend: std.builtin.CompilerBackend
}
}

pub fn supportsThreads(target: std.Target, backend: std.builtin.CompilerBackend) bool {
return switch (backend) {
.stage2_x86_64 => target.ofmt == .macho,
else => true,
};
}

pub fn libcFloatPrefix(float_bits: u16) []const u8 {
return switch (float_bits) {
16, 80 => "__",
Expand Down

0 comments on commit e74ced2

Please sign in to comment.