Repository navigation
Expand file tree
/
Copy pathcommon.nix
More file actions
345 lines (321 loc) · 11.5 KB
/
Copy pathcommon.nix
File metadata and controls
345 lines (321 loc) · 11.5 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
{ inputs, system }:
let
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [ inputs.rust-overlay.overlays.default ];
};
version = "0.55.1";
cargoLockConfig = {
lockFile = ../Cargo.lock;
};
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
targets = [
"wasm32-unknown-unknown"
"x86_64-unknown-linux-musl"
"aarch64-unknown-linux-musl"
];
extensions = [
"clippy"
"llvm-tools"
];
};
rustPlatform = pkgs.makeRustPlatform {
cargo = rustToolchain;
rustc = rustToolchain;
};
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain rustToolchain;
# Shared source filtering — only include Rust/Cargo files and assets crane
# needs.
src =
let
# Keep Cargo manifests, Rust source, build scripts, and non-Rust assets
# the build needs (web dist, man pages, etc.).
filter =
path: type:
(craneLib.filterCargoSources path type)
|| pkgs.lib.hasSuffix ".html" path
|| pkgs.lib.hasSuffix ".html.br" path
|| baseNameOf path == "learn.md"
|| pkgs.lib.hasSuffix "/crates/guest/README.md" path
|| baseNameOf path == "LICENSE"
|| pkgs.lib.hasInfix "/js/ui/dist/" path
|| pkgs.lib.hasSuffix ".h264" path
|| pkgs.lib.hasSuffix ".xkb" path
|| pkgs.lib.hasSuffix ".spv" path
# C shims a vendored crate's build.rs compiles (wayland-backend's
# log_shim.c, behind its `log` feature).
|| pkgs.lib.hasSuffix ".c" path;
in
pkgs.lib.cleanSourceWith {
src = ../.;
inherit filter;
};
# `[patch.crates-io]` points wayland-backend at vendor/ (see Cargo.toml), and
# crane's deps-only build replaces every Cargo.toml it finds in the source
# tree with a stub crate. Stubbing the workspace members is the whole point;
# stubbing this one is not. It is a real dependency of wayland-server, which
# the deps-only build compiles for real, and wayland-server does not compile
# against an empty `wayland_backend` — 17 unresolved imports. So restore the
# vendored crate on top of the dummy tree.
#
# Filtered on its own rather than reused out of `src` so that editing blit's
# own sources does not invalidate the dependency cache.
vendoredWaylandBackend = pkgs.lib.cleanSourceWith {
src = ../vendor/wayland-backend;
filter = path: type: (craneLib.filterCargoSources path type) || pkgs.lib.hasSuffix ".c" path;
};
restoreVendoredCrates = {
extraDummyScript = ''
rm -rf $out/vendor/wayland-backend
cp --recursive --no-preserve=ownership ${vendoredWaylandBackend} $out/vendor/wayland-backend
chmod +w -R $out/vendor/wayland-backend
'';
};
# Common args shared by all crane builds.
commonArgs = {
inherit src version;
strictDeps = true;
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [
pkgs.libopus # system Opus for audiopus_sys (avoids cmake source build)
]
# System x264 for x264-sys (H.264 software surface encoder). The
# dependency is Linux-only in crates/server/Cargo.toml.
++ pkgs.lib.optional pkgs.stdenv.isLinux pkgs.x264;
nativeCheckInputs = [ ];
}
// pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
BINDGEN_EXTRA_CLANG_ARGS = "-isystem ${pkgs.lib.getDev pkgs.stdenv.cc.libc}/include";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
nativeBuildInputs = [
pkgs.pkg-config
pkgs.llvmPackages.libclang
];
};
# Build workspace deps once — reused by the workspace build.
cargoArtifacts = craneLib.buildDepsOnly (
commonArgs
// restoreVendoredCrates
// {
pname = "blit-workspace-deps";
cargoExtraArgs = "--workspace --exclude blit-browser";
doCheck = false;
}
);
# LLVM-based musl static environment for release tarballs.
# Combines isStatic (musl) + useLLVM (clang/lld/compiler-rt/libc++)
# so we get PIC-clean C++ libs and avoid the libgcc_s hacks that the
# GCC musl toolchain requires.
pkgsStaticLLVM =
if pkgs.stdenv.isLinux then
import inputs.nixpkgs {
inherit system;
overlays = [ inputs.rust-overlay.overlays.default ];
crossSystem = {
isStatic = true;
useLLVM = true;
linker = "lld";
config = pkgs.lib.systems.parse.tripleFromSystem (
pkgs.lib.systems.parse.mkMuslSystem pkgs.stdenv.hostPlatform.parsed
);
};
}
else
# macOS doesn't cross-compile to musl — use pkgsStatic as-is.
pkgs.pkgsStatic;
craneLibStatic = (inputs.crane.mkLib pkgsStaticLLVM).overrideToolchain (
p:
p.rust-bin.stable.latest.default.override {
targets = [
"wasm32-unknown-unknown"
"x86_64-unknown-linux-musl"
"aarch64-unknown-linux-musl"
];
extensions = [
"clippy"
"llvm-tools"
];
}
);
# Opus's meson.build doesn't support arm64 intrinsics, so the default
# -Dintrinsics=enabled fails on aarch64 in pkgsStatic. Disable
# intrinsics and rtcd (runtime CPU detection depends on intrinsics).
staticLibopus =
if pkgs.stdenv.hostPlatform.isAarch64 then
pkgsStaticLLVM.libopus.overrideAttrs (old: {
mesonFlags = map (
f:
if f == "-Dintrinsics=enabled" then
"-Dintrinsics=disabled"
else if f == "-Drtcd=enabled" then
"-Drtcd=disabled"
else
f
) (old.mesonFlags or [ ]);
})
else
pkgsStaticLLVM.libopus;
commonArgsStatic = {
inherit src version;
strictDeps = true;
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [
staticLibopus
]
# pkgsStatic x264 is built --enable-static --disable-shared.
++ pkgs.lib.optional pkgs.stdenv.isLinux pkgsStaticLLVM.x264;
# Link musl libc dynamically so dlopen works (GPU acceleration).
RUSTFLAGS = "-C target-feature=-crt-static";
}
// pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
# Make system-deps link libx264.a explicitly rather than defaulting
# to dynamic-style -lx264.
SYSTEM_DEPS_X264_LINK = "static";
CARGO_BUILD_TARGET = pkgsStaticLLVM.stdenv.hostPlatform.rust.rustcTargetSpec;
# Tell the `cc` crate to link libc++ instead of libstdc++ (LLVM toolchain).
CXXSTDLIB = "c++";
BINDGEN_EXTRA_CLANG_ARGS = "-isystem ${pkgs.lib.getDev pkgsStaticLLVM.stdenv.cc.libc}/include";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
nativeBuildInputs = [
pkgs.pkg-config
pkgs.llvmPackages.libclang
];
# Rustc hardcodes `-lgcc_s` for dynamic-musl targets. With the
# LLVM toolchain we provide compiler-rt builtins under that name.
# Scoped to the musl target via NIX_LDFLAGS_<role> so the glibc CC
# used for build scripts is unaffected.
postUnpack =
let
compilerRt = pkgsStaticLLVM.llvmPackages.compiler-rt;
role =
builtins.replaceStrings [ "-" ] [ "_" ]
pkgsStaticLLVM.stdenv.hostPlatform.rust.rustcTargetSpec;
in
''
export NIX_CFLAGS_LINK=""
mkdir -p $TMPDIR/rt-compat
builtins_lib=$(echo ${compilerRt}/lib/*/libclang_rt.builtins-*.a)
ln -s "$builtins_lib" $TMPDIR/rt-compat/libgcc_s.a
export NIX_LDFLAGS_${role}="-L$TMPDIR/rt-compat ''${NIX_LDFLAGS_${role}:-}"
'';
};
cargoArtifactsStatic = craneLibStatic.buildDepsOnly (
commonArgsStatic
// restoreVendoredCrates
// {
pname = "blit-workspace-deps-static";
cargoExtraArgs = "--workspace --exclude blit-browser";
doCheck = false;
}
);
# ------------------------------------------------------------------
# Glibc + cargo-zigbuild for portable Linux release binaries.
#
# cargo-zigbuild uses zig as the linker to enforce a glibc version
# floor. It also sets CC/CXX to zig wrappers for C deps.
#
# aws-lc-sys's default cc-crate builder falsely detects zig cc as
# a buggy GCC (memcmp check). Setting AWS_LC_SYS_CMAKE_BUILDER=1
# switches it to cmake, which identifies zig cc as Clang and works.
# ------------------------------------------------------------------
minGlibcVersion = "2.31";
rustTargetGnu =
if pkgs.stdenv.hostPlatform.isAarch64 then
"aarch64-unknown-linux-gnu"
else
"x86_64-unknown-linux-gnu";
# Static libopus for the glibc release build so the binary is
# fully self-contained (only glibc itself is dynamic).
gnuStaticLibopus = pkgs.libopus.overrideAttrs (old: {
mesonFlags = (old.mesonFlags or [ ]) ++ [ "-Ddefault_library=static" ];
});
# Static libx264 for the glibc release build, same reasoning as libopus:
# zig's linker rejects .so files built against a newer glibc than the
# target version, so the library must be linked statically.
#
# Static archives dodge that version check but still carry symbol
# references from whatever glibc headers compiled them: nixpkgs'
# glibc (>= 2.38) redirects sscanf to __isoc23_sscanf under
# _GNU_SOURCE, which doesn't exist at the 2.31 floor and breaks the
# final link. Compile with zig cc targeting the same floor instead.
zigTargetGnu =
if pkgs.stdenv.hostPlatform.isAarch64 then
"aarch64-linux-gnu.${minGlibcVersion}"
else
"x86_64-linux-gnu.${minGlibcVersion}";
# pkgs.zig is referenced by path, not via nativeBuildInputs: its
# setup hooks would take over the configure/build phases.
gnuStaticX264 = pkgs.x264.overrideAttrs (old: {
configureFlags =
map (f: if f == "--enable-shared" then "--enable-static" else f) (old.configureFlags or [ ])
++ [
"--disable-shared"
# zig cc promotes -Wdate-time to an error; the x264 CLI's
# version banner uses __DATE__.
"--extra-cflags=-Wno-date-time"
];
preConfigure = (old.preConfigure or "") + ''
export ZIG_GLOBAL_CACHE_DIR=$TMPDIR/zig-global-cache
export ZIG_LOCAL_CACHE_DIR=$TMPDIR/zig-local-cache
export CC="${pkgs.zig}/bin/zig cc -target ${zigTargetGnu}"
'';
});
commonArgsGnu = {
inherit src version;
strictDeps = true;
nativeBuildInputs = [
pkgs.pkg-config
pkgs.llvmPackages.libclang
pkgs.cargo-zigbuild
pkgs.zig
pkgs.cmake # needed by aws-lc-sys cmake builder
];
buildInputs = [
gnuStaticLibopus
gnuStaticX264
];
BINDGEN_EXTRA_CLANG_ARGS = "-isystem ${pkgs.lib.getDev pkgs.stdenv.cc.libc}/include";
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
CARGO_BUILD_TARGET = rustTargetGnu;
# Make system-deps link libx264.a explicitly (see commonArgsStatic).
SYSTEM_DEPS_X264_LINK = "static";
# Use cmake builder for aws-lc-sys to avoid its false-positive
# zig-cc memcmp bug check in the cc-crate code path.
AWS_LC_SYS_CMAKE_BUILDER = "1";
# Force static linking of opus — zig's linker rejects .so files
# built against a newer glibc than the target version.
OPUS_STATIC = "1";
};
cargoArtifactsGnu = craneLib.buildDepsOnly (
commonArgsGnu
// restoreVendoredCrates
// {
pname = "blit-workspace-deps-gnu";
cargoExtraArgs = "--workspace --exclude blit-browser";
doCheck = false;
buildPhaseCargoCommand = "HOME=$TMPDIR cargo zigbuild --profile release --target ${rustTargetGnu}.${minGlibcVersion} --workspace --exclude blit-browser";
doNotPostBuildInstallCargoBinaries = true;
}
);
in
{
inherit
pkgs
pkgsStaticLLVM
version
minGlibcVersion
rustTargetGnu
cargoLockConfig
rustToolchain
rustPlatform
craneLib
craneLibStatic
src
commonArgs
commonArgsGnu
commonArgsStatic
cargoArtifacts
cargoArtifactsGnu
cargoArtifactsStatic
;
}