-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.zig
156 lines (135 loc) · 5.4 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
const std = @import("std");
const Build = std.Build;
const basisu_root = sdkPath("/upstream/basisu");
var _module: ?*std.build.Module = null;
pub fn module(b: *std.Build) *std.build.Module {
if (_module) |m| return m;
_module = b.createModule(.{
.source = .{
.path = "src/main.zig",
},
});
return _module.?;
}
pub const Options = struct {
encoder: ?EncoderOptions,
transcoder: ?TranscoderOptions,
};
pub const EncoderOptions = struct {
install_libs: bool = false,
};
pub const TranscoderOptions = struct {
install_libs: bool = false,
};
pub fn build(b: *Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&testStep(b, optimize, target).step);
}
pub fn testStep(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTest(.{
.name = "basisu-tests",
.root_source_file = .{ .path = sdkPath("/src/main.zig") },
.target = target,
.optimize = optimize,
});
main_tests.main_pkg_path = sdkPath("/");
link(b, main_tests, target, optimize, .{
.encoder = .{},
.transcoder = .{},
});
main_tests.install();
return main_tests.run();
}
pub fn link(b: *Build, step: *std.build.CompileStep, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode, options: Options) void {
if (options.encoder) |encoder_options| {
step.linkLibrary(buildEncoder(b, target, optimize, encoder_options));
step.addCSourceFile(sdkPath("/src/encoder/wrapper.cpp"), &.{});
step.addIncludePath(basisu_root ++ "/encoder");
}
if (options.transcoder) |transcoder_options| {
step.linkLibrary(buildTranscoder(b, target, optimize, transcoder_options));
step.addCSourceFile(sdkPath("/src/transcoder/wrapper.cpp"), &.{});
step.addIncludePath(basisu_root ++ "/transcoder");
}
}
pub fn buildEncoder(b: *Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode, options: EncoderOptions) *std.build.CompileStep {
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
const encoder = b.addStaticLibrary(.{
.name = "basisu-encoder",
.target = target,
.optimize = optimize,
});
encoder.linkLibCpp();
encoder.addCSourceFiles(
encoder_sources,
&.{},
);
encoder.defineCMacro("BASISU_FORCE_DEVEL_MESSAGES", "0");
encoder.defineCMacro("BASISD_SUPPORT_KTX2_ZSTD", "0");
if (options.install_libs)
encoder.install();
return encoder;
}
pub fn buildTranscoder(b: *Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode, options: TranscoderOptions) *std.build.CompileStep {
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
const transcoder = b.addStaticLibrary(.{
.name = "basisu-transcoder",
.target = target,
.optimize = optimize,
});
transcoder.linkLibCpp();
transcoder.addCSourceFiles(transcoder_sources, &.{
"-Wno-deprecated-builtins",
"-Wno-deprecated-declarations",
"-Wno-array-bounds",
});
transcoder.defineCMacro("BASISU_FORCE_DEVEL_MESSAGES", "0");
transcoder.defineCMacro("BASISD_SUPPORT_KTX2_ZSTD", "0");
if (options.install_libs)
transcoder.install();
return transcoder;
}
fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !void {
if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |no_ensure_submodules| {
defer allocator.free(no_ensure_submodules);
if (std.mem.eql(u8, no_ensure_submodules, "true")) return;
} else |_| {}
var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator);
child.cwd = sdkPath("/");
child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut();
_ = try child.spawnAndWait();
}
fn sdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}
const transcoder_sources = &[_][]const u8{
basisu_root ++ "/transcoder/basisu_transcoder.cpp",
};
const encoder_sources = &[_][]const u8{
basisu_root ++ "/encoder/basisu_backend.cpp",
basisu_root ++ "/encoder/basisu_basis_file.cpp",
basisu_root ++ "/encoder/basisu_bc7enc.cpp",
basisu_root ++ "/encoder/basisu_comp.cpp",
basisu_root ++ "/encoder/basisu_enc.cpp",
basisu_root ++ "/encoder/basisu_etc.cpp",
basisu_root ++ "/encoder/basisu_frontend.cpp",
basisu_root ++ "/encoder/basisu_gpu_texture.cpp",
basisu_root ++ "/encoder/basisu_kernels_sse.cpp",
basisu_root ++ "/encoder/basisu_opencl.cpp",
basisu_root ++ "/encoder/basisu_pvrtc1_4.cpp",
basisu_root ++ "/encoder/basisu_resample_filters.cpp",
basisu_root ++ "/encoder/basisu_resampler.cpp",
basisu_root ++ "/encoder/basisu_ssim.cpp",
basisu_root ++ "/encoder/basisu_uastc_enc.cpp",
basisu_root ++ "/encoder/jpgd.cpp",
basisu_root ++ "/encoder/pvpngreader.cpp",
};