| 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 no_gestures = b.option(bool, "no_gestures", "Build without libinput touchpad gestures (implied on BSD targets)") orelse false; |
| 11 | const gestures = !no_gestures and !isBsd(target.result.os.tag); |
| 12 | |
| 13 | const lua = buildLua(b, lua_dep, target, optimize); |
| 14 | |
| 15 | const exe_options = b.addOptions(); |
| 16 | exe_options.addOption([]const u8, "version", zon.version); |
| 17 | exe_options.addOption(bool, "gestures", gestures); |
| 18 | |
| 19 | const exe = addOxwm(b, target, optimize, lua, exe_options, gestures); |
| 20 | b.installArtifact(exe); |
| 21 | |
| 22 | const bsd_options = b.addOptions(); |
| 23 | bsd_options.addOption([]const u8, "version", zon.version); |
| 24 | bsd_options.addOption(bool, "gestures", false); |
| 25 | const bsd_exe = addOxwm(b, target, optimize, lua, bsd_options, false); |
| 26 | const bsd_install = b.addInstallArtifact(bsd_exe, .{}); |
| 27 | for ([_][]const u8{ "openbsd", "freebsd", "netbsd", "bsd" }) |name| { |
| 28 | const bsd_step = b.step(name, "Build oxwm without Linux-only features (libinput gestures)"); |
| 29 | bsd_step.dependOn(&bsd_install.step); |
| 30 | } |
| 31 | |
| 32 | const run_step = b.step("run", "Run oxwm"); |
| 33 | const run_cmd = b.addRunArtifact(exe); |
| 34 | run_step.dependOn(&run_cmd.step); |
| 35 | run_cmd.step.dependOn(b.getInstallStep()); |
| 36 | if (b.args) |args| { |
| 37 | run_cmd.addArgs(args); |
| 38 | } |
| 39 | |
| 40 | const test_step = b.step("test", "Run unit tests"); |
| 41 | const unit_tests = b.addTest(.{ |
| 42 | .root_module = b.createModule(.{ |
| 43 | .root_source_file = b.path("tests/main_tests.zig"), |
| 44 | .target = target, |
| 45 | .optimize = optimize, |
| 46 | }), |
| 47 | }); |
| 48 | unit_tests.use_lld = false; |
| 49 | test_step.dependOn(&b.addRunArtifact(unit_tests).step); |
| 50 | |
| 51 | const src_main_unit_tests = b.addTest(.{ |
| 52 | .root_module = b.createModule(.{ |
| 53 | .root_source_file = b.path("src/main.zig"), |
| 54 | .target = target, |
| 55 | .optimize = optimize, |
| 56 | .link_libc = true, |
| 57 | }), |
| 58 | }); |
| 59 | src_main_unit_tests.use_lld = false; |
| 60 | src_main_unit_tests.root_module.addIncludePath(lua_headers); |
| 61 | src_main_unit_tests.root_module.linkLibrary(lua); |
| 62 | src_main_unit_tests.root_module.linkSystemLibrary("X11", .{}); |
| 63 | src_main_unit_tests.root_module.linkSystemLibrary("Xinerama", .{}); |
| 64 | src_main_unit_tests.root_module.linkSystemLibrary("Xft", .{}); |
| 65 | src_main_unit_tests.root_module.linkSystemLibrary("fontconfig", .{}); |
| 66 | src_main_unit_tests.root_module.addOptions("build_options", exe_options); |
| 67 | if (gestures) { |
| 68 | src_main_unit_tests.root_module.linkSystemLibrary("libinput", .{}); |
| 69 | src_main_unit_tests.root_module.linkSystemLibrary("libudev", .{}); |
| 70 | } |
| 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 | .link_libc = true, |
| 79 | }), |
| 80 | }); |
| 81 | lua_config_tests.use_lld = false; |
| 82 | lua_config_tests.root_module.addIncludePath(lua_headers); |
| 83 | const lua_config_module = b.createModule(.{ |
| 84 | .root_source_file = b.path("src/config/lua.zig"), |
| 85 | .target = target, |
| 86 | .optimize = optimize, |
| 87 | }); |
| 88 | lua_config_module.addIncludePath(lua_headers); |
| 89 | lua_config_tests.root_module.addImport("lua", lua_config_module); |
| 90 | lua_config_tests.root_module.linkLibrary(lua); |
| 91 | test_step.dependOn(&b.addRunArtifact(lua_config_tests).step); |
| 92 | |
| 93 | const xephyr_step = b.step("xephyr", "Run in Xephyr (1280x800 on :2)"); |
| 94 | xephyr_step.dependOn(&addXephyrRun(b, exe, false).step); |
| 95 | |
| 96 | const xephyr_multi_step = b.step("xephyr-multi", "Run in Xephyr multi-monitor on :2"); |
| 97 | xephyr_multi_step.dependOn(&addXephyrRun(b, exe, true).step); |
| 98 | |
| 99 | const multimon_step = b.step("multimon", "Alias for xephyr-multi"); |
| 100 | multimon_step.dependOn(&addXephyrRun(b, exe, true).step); |
| 101 | |
| 102 | const xwayland_step = b.step("xwayland", "Run in Xwayland on :2"); |
| 103 | xwayland_step.dependOn(&addXwaylandRun(b, exe).step); |
| 104 | |
| 105 | const kill_step = b.step("kill", "Kill Xephyr and oxwm"); |
| 106 | kill_step.dependOn(&b.addSystemCommand(&.{ "sh", "-c", "pkill -9 Xephyr || true; pkill -9 oxwm || true" }).step); |
| 107 | |
| 108 | const fmt_step = b.step("fmt", "Format source files"); |
| 109 | fmt_step.dependOn(&b.addFmt(.{ .paths = &.{"src/"} }).step); |
| 110 | |
| 111 | const clean_step = b.step("clean", "Remove build artifacts"); |
| 112 | clean_step.dependOn(&b.addSystemCommand(&.{ "rm", "-rf", "zig-out", ".zig-cache" }).step); |
| 113 | |
| 114 | const install_step = b.step("install-system", "Install oxwm system-wide (requires sudo)"); |
| 115 | install_step.dependOn(b.getInstallStep()); |
| 116 | install_step.dependOn(&b.addSystemCommand(&.{ |
| 117 | "sudo", "sh", "-c", |
| 118 | "cp zig-out/bin/oxwm /usr/bin/oxwm && " ++ |
| 119 | "chmod +x /usr/bin/oxwm && " ++ |
| 120 | "mkdir -p /usr/share/xsessions && " ++ |
| 121 | "cp resources/oxwm.desktop /usr/share/xsessions/oxwm.desktop && " ++ |
| 122 | "mkdir -p /usr/share/man/man1 && " ++ |
| 123 | "cp resources/oxwm.1 /usr/share/man/man1/oxwm.1 && " ++ |
| 124 | "mkdir -p /usr/share/oxwm && " ++ |
| 125 | "cp templates/oxwm.lua /usr/share/oxwm/oxwm.lua && " ++ |
| 126 | "echo 'oxwm installed to /usr/bin/oxwm'", |
| 127 | }).step); |
| 128 | |
| 129 | const uninstall_step = b.step("uninstall-system", "Uninstall oxwm from system"); |
| 130 | uninstall_step.dependOn(&b.addSystemCommand(&.{ |
| 131 | "sudo", "sh", "-c", |
| 132 | "rm -f /usr/bin/oxwm /usr/share/xsessions/oxwm.desktop /usr/share/man/man1/oxwm.1 && " ++ |
| 133 | "rm -rf /usr/share/oxwm && " ++ |
| 134 | "echo 'oxwm uninstalled (config at ~/.config/oxwm preserved)'", |
| 135 | }).step); |
| 136 | } |
| 137 | |
| 138 | fn isBsd(tag: std.Target.Os.Tag) bool { |
| 139 | return switch (tag) { |
| 140 | .freebsd, .openbsd, .netbsd, .dragonfly => true, |
| 141 | else => false, |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | fn addOxwm( |
| 146 | b: *std.Build, |
| 147 | target: std.Build.ResolvedTarget, |
| 148 | optimize: std.builtin.OptimizeMode, |
| 149 | lua: *std.Build.Step.Compile, |
| 150 | options: *std.Build.Step.Options, |
| 151 | gestures: bool, |
| 152 | ) *std.Build.Step.Compile { |
| 153 | const exe = b.addExecutable(.{ |
| 154 | .name = "oxwm", |
| 155 | .root_module = b.createModule(.{ |
| 156 | .root_source_file = b.path("src/main.zig"), |
| 157 | .target = target, |
| 158 | .optimize = optimize, |
| 159 | .link_libc = true, |
| 160 | }), |
| 161 | }); |
| 162 | |
| 163 | exe.root_module.addOptions("build_options", options); |
| 164 | exe.root_module.addAnonymousImport("templates/config.lua", .{ |
| 165 | .root_source_file = b.path("templates/config.lua"), |
| 166 | }); |
| 167 | |
| 168 | exe.use_lld = false; |
| 169 | |
| 170 | exe.root_module.linkLibrary(lua); |
| 171 | exe.root_module.linkSystemLibrary("X11", .{}); |
| 172 | exe.root_module.linkSystemLibrary("Xinerama", .{}); |
| 173 | exe.root_module.linkSystemLibrary("Xft", .{}); |
| 174 | exe.root_module.linkSystemLibrary("fontconfig", .{}); |
| 175 | if (gestures) { |
| 176 | exe.root_module.linkSystemLibrary("libinput", .{}); |
| 177 | exe.root_module.linkSystemLibrary("libudev", .{}); |
| 178 | } |
| 179 | return exe; |
| 180 | } |
| 181 | |
| 182 | fn addXephyrRun(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run { |
| 183 | const kill_cmd = if (multimon) |
| 184 | "pkill -9 Xephyr || true; Xephyr +xinerama -glamor -screen 640x480 -screen 640x480 :2 & sleep 1" |
| 185 | else |
| 186 | "pkill -9 Xephyr || true; Xephyr -screen 1280x800 :2 & sleep 1"; |
| 187 | |
| 188 | const setup = b.addSystemCommand(&.{ "sh", "-c", kill_cmd }); |
| 189 | |
| 190 | const run_wm = b.addRunArtifact(exe); |
| 191 | run_wm.step.dependOn(&setup.step); |
| 192 | run_wm.setEnvironmentVariable("DISPLAY", ":2"); |
| 193 | run_wm.addArgs(&.{ "-c", "resources/test-config.lua" }); |
| 194 | |
| 195 | return run_wm; |
| 196 | } |
| 197 | |
| 198 | fn addXwaylandRun(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step.Run { |
| 199 | const cmd = "Xwayland -retro -noreset :2 & sleep 1"; |
| 200 | |
| 201 | const setup = b.addSystemCommand(&.{ "sh", "-c", cmd }); |
| 202 | |
| 203 | const run_wm = b.addRunArtifact(exe); |
| 204 | run_wm.step.dependOn(&setup.step); |
| 205 | run_wm.setEnvironmentVariable("DISPLAY", ":2"); |
| 206 | |
| 207 | return run_wm; |
| 208 | } |
| 209 | |
| 210 | fn buildLua(b: *std.Build, lua_dep: *std.Build.Dependency, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile { |
| 211 | const lua_src = lua_dep; |
| 212 | const lua = b.addLibrary(.{ |
| 213 | .name = "lua", |
| 214 | .root_module = b.createModule(.{ |
| 215 | .target = target, |
| 216 | .optimize = optimize, |
| 217 | .link_libc = true, |
| 218 | }), |
| 219 | }); |
| 220 | lua.root_module.addIncludePath(lua_src.path("src/")); |
| 221 | lua.root_module.addCSourceFiles(.{ |
| 222 | .root = lua_src.path("src/"), |
| 223 | .files = &.{ |
| 224 | "lapi.c", |
| 225 | "lauxlib.c", |
| 226 | "lbaselib.c", |
| 227 | "lcode.c", |
| 228 | "lcorolib.c", |
| 229 | "lctype.c", |
| 230 | "ldblib.c", |
| 231 | "ldebug.c", |
| 232 | "ldo.c", |
| 233 | "ldump.c", |
| 234 | "lfunc.c", |
| 235 | "lgc.c", |
| 236 | "linit.c", |
| 237 | "liolib.c", |
| 238 | "llex.c", |
| 239 | "lmathlib.c", |
| 240 | "lmem.c", |
| 241 | "loadlib.c", |
| 242 | "lobject.c", |
| 243 | "lopcodes.c", |
| 244 | "loslib.c", |
| 245 | "lparser.c", |
| 246 | "lstate.c", |
| 247 | "lstring.c", |
| 248 | "lstrlib.c", |
| 249 | "ltable.c", |
| 250 | "ltablib.c", |
| 251 | "ltm.c", |
| 252 | "lundump.c", |
| 253 | "lutf8lib.c", |
| 254 | "lvm.c", |
| 255 | "lzio.c", |
| 256 | }, |
| 257 | }); |
| 258 | lua.installHeader(lua_src.path("src/lua.h"), "lua.h"); |
| 259 | lua.installHeader(lua_src.path("src/lualib.h"), "lualib.h"); |
| 260 | lua.installHeader(lua_src.path("src/lauxlib.h"), "lauxlib.h"); |
| 261 | lua.installHeader(lua_src.path("src/luaconf.h"), "luaconf.h"); |
| 262 | |
| 263 | return lua; |
| 264 | } |