tony / oxwm

Branch: 9fac3c6
Switch branches/tags
master
emzywastaken generate default config if config file is not found/set 3f07d04 2026-02-16
149 lines · 5.6 KB Raw Blame History
1 const std = @import("std");
2
3 pub fn build(b: *std.Build) void {
4 const target = b.standardTargetOptions(.{});
5 const optimize = b.standardOptimizeOption(.{});
6
7 const exe = b.addExecutable(.{
8 .name = "oxwm",
9 .root_module = b.createModule(.{
10 .root_source_file = b.path("src/main.zig"),
11 .target = target,
12 .optimize = optimize,
13 }),
14 });
15
16 exe.root_module.addAnonymousImport("templates/config.lua", .{
17 .root_source_file = b.path("templates/config.lua"),
18 });
19
20 exe.linkSystemLibrary("lua5.4");
21 exe.linkSystemLibrary("X11");
22 exe.linkSystemLibrary("Xinerama");
23 exe.linkSystemLibrary("Xft");
24 exe.linkSystemLibrary("fontconfig");
25 exe.linkLibC();
26
27 b.installArtifact(exe);
28
29 const run_step = b.step("run", "Run oxwm");
30 const run_cmd = b.addRunArtifact(exe);
31 run_step.dependOn(&run_cmd.step);
32 run_cmd.step.dependOn(b.getInstallStep());
33 if (b.args) |args| {
34 run_cmd.addArgs(args);
35 }
36
37 const test_step = b.step("test", "Run unit tests");
38 const unit_tests = b.addTest(.{
39 .root_module = b.createModule(.{
40 .root_source_file = b.path("tests/main_tests.zig"),
41 .target = target,
42 .optimize = optimize,
43 }),
44 });
45 test_step.dependOn(&b.addRunArtifact(unit_tests).step);
46
47 const src_main_unit_tests = b.addTest(.{
48 .root_module = b.createModule(.{
49 .root_source_file = b.path("src/main.zig"),
50 .target = target,
51 .optimize = optimize,
52 }),
53 });
54 src_main_unit_tests.linkSystemLibrary("lua5.4");
55 src_main_unit_tests.linkSystemLibrary("X11");
56 src_main_unit_tests.linkSystemLibrary("Xinerama");
57 src_main_unit_tests.linkSystemLibrary("Xft");
58 src_main_unit_tests.linkSystemLibrary("fontconfig");
59 src_main_unit_tests.linkLibC();
60 test_step.dependOn(&b.addRunArtifact(src_main_unit_tests).step);
61
62 const lua_config_tests = b.addTest(.{
63 .root_module = b.createModule(.{
64 .root_source_file = b.path("tests/lua_config_tests.zig"),
65 .target = target,
66 .optimize = optimize,
67 }),
68 });
69 lua_config_tests.root_module.addImport("lua", b.createModule(.{
70 .root_source_file = b.path("src/config/lua.zig"),
71 .target = target,
72 .optimize = optimize,
73 }));
74 lua_config_tests.linkSystemLibrary("lua5.4");
75 lua_config_tests.linkLibC();
76 test_step.dependOn(&b.addRunArtifact(lua_config_tests).step);
77
78 const xephyr_step = b.step("xephyr", "Run in Xephyr (1280x800 on :2)");
79 xephyr_step.dependOn(&add_xephyr_run(b, exe, false).step);
80
81 const xephyr_multi_step = b.step("xephyr-multi", "Run in Xephyr multi-monitor on :2");
82 xephyr_multi_step.dependOn(&add_xephyr_run(b, exe, true).step);
83
84 const multimon_step = b.step("multimon", "Alias for xephyr-multi");
85 multimon_step.dependOn(&add_xephyr_run(b, exe, true).step);
86
87 const xwayland_step = b.step("xwayland", "Run in Xwayland on :2");
88 xwayland_step.dependOn(&add_xwayland_run(b, exe).step);
89
90 const kill_step = b.step("kill", "Kill Xephyr and oxwm");
91 kill_step.dependOn(&b.addSystemCommand(&.{ "sh", "-c", "pkill -9 Xephyr || true; pkill -9 oxwm || true" }).step);
92
93 const fmt_step = b.step("fmt", "Format source files");
94 fmt_step.dependOn(&b.addFmt(.{ .paths = &.{"src/"} }).step);
95
96 const clean_step = b.step("clean", "Remove build artifacts");
97 clean_step.dependOn(&b.addSystemCommand(&.{ "rm", "-rf", "zig-out", ".zig-cache" }).step);
98
99 const install_step = b.step("install-system", "Install oxwm system-wide (requires sudo)");
100 install_step.dependOn(b.getInstallStep());
101 install_step.dependOn(&b.addSystemCommand(&.{
102 "sudo", "sh", "-c",
103 "cp zig-out/bin/oxwm /usr/bin/oxwm && " ++
104 "chmod +x /usr/bin/oxwm && " ++
105 "mkdir -p /usr/share/xsessions && " ++
106 "cp resources/oxwm.desktop /usr/share/xsessions/oxwm.desktop && " ++
107 "mkdir -p /usr/share/man/man1 && " ++
108 "cp resources/oxwm.1 /usr/share/man/man1/oxwm.1 && " ++
109 "mkdir -p /usr/share/oxwm && " ++
110 "cp templates/oxwm.lua /usr/share/oxwm/oxwm.lua && " ++
111 "echo 'oxwm installed to /usr/bin/oxwm'",
112 }).step);
113
114 const uninstall_step = b.step("uninstall-system", "Uninstall oxwm from system");
115 uninstall_step.dependOn(&b.addSystemCommand(&.{
116 "sudo", "sh", "-c",
117 "rm -f /usr/bin/oxwm /usr/share/xsessions/oxwm.desktop /usr/share/man/man1/oxwm.1 && " ++
118 "rm -rf /usr/share/oxwm && " ++
119 "echo 'oxwm uninstalled (config at ~/.config/oxwm preserved)'",
120 }).step);
121 }
122
123 fn add_xephyr_run(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run {
124 const kill_cmd = if (multimon)
125 "pkill -9 Xephyr || true; Xephyr +xinerama -glamor -screen 640x480 -screen 640x480 :2 & sleep 1"
126 else
127 "pkill -9 Xephyr || true; Xephyr -screen 1280x800 :2 & sleep 1";
128
129 const setup = b.addSystemCommand(&.{ "sh", "-c", kill_cmd });
130
131 const run_wm = b.addRunArtifact(exe);
132 run_wm.step.dependOn(&setup.step);
133 run_wm.setEnvironmentVariable("DISPLAY", ":2");
134 run_wm.addArgs(&.{ "-c", "resources/test-config.lua" });
135
136 return run_wm;
137 }
138
139 fn add_xwayland_run(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step.Run {
140 const cmd = "Xwayland -retro -noreset :2 & sleep 1";
141
142 const setup = b.addSystemCommand(&.{ "sh", "-c", cmd });
143
144 const run_wm = b.addRunArtifact(exe);
145 run_wm.step.dependOn(&setup.step);
146 run_wm.setEnvironmentVariable("DISPLAY", ":2");
147
148 return run_wm;
149 }