CBE: Make C an ObjectFormat instead of a special bool (#5849)

This commit is contained in:
pixelherodev 2020-07-12 22:56:31 -04:00 committed by GitHub
parent dff1ac1089
commit 2c882b2e65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 11 deletions

View File

@ -435,6 +435,7 @@ pub const Target = struct {
elf, elf,
macho, macho,
wasm, wasm,
c,
}; };
pub const SubSystem = enum { pub const SubSystem = enum {

View File

@ -723,7 +723,6 @@ pub const InitOptions = struct {
object_format: ?std.builtin.ObjectFormat = null, object_format: ?std.builtin.ObjectFormat = null,
optimize_mode: std.builtin.Mode = .Debug, optimize_mode: std.builtin.Mode = .Debug,
keep_source_files_loaded: bool = false, keep_source_files_loaded: bool = false,
cbe: bool = false,
}; };
pub fn init(gpa: *Allocator, options: InitOptions) !Module { pub fn init(gpa: *Allocator, options: InitOptions) !Module {
@ -733,7 +732,6 @@ pub fn init(gpa: *Allocator, options: InitOptions) !Module {
.output_mode = options.output_mode, .output_mode = options.output_mode,
.link_mode = options.link_mode orelse .Static, .link_mode = options.link_mode orelse .Static,
.object_format = options.object_format orelse options.target.getObjectFormat(), .object_format = options.object_format orelse options.target.getObjectFormat(),
.cbe = options.cbe,
}); });
errdefer bin_file.destroy(); errdefer bin_file.destroy();

View File

@ -22,7 +22,6 @@ pub const Options = struct {
/// Used for calculating how much space to reserve for executable program code in case /// Used for calculating how much space to reserve for executable program code in case
/// the binary file deos not already have such a section. /// the binary file deos not already have such a section.
program_code_size_hint: u64 = 256 * 1024, program_code_size_hint: u64 = 256 * 1024,
cbe: bool = false,
}; };
/// Attempts incremental linking, if the file already exists. /// Attempts incremental linking, if the file already exists.
@ -35,10 +34,11 @@ pub fn openBinFilePath(
sub_path: []const u8, sub_path: []const u8,
options: Options, options: Options,
) !*File { ) !*File {
const file = try dir.createFile(sub_path, .{ .truncate = options.cbe, .read = true, .mode = determineMode(options) }); const cbe = options.object_format == .c;
const file = try dir.createFile(sub_path, .{ .truncate = cbe, .read = true, .mode = determineMode(options) });
errdefer file.close(); errdefer file.close();
if (options.cbe) { if (cbe) {
var bin_file = try allocator.create(File.C); var bin_file = try allocator.create(File.C);
errdefer allocator.destroy(bin_file); errdefer allocator.destroy(bin_file);
bin_file.* = try openCFile(allocator, file, options); bin_file.* = try openCFile(allocator, file, options);
@ -1573,6 +1573,7 @@ pub fn createElfFile(allocator: *Allocator, file: fs.File, options: Options) !Fi
.Lib => return error.TODOImplementWritingLibFiles, .Lib => return error.TODOImplementWritingLibFiles,
} }
switch (options.object_format) { switch (options.object_format) {
.c => unreachable,
.unknown => unreachable, // TODO remove this tag from the enum .unknown => unreachable, // TODO remove this tag from the enum
.coff => return error.TODOImplementWritingCOFF, .coff => return error.TODOImplementWritingCOFF,
.elf => {}, .elf => {},
@ -1632,6 +1633,7 @@ fn openBinFileInner(allocator: *Allocator, file: fs.File, options: Options) !Fil
} }
switch (options.object_format) { switch (options.object_format) {
.unknown => unreachable, // TODO remove this tag from the enum .unknown => unreachable, // TODO remove this tag from the enum
.c => unreachable,
.coff => return error.IncrFailed, .coff => return error.IncrFailed,
.elf => {}, .elf => {},
.macho => return error.IncrFailed, .macho => return error.IncrFailed,

View File

@ -194,8 +194,8 @@ fn buildOutputType(
var emit_zir: Emit = .no; var emit_zir: Emit = .no;
var target_arch_os_abi: []const u8 = "native"; var target_arch_os_abi: []const u8 = "native";
var target_mcpu: ?[]const u8 = null; var target_mcpu: ?[]const u8 = null;
var cbe: bool = false;
var target_dynamic_linker: ?[]const u8 = null; var target_dynamic_linker: ?[]const u8 = null;
var object_format: ?std.builtin.ObjectFormat = null;
var system_libs = std.ArrayList([]const u8).init(gpa); var system_libs = std.ArrayList([]const u8).init(gpa);
defer system_libs.deinit(); defer system_libs.deinit();
@ -283,7 +283,11 @@ fn buildOutputType(
i += 1; i += 1;
target_mcpu = args[i]; target_mcpu = args[i];
} else if (mem.eql(u8, arg, "--c")) { } else if (mem.eql(u8, arg, "--c")) {
cbe = true; if (object_format) |old| {
std.debug.print("attempted to override object format {} with C\n", .{old});
process.exit(1);
}
object_format = .c;
} else if (mem.startsWith(u8, arg, "-mcpu=")) { } else if (mem.startsWith(u8, arg, "-mcpu=")) {
target_mcpu = arg["-mcpu=".len..]; target_mcpu = arg["-mcpu=".len..];
} else if (mem.eql(u8, arg, "--dynamic-linker")) { } else if (mem.eql(u8, arg, "--dynamic-linker")) {
@ -417,7 +421,6 @@ fn buildOutputType(
else => |e| return e, else => |e| return e,
}; };
const object_format: ?std.builtin.ObjectFormat = null;
var target_info = try std.zig.system.NativeTargetInfo.detect(gpa, cross_target); var target_info = try std.zig.system.NativeTargetInfo.detect(gpa, cross_target);
if (target_info.cpu_detection_unimplemented) { if (target_info.cpu_detection_unimplemented) {
// TODO We want to just use detected_info.target but implementing // TODO We want to just use detected_info.target but implementing
@ -436,7 +439,7 @@ fn buildOutputType(
std.debug.print("-fno-emit-bin not supported yet", .{}); std.debug.print("-fno-emit-bin not supported yet", .{});
process.exit(1); process.exit(1);
}, },
.yes_default_path => if (cbe) .yes_default_path => if (object_format != null and object_format.? == .c)
try std.fmt.allocPrint(arena, "{}.c", .{root_name}) try std.fmt.allocPrint(arena, "{}.c", .{root_name})
else else
try std.zig.binNameAlloc(arena, root_name, target_info.target, output_mode, link_mode), try std.zig.binNameAlloc(arena, root_name, target_info.target, output_mode, link_mode),
@ -470,7 +473,6 @@ fn buildOutputType(
.object_format = object_format, .object_format = object_format,
.optimize_mode = build_mode, .optimize_mode = build_mode,
.keep_source_files_loaded = zir_out_path != null, .keep_source_files_loaded = zir_out_path != null,
.cbe = cbe,
}); });
defer module.deinit(); defer module.deinit();

View File

@ -444,7 +444,7 @@ pub const TestContext = struct {
.bin_file_path = bin_name, .bin_file_path = bin_name,
.root_pkg = root_pkg, .root_pkg = root_pkg,
.keep_source_files_loaded = true, .keep_source_files_loaded = true,
.cbe = case.cbe, .object_format = if (case.cbe) .c else null,
}); });
defer module.deinit(); defer module.deinit();