|
| 1 | +const std = @import("std"); |
| 2 | +const Build = std.Build; |
| 3 | +const Step = Build.Step; |
| 4 | + |
| 5 | +const Self = @This(); |
| 6 | + |
| 7 | +const BuildOptions = struct { |
| 8 | + linker_script_rel_path: []const u8, |
| 9 | + libcm_rel_path: []const u8, |
| 10 | + crta_rel_path: []const u8, |
| 11 | + include_rel_path: []const u8, |
| 12 | + entry_point: []const u8, |
| 13 | +}; |
| 14 | + |
| 15 | +const i686_cpu_model = std.Target.Cpu.Model{ |
| 16 | + .name = "i686", |
| 17 | + .llvm_name = "", |
| 18 | + .features = .empty, |
| 19 | +}; |
| 20 | + |
| 21 | +options: BuildOptions, |
| 22 | +b: *Build, |
| 23 | +optimize: std.builtin.OptimizeMode, |
| 24 | +target: std.Build.ResolvedTarget, |
| 25 | + |
| 26 | +pub fn init(b: *std.Build) !Self { |
| 27 | + return .{ |
| 28 | + .target = b.resolveTargetQuery(.{ |
| 29 | + .abi = .none, |
| 30 | + .cpu_arch = .x86, |
| 31 | + .cpu_model = .{ .explicit = &i686_cpu_model }, |
| 32 | + .os_tag = .freestanding, |
| 33 | + }), |
| 34 | + .optimize = b.standardOptimizeOption(.{}), |
| 35 | + .b = b, |
| 36 | + .options = try getMosBuildOptions(b), |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +fn getMosBuildOptions(b: *Build) !BuildOptions { |
| 41 | + const linker_script_path = b.option([]const u8, "LinkerScriptPath", "Absolute path to linker script") orelse ""; // Must provide 'LinkerScriptPath' |
| 42 | + const libcm_path = b.option([]const u8, "LibCMPath", "Absolute path to libcm library") orelse ""; // Must provide 'LibCMPath' |
| 43 | + const crt_path = b.option([]const u8, "CRTPath", "Absolute path to crt.o") orelse ""; // Must provide 'CRTPath' |
| 44 | + const include_path = b.option([]const u8, "CInludePath", "Absolute path to C include root dir") orelse ""; // Must provide 'CInludePath' |
| 45 | + const entry_point = b.option([]const u8, "EntryPoint", "Entry point of user application") orelse ""; // Must provide 'EntryPoint' |
| 46 | + |
| 47 | + return .{ |
| 48 | + .crta_rel_path = try std.fs.path.relative(b.allocator, ".", crt_path), |
| 49 | + .include_rel_path = try std.fs.path.relative(b.allocator, ".", include_path), |
| 50 | + .libcm_rel_path = try std.fs.path.relative(b.allocator, ".", libcm_path), |
| 51 | + .linker_script_rel_path = try std.fs.path.relative(b.allocator, ".", linker_script_path), |
| 52 | + .entry_point = entry_point, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +pub fn addExecutable(self: Self, comptime name: []const u8, comptime root_src_file: []const u8) *Step { |
| 57 | + const b = self.b; |
| 58 | + const exe = self.elf_compilation(name, root_src_file); |
| 59 | + const exe_install = b.addInstallArtifact(exe, .{}); |
| 60 | + |
| 61 | + const flatten_output_file = name ++ ".flt"; |
| 62 | + const objcopy_run = b.addSystemCommand(&.{ "objcopy", "-O", "binary" }); |
| 63 | + objcopy_run.addArg(b.getInstallPath(.bin, exe.name)); |
| 64 | + const flatten_output_path = objcopy_run.addOutputFileArg(flatten_output_file); |
| 65 | + |
| 66 | + const flatten_install = b.addInstallFileWithDir(flatten_output_path, .bin, flatten_output_file); |
| 67 | + |
| 68 | + objcopy_run.step.dependOn(&exe_install.step); |
| 69 | + return &flatten_install.step; |
| 70 | +} |
| 71 | + |
| 72 | +fn elf_compilation(self: Self, name: []const u8, root_src_file: []const u8) *Step.Compile { |
| 73 | + const b = self.b; |
| 74 | + const exe = b.addExecutable(.{ |
| 75 | + .name = name, |
| 76 | + .root_module = b.createModule(.{ |
| 77 | + .root_source_file = b.path(root_src_file), |
| 78 | + .target = self.target, |
| 79 | + .optimize = self.optimize, |
| 80 | + .omit_frame_pointer = false, |
| 81 | + .red_zone = false, |
| 82 | + .unwind_tables = .none, |
| 83 | + .stack_protector = false, |
| 84 | + .single_threaded = true, |
| 85 | + .pic = false, |
| 86 | + }), |
| 87 | + }); |
| 88 | + |
| 89 | + exe.root_module.addLibraryPath(b.path(self.options.libcm_rel_path)); |
| 90 | + exe.root_module.linkSystemLibrary("cm", .{}); |
| 91 | + exe.addObjectFile(b.path(self.options.crta_rel_path)); |
| 92 | + exe.addIncludePath(b.path(self.options.include_rel_path)); |
| 93 | + |
| 94 | + exe.linker_script = b.path(self.options.linker_script_rel_path); |
| 95 | + exe.link_gc_sections = true; |
| 96 | + exe.link_eh_frame_hdr = false; |
| 97 | + exe.entry = .{ .symbol_name = self.options.entry_point }; |
| 98 | + |
| 99 | + return exe; |
| 100 | +} |
0 commit comments