-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
79 lines (59 loc) · 2.32 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const std = @import("std");
const buildError = error{
InvalidArch,
};
fn libs(b: *std.Build) !std.ArrayList(*std.Build.Module) {
var liblist = std.ArrayList(*std.Build.Module).init(b.allocator);
var dir = try std.fs.cwd().openDir("src/libs/", .{ .iterate = true });
defer dir.close();
var walker = try dir.walk(b.allocator);
defer walker.deinit();
while (try walker.next()) |entry| {
if (std.mem.indexOf(u8, entry.path, "mod.zig") != null) {
const path = try std.fmt.allocPrint(b.allocator, "src/libs/{s}", .{entry.path});
try liblist.append(b.createModule(.{ .root_source_file = b.path(path) }));
}
}
return liblist;
}
fn getFileName(b: *std.Build, path: []const u8) []const u8 {
const pathBuf = b.allocator.dupe(u8, path) catch "";
std.mem.reverse(u8, @constCast(pathBuf));
for (0..path.len, pathBuf) |i, char| {
if (char == '/') {
return path[(path.len - i)..];
}
}
return pathBuf;
}
pub fn build(b: *std.Build) !void {
const cross_target_spec = b.option(std.Target.Cpu.Arch, "arch", "Target Architecture") orelse std.Target.Cpu.Arch.x86_64;
const optimize = b.standardOptimizeOption(.{});
var liblist = try libs(b);
defer liblist.deinit();
const arch = switch (cross_target_spec) {
std.Target.Cpu.Arch.x86_64 => @import("meta/targets/kernel-x86_64.zig"),
else => return error.InvalidArch,
};
const target = arch.getBuildTarget();
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = b.path("src/kernel/core/main.zig"),
.target = b.resolveTargetQuery(target),
.optimize = optimize,
.code_model = .kernel,
});
var modules = std.StringHashMap(*std.Build.Module).init(b.allocator);
defer modules.deinit();
for (liblist.items) |lib| {
const libname = getFileName(b, lib.root_source_file.?.dirname().src_path.sub_path);
try modules.put(libname, lib);
kernel.root_module.addImport(libname, lib);
}
const limine = b.dependency("limine", .{});
kernel.root_module.addImport("limine", limine.module("limine"));
try modules.put("limine", limine.module("limine"));
arch.addBuildOption(b, kernel, modules);
kernel.want_lto = false;
b.installArtifact(kernel);
}