Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
263 lines (244 loc) · 13.2 KB
/
Copy pathbuild.zig
File metadata and controls
263 lines (244 loc) · 13.2 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// build.zig — jxc (HDR JXR → JXL batch converter)
//
// Builds jxc as a single statically-linked binary that runs on Windows,
// macOS, and Linux with no codec installation required.
//
// Vendored C libraries:
// - 4creators/jxrlib (Makefile-based, JPEG XR codec)
// - libjxl v0.11.2 (CMake-based, JPEG XL codec with HDR support)
//
// The pipeline:
// 1. jxrlib build step → vendor/jxrlib/build/lib{jpegxr,jxrglue}.a
// 2. libjxl build step → vendor/libjxl/build/lib/libjxl*.a
// 3. addTranslateC for each umbrella header → Zig modules "jxrlib" / "jxl"
// 4. addExecutable → statically links all .a archives + system brotli/lcms2
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// ─────────────────────────────────────────────────────────────────────
// Step 1: build jxrlib via its patched Makefile.
//
// The vendored Makefile is patched (see Phase 1 commit) to:
// - force -fPIC (so the .a works with Zig's linker)
// - suppress implicit-function-declaration errors (modern Clang is
// stricter than the 2017 code expects)
//
// The Makefile target that produces both archives is the default `all`.
// ─────────────────────────────────────────────────────────────────────
const jxrlib_make = b.addSystemCommand(&.{
if (target.result.os.tag == .windows) "mingw32-make" else "make",
"-j4",
"-C",
"vendor/jxrlib",
});
jxrlib_make.setEnvironmentVariable(
"CC",
if (target.result.os.tag == .windows) "gcc" else "cc",
);
const jxrlib_include_paths = &[_]std.Build.LazyPath{
b.path("vendor/jxrlib/common/include"),
b.path("vendor/jxrlib/image/sys"),
b.path("vendor/jxrlib/jxrgluelib"),
};
const jxrlib_archive_paths = [_]std.Build.LazyPath{
b.path("vendor/jxrlib/build/libjpegxr.a"),
b.path("vendor/jxrlib/build/libjxrglue.a"),
};
// ─────────────────────────────────────────────────────────────────────
// Step 2: build libjxl via CMake (configure + build).
//
// The CMake configure step is split from the build step so that the
// build step can depend on it without re-running configure when nothing
// has changed. The configure uses flags from the plan (Phase 2.3):
// - BUILD_SHARED_LIBS=OFF (produce .a)
// - BUILD_TESTING=OFF (skip gtest)
// - JPEGXL_ENABLE_FUZZERS/TOOLS/BENCHMARK/EXAMPLES/VIEWERS/PLUGINS=
// DOXYGEN/MANPAGES/JNI/SKCMS/SJPEG = OFF (minimal build)
// - JPEGXL_FORCE_SYSTEM_BROTLI/HWY/LCMS2 = ON (use MacPorts libs)
// - JPEGXL_BUNDLE_LIBPNG = OFF
// ─────────────────────────────────────────────────────────────────────
const libjxl_configure = if (target.result.os.tag == .windows)
b.addSystemCommand(&.{
"cmake",
"-G", "MinGW Makefiles",
"-DCMAKE_C_COMPILER=gcc",
"-DCMAKE_CXX_COMPILER=g++",
"-S", "vendor/libjxl",
"-B", "vendor/libjxl/build",
"-DCMAKE_BUILD_TYPE=Release",
"-DBUILD_SHARED_LIBS=OFF",
"-DBUILD_TESTING=OFF",
"-DJPEGXL_ENABLE_FUZZERS=OFF",
"-DJPEGXL_ENABLE_TOOLS=OFF",
"-DJPEGXL_ENABLE_BENCHMARK=OFF",
"-DJPEGXL_ENABLE_EXAMPLES=OFF",
"-DJPEGXL_ENABLE_VIEWERS=OFF",
"-DJPEGXL_ENABLE_PLUGINS=OFF",
"-DJPEGXL_ENABLE_DOXYGEN=OFF",
"-DJPEGXL_ENABLE_MANPAGES=OFF",
"-DJPEGXL_ENABLE_JNI=OFF",
"-DJPEGXL_ENABLE_SKCMS=OFF",
"-DJPEGXL_ENABLE_SJPEG=OFF",
"-DJPEGXL_BUNDLE_LIBPNG=OFF",
})
else
b.addSystemCommand(&.{
"cmake",
"-S", "vendor/libjxl",
"-B", "vendor/libjxl/build",
"-DCMAKE_BUILD_TYPE=Release",
"-DBUILD_SHARED_LIBS=OFF",
"-DBUILD_TESTING=OFF",
"-DJPEGXL_ENABLE_FUZZERS=OFF",
"-DJPEGXL_ENABLE_TOOLS=OFF",
"-DJPEGXL_ENABLE_BENCHMARK=OFF",
"-DJPEGXL_ENABLE_EXAMPLES=OFF",
"-DJPEGXL_ENABLE_VIEWERS=OFF",
"-DJPEGXL_ENABLE_PLUGINS=OFF",
"-DJPEGXL_ENABLE_DOXYGEN=OFF",
"-DJPEGXL_ENABLE_MANPAGES=OFF",
"-DJPEGXL_ENABLE_JNI=OFF",
"-DJPEGXL_ENABLE_SKCMS=OFF",
"-DJPEGXL_ENABLE_SJPEG=OFF",
"-DJPEGXL_BUNDLE_LIBPNG=OFF",
});
const libjxl_build = b.addSystemCommand(&.{
"cmake", "--build", "vendor/libjxl/build", "-j4",
});
libjxl_build.step.dependOn(&libjxl_configure.step);
const libjxl_include_paths = &[_]std.Build.LazyPath{
b.path("vendor/libjxl/lib/include"),
b.path("vendor/libjxl/build/lib/include"),
};
const libjxl_archive_paths = [_]std.Build.LazyPath{
b.path("vendor/libjxl/build/lib/libjxl.a"),
b.path("vendor/libjxl/build/lib/libjxl_cms.a"),
b.path("vendor/libjxl/build/lib/libjxl_threads.a"),
// brotli, hwy, lcms are vendored under vendor/libjxl/third_party/
// and libjxl's CMake configures them as separate static targets; their
// .a files live under build/third_party/.
b.path("vendor/libjxl/build/third_party/brotli/libbrotlienc.a"),
b.path("vendor/libjxl/build/third_party/brotli/libbrotlidec.a"),
b.path("vendor/libjxl/build/third_party/brotli/libbrotlicommon.a"),
b.path("vendor/libjxl/build/third_party/highway/libhwy.a"),
b.path("vendor/libjxl/build/third_party/liblcms2.a"),
};
// ─────────────────────────────────────────────────────────────────────
// Step 3: translate the C headers into Zig modules.
//
// The umbrella headers in src/c/ include only what we use. The translate-c
// step caches output based on input hashes; changes to the umbrella or to
// the vendored headers trigger re-translation automatically.
//
// We pass the vendored include paths explicitly so translate-c finds the
// headers (some use `<winspecstring.h>`-style angle-bracket includes that
// resolve via the include search path, not relative paths).
// ─────────────────────────────────────────────────────────────────────
const jxrlib_translate = b.addTranslateC(.{
.root_source_file = b.path("src/c/jxrlib_umbrella.h"),
.target = target,
.optimize = optimize,
});
jxrlib_translate.step.dependOn(&jxrlib_make.step);
inline for (jxrlib_include_paths) |p| jxrlib_translate.addIncludePath(p);
jxrlib_translate.addIncludePath(b.path("vendor/libjxl/lib/include")); // for compat headers
// jxrlib's public headers use `__ANSI__` to gate `FAR` macro expansion.
// The vendored Makefile defines this; we mirror it here for translate-c.
jxrlib_translate.defineCMacro("__ANSI__", "1");
jxrlib_translate.defineCMacro("DISABLE_PERF_MEASUREMENT", "1");
const libjxl_translate = b.addTranslateC(.{
.root_source_file = b.path("src/c/libjxl_umbrella.h"),
.target = target,
.optimize = optimize,
});
libjxl_translate.step.dependOn(&libjxl_build.step);
inline for (libjxl_include_paths) |p| libjxl_translate.addIncludePath(p);
// ─────────────────────────────────────────────────────────────────────
// Step 4: build the jxc executable.
// ─────────────────────────────────────────────────────────────────────
const exe = b.addExecutable(.{
.name = "jxc",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
// Zig's bundled libcxx is the right C++ runtime on macOS (matches
// Apple's libc++). On Linux/MinGW libjxl was built with gcc/g++
// → libstdc++, so we must NOT also pull in Zig's libcxx (different
// ABI). The explicit linkSystemLibrary("stdc++") below provides
// the matching runtime.
.link_libcpp = target.result.os.tag == .macos,
.imports = &.{
.{ .name = "jxrlib", .module = jxrlib_translate.createModule() },
.{ .name = "libjxl", .module = libjxl_translate.createModule() },
},
}),
});
// Statically link the vendored .a archives.
const root_mod = exe.root_module;
inline for (jxrlib_archive_paths) |p| root_mod.addObjectFile(p);
inline for (libjxl_archive_paths) |p| root_mod.addObjectFile(p);
// highway / libjxl were built with emulated TLS, which leaves an
// unresolved reference to `__emutls_get_address`. Zig's compiler-rt
// provides one on Android / OpenHarmony / OpenBSD but not Windows;
// tools/emutls_stub.o supplies a single-threaded implementation
// sufficient to satisfy the linker (jxc's CLI process does not
// itself use emutls).
if (target.result.os.tag == .windows) {
root_mod.addObjectFile(b.path("tools/emutls_stub.o"));
}
// brotli, hwy, and lcms2 are vendored under vendor/libjxl/third_party/
// and built as part of libjxl's CMake subdirectories, so their symbols
// are already in libjxl.a / libjxl_cms.a — no separate link needed.
// Link the C++ runtime that matches what libjxl was actually built
// against. `.link_libcpp = true` links Zig's bundled libcxx, which is
// correct on macOS (matches Apple's libc++) but wrong on Linux/MinGW
// (where libjxl was built with gcc/g++ → libstdc++). The mismatched
// mangled symbols cause undefined-reference errors at link time.
if (target.result.os.tag == .macos) {
root_mod.linkSystemLibrary("c++", .{});
} else if (target.result.os.tag == .windows) {
// libjxl is C++ and was built with gcc/g++ on MinGW, so it
// references libstdc++. Zig ≥ 0.16 treats "stdc++" as a known
// C++ runtime name and just sets link_libcpp=true (which would
// link libcxx instead) rather than emitting -lstdc++. Add the
// .a directly via addObjectFile to bypass that detection.
// GitHub-hosted windows-latest ships Strawberry Perl's MinGW-w64
// 13.2.0 at C:\Strawberry\c\, with the static .a under
// c\lib\libstdc++.a. msys2/setup-msys2@v2 was a no-op on this
// runner, so we use whatever is actually on disk.
root_mod.addObjectFile(.{ .cwd_relative = "C:/Strawberry/c/lib/libstdc++.a" });
root_mod.linkSystemLibrary("gcc_s", .{});
root_mod.linkSystemLibrary("gcc", .{});
root_mod.linkSystemLibrary("pthread", .{});
root_mod.linkSystemLibrary("m", .{});
} else {
// Same workaround for Linux. Ubuntu ships the static libstdc++.a
// inside the gcc versioned directory; ubuntu-latest (24.04) is on
// gcc-12 by default.
root_mod.addObjectFile(.{ .cwd_relative = "/usr/lib/gcc/x86_64-linux-gnu/12/libstdc++.a" });
root_mod.linkSystemLibrary("gcc_s", .{});
root_mod.linkSystemLibrary("gcc", .{});
root_mod.linkSystemLibrary("pthread", .{});
root_mod.linkSystemLibrary("m", .{});
}
b.installArtifact(exe);
// ─────────────────────────────────────────────────────────────────────
// Top-level steps.
// ─────────────────────────────────────────────────────────────────────
const run_step = b.step("run", "Run jxc");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args);
const test_step = b.step("test", "Run tests (none yet)");
_ = test_step;
// Convenience steps for the C library builds (so devs can iterate on
// just one library without paying for both).
const build_jxrlib = b.step("build-jxrlib", "Build vendored jxrlib only");
build_jxrlib.dependOn(&jxrlib_make.step);
const build_libjxl = b.step("build-libjxl", "Build vendored libjxl only");
build_libjxl.dependOn(&libjxl_build.step);
}