Skip to content

Commit 5924789

Browse files
author
vole-dev
committed
add -mabi parameter, affects cache, no build.zig support
1 parent d674e2f commit 5924789

File tree

13 files changed

+48
-52
lines changed

13 files changed

+48
-52
lines changed

lib/std/target.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,16 @@ pub const Target = struct {
723723
else => return null,
724724
}
725725
}
726+
fn parseAbi(comptime T: type, abi_string: []const u8) error{InvalidAbi}!T {
727+
return std.meta.stringToEnum(T, abi_string) orelse error.InvalidAbi;
728+
}
729+
pub fn parse(cpu: Cpu, abi_string: []const u8) error{InvalidAbi}!TargetAbi {
730+
return switch (cpu.arch) {
731+
.riscv32 => .{ .riscv32 = try parseAbi(Riscv32ABI, abi_string) },
732+
.riscv64 => .{ .riscv64 = try parseAbi(Riscv64ABI, abi_string) },
733+
else => error.InvalidAbi,
734+
};
735+
}
726736
};
727737

728738
pub const ObjectFormat = enum {

src/Compilation.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ pub const InitOptions = struct {
749749
version: ?std.builtin.Version = null,
750750
libc_installation: ?*const LibCInstallation = null,
751751
machine_code_model: std.builtin.CodeModel = .default,
752+
target_abi: std.Target.TargetAbi,
752753
clang_preprocessor_mode: ClangPreprocessorMode = .no,
753754
/// This is for stage1 and should be deleted upon completion of self-hosting.
754755
color: @import("main.zig").Color = .auto,
@@ -1146,6 +1147,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11461147
cache.hash.add(options.target.os.getVersionRange());
11471148
cache.hash.add(options.is_native_os);
11481149
cache.hash.add(options.target.abi);
1150+
cache.hash.addBytes(options.target_abi.string() orelse ".");
11491151
cache.hash.add(ofmt);
11501152
cache.hash.add(pic);
11511153
cache.hash.add(pie);
@@ -1427,6 +1429,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14271429
.single_threaded = single_threaded,
14281430
.verbose_link = options.verbose_link,
14291431
.machine_code_model = options.machine_code_model,
1432+
.target_abi = options.target_abi,
14301433
.dll_export_fns = dll_export_fns,
14311434
.error_return_tracing = error_return_tracing,
14321435
.llvm_cpu_features = llvm_cpu_features,
@@ -3267,7 +3270,7 @@ pub fn addCCArgs(
32673270
try argv.append("-mthumb");
32683271
}
32693272

3270-
if (std.Target.TargetAbi.default(target.cpu).string()) |mabi| {
3273+
if (comp.bin_file.options.target_abi.string()) |mabi| {
32713274
try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{mabi}));
32723275
}
32733276

@@ -3404,7 +3407,7 @@ pub fn addCCArgs(
34043407
},
34053408
}
34063409

3407-
if (std.Target.TargetAbi.default(target.cpu).string()) |mabi| {
3410+
if (comp.bin_file.options.target_abi.string()) |mabi| {
34083411
try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{mabi}));
34093412
}
34103413

@@ -4136,6 +4139,7 @@ fn buildOutputFromZig(
41364139
.strip = comp.compilerRtStrip(),
41374140
.is_native_os = comp.bin_file.options.is_native_os,
41384141
.is_native_abi = comp.bin_file.options.is_native_abi,
4142+
.target_abi = comp.bin_file.options.target_abi,
41394143
.self_exe_path = comp.self_exe_path,
41404144
.verbose_cc = comp.verbose_cc,
41414145
.verbose_link = comp.bin_file.options.verbose_link,
@@ -4323,6 +4327,7 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
43234327
.is_native_cpu = false, // Only true when bootstrapping the compiler.
43244328
.llvm_cpu_name = if (target.cpu.model.llvm_name) |s| s.ptr else null,
43254329
.llvm_cpu_features = comp.bin_file.options.llvm_cpu_features.?,
4330+
.llvm_target_abi = if (comp.bin_file.options.target_abi.string()) |s| s.ptr else null,
43264331
};
43274332

43284333
comp.stage1_cache_manifest = &man;
@@ -4572,6 +4577,7 @@ pub fn build_crt_file(
45724577
.strip = comp.compilerRtStrip(),
45734578
.is_native_os = comp.bin_file.options.is_native_os,
45744579
.is_native_abi = comp.bin_file.options.is_native_abi,
4580+
.target_abi = comp.bin_file.options.target_abi,
45754581
.self_exe_path = comp.self_exe_path,
45764582
.c_source_files = c_source_files,
45774583
.verbose_cc = comp.verbose_cc,

src/codegen/llvm.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ pub const Object = struct {
244244
// TODO handle float ABI better- it should depend on the ABI portion of std.Target
245245
const float_abi: llvm.ABIType = .Default;
246246

247-
// TODO a way to override this as part of std.Target ABI?
248-
const abi_name: ?[*:0]const u8 = if (std.Target.TargetAbi.default(options.target.cpu).string()) |mabi|
247+
const abi_name: ?[*:0]const u8 = if (options.target_abi.string()) |mabi|
249248
mabi.ptr
250249
else
251250
null;

src/glibc.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ fn buildSharedLib(
959959
.strip = comp.compilerRtStrip(),
960960
.is_native_os = false,
961961
.is_native_abi = false,
962+
.target_abi = comp.bin_file.options.target_abi,
962963
.self_exe_path = comp.self_exe_path,
963964
.verbose_cc = comp.verbose_cc,
964965
.verbose_link = comp.bin_file.options.verbose_link,

src/libcxx.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {
199199
.strip = comp.compilerRtStrip(),
200200
.is_native_os = comp.bin_file.options.is_native_os,
201201
.is_native_abi = comp.bin_file.options.is_native_abi,
202+
.target_abi = comp.bin_file.options.target_abi,
202203
.self_exe_path = comp.self_exe_path,
203204
.c_source_files = c_source_files.items,
204205
.verbose_cc = comp.verbose_cc,
@@ -331,6 +332,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
331332
.strip = comp.compilerRtStrip(),
332333
.is_native_os = comp.bin_file.options.is_native_os,
333334
.is_native_abi = comp.bin_file.options.is_native_abi,
335+
.target_abi = comp.bin_file.options.target_abi,
334336
.self_exe_path = comp.self_exe_path,
335337
.c_source_files = c_source_files.items,
336338
.verbose_cc = comp.verbose_cc,

src/libtsan.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ pub fn buildTsan(comp: *Compilation) !void {
218218
.strip = comp.compilerRtStrip(),
219219
.is_native_os = comp.bin_file.options.is_native_os,
220220
.is_native_abi = comp.bin_file.options.is_native_abi,
221+
.target_abi = comp.bin_file.options.target_abi,
221222
.self_exe_path = comp.self_exe_path,
222223
.c_source_files = c_source_files.items,
223224
.verbose_cc = comp.verbose_cc,

src/libunwind.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ pub fn buildStaticLib(comp: *Compilation) !void {
123123
.strip = comp.compilerRtStrip(),
124124
.is_native_os = comp.bin_file.options.is_native_os,
125125
.is_native_abi = comp.bin_file.options.is_native_abi,
126+
.target_abi = comp.bin_file.options.target_abi,
126127
.self_exe_path = comp.self_exe_path,
127128
.c_source_files = &c_source_files,
128129
.verbose_cc = comp.verbose_cc,

src/link.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub const Options = struct {
109109
version_script: ?[]const u8,
110110
soname: ?[]const u8,
111111
llvm_cpu_features: ?[*:0]const u8,
112+
target_abi: std.Target.TargetAbi,
112113
/// Extra args passed directly to LLD. Ignored when not linking with LLD.
113114
extra_lld_args: []const []const u8,
114115

src/main.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ const usage_build_generic =
325325
\\Compile Options:
326326
\\ -target [name] <arch><sub>-<os>-<abi> see the targets command
327327
\\ -mcpu [cpu] Specify target CPU and feature set
328+
\\ -mabi [target-abi] Specify processor specific target-abi
328329
\\ -mcmodel=[default|tiny| Limit range of code and data virtual addresses
329330
\\ small|kernel|
330331
\\ medium|large]
@@ -620,6 +621,7 @@ fn buildOutputType(
620621
var sysroot: ?[]const u8 = null;
621622
var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC");
622623
var machine_code_model: std.builtin.CodeModel = .default;
624+
var target_abi_str: ?[]const u8 = null;
623625
var runtime_args_start: ?usize = null;
624626
var test_filter: ?[]const u8 = null;
625627
var test_name_prefix: ?[]const u8 = null;
@@ -881,6 +883,12 @@ fn buildOutputType(
881883
target_mcpu = arg["-mcpu=".len..];
882884
} else if (mem.startsWith(u8, arg, "-mcmodel=")) {
883885
machine_code_model = parseCodeModel(arg["-mcmodel=".len..]);
886+
} else if (mem.eql(u8, arg, "-mabi")) {
887+
if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
888+
i += 1;
889+
target_abi_str = args[i];
890+
} else if (mem.startsWith(u8, arg, "-mabi=")) {
891+
target_abi_str = arg["-mabi=".len..];
884892
} else if (mem.startsWith(u8, arg, "-O")) {
885893
optimize_mode_string = arg["-O".len..];
886894
} else if (mem.eql(u8, arg, "--dynamic-linker")) {
@@ -1637,6 +1645,10 @@ fn buildOutputType(
16371645
});
16381646

16391647
const target_info = try detectNativeTargetInfo(gpa, cross_target);
1648+
const target_abi = if (target_abi_str) |s|
1649+
std.Target.TargetAbi.parse(target_info.target.cpu, s) catch fatal("unsupported target-abi '{s}' for cpu", .{target_abi_str})
1650+
else
1651+
std.Target.TargetAbi.default(target_info.target.cpu);
16401652

16411653
if (target_info.target.os.tag != .freestanding) {
16421654
if (ensure_libc_on_non_freestanding)
@@ -2127,6 +2139,7 @@ fn buildOutputType(
21272139
.verbose_cimport = verbose_cimport,
21282140
.verbose_llvm_cpu_features = verbose_llvm_cpu_features,
21292141
.machine_code_model = machine_code_model,
2142+
.target_abi = target_abi,
21302143
.color = color,
21312144
.time_report = time_report,
21322145
.stack_report = stack_report,
@@ -3038,6 +3051,7 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
30383051
.target = target_info.target,
30393052
.is_native_os = cross_target.isNativeOs(),
30403053
.is_native_abi = cross_target.isNativeAbi(),
3054+
.target_abi = std.Target.TargetAbi.default(target_info.target.cpu),
30413055
.dynamic_linker = target_info.dynamic_linker.get(),
30423056
.output_mode = .Exe,
30433057
.main_pkg = &main_pkg,

src/musl.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
213213
.strip = comp.compilerRtStrip(),
214214
.is_native_os = false,
215215
.is_native_abi = false,
216+
.target_abi = comp.bin_file.options.target_abi,
216217
.self_exe_path = comp.self_exe_path,
217218
.verbose_cc = comp.verbose_cc,
218219
.verbose_link = comp.bin_file.options.verbose_link,

0 commit comments

Comments
 (0)