| 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 | exe.linkSystemLibrary("lua5.4"); |
| 28 | exe.linkSystemLibrary("X11"); |
| 29 | exe.linkSystemLibrary("Xinerama"); |
| 30 | exe.linkSystemLibrary("Xft"); |
| 31 | exe.linkSystemLibrary("fontconfig"); |
| 32 | exe.linkLibC(); |
| 33 | |
| 34 | b.installArtifact(exe); |
| 35 | |
| 36 | const run_step = b.step("run", "Run oxwm"); |
| 37 | const run_cmd = b.addRunArtifact(exe); |
| 38 | run_step.dependOn(&run_cmd.step); |
| 39 | run_cmd.step.dependOn(b.getInstallStep()); |
| 40 | if (b.args) |args| { |
| 41 | run_cmd.addArgs(args); |
| 42 | } |
| 43 | |
| 44 | const test_step = b.step("test", "Run unit tests"); |
| 45 | const unit_tests = b.addTest(.{ |
| 46 | .root_module = b.createModule(.{ |
| 47 | .root_source_file = b.path("tests/main_tests.zig"), |
| 48 | .target = target, |
| 49 | .optimize = optimize, |
| 50 | }), |
| 51 | }); |
| 52 | unit_tests.use_lld = false; |
| 53 | test_step.dependOn(&b.addRunArtifact(unit_tests).step); |
| 54 | |
| 55 | const src_main_unit_tests = b.addTest(.{ |
| 56 | .root_module = b.createModule(.{ |
| 57 | .root_source_file = b.path("src/main.zig"), |
| 58 | .target = target, |
| 59 | .optimize = optimize, |
| 60 | }), |
| 61 | }); |
| 62 | src_main_unit_tests.use_lld = false; |
| 63 | src_main_unit_tests.linkSystemLibrary("lua5.4"); |
| 64 | src_main_unit_tests.linkSystemLibrary("X11"); |
| 65 | src_main_unit_tests.linkSystemLibrary("Xinerama"); |
| 66 | src_main_unit_tests.linkSystemLibrary("Xft"); |
| 67 | src_main_unit_tests.linkSystemLibrary("fontconfig"); |
| 68 | src_main_unit_tests.linkLibC(); |
| 69 | test_step.dependOn(&b.addRunArtifact(src_main_unit_tests).step); |
| 70 | |
| 71 | const lua_config_tests = b.addTest(.{ |
| 72 | .root_module = b.createModule(.{ |
| 73 | .root_source_file = b.path("tests/lua_config_tests.zig"), |
| 74 | .target = target, |
| 75 | .optimize = optimize, |
| 76 | }), |
| 77 | }); |
| 78 | lua_config_tests.use_lld = false; |
| 79 | lua_config_tests.root_module.addImport("lua", b.createModule(.{ |
| 80 | .root_source_file = b.path("src/config/lua.zig"), |
| 81 | .target = target, |
| 82 | .optimize = optimize, |
| 83 | })); |
| 84 | lua_config_tests.linkSystemLibrary("lua5.4"); |
| 85 | lua_config_tests.linkLibC(); |
| 86 | test_step.dependOn(&b.addRunArtifact(lua_config_tests).step); |
| 87 | |
| 88 | const xephyr_step = b.step("xephyr", "Run in Xephyr (1280x800 on :2)"); |
| 89 | xephyr_step.dependOn(&addXephyrRun(b, exe, false).step); |
| 90 | |
| 91 | const xephyr_multi_step = b.step("xephyr-multi", "Run in Xephyr multi-monitor on :2"); |
| 92 | xephyr_multi_step.dependOn(&addXephyrRun(b, exe, true).step); |
| 93 | |
| 94 | const multimon_step = b.step("multimon", "Alias for xephyr-multi"); |
| 95 | multimon_step.dependOn(&addXephyrRun(b, exe, true).step); |
| 96 | |
| 97 | const xwayland_step = b.step("xwayland", "Run in Xwayland on :2"); |
| 98 | xwayland_step.dependOn(&addXwaylandRun(b, exe).step); |
| 99 | |
| 100 | const kill_step = b.step("kill", "Kill Xephyr and oxwm"); |
| 101 | kill_step.dependOn(&b.addSystemCommand(&.{ "sh", "-c", "pkill -9 Xephyr || true; pkill -9 oxwm || true" }).step); |
| 102 | |
| 103 | const fmt_step = b.step("fmt", "Format source files"); |
| 104 | fmt_step.dependOn(&b.addFmt(.{ .paths = &.{"src/"} }).step); |
| 105 | |
| 106 | const clean_step = b.step("clean", "Remove build artifacts"); |
| 107 | clean_step.dependOn(&b.addSystemCommand(&.{ "rm", "-rf", "zig-out", ".zig-cache" }).step); |
| 108 | |
| 109 | const install_step = b.step("install-system", "Install oxwm system-wide (requires sudo)"); |
| 110 | install_step.dependOn(b.getInstallStep()); |
| 111 | install_step.dependOn(&b.addSystemCommand(&.{ |
| 112 | "sudo", "sh", "-c", |
| 113 | "cp zig-out/bin/oxwm /usr/bin/oxwm && " ++ |
| 114 | "chmod +x /usr/bin/oxwm && " ++ |
| 115 | "mkdir -p /usr/share/xsessions && " ++ |
| 116 | "cp resources/oxwm.desktop /usr/share/xsessions/oxwm.desktop && " ++ |
| 117 | "mkdir -p /usr/share/man/man1 && " ++ |
| 118 | "cp resources/oxwm.1 /usr/share/man/man1/oxwm.1 && " ++ |
| 119 | "mkdir -p /usr/share/oxwm && " ++ |
| 120 | "cp templates/oxwm.lua /usr/share/oxwm/oxwm.lua && " ++ |
| 121 | "echo 'oxwm installed to /usr/bin/oxwm'", |
| 122 | }).step); |
| 123 | |
| 124 | const uninstall_step = b.step("uninstall-system", "Uninstall oxwm from system"); |
| 125 | uninstall_step.dependOn(&b.addSystemCommand(&.{ |
| 126 | "sudo", "sh", "-c", |
| 127 | "rm -f /usr/bin/oxwm /usr/share/xsessions/oxwm.desktop /usr/share/man/man1/oxwm.1 && " ++ |
| 128 | "rm -rf /usr/share/oxwm && " ++ |
| 129 | "echo 'oxwm uninstalled (config at ~/.config/oxwm preserved)'", |
| 130 | }).step); |
| 131 | } |
| 132 | |
| 133 | fn addXephyrRun(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run { |
| 134 | const kill_cmd = if (multimon) |
| 135 | "pkill -9 Xephyr || true; Xephyr +xinerama -glamor -screen 640x480 -screen 640x480 :2 & sleep 1" |
| 136 | else |
| 137 | "pkill -9 Xephyr || true; Xephyr -screen 1280x800 :2 & sleep 1"; |
| 138 | |
| 139 | const setup = b.addSystemCommand(&.{ "sh", "-c", kill_cmd }); |
| 140 | |
| 141 | const run_wm = b.addRunArtifact(exe); |
| 142 | run_wm.step.dependOn(&setup.step); |
| 143 | run_wm.setEnvironmentVariable("DISPLAY", ":2"); |
| 144 | run_wm.addArgs(&.{ "-c", "resources/test-config.lua" }); |
| 145 | |
| 146 | return run_wm; |
| 147 | } |
| 148 | |
| 149 | fn addXwaylandRun(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step.Run { |
| 150 | const cmd = "Xwayland -retro -noreset :2 & sleep 1"; |
| 151 | |
| 152 | const setup = b.addSystemCommand(&.{ "sh", "-c", cmd }); |
| 153 | |
| 154 | const run_wm = b.addRunArtifact(exe); |
| 155 | run_wm.step.dependOn(&setup.step); |
| 156 | run_wm.setEnvironmentVariable("DISPLAY", ":2"); |
| 157 | |
| 158 | return run_wm; |
| 159 | } |