-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
81 lines (65 loc) · 3.23 KB
/
build.zig
File metadata and controls
81 lines (65 loc) · 3.23 KB
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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const mod = b.addModule("zlibsql", .{
.root_source_file = b.path("libsql.zig"),
.target = target,
.optimize = optimize,
});
var cargo_cmd: ?*std.Build.Step.Run = null;
const rust_target_opt = b.option([]const u8, "rust_target", "override the --target argument passed to cargo");
const use_bundled_libsql = b.option(bool, "use_bundled_libsql", "build and compile the libsql-c binding with cargo (default true, requires an existing Rust install)") orelse true;
const use_cargo_zigbuild = b.option(bool, "use_cargo_zigbuild", "use cargo-zigbuild to build libsql-c") orelse false;
if (use_bundled_libsql) {
const libsql_dep = b.lazyDependency("libsql", .{});
if (libsql_dep) |libsql| {
const libsql_path = libsql.path(".");
const mapped_target_str = zigTargetToRustTarget(b.allocator, target.result) catch |err| @panic(@errorName(err));
defer b.allocator.free(mapped_target_str);
const rust_target = rust_target_opt orelse mapped_target_str;
const libsql_lib_dir = std.fmt.allocPrint(b.allocator, "target/{s}/{s}/", .{
rust_target,
if (optimize == .Debug) "debug" else "release",
}) catch |err| @panic(@errorName(err));
defer b.allocator.free(libsql_lib_dir);
mod.link_libc = true;
mod.link_libcpp = true;
mod.addIncludePath(libsql_path);
mod.addLibraryPath(libsql.path(libsql_lib_dir));
mod.linkSystemLibrary("libsql", .{ .preferred_link_mode = .static });
const build_cmd = switch (use_cargo_zigbuild) {
true => "zigbuild",
false => "build",
};
const cargo_build_cmd: []const []const u8 = switch (optimize) {
.Debug => &.{ "cargo", build_cmd, "--target", rust_target },
else => &.{ "cargo", build_cmd, "--release", "--target", rust_target },
};
cargo_cmd = b.addSystemCommand(cargo_build_cmd);
cargo_cmd.?.has_side_effects = true;
cargo_cmd.?.setCwd(libsql_path);
b.getInstallStep().dependOn(&cargo_cmd.?.step);
}
}
const mod_tests = b.addTest(.{
.root_module = mod,
});
const run_mod_tests = b.addRunArtifact(mod_tests);
const test_step = b.step("test", "Run tests");
if (cargo_cmd != null) {
test_step.dependOn(&cargo_cmd.?.step);
}
test_step.dependOn(&run_mod_tests.step);
}
// TODO: This probably only works with x86_64 Linux
fn zigTargetToRustTarget(allocator: std.mem.Allocator, target: std.Target) ![]u8 {
const linux_target_str = try target.linuxTriple(allocator);
defer allocator.free(linux_target_str);
var split = std.mem.splitScalar(u8, linux_target_str, '-');
const arch = split.next() orelse unreachable;
const os = split.next() orelse unreachable;
const abi = split.next() orelse unreachable;
const rust_target_str = try std.fmt.allocPrint(allocator, "{s}-unknown-{s}-{s}", .{ arch, os, abi });
return rust_target_str;
}