-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
196 lines (175 loc) · 6.24 KB
/
build.zig
File metadata and controls
196 lines (175 loc) · 6.24 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
const std = @import("std");
const brotli_version = std.SemanticVersion{
.major = 1,
.minor = 1,
.patch = 0,
};
const brotli_common_sources = [_][]const u8{
"c/common/constants.c",
"c/common/context.c",
"c/common/dictionary.c",
"c/common/platform.c",
"c/common/shared_dictionary.c",
"c/common/transform.c",
};
const brotli_enc_sources = [_][]const u8{
"c/enc/backward_references.c",
"c/enc/backward_references_hq.c",
"c/enc/bit_cost.c",
"c/enc/block_splitter.c",
"c/enc/brotli_bit_stream.c",
"c/enc/cluster.c",
"c/enc/command.c",
"c/enc/compound_dictionary.c",
"c/enc/compress_fragment.c",
"c/enc/compress_fragment_two_pass.c",
"c/enc/dictionary_hash.c",
"c/enc/encode.c",
"c/enc/encoder_dict.c",
"c/enc/entropy_encode.c",
"c/enc/fast_log.c",
"c/enc/histogram.c",
"c/enc/literal_cost.c",
"c/enc/memory.c",
"c/enc/metablock.c",
"c/enc/static_dict_lut.c",
"c/enc/static_init.c",
"c/enc/static_dict.c",
"c/enc/utf8_util.c",
};
const brotli_dec_sources = [_][]const u8{
"c/dec/bit_reader.c",
"c/dec/decode.c",
"c/dec/huffman.c",
"c/dec/prefix.c",
"c/dec/state.c",
"c/dec/static_init.c",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const shared = b.option(bool, "shared", "Build libbrotli as a shared library") orelse false;
const static_libc = b.option(bool, "static_libc", "Link against static ziglibc instead of system libc") orelse true;
const brotli_upstream = b.dependency("brotli_upstream", .{});
const lib = b.addLibrary(.{
.name = "brotli",
.linkage = if (shared) .dynamic else .static,
.version = brotli_version,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
.sanitize_c = .off,
}),
});
configureBrotliLibrary(lib.root_module, brotli_upstream);
const ziglibc_dep = if (static_libc) b.dependency("ziglibc", .{
.target = target,
.optimize = optimize,
.trace = false,
}) else null;
const static_libc_artifact = if (ziglibc_dep) |dep| blk: {
const ziglibc_lib = findDependencyArtifactByLinkage(dep, "cguana", .static);
configureStaticLibc(lib.root_module, ziglibc_lib, dep);
break :blk ziglibc_lib;
} else null;
b.installArtifact(lib);
const mod = b.addModule("libbrotli", .{
.root_source_file = b.path("src/brotli.zig"),
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
});
mod.addIncludePath(brotli_upstream.path("c/include"));
mod.addIncludePath(brotli_upstream.path("c"));
mod.linkLibrary(lib);
if (static_libc_artifact) |artifact| {
configureStaticLibc(mod, artifact, ziglibc_dep.?);
}
const tests = b.addTest(.{
.root_module = b.addModule("libbrotli_tests", .{
.root_source_file = b.path("test/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
}),
});
tests.root_module.addImport("libbrotli", mod);
if (static_libc_artifact) |artifact| {
configureStaticLibc(tests.root_module, artifact, ziglibc_dep.?);
}
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_tests.step);
const example = b.addExecutable(.{
.name = "brotli-roundtrip",
.root_module = b.createModule(.{
.root_source_file = b.path("examples/brotli_roundtrip.zig"),
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
}),
});
example.root_module.addImport("libbrotli", mod);
if (static_libc_artifact) |artifact| {
configureStaticLibc(example.root_module, artifact, ziglibc_dep.?);
}
b.installArtifact(example);
const run_example = b.addRunArtifact(example);
const example_step = b.step("example", "Run brotli roundtrip example");
example_step.dependOn(&run_example.step);
const check = b.step("check", "Compile library, tests and example without running");
check.dependOn(&lib.step);
check.dependOn(&tests.step);
check.dependOn(&example.step);
}
fn configureBrotliLibrary(module: *std.Build.Module, dep: *std.Build.Dependency) void {
module.addIncludePath(.{ .cwd_relative = "include" });
module.addIncludePath(dep.path("c/include"));
module.addIncludePath(dep.path("c"));
module.addCSourceFiles(.{
.root = dep.path(""),
.files = &brotli_common_sources,
.flags = &.{"-std=c99"},
});
module.addCSourceFiles(.{
.root = dep.path(""),
.files = &brotli_enc_sources,
.flags = &.{"-std=c99"},
});
module.addCSourceFiles(.{
.root = dep.path(""),
.files = &brotli_dec_sources,
.flags = &.{"-std=c99"},
});
}
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) },
);
}