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