Commit 2c882b2e65

pixelherodev <pleasantatk@gmail.com>
2020-07-13 04:56:31
CBE: Make C an ObjectFormat instead of a special bool (#5849)
1 parent dff1ac1
Changed files (5)
lib/std/target.zig
@@ -435,6 +435,7 @@ pub const Target = struct {
         elf,
         macho,
         wasm,
+        c,
     };
 
     pub const SubSystem = enum {
src-self-hosted/link.zig
@@ -22,7 +22,6 @@ pub const Options = struct {
     /// Used for calculating how much space to reserve for executable program code in case
     /// the binary file deos not already have such a section.
     program_code_size_hint: u64 = 256 * 1024,
-    cbe: bool = false,
 };
 
 /// Attempts incremental linking, if the file already exists.
@@ -35,10 +34,11 @@ pub fn openBinFilePath(
     sub_path: []const u8,
     options: Options,
 ) !*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();
 
-    if (options.cbe) {
+    if (cbe) {
         var bin_file = try allocator.create(File.C);
         errdefer allocator.destroy(bin_file);
         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,
     }
     switch (options.object_format) {
+        .c => unreachable,
         .unknown => unreachable, // TODO remove this tag from the enum
         .coff => return error.TODOImplementWritingCOFF,
         .elf => {},
@@ -1632,6 +1633,7 @@ fn openBinFileInner(allocator: *Allocator, file: fs.File, options: Options) !Fil
     }
     switch (options.object_format) {
         .unknown => unreachable, // TODO remove this tag from the enum
+        .c => unreachable,
         .coff => return error.IncrFailed,
         .elf => {},
         .macho => return error.IncrFailed,
src-self-hosted/main.zig
@@ -194,8 +194,8 @@ fn buildOutputType(
     var emit_zir: Emit = .no;
     var target_arch_os_abi: []const u8 = "native";
     var target_mcpu: ?[]const u8 = null;
-    var cbe: bool = false;
     var target_dynamic_linker: ?[]const u8 = null;
+    var object_format: ?std.builtin.ObjectFormat = null;
 
     var system_libs = std.ArrayList([]const u8).init(gpa);
     defer system_libs.deinit();
@@ -283,7 +283,11 @@ fn buildOutputType(
                     i += 1;
                     target_mcpu = args[i];
                 } 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=")) {
                     target_mcpu = arg["-mcpu=".len..];
                 } else if (mem.eql(u8, arg, "--dynamic-linker")) {
@@ -417,7 +421,6 @@ fn buildOutputType(
         else => |e| return e,
     };
 
-    const object_format: ?std.builtin.ObjectFormat = null;
     var target_info = try std.zig.system.NativeTargetInfo.detect(gpa, cross_target);
     if (target_info.cpu_detection_unimplemented) {
         // 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", .{});
             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})
         else
             try std.zig.binNameAlloc(arena, root_name, target_info.target, output_mode, link_mode),
@@ -470,7 +473,6 @@ fn buildOutputType(
         .object_format = object_format,
         .optimize_mode = build_mode,
         .keep_source_files_loaded = zir_out_path != null,
-        .cbe = cbe,
     });
     defer module.deinit();
 
src-self-hosted/Module.zig
@@ -723,7 +723,6 @@ pub const InitOptions = struct {
     object_format: ?std.builtin.ObjectFormat = null,
     optimize_mode: std.builtin.Mode = .Debug,
     keep_source_files_loaded: bool = false,
-    cbe: bool = false,
 };
 
 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,
         .link_mode = options.link_mode orelse .Static,
         .object_format = options.object_format orelse options.target.getObjectFormat(),
-        .cbe = options.cbe,
     });
     errdefer bin_file.destroy();
 
src-self-hosted/test.zig
@@ -444,7 +444,7 @@ pub const TestContext = struct {
             .bin_file_path = bin_name,
             .root_pkg = root_pkg,
             .keep_source_files_loaded = true,
-            .cbe = case.cbe,
+            .object_format = if (case.cbe) .c else null,
         });
         defer module.deinit();