diff options
| author | 2021-03-22 17:10:17 +0100 | |
|---|---|---|
| committer | 2021-03-22 20:36:17 +0100 | |
| commit | 7e021d6c0d05eca2a441e4e0974974199cd2f455 (patch) | |
| tree | 71e25bc6c149433aeaa140e5338294b927baebba | |
| parent | Merge pull request #22 from data-man/sqlite_fresh (diff) | |
| download | zig-sqlite-7e021d6c0d05eca2a441e4e0974974199cd2f455.tar.gz zig-sqlite-7e021d6c0d05eca2a441e4e0974974199cd2f455.tar.xz zig-sqlite-7e021d6c0d05eca2a441e4e0974974199cd2f455.zip | |
build: run the tests on all targets we support for a given host OS
For a given host OS (Linux or Windows), define all the test targets
directly in the build script.
Additionally, support using QEMU to run the non-native tests.
| -rw-r--r-- | build.zig | 178 |
1 files changed, 150 insertions, 28 deletions
| @@ -1,3 +1,4 @@ | |||
| 1 | const builtin = @import("builtin"); | ||
| 1 | const std = @import("std"); | 2 | const std = @import("std"); |
| 2 | 3 | ||
| 3 | var sqlite3: ?*std.build.LibExeObjStep = null; | 4 | var sqlite3: ?*std.build.LibExeObjStep = null; |
| @@ -37,40 +38,161 @@ fn getTarget(original_target: std.zig.CrossTarget, bundled: bool) std.zig.CrossT | |||
| 37 | return original_target; | 38 | return original_target; |
| 38 | } | 39 | } |
| 39 | 40 | ||
| 41 | const TestTarget = struct { | ||
| 42 | target: std.zig.CrossTarget = @as(std.zig.CrossTarget, .{}), | ||
| 43 | mode: builtin.Mode = .Debug, | ||
| 44 | single_threaded: bool = false, | ||
| 45 | bundled: bool, | ||
| 46 | }; | ||
| 47 | |||
| 48 | const all_test_targets = switch (std.Target.current.cpu.arch) { | ||
| 49 | .x86_64 => switch (std.Target.current.os.tag) { | ||
| 50 | .linux => [_]TestTarget{ | ||
| 51 | TestTarget{ | ||
| 52 | .target = .{}, | ||
| 53 | .bundled = false, | ||
| 54 | }, | ||
| 55 | TestTarget{ | ||
| 56 | .target = .{ | ||
| 57 | .cpu_arch = .x86_64, | ||
| 58 | .abi = .musl, | ||
| 59 | }, | ||
| 60 | .bundled = true, | ||
| 61 | }, | ||
| 62 | TestTarget{ | ||
| 63 | .target = .{ | ||
| 64 | .cpu_arch = .i386, | ||
| 65 | .abi = .musl, | ||
| 66 | }, | ||
| 67 | .bundled = true, | ||
| 68 | }, | ||
| 69 | TestTarget{ | ||
| 70 | .target = .{ | ||
| 71 | .cpu_arch = .aarch64, | ||
| 72 | .abi = .musl, | ||
| 73 | }, | ||
| 74 | .bundled = true, | ||
| 75 | }, | ||
| 76 | TestTarget{ | ||
| 77 | .target = .{ | ||
| 78 | .cpu_arch = .riscv64, | ||
| 79 | .abi = .musl, | ||
| 80 | }, | ||
| 81 | .bundled = true, | ||
| 82 | }, | ||
| 83 | TestTarget{ | ||
| 84 | .target = .{ | ||
| 85 | .cpu_arch = .mips, | ||
| 86 | .abi = .musl, | ||
| 87 | }, | ||
| 88 | .bundled = true, | ||
| 89 | }, | ||
| 90 | TestTarget{ | ||
| 91 | .target = .{ | ||
| 92 | .cpu_arch = .arm, | ||
| 93 | .abi = .musleabihf, | ||
| 94 | }, | ||
| 95 | .bundled = true, | ||
| 96 | }, | ||
| 97 | }, | ||
| 98 | .windows => [_]TestTarget{ | ||
| 99 | TestTarget{ | ||
| 100 | .target = .{}, | ||
| 101 | .bundled = false, | ||
| 102 | }, | ||
| 103 | TestTarget{ | ||
| 104 | .target = .{ | ||
| 105 | .cpu_arch = .x86_64, | ||
| 106 | }, | ||
| 107 | .bundled = true, | ||
| 108 | }, | ||
| 109 | TestTarget{ | ||
| 110 | .target = .{ | ||
| 111 | .cpu_arch = .i386, | ||
| 112 | }, | ||
| 113 | .bundled = true, | ||
| 114 | }, | ||
| 115 | }, | ||
| 116 | .freebsd => [_]TestTarget{ | ||
| 117 | TestTarget{ | ||
| 118 | .target = .{}, | ||
| 119 | .bundled = false, | ||
| 120 | }, | ||
| 121 | TestTarget{ | ||
| 122 | .target = .{ | ||
| 123 | .cpu_arch = .x86_64, | ||
| 124 | }, | ||
| 125 | .bundled = true, | ||
| 126 | }, | ||
| 127 | }, | ||
| 128 | else => [_]TestTarget{ | ||
| 129 | TestTarget{ | ||
| 130 | .target = .{}, | ||
| 131 | .bundled = false, | ||
| 132 | }, | ||
| 133 | }, | ||
| 134 | }, | ||
| 135 | else => [_]TestTarget{ | ||
| 136 | TestTarget{ | ||
| 137 | .target = .{}, | ||
| 138 | .bundled = false, | ||
| 139 | }, | ||
| 140 | }, | ||
| 141 | }; | ||
| 142 | |||
| 40 | pub fn build(b: *std.build.Builder) void { | 143 | pub fn build(b: *std.build.Builder) void { |
| 41 | const in_memory = b.option(bool, "in_memory", "Should the tests run with sqlite in memory (default true)") orelse true; | 144 | const in_memory = b.option(bool, "in_memory", "Should the tests run with sqlite in memory (default true)") orelse true; |
| 42 | const dbfile = b.option([]const u8, "dbfile", "Always use this database file instead of a temporary one"); | 145 | const dbfile = b.option([]const u8, "dbfile", "Always use this database file instead of a temporary one"); |
| 43 | const use_bundled = b.option(bool, "use_bundled", "Use the bundled sqlite3 source instead of linking the system library (default false)") orelse false; | 146 | const use_bundled = b.option(bool, "use_bundled", "Use the bundled sqlite3 source instead of linking the system library (default false)") orelse false; |
| 147 | const enable_qemu = b.option(bool, "enable_qemu", "Enable qemu for running tests (default false)") orelse false; | ||
| 44 | 148 | ||
| 45 | const target = getTarget(b.standardTargetOptions(.{}), use_bundled); | 149 | const target = b.standardTargetOptions(.{}); |
| 46 | const mode = b.standardReleaseOptions(); | ||
| 47 | |||
| 48 | // Build sqlite from source if asked | ||
| 49 | if (use_bundled) { | ||
| 50 | const lib = b.addStaticLibrary("sqlite", null); | ||
| 51 | lib.addCSourceFile("c/sqlite3.c", &[_][]const u8{"-std=c99"}); | ||
| 52 | lib.linkLibC(); | ||
| 53 | lib.setTarget(target); | ||
| 54 | lib.setBuildMode(mode); | ||
| 55 | sqlite3 = lib; | ||
| 56 | } | ||
| 57 | 150 | ||
| 58 | const lib = b.addStaticLibrary("zig-sqlite", "sqlite.zig"); | 151 | const test_targets = if (target.isNative()) |
| 59 | lib.addIncludeDir("c"); | 152 | &all_test_targets |
| 60 | linkSqlite(lib); | 153 | else |
| 61 | lib.setTarget(target); | 154 | &[_]TestTarget{.{ |
| 62 | lib.setBuildMode(mode); | 155 | .target = target, |
| 63 | lib.install(); | 156 | .bundled = use_bundled, |
| 64 | 157 | }}; | |
| 65 | var main_tests = b.addTest("sqlite.zig"); | ||
| 66 | main_tests.addIncludeDir("c"); | ||
| 67 | linkSqlite(main_tests); | ||
| 68 | main_tests.setBuildMode(mode); | ||
| 69 | main_tests.setTarget(target); | ||
| 70 | main_tests.setBuildMode(mode); | ||
| 71 | main_tests.addBuildOption(bool, "in_memory", in_memory); | ||
| 72 | main_tests.addBuildOption(?[]const u8, "dbfile", dbfile); | ||
| 73 | 158 | ||
| 74 | const test_step = b.step("test", "Run library tests"); | 159 | const test_step = b.step("test", "Run library tests"); |
| 75 | test_step.dependOn(&main_tests.step); | 160 | for (test_targets) |test_target| { |
| 161 | const cross_target = getTarget(test_target.target, test_target.bundled); | ||
| 162 | |||
| 163 | const tests = b.addTest("sqlite.zig"); | ||
| 164 | |||
| 165 | if (test_target.bundled) { | ||
| 166 | const lib = b.addStaticLibrary("sqlite", null); | ||
| 167 | lib.addCSourceFile("c/sqlite3.c", &[_][]const u8{"-std=c99"}); | ||
| 168 | lib.linkLibC(); | ||
| 169 | lib.setTarget(cross_target); | ||
| 170 | lib.setBuildMode(test_target.mode); | ||
| 171 | sqlite3 = lib; | ||
| 172 | } | ||
| 173 | |||
| 174 | const lib = b.addStaticLibrary("zig-sqlite", "sqlite.zig"); | ||
| 175 | lib.addIncludeDir("c"); | ||
| 176 | linkSqlite(lib); | ||
| 177 | lib.setTarget(cross_target); | ||
| 178 | lib.setBuildMode(test_target.mode); | ||
| 179 | |||
| 180 | const single_threaded_txt = if (test_target.single_threaded) "single" else "multi"; | ||
| 181 | tests.setNamePrefix(b.fmt("{s}-{s}-{s} ", .{ | ||
| 182 | cross_target.zigTriple(b.allocator), | ||
| 183 | @tagName(test_target.mode), | ||
| 184 | single_threaded_txt, | ||
| 185 | })); | ||
| 186 | tests.single_threaded = test_target.single_threaded; | ||
| 187 | tests.setBuildMode(test_target.mode); | ||
| 188 | tests.setTarget(cross_target); | ||
| 189 | tests.addIncludeDir("c"); | ||
| 190 | linkSqlite(tests); | ||
| 191 | tests.enable_qemu = enable_qemu; | ||
| 192 | |||
| 193 | tests.addBuildOption(bool, "in_memory", in_memory); | ||
| 194 | tests.addBuildOption(?[]const u8, "dbfile", dbfile); | ||
| 195 | |||
| 196 | test_step.dependOn(&tests.step); | ||
| 197 | } | ||
| 76 | } | 198 | } |