Skip to content

Commit e526290

Browse files
committed
Mos Build is now a namespace not a type
Having a separate type for Mos Application building seemed wrong as the associated functions are just a wrapper around the std.Build, not something separate. * Rename MosBuild.zig to mos_build.zig for the same reason.
1 parent 1822755 commit e526290

File tree

4 files changed

+127
-106
lines changed

4 files changed

+127
-106
lines changed

src/apps/zapps/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
find_package(Zig 0.15.1 EXACT REQUIRED)
22

3+
set(ZIG_COMMON_SOURCE_FILES
4+
${CMAKE_CURRENT_LIST_DIR}/build.zig
5+
${CMAKE_CURRENT_LIST_DIR}/mos_build.zig
6+
)
7+
38
add_zig_build(
49
NAME hello
510
OUTPUTS hello
611
SOURCES
7-
${CMAKE_CURRENT_LIST_DIR}/build.zig
8-
${CMAKE_CURRENT_LIST_DIR}/MosBuild.zig
12+
${ZIG_COMMON_SOURCE_FILES}
913
${CMAKE_CURRENT_LIST_DIR}/hello.zig
1014
DEPENDS
1115
cm

src/apps/zapps/MosBuild.zig

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/apps/zapps/build.zig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
const std = @import("std");
2-
const MosBuild = @import("MosBuild.zig");
2+
const mos = @import("mos_build.zig");
33

44
pub fn build(b: *std.Build) !void {
5-
const mos_build = try MosBuild.init(b);
6-
const hello_install = mos_build.addExecutable("hello", "./hello.zig");
7-
b.getInstallStep().dependOn(hello_install);
5+
const optimize = b.standardOptimizeOption(.{});
6+
const exe = mos.addExecutable(b, "hello", .{
7+
.optimize = optimize,
8+
.root_src_file = "./hello.zig",
9+
.options = try mos.addDefaultOptions(b),
10+
.target = b.resolveTargetQuery(mos.i686_target_query),
11+
});
12+
b.getInstallStep().dependOn(exe);
813
}

src/apps/zapps/mos_build.zig

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)