tony / oxwm

Branch: 80aad5c
Switch branches/tags
master
tonybanters rmeoved root_module path for zig build since linkLibrary already propagates include paths to the exe module tree. the above fix is for test modules explicitly. 80aad5c 2026-03-25
222 lines · 7.7 KB Raw Blame History
1 const std = @import("std");
2 const zon = @import("build.zig.zon");
3
4 pub fn build(b: *std.Build) void {
5 const target = b.standardTargetOptions(.{});
6 const optimize = b.standardOptimizeOption(.{});
7 const lua_dep = b.dependency("lua", .{});
8 const lua_headers = lua_dep.path("src/");
9
10 const exe = b.addExecutable(.{
11 .name = "oxwm",
12 .root_module = b.createModule(.{
13 .root_source_file = b.path("src/main.zig"),
14 .target = target,
15 .optimize = optimize,
16 }),
17 });
18
19 const exe_options = b.addOptions();
20 exe_options.addOption([]const u8, "version", zon.version);
21 exe.root_module.addOptions("build_options", exe_options);
22
23 exe.root_module.addAnonymousImport("templates/config.lua", .{
24 .root_source_file = b.path("templates/config.lua"),
25 });
26
27 exe.use_lld = false;
28
29 const lua = buildLua(b, lua_dep, target, optimize);
30 exe.linkLibrary(lua);
31 exe.linkSystemLibrary("X11");
32 exe.linkSystemLibrary("Xinerama");
33 exe.linkSystemLibrary("Xft");
34 exe.linkSystemLibrary("fontconfig");
35 exe.linkLibC();
36
37 b.installArtifact(exe);
38
39 const run_step = b.step("run", "Run oxwm");
40 const run_cmd = b.addRunArtifact(exe);
41 run_step.dependOn(&run_cmd.step);
42 run_cmd.step.dependOn(b.getInstallStep());
43 if (b.args) |args| {
44 run_cmd.addArgs(args);
45 }
46
47 const test_step = b.step("test", "Run unit tests");
48 const unit_tests = b.addTest(.{
49 .root_module = b.createModule(.{
50 .root_source_file = b.path("tests/main_tests.zig"),
51 .target = target,
52 .optimize = optimize,
53 }),
54 });
55 unit_tests.use_lld = false;
56 test_step.dependOn(&b.addRunArtifact(unit_tests).step);
57
58 const src_main_unit_tests = b.addTest(.{
59 .root_module = b.createModule(.{
60 .root_source_file = b.path("src/main.zig"),
61 .target = target,
62 .optimize = optimize,
63 }),
64 });
65 src_main_unit_tests.use_lld = false;
66 src_main_unit_tests.root_module.addIncludePath(lua_headers);
67 src_main_unit_tests.linkLibrary(lua);
68 src_main_unit_tests.linkSystemLibrary("X11");
69 src_main_unit_tests.linkSystemLibrary("Xinerama");
70 src_main_unit_tests.linkSystemLibrary("Xft");
71 src_main_unit_tests.linkSystemLibrary("fontconfig");
72 src_main_unit_tests.linkLibC();
73 test_step.dependOn(&b.addRunArtifact(src_main_unit_tests).step);
74
75 const lua_config_tests = b.addTest(.{
76 .root_module = b.createModule(.{
77 .root_source_file = b.path("tests/lua_config_tests.zig"),
78 .target = target,
79 .optimize = optimize,
80 }),
81 });
82 lua_config_tests.use_lld = false;
83 lua_config_tests.root_module.addIncludePath(lua_headers);
84 const lua_config_module = b.createModule(.{
85 .root_source_file = b.path("src/config/lua.zig"),
86 .target = target,
87 .optimize = optimize,
88 });
89 lua_config_module.addIncludePath(lua_headers);
90 lua_config_tests.root_module.addImport("lua", lua_config_module);
91 lua_config_tests.linkLibrary(lua);
92 lua_config_tests.linkLibC();
93 test_step.dependOn(&b.addRunArtifact(lua_config_tests).step);
94
95 const xephyr_step = b.step("xephyr", "Run in Xephyr (1280x800 on :2)");
96 xephyr_step.dependOn(&addXephyrRun(b, exe, false).step);
97
98 const xephyr_multi_step = b.step("xephyr-multi", "Run in Xephyr multi-monitor on :2");
99 xephyr_multi_step.dependOn(&addXephyrRun(b, exe, true).step);
100
101 const multimon_step = b.step("multimon", "Alias for xephyr-multi");
102 multimon_step.dependOn(&addXephyrRun(b, exe, true).step);
103
104 const xwayland_step = b.step("xwayland", "Run in Xwayland on :2");
105 xwayland_step.dependOn(&addXwaylandRun(b, exe).step);
106
107 const kill_step = b.step("kill", "Kill Xephyr and oxwm");
108 kill_step.dependOn(&b.addSystemCommand(&.{ "sh", "-c", "pkill -9 Xephyr || true; pkill -9 oxwm || true" }).step);
109
110 const fmt_step = b.step("fmt", "Format source files");
111 fmt_step.dependOn(&b.addFmt(.{ .paths = &.{"src/"} }).step);
112
113 const clean_step = b.step("clean", "Remove build artifacts");
114 clean_step.dependOn(&b.addSystemCommand(&.{ "rm", "-rf", "zig-out", ".zig-cache" }).step);
115
116 const install_step = b.step("install-system", "Install oxwm system-wide (requires sudo)");
117 install_step.dependOn(b.getInstallStep());
118 install_step.dependOn(&b.addSystemCommand(&.{
119 "sudo", "sh", "-c",
120 "cp zig-out/bin/oxwm /usr/bin/oxwm && " ++
121 "chmod +x /usr/bin/oxwm && " ++
122 "mkdir -p /usr/share/xsessions && " ++
123 "cp resources/oxwm.desktop /usr/share/xsessions/oxwm.desktop && " ++
124 "mkdir -p /usr/share/man/man1 && " ++
125 "cp resources/oxwm.1 /usr/share/man/man1/oxwm.1 && " ++
126 "mkdir -p /usr/share/oxwm && " ++
127 "cp templates/oxwm.lua /usr/share/oxwm/oxwm.lua && " ++
128 "echo 'oxwm installed to /usr/bin/oxwm'",
129 }).step);
130
131 const uninstall_step = b.step("uninstall-system", "Uninstall oxwm from system");
132 uninstall_step.dependOn(&b.addSystemCommand(&.{
133 "sudo", "sh", "-c",
134 "rm -f /usr/bin/oxwm /usr/share/xsessions/oxwm.desktop /usr/share/man/man1/oxwm.1 && " ++
135 "rm -rf /usr/share/oxwm && " ++
136 "echo 'oxwm uninstalled (config at ~/.config/oxwm preserved)'",
137 }).step);
138 }
139
140 fn addXephyrRun(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run {
141 const kill_cmd = if (multimon)
142 "pkill -9 Xephyr || true; Xephyr +xinerama -glamor -screen 640x480 -screen 640x480 :2 & sleep 1"
143 else
144 "pkill -9 Xephyr || true; Xephyr -screen 1280x800 :2 & sleep 1";
145
146 const setup = b.addSystemCommand(&.{ "sh", "-c", kill_cmd });
147
148 const run_wm = b.addRunArtifact(exe);
149 run_wm.step.dependOn(&setup.step);
150 run_wm.setEnvironmentVariable("DISPLAY", ":2");
151 run_wm.addArgs(&.{ "-c", "resources/test-config.lua" });
152
153 return run_wm;
154 }
155
156 fn addXwaylandRun(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step.Run {
157 const cmd = "Xwayland -retro -noreset :2 & sleep 1";
158
159 const setup = b.addSystemCommand(&.{ "sh", "-c", cmd });
160
161 const run_wm = b.addRunArtifact(exe);
162 run_wm.step.dependOn(&setup.step);
163 run_wm.setEnvironmentVariable("DISPLAY", ":2");
164
165 return run_wm;
166 }
167
168 fn buildLua(b: *std.Build, lua_dep: *std.Build.Dependency, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
169 const lua_src = lua_dep;
170 const lua = b.addLibrary(.{
171 .name = "lua",
172 .root_module = b.createModule(.{
173 .target = target,
174 .optimize = optimize,
175 }),
176 });
177 lua.linkLibC();
178 lua.addIncludePath(lua_src.path("src/"));
179 lua.addCSourceFiles(.{
180 .root = lua_src.path("src/"),
181 .files = &.{
182 "lapi.c",
183 "lauxlib.c",
184 "lbaselib.c",
185 "lcode.c",
186 "lcorolib.c",
187 "lctype.c",
188 "ldblib.c",
189 "ldebug.c",
190 "ldo.c",
191 "ldump.c",
192 "lfunc.c",
193 "lgc.c",
194 "linit.c",
195 "liolib.c",
196 "llex.c",
197 "lmathlib.c",
198 "lmem.c",
199 "loadlib.c",
200 "lobject.c",
201 "lopcodes.c",
202 "loslib.c",
203 "lparser.c",
204 "lstate.c",
205 "lstring.c",
206 "lstrlib.c",
207 "ltable.c",
208 "ltablib.c",
209 "ltm.c",
210 "lundump.c",
211 "lutf8lib.c",
212 "lvm.c",
213 "lzio.c",
214 },
215 });
216 lua.installHeader(lua_src.path("src/lua.h"), "lua.h");
217 lua.installHeader(lua_src.path("src/lualib.h"), "lualib.h");
218 lua.installHeader(lua_src.path("src/lauxlib.h"), "lauxlib.h");
219 lua.installHeader(lua_src.path("src/luaconf.h"), "luaconf.h");
220
221 return lua;
222 }