-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
158 lines (135 loc) · 5.98 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const std = @import("std");
const test_device_trees = .{
"qemu_virt_aarch64",
"odroidc4",
"star64",
"maaxboard",
};
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const dtbzig_dep = b.dependency("dtb.zig", .{});
const dtb_module = dtbzig_dep.module("dtb");
const sdf_module = b.addModule("sdf", .{
.root_source_file = b.path("src/mod.zig"),
.target = target,
.optimize = optimize,
});
sdf_module.addImport("dtb", dtb_module);
const dtb_step = b.step("dtbs", "Compile Device Tree Sources into .dtb");
inline for (test_device_trees) |device_tree| {
const dtc_cmd = b.addSystemCommand(&[_][]const u8{
"dtc", "-q", "-I", "dts", "-O", "dtb"
});
const device_tree_path = b.path(b.fmt("dts/{s}.dts", .{ device_tree }));
dtc_cmd.addFileInput(device_tree_path);
dtc_cmd.addFileArg(device_tree_path);
const dtb = dtc_cmd.captureStdOut();
dtb_step.dependOn(&b.addInstallFileWithDir(dtb, .{ .custom = "dtb" }, b.fmt("{s}.dtb", .{ device_tree })).step);
}
const zig_example_step = b.step("zig_example", "Exmaples of using Zig bindings");
const zig_example = b.addExecutable(.{
.name = "zig_example",
.root_source_file = b.path("examples/examples.zig"),
.target = target,
.optimize = optimize,
});
// TODO: should these be runtime options instead?
const zig_example_options = b.addOptions();
zig_example_options.addOptionPath("sddf", b.path("sddf"));
zig_example_options.addOption([]const u8, "dtbs", b.getInstallPath(.{ .custom = "dtb"}, ""));
zig_example_options.addOption([]const u8, "data_output", b.getInstallPath(.prefix, ""));
zig_example.root_module.addOptions("config", zig_example_options);
zig_example.root_module.addImport("sdf", sdf_module);
const zig_example_cmd = b.addRunArtifact(zig_example);
if (b.args) |args| {
zig_example_cmd.addArgs(args);
}
// In case any sDDF configuration files are changed
_ = try zig_example_cmd.step.addDirectoryWatchInput(b.path("sddf"));
zig_example_cmd.step.dependOn(dtb_step);
zig_example_step.dependOn(&zig_example_cmd.step);
const zig_example_install = b.addInstallArtifact(zig_example, .{});
zig_example_step.dependOn(&zig_example_install.step);
const modsdf = b.addModule("sdf", .{ .root_source_file = b.path("src/mod.zig") });
modsdf.addImport("dtb", dtbzig_dep.module("dtb"));
const c_dynamic = b.option(bool, "c-dynamic", "Build C bindings as dynamic library") orelse false;
const csdfgen = blk: {
if (!c_dynamic) {
break :blk b.addStaticLibrary(.{
.name = "csdfgen",
.root_source_file = b.path("src/c/c.zig"),
.target = target,
.optimize = optimize,
});
} else {
break :blk b.addSharedLibrary(.{
.name = "csdfgen",
.root_source_file = b.path("src/c/c.zig"),
.target = target,
.optimize = optimize,
});
}
};
csdfgen.linkLibC();
csdfgen.installHeader(b.path("src/c/sdfgen.h"), "sdfgen.h");
csdfgen.addIncludePath(b.path("src/c"));
csdfgen.root_module.addImport("sdf", modsdf);
b.installArtifact(csdfgen);
const c_step = b.step("c", "Static library for C bindings");
const csdfgen_emit = b.option([]const u8, "csdfgen-emit", "C sdfgen emit") orelse "csdfgen";
c_step.dependOn(&b.addInstallFileWithDir(csdfgen.getEmittedBin(), .lib, csdfgen_emit).step);
c_step.dependOn(&csdfgen.step);
const c_example = b.addExecutable(.{
.name = "c_example",
.target = target,
.optimize = optimize,
});
c_example.addCSourceFile(.{ .file = b.path("examples/examples.c") });
c_example.linkLibrary(csdfgen);
c_example.linkLibC();
const c_example_step = b.step("c_example", "Run example program using C bindings");
const c_example_cmd = b.addRunArtifact(c_example);
// In case any sDDF configuration files are changed
c_example_cmd.addDirectoryArg(b.path("sddf"));
_ = try c_example_cmd.step.addDirectoryWatchInput(b.path("sddf"));
c_example_step.dependOn(&c_example_cmd.step);
const c_example_install = b.addInstallFileWithDir(c_example.getEmittedBin(), .bin, "c_example");
// wasm executable
const wasm_target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});
const wasm = b.addExecutable(.{
.name = "gui_sdfgen",
.root_source_file = b.path("src/gui_sdfgen.zig"),
.target = wasm_target,
.optimize = .Debug,
.strip = false,
});
wasm.root_module.addImport("dtb", dtb_module);
wasm.entry = .disabled;
wasm.root_module.export_symbol_names = &.{ "fetchInitInfo", "jsonToXml" };
const wasm_step = b.step("wasm", "build wasm");
const wasm_install = b.addInstallArtifact(wasm, .{});
wasm_step.dependOn(&wasm_install.step);
const tests = b.addTest(.{
.root_source_file = b.path("src/test.zig"),
.target = target,
.optimize = optimize,
});
const test_options = b.addOptions();
test_options.addOptionPath("c_example", .{ .cwd_relative = b.getInstallPath(.bin, c_example.name) });
test_options.addOptionPath("test_dir", b.path("tests"));
test_options.addOptionPath("sddf", b.path("sddf"));
test_options.addOption([]const u8, "dtb", b.getInstallPath(.{ .custom = "dtb" }, ""));
tests.root_module.addImport("sdf", sdf_module);
tests.root_module.addOptions("config", test_options);
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_tests.step);
run_tests.step.dependOn(&c_example_install.step);
run_tests.step.dependOn(dtb_step);
// In case any sDDF configuration files are changed
_ = try test_step.addDirectoryWatchInput(b.path("sddf"));
}