summaryrefslogtreecommitdiff
path: root/build.zig
blob: 21f09ad0ef7c607412858a9eba9fb8684591f24b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const std = @import("std");

var sqlite3: ?*std.build.LibExeObjStep = null;

fn linkSqlite(b: *std.build.LibExeObjStep) void {
    b.linkLibC();

    if (sqlite3) |lib| {
        b.linkLibrary(lib);
    } else {
        b.linkSystemLibrary("sqlite3");
    }
}

fn getTarget(original_target: std.zig.CrossTarget, bundled: bool) std.zig.CrossTarget {
    if (bundled) {
        var tmp = original_target;

        if (tmp.isGnuLibC()) {
            const min_glibc_version = std.builtin.Version{
                .major = 2,
                .minor = 28,
                .patch = 0,
            };
            if (tmp.glibc_version) |ver| {
                if (ver.order(min_glibc_version) == .lt) {
                    std.debug.panic("sqlite requires glibc version >= 2.28", .{});
                }
            } else {
                tmp.setGnuLibCVersion(2, 28, 0);
            }
        }

        return tmp;
    }

    return original_target;
}

const TestTarget = struct {
    target: std.zig.CrossTarget = @as(std.zig.CrossTarget, .{}),
    mode: std.builtin.Mode = .Debug,
    single_threaded: bool = false,
    bundled: bool,
};

const all_test_targets = switch (std.Target.current.cpu.arch) {
    .x86_64 => switch (std.Target.current.os.tag) {
        .linux => [_]TestTarget{
            TestTarget{
                .target = .{},
                .bundled = false,
            },
            TestTarget{
                .target = .{
                    .cpu_arch = .x86_64,
                    .abi = .musl,
                },
                .bundled = true,
            },
            TestTarget{
                .target = .{
                    .cpu_arch = .i386,
                    .abi = .musl,
                },
                .bundled = true,
            },
            TestTarget{
                .target = .{
                    .cpu_arch = .aarch64,
                    .abi = .musl,
                },
                .bundled = true,
            },
            TestTarget{
                .target = .{
                    .cpu_arch = .riscv64,
                    .abi = .musl,
                },
                .bundled = true,
            },
            TestTarget{
                .target = .{
                    .cpu_arch = .mips,
                    .abi = .musl,
                },
                .bundled = true,
            },
            TestTarget{
                .target = .{
                    .cpu_arch = .arm,
                    .abi = .musleabihf,
                },
                .bundled = true,
            },
        },
        .windows => [_]TestTarget{
            TestTarget{
                .target = .{
                    .cpu_arch = .x86_64,
                    .abi = .gnu,
                },
                .bundled = true,
            },
            TestTarget{
                .target = .{
                    .cpu_arch = .i386,
                    .abi = .gnu,
                },
                .bundled = true,
            },
        },
        .freebsd => [_]TestTarget{
            TestTarget{
                .target = .{},
                .bundled = false,
            },
            TestTarget{
                .target = .{
                    .cpu_arch = .x86_64,
                },
                .bundled = true,
            },
        },
        else => [_]TestTarget{
            TestTarget{
                .target = .{},
                .bundled = false,
            },
        },
    },
    else => [_]TestTarget{
        TestTarget{
            .target = .{},
            .bundled = false,
        },
    },
};

pub fn build(b: *std.build.Builder) void {
    const in_memory = b.option(bool, "in_memory", "Should the tests run with sqlite in memory (default true)") orelse true;
    const dbfile = b.option([]const u8, "dbfile", "Always use this database file instead of a temporary one");
    const use_bundled = b.option(bool, "use_bundled", "Use the bundled sqlite3 source instead of linking the system library (default false)");
    const enable_qemu = b.option(bool, "enable_qemu", "Enable qemu for running tests (default false)") orelse false;

    const target = b.standardTargetOptions(.{});

    const test_targets = if (target.isNative())
        &all_test_targets
    else
        &[_]TestTarget{.{
            .target = target,
            .bundled = use_bundled orelse false,
        }};

    const test_step = b.step("test", "Run library tests");
    for (test_targets) |test_target| {
        const bundled = use_bundled orelse test_target.bundled;
        const cross_target = getTarget(test_target.target, bundled);

        const tests = b.addTest("sqlite.zig");

        if (bundled) {
            const lib = b.addStaticLibrary("sqlite", null);
            lib.addCSourceFile("c/sqlite3.c", &[_][]const u8{"-std=c99"});
            lib.linkLibC();
            lib.setTarget(cross_target);
            lib.setBuildMode(test_target.mode);
            sqlite3 = lib;
        }

        const lib = b.addStaticLibrary("zig-sqlite", "sqlite.zig");
        lib.addIncludeDir("c");
        linkSqlite(lib);
        lib.setTarget(cross_target);
        lib.setBuildMode(test_target.mode);

        const single_threaded_txt = if (test_target.single_threaded) "single" else "multi";
        tests.setNamePrefix(b.fmt("{s}-{s}-{s} ", .{
            cross_target.zigTriple(b.allocator),
            @tagName(test_target.mode),
            single_threaded_txt,
        }));
        tests.single_threaded = test_target.single_threaded;
        tests.setBuildMode(test_target.mode);
        tests.setTarget(cross_target);
        tests.addIncludeDir("c");
        linkSqlite(tests);
        tests.enable_qemu = enable_qemu;

        tests.addBuildOption(bool, "in_memory", in_memory);
        tests.addBuildOption(?[]const u8, "dbfile", dbfile);

        test_step.dependOn(&tests.step);
    }
}