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