|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const TargetSpec = struct { |
| 4 | + const CpuModelOverride = enum { |
| 5 | + arm1176jzf_s, |
| 6 | + cortex_a7, |
| 7 | + }; |
| 8 | + |
| 9 | + step_name: []const u8, |
| 10 | + out_name: []const u8, |
| 11 | + triple: []const u8, |
| 12 | + c_flags: []const []const u8, |
| 13 | + cpu_model: ?CpuModelOverride = null, |
| 14 | + windows: bool = false, |
| 15 | +}; |
| 16 | + |
| 17 | +fn resolveQuery( |
| 18 | + b: *std.Build, |
| 19 | + triple: []const u8, |
| 20 | + cpu_model: ?TargetSpec.CpuModelOverride, |
| 21 | +) std.Build.ResolvedTarget { |
| 22 | + var q = std.Target.Query.parse(.{ .arch_os_abi = triple }) catch |err| { |
| 23 | + std.debug.panic("invalid target triple '{s}': {s}", .{ triple, @errorName(err) }); |
| 24 | + }; |
| 25 | + if (cpu_model) |cpu| { |
| 26 | + q.cpu_model = switch (cpu) { |
| 27 | + .arm1176jzf_s => .{ .explicit = &std.Target.arm.cpu.arm1176jzf_s }, |
| 28 | + .cortex_a7 => .{ .explicit = &std.Target.arm.cpu.cortex_a7 }, |
| 29 | + }; |
| 30 | + } |
| 31 | + return b.resolveTargetQuery(q); |
| 32 | +} |
| 33 | + |
| 34 | +fn addWebdavExe( |
| 35 | + b: *std.Build, |
| 36 | + target: std.Build.ResolvedTarget, |
| 37 | + optimize: std.builtin.OptimizeMode, |
| 38 | + name: []const u8, |
| 39 | + c_flags: []const []const u8, |
| 40 | + is_windows: bool, |
| 41 | +) *std.Build.Step.Compile { |
| 42 | + const mod = b.createModule(.{ |
| 43 | + .target = target, |
| 44 | + .optimize = optimize, |
| 45 | + .link_libc = true, |
| 46 | + .link_libcpp = true, |
| 47 | + }); |
| 48 | + mod.addCSourceFile(.{ |
| 49 | + .file = b.path("webdav.cpp"), |
| 50 | + .flags = c_flags, |
| 51 | + .language = .cpp, |
| 52 | + }); |
| 53 | + |
| 54 | + const exe = b.addExecutable(.{ |
| 55 | + .name = name, |
| 56 | + .root_module = mod, |
| 57 | + }); |
| 58 | + |
| 59 | + if (is_windows) { |
| 60 | + exe.linkSystemLibrary("ws2_32"); |
| 61 | + } |
| 62 | + |
| 63 | + return exe; |
| 64 | +} |
| 65 | + |
| 66 | +pub fn build(b: *std.Build) void { |
| 67 | + // Keep all installed artifacts in ./release instead of ./zig-out. |
| 68 | + b.resolveInstallPrefix("release", .{}); |
| 69 | + |
| 70 | + const optimize = b.option( |
| 71 | + std.builtin.OptimizeMode, |
| 72 | + "optimize", |
| 73 | + "Optimization mode (default: ReleaseFast)", |
| 74 | + ) orelse .ReleaseFast; |
| 75 | + const keep_pdb = optimize == .Debug; |
| 76 | + const default_triple = b.option([]const u8, "default-triple", "Default triple for `zig build` and `zig build native`") orelse "x86_64-linux-musl"; |
| 77 | + const default_target = resolveQuery(b, default_triple, null); |
| 78 | + |
| 79 | + const base_flags = &.{ |
| 80 | + "-std=c++17", |
| 81 | + "-Wall", |
| 82 | + "-Wextra", |
| 83 | + }; |
| 84 | + |
| 85 | + // Fast local build (current target) |
| 86 | + const native_exe = addWebdavExe(b, default_target, optimize, "webdav", base_flags, default_target.result.os.tag == .windows); |
| 87 | + const native_install = b.addInstallArtifact(native_exe, .{ |
| 88 | + .pdb_dir = if (keep_pdb) .default else .disabled, |
| 89 | + }); |
| 90 | + const native_step = b.step("native", "Build webdav for the selected/host target"); |
| 91 | + native_step.dependOn(&native_install.step); |
| 92 | + |
| 93 | + // Cross-build matrix |
| 94 | + const matrix_step = b.step("matrix", "Build the full cross-platform/cross-arch matrix"); |
| 95 | + |
| 96 | + // Cosmopolitan build via cosmoc++. |
| 97 | + const cosmo_out = b.getInstallPath(.bin, "webdav_x86_64-unknown-cosmo.com"); |
| 98 | + const mkdir_release_bin = b.addSystemCommand(&.{ "mkdir", "-p", b.exe_dir }); |
| 99 | + const cosmo_cmd = b.addSystemCommand(&.{ |
| 100 | + "cosmoc++", |
| 101 | + "-O2", |
| 102 | + "-std=c++17", |
| 103 | + "-pthread", |
| 104 | + "-o", |
| 105 | + cosmo_out, |
| 106 | + "webdav.cpp", |
| 107 | + }); |
| 108 | + cosmo_cmd.step.dependOn(&mkdir_release_bin.step); |
| 109 | + const cosmo_step = b.step("cosmo", "Build x86_64-unknown-cosmo with cosmoc++"); |
| 110 | + if (optimize == .Debug) { |
| 111 | + cosmo_step.dependOn(&cosmo_cmd.step); |
| 112 | + } else { |
| 113 | + const cosmo_dbg = b.fmt("{s}.dbg", .{cosmo_out}); |
| 114 | + const rm_cosmo_dbg = b.addSystemCommand(&.{ "rm", "-f", cosmo_dbg }); |
| 115 | + rm_cosmo_dbg.step.dependOn(&cosmo_cmd.step); |
| 116 | + cosmo_step.dependOn(&rm_cosmo_dbg.step); |
| 117 | + } |
| 118 | + matrix_step.dependOn(cosmo_step); |
| 119 | + |
| 120 | + const specs = [_]TargetSpec{ |
| 121 | + .{ .step_name = "linux-x86_64", .out_name = "webdav_linux_x86_64", .triple = "x86_64-linux-musl", .c_flags = base_flags }, |
| 122 | + .{ .step_name = "linux-x86", .out_name = "webdav_linux_x86", .triple = "x86-linux-musl", .c_flags = base_flags }, |
| 123 | + .{ .step_name = "linux-armv6", .out_name = "webdav_linux_armv6", .triple = "arm-linux-musleabihf", .c_flags = base_flags, .cpu_model = .arm1176jzf_s }, |
| 124 | + .{ .step_name = "linux-armv7", .out_name = "webdav_linux_armv7", .triple = "arm-linux-musleabihf", .c_flags = base_flags, .cpu_model = .cortex_a7 }, |
| 125 | + .{ .step_name = "linux-armv8", .out_name = "webdav_linux_arm64", .triple = "aarch64-linux-musl", .c_flags = base_flags }, |
| 126 | + .{ .step_name = "linux-riscv64", .out_name = "webdav_linux_riscv64", .triple = "riscv64-linux-musl", .c_flags = base_flags }, |
| 127 | + .{ .step_name = "linux-mips", .out_name = "webdav_linux_mips", .triple = "mips-linux-musleabi", .c_flags = base_flags }, |
| 128 | + .{ .step_name = "linux-mipsel", .out_name = "webdav_linux_mipsel", .triple = "mipsel-linux-musleabi", .c_flags = base_flags }, |
| 129 | + .{ .step_name = "linux-mips-hf", .out_name = "webdav_linux_mips_hf", .triple = "mips-linux-musleabihf", .c_flags = base_flags }, |
| 130 | + .{ .step_name = "linux-mipsel-hf", .out_name = "webdav_linux_mipsel_hf", .triple = "mipsel-linux-musleabihf", .c_flags = base_flags }, |
| 131 | + .{ .step_name = "linux-mips64", .out_name = "webdav_linux_mips64", .triple = "mips64-linux-muslabi64", .c_flags = base_flags }, |
| 132 | + .{ .step_name = "linux-mips64el", .out_name = "webdav_linux_mips64el", .triple = "mips64el-linux-muslabi64", .c_flags = base_flags }, |
| 133 | + .{ .step_name = "linux-mips64n32", .out_name = "webdav_linux_mips64n32", .triple = "mips64-linux-muslabin32", .c_flags = base_flags }, |
| 134 | + .{ .step_name = "linux-mips64eln32", .out_name = "webdav_linux_mips64eln32", .triple = "mips64el-linux-muslabin32", .c_flags = base_flags }, |
| 135 | + .{ .step_name = "macos-x86_64", .out_name = "webdav_macos_x86_64", .triple = "x86_64-macos", .c_flags = base_flags }, |
| 136 | + .{ .step_name = "macos-arm64", .out_name = "webdav_macos_arm64", .triple = "aarch64-macos", .c_flags = base_flags }, |
| 137 | + .{ .step_name = "windows-x86", .out_name = "webdav_windows_x86.exe", .triple = "x86-windows-gnu", .c_flags = base_flags, .windows = true }, |
| 138 | + .{ .step_name = "windows-amd64", .out_name = "webdav_windows_amd64.exe", .triple = "x86_64-windows-gnu", .c_flags = base_flags, .windows = true }, |
| 139 | + .{ .step_name = "windows-arm64", .out_name = "webdav_windows_arm64.exe", .triple = "aarch64-windows-gnu", .c_flags = base_flags, .windows = true }, |
| 140 | + .{ .step_name = "freebsd-x86_64", .out_name = "webdav_freebsd_x86_64", .triple = "x86_64-freebsd", .c_flags = base_flags }, |
| 141 | + .{ .step_name = "freebsd-arm64", .out_name = "webdav_freebsd_arm64", .triple = "aarch64-freebsd", .c_flags = base_flags }, |
| 142 | + }; |
| 143 | + |
| 144 | + inline for (specs) |spec| { |
| 145 | + const resolved = resolveQuery(b, spec.triple, spec.cpu_model); |
| 146 | + const exe = addWebdavExe(b, resolved, optimize, spec.step_name, spec.c_flags, spec.windows); |
| 147 | + const install_artifact = b.addInstallArtifact(exe, .{ |
| 148 | + .dest_sub_path = spec.out_name, |
| 149 | + .pdb_dir = if (keep_pdb) .default else .disabled, |
| 150 | + }); |
| 151 | + |
| 152 | + const per_target_step = b.step(spec.step_name, b.fmt("Build target {s}", .{spec.triple})); |
| 153 | + per_target_step.dependOn(&install_artifact.step); |
| 154 | + matrix_step.dependOn(&install_artifact.step); |
| 155 | + } |
| 156 | + |
| 157 | + // Keep default `zig build` fast/local. |
| 158 | + b.default_step = native_step; |
| 159 | +} |
0 commit comments