tony / oxwm

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