-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.zig
190 lines (163 loc) · 6.88 KB
/
build.zig
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
const std = @import("std");
//const deps = @import("deps.zig");
const EXECUTABLES = .{
.{ "awtfdb-manage", "src/main.zig" },
.{ "awtfdb-watcher", "src/rename_watcher_main.zig" },
.{ "awtfdb-janitor", "src/janitor_main.zig" },
.{ "ainclude", "src/include_main.zig" },
.{ "afind", "src/find_main.zig" },
.{ "als", "src/ls_main.zig" },
.{ "arm", "src/rm_main.zig" },
.{ "atags", "src/tags_main.zig" },
.{ "awtfdb-metrics", "src/metrics_main.zig" },
.{ "amv", "src/mv_main.zig" },
};
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const optimize = b.standardOptimizeOption(.{});
const sqlite_pkg = b.dependency("sqlite", .{ .optimize = optimize, .target = target });
const pcre_pkg = b.dependency("libpcre.zig", .{ .optimize = optimize, .target = target });
const magic_pkg = b.dependency("libmagic.zig", .{ .optimize = optimize, .target = target });
const expiring_hash_map_pkg = b.dependency("expiring_hash_map", .{ .optimize = optimize, .target = target });
const tunez_pkg = b.dependency("tunez", .{ .optimize = optimize, .target = target });
const ulid_pkg = b.dependency("zig-ulid", .{ .optimize = optimize, .target = target });
const libexif_pkg = b.dependency("libexif", .{ .optimize = optimize, .target = target });
const Mod = struct { name: []const u8, mod: *std.Build.Module };
const mod_deps = &[_]Mod{
.{ .name = "sqlite", .mod = sqlite_pkg.module("sqlite") },
.{ .name = "libpcre", .mod = pcre_pkg.module("libpcre") },
.{ .name = "libmagic.zig", .mod = magic_pkg.module("libmagic") },
.{ .name = "expiring_hash_map", .mod = expiring_hash_map_pkg.module("expiring-hash-map") },
.{ .name = "tunez", .mod = tunez_pkg.module("tunez") },
.{ .name = "ulid", .mod = ulid_pkg.module("zig-ulid") },
};
const exif_artifact = libexif_pkg.artifact("exif");
if (target.result.os.tag == .macos) {
// i really dislike macos
exif_artifact.linkLibC();
exif_artifact.linkSystemLibrary("intl");
// macports
exif_artifact.addLibraryPath(.{ .cwd_relative = "/opt/local/lib" });
exif_artifact.addIncludePath(.{ .cwd_relative = "/opt/local/include" });
// homebrew
exif_artifact.addLibraryPath(.{ .cwd_relative = "/opt/homebrew/lib" });
exif_artifact.addIncludePath(.{ .cwd_relative = "/opt/homebrew/include" });
}
const static_deps = &[_]*std.Build.Step.Compile{
sqlite_pkg.artifact("sqlite"),
exif_artifact,
};
const exe_tests = b.addTest(
.{
.root_source_file = b.path("src/main.zig"),
.optimize = optimize,
.target = target,
},
);
if (target.result.os.tag == .macos) {
// macports
exe_tests.addLibraryPath(.{ .cwd_relative = "/opt/local/lib" });
// homebrew
exe_tests.addLibraryPath(.{ .cwd_relative = "/opt/homebrew/lib" });
}
const run_unit_tests = b.addRunArtifact(exe_tests);
for (mod_deps) |dep| {
exe_tests.root_module.addImport(dep.name, dep.mod);
}
for (static_deps) |lib| {
exe_tests.linkLibrary(lib);
}
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
// TODO make test spit out the path on stdout
const build_test_step = b.step("build-test-only", "Only build tests (sidestepping some sort of bug on 0.13)");
build_test_step.dependOn(&exe_tests.step);
//if (optimize == .Debug or optimize == .ReleaseSafe) {
if (true) {
// faster build by making a single executable
const single_exe = b.addExecutable(
.{
.name = "wrapper-awtfdb",
.root_source_file = b.path("src/wrapmain.zig"),
.optimize = optimize,
.target = target,
},
);
for (mod_deps) |dep| {
single_exe.root_module.addImport(dep.name, dep.mod);
}
for (static_deps) |lib| {
single_exe.linkLibrary(lib);
}
if (target.result.os.tag == .macos) {
// macports
single_exe.addLibraryPath(.{ .cwd_relative = "/opt/local/lib" });
// homebrew
single_exe.addLibraryPath(.{ .cwd_relative = "/opt/homebrew/lib" });
}
b.installArtifact(single_exe);
const hardlink_install = try b.allocator.create(CustomHardLinkStep);
const hardlink_step = std.Build.Step.init(.{
.id = .custom,
.name = "link the utils",
.owner = b,
.makeFn = CustomHardLinkStep.make,
});
hardlink_install.* = .{
.builder = b,
.step = hardlink_step,
.exe = single_exe,
};
hardlink_install.step.dependOn(&single_exe.step);
b.getInstallStep().dependOn(&hardlink_install.step);
}
// } else {
// // release modes build all exes separately
// inline for (EXECUTABLES) |exec_decl| {
// const exec_name = exec_decl.@"0";
// const exec_entrypoint = exec_decl.@"1";
//
// const tool_exe = b.addExecutable(
// .{
// .name = exec_name,
// .root_source_file = .{ .path = exec_entrypoint },
// .optimize = optimize,
// .target = target,
// },
// );
//
// b.installArtifact(tool_exe);
// comptime addAllTo(mod_deps, static_deps, tool_exe);
// }
// }
}
const CustomHardLinkStep = struct {
builder: *std.Build,
step: std.Build.Step,
exe: *std.Build.Step.Compile,
const Self = @This();
fn make(step: *std.Build.Step, node: std.Progress.Node) !void {
_ = node;
const self: *Self = @fieldParentPtr("step", step);
const builder = self.builder;
const wrapmain_path = self.exe.getEmittedBin().getPath(builder);
inline for (EXECUTABLES) |exec_decl| {
const exec_name = exec_decl.@"0";
const full_dest_path = builder.getInstallPath(.{ .bin = {} }, exec_name);
std.debug.print("{s} -> {s}\n", .{ wrapmain_path, full_dest_path });
_ = try std.fs.Dir.updateFile(
std.fs.cwd(),
wrapmain_path,
std.fs.cwd(),
full_dest_path,
.{},
);
}
}
};