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