-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
174 lines (155 loc) · 5.75 KB
/
build.zig
File metadata and controls
174 lines (155 loc) · 5.75 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
const std = @import("std");
const zstd_version = std.SemanticVersion{
.major = 1,
.minor = 5,
.patch = 7,
};
const zstd_sources = [_][]const u8{
"lib/common/debug.c",
"lib/common/entropy_common.c",
"lib/common/error_private.c",
"lib/common/fse_decompress.c",
"lib/common/pool.c",
"lib/common/threading.c",
"lib/common/xxhash.c",
"lib/common/zstd_common.c",
"lib/compress/fse_compress.c",
"lib/compress/hist.c",
"lib/compress/huf_compress.c",
"lib/compress/zstd_compress.c",
"lib/compress/zstd_compress_literals.c",
"lib/compress/zstd_compress_sequences.c",
"lib/compress/zstd_compress_superblock.c",
"lib/compress/zstd_double_fast.c",
"lib/compress/zstd_fast.c",
"lib/compress/zstd_lazy.c",
"lib/compress/zstd_ldm.c",
"lib/compress/zstd_opt.c",
"lib/compress/zstd_preSplit.c",
"lib/decompress/huf_decompress.c",
"lib/decompress/zstd_ddict.c",
"lib/decompress/zstd_decompress.c",
"lib/decompress/zstd_decompress_block.c",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const shared = b.option(bool, "shared", "Build libzstd 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 zstd_upstream = b.dependency("zstd_upstream", .{});
const lib = b.addLibrary(.{
.name = "zstd",
.linkage = if (shared) .dynamic else .static,
.version = zstd_version,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
.sanitize_c = .off,
}),
});
configureZstdLibrary(lib.root_module, zstd_upstream, &zstd_sources);
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("libzstd", .{
.root_source_file = b.path("src/zstd.zig"),
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
});
mod.addIncludePath(zstd_upstream.path("lib"));
mod.linkLibrary(lib);
if (static_libc_artifact) |artifact| {
configureStaticLibc(mod, artifact, ziglibc_dep.?);
}
const tests = b.addTest(.{
.root_module = b.addModule("libzstd_tests", .{
.root_source_file = b.path("test/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
}),
});
tests.root_module.addImport("libzstd", 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 tests");
test_step.dependOn(&run_tests.step);
const example = b.addExecutable(.{
.name = "zstd-roundtrip",
.root_module = b.createModule(.{
.root_source_file = b.path("examples/zstd_roundtrip.zig"),
.target = target,
.optimize = optimize,
.link_libc = !static_libc,
}),
});
example.root_module.addImport("libzstd", 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 zstd 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 configureZstdLibrary(
module: *std.Build.Module,
dep: *std.Build.Dependency,
files: []const []const u8,
) void {
module.addIncludePath(dep.path("lib"));
module.addCMacro("XXH_NAMESPACE", "ZSTD_");
module.addCMacro("ZSTD_DISABLE_ASM", "1");
module.addCMacro("ZSTD_LEGACY_SUPPORT", "0");
module.addCSourceFiles(.{
.root = dep.path(""),
.files = files,
.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) },
);
}