-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
223 lines (198 loc) · 7.25 KB
/
build.zig
File metadata and controls
223 lines (198 loc) · 7.25 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const std = @import("std");
const unarr_version = std.SemanticVersion{
.major = 1,
.minor = 2,
.patch = 0,
};
const unarr_version_string = "1.2.0";
const base_sources = [_][]const u8{
"_7z/_7z.c",
"common/conv.c",
"common/crc32.c",
"common/stream.c",
"common/unarr.c",
"lzmasdk/CpuArch.c",
"lzmasdk/LzmaDec.c",
"lzmasdk/Ppmd7.c",
"lzmasdk/Ppmd7Dec.c",
"lzmasdk/Ppmd7aDec.c",
"lzmasdk/Ppmd8.c",
"lzmasdk/Ppmd8Dec.c",
"rar/filter-rar.c",
"rar/huffman-rar.c",
"rar/parse-rar.c",
"rar/rar.c",
"rar/rarvm.c",
"rar/uncompress-rar.c",
"tar/parse-tar.c",
"tar/tar.c",
"zip/inflate.c",
"zip/parse-zip.c",
"zip/uncompress-zip.c",
"zip/zip.c",
};
const seven_zip_sources = [_][]const u8{
"lzmasdk/7zArcIn.c",
"lzmasdk/7zBuf.c",
"lzmasdk/7zDec.c",
"lzmasdk/7zStream.c",
"lzmasdk/Bcj2.c",
"lzmasdk/Bra.c",
"lzmasdk/Bra86.c",
"lzmasdk/Delta.c",
"lzmasdk/Lzma2Dec.c",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const shared = b.option(bool, "shared", "Build libunarr as a shared library") orelse false;
const enable_7z = b.option(bool, "enable_7z", "Enable 7z format support") orelse true;
const static_libc = b.option(bool, "static_libc", "Link against static ziglibc instead of system libc") orelse true;
const unarr_upstream = b.dependency("unarr_upstream", .{});
const generated_unarr_h = b.addConfigHeader(.{
.style = .{ .cmake = unarr_upstream.path("unarr.h.in") },
.include_path = "unarr.h",
}, .{
.unarr_VERSION_MAJOR = @as(i64, @intCast(unarr_version.major)),
.unarr_VERSION_MINOR = @as(i64, @intCast(unarr_version.minor)),
.unarr_VERSION_PATCH = @as(i64, @intCast(unarr_version.patch)),
.unarr_VERSION = unarr_version_string,
});
const lib = b.addLibrary(.{
.name = "unarr",
.linkage = if (shared) .dynamic else .static,
.version = unarr_version,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
.sanitize_c = .off,
}),
});
lib.root_module.addIncludePath(unarr_upstream.path(""));
lib.root_module.addConfigHeader(generated_unarr_h);
lib.root_module.addCMacro("_FILE_OFFSET_BITS", "64");
lib.root_module.addCMacro("UNARR_EXPORT_SYMBOLS", "1");
if (shared) lib.root_module.addCMacro("UNARR_IS_SHARED_LIBRARY", "1");
lib.root_module.addCSourceFiles(.{
.root = unarr_upstream.path(""),
.files = &base_sources,
.flags = &.{"-std=c99"},
});
if (enable_7z) {
lib.root_module.addCMacro("HAVE_7Z", "1");
lib.root_module.addCMacro("Z7_PPMD_SUPPORT", "1");
lib.root_module.addCSourceFiles(.{
.root = unarr_upstream.path(""),
.files = &seven_zip_sources,
.flags = &.{"-std=c99"},
});
}
const static_libc_artifact = if (static_libc) blk: {
const ziglibc_dep = b.lazyDependency("ziglibc", .{
.target = target,
.optimize = optimize,
.trace = false,
}) orelse return;
const ziglibc_lib = findDependencyArtifactByLinkage(ziglibc_dep, "cguana", .static);
configureStaticLibc(lib.root_module, ziglibc_lib, ziglibc_dep);
break :blk ziglibc_lib;
} else null;
lib.installConfigHeader(generated_unarr_h);
b.installArtifact(lib);
var lib_for_tests = lib;
if (static_libc) {
const test_lib = b.addLibrary(.{
.name = "unarr_test",
.linkage = .static,
.version = unarr_version,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = false,
.sanitize_c = .off,
}),
});
test_lib.root_module.addIncludePath(unarr_upstream.path(""));
test_lib.root_module.addConfigHeader(generated_unarr_h);
test_lib.root_module.addCMacro("_FILE_OFFSET_BITS", "64");
test_lib.root_module.addCMacro("UNARR_EXPORT_SYMBOLS", "1");
test_lib.root_module.addCSourceFiles(.{
.root = unarr_upstream.path(""),
.files = &base_sources,
.flags = &.{"-std=c99"},
});
if (enable_7z) {
test_lib.root_module.addCMacro("HAVE_7Z", "1");
test_lib.root_module.addCMacro("Z7_PPMD_SUPPORT", "1");
test_lib.root_module.addCSourceFiles(.{
.root = unarr_upstream.path(""),
.files = &seven_zip_sources,
.flags = &.{"-std=c99"},
});
}
if (static_libc_artifact) |_| {
const ziglibc_dep = b.lazyDependency("ziglibc", .{
.target = target,
.optimize = optimize,
.trace = false,
}) orelse return;
configureStaticLibc(test_lib.root_module, findDependencyArtifactByLinkage(ziglibc_dep, "cguana", .static), ziglibc_dep);
}
lib_for_tests = test_lib;
}
const zig_api = b.addModule("unarr", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
});
zig_api.addConfigHeader(generated_unarr_h);
if (static_libc_artifact) |artifact| {
const ziglibc_dep = b.lazyDependency("ziglibc", .{
.target = target,
.optimize = optimize,
.trace = false,
}) orelse return;
configureStaticLibc(zig_api, artifact, ziglibc_dep);
}
zig_api.linkLibrary(lib_for_tests);
const tests = b.addTest(.{
.root_module = zig_api,
});
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run Zig API tests");
test_step.dependOn(&run_tests.step);
const check = b.step("check", "Compile libunarr without installing");
check.dependOn(&lib.step);
}
fn configureStaticLibc(module: *std.Build.Module, artifact: *std.Build.Step.Compile, dep: *std.Build.Dependency) void {
module.addIncludePath(dep.path("inc/libc"));
module.addIncludePath(dep.path("inc/posix"));
module.addIncludePath(dep.path("inc/gnu"));
module.linkLibrary(artifact);
}
fn findDependencyArtifactByLinkage(
dep: *std.Build.Dependency,
name: []const u8,
linkage: std.builtin.LinkMode,
) *std.Build.Step.Compile {
var found: ?*std.Build.Step.Compile = null;
for (dep.builder.install_tls.step.dependencies.items) |dep_step| {
const install_artifact = dep_step.cast(std.Build.Step.InstallArtifact) orelse continue;
if (!std.mem.eql(u8, install_artifact.artifact.name, name)) continue;
if (install_artifact.artifact.linkage != linkage) continue;
if (found != null) {
std.debug.panic(
"artifact '{s}' with linkage '{s}' is ambiguous in dependency",
.{ name, @tagName(linkage) },
);
}
found = install_artifact.artifact;
}
if (found) |artifact| return artifact;
std.debug.panic(
"unable to find artifact '{s}' with linkage '{s}' in dependency install graph",
.{ name, @tagName(linkage) },
);
}