Skip to content

Commit ac74676

Browse files
committed
Actual static build on windows
1 parent 4b15ccc commit ac74676

File tree

2 files changed

+124
-16
lines changed

2 files changed

+124
-16
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ endif ()
157157

158158
ADD_EXECUTABLE(ffmpegthumbnailer main.cpp)
159159
target_include_directories(ffmpegthumbnailer PRIVATE ${CMAKE_BINARY_DIR})
160+
target_link_options(ffmpegthumbnailer
161+
PRIVATE
162+
$<$<AND:$<BOOL:${ENABLE_FULL_STATIC}>,$<OR:$<PLATFORM_ID:Linux>,$<PLATFORM_ID:Windows>>>:-static>
163+
)
160164

161165
if (ENABLE_GIO)
162166
find_path(DL_INCLUDE dlfcn.h)

flake.nix

Lines changed: 120 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,31 @@
1515
"x86_64-darwin"
1616
"aarch64-darwin"
1717
];
18+
1819
forEachSupportedSystem =
1920
f:
2021
inputs.nixpkgs.lib.genAttrs supportedSystems (
2122
system:
2223
f {
23-
pkgs = import inputs.nixpkgs { inherit system; };
24-
pkgsStatic = import inputs.nixpkgs {
24+
pkgs = import inputs.nixpkgs {
2525
inherit system;
2626
};
27-
pkgsWindows = import inputs.nixpkgs {
27+
28+
pkgsStatic = import inputs.nixpkgs {
2829
inherit system;
29-
crossSystem = {
30-
config = "x86_64-w64-mingw32";
31-
};
32-
config = {
33-
allowBroken = true;
34-
};
3530
};
31+
32+
pkgsWindows = (
33+
import inputs.nixpkgs {
34+
inherit system;
35+
crossSystem = {
36+
config = "x86_64-w64-mingw32";
37+
};
38+
config = {
39+
allowBroken = true;
40+
};
41+
}
42+
);
3643
}
3744
);
3845
in
@@ -46,7 +53,47 @@
4653
let
4754
mkPackage =
4855
pkgsForBuild: pkgsForHost: isStatic: isWindows:
49-
pkgsForHost.stdenv.mkDerivation {
56+
let
57+
baseStdenv = pkgsForHost.stdenv;
58+
59+
# On Windows, use win32 threads to get a fully static binary
60+
stdenv' =
61+
if isWindows && baseStdenv.cc.isGNU && baseStdenv.targetPlatform.isWindows then
62+
let
63+
buildPkgs = pkgsForHost.buildPackages;
64+
gccWin32 = buildPkgs.wrapCC (
65+
buildPkgs.gcc-unwrapped.override {
66+
threadsCross = {
67+
model = "win32";
68+
package = null;
69+
};
70+
}
71+
);
72+
in
73+
pkgsForHost.overrideCC baseStdenv gccWin32
74+
else
75+
baseStdenv;
76+
77+
# zlib-ng in zlib-compatible mode
78+
zlibNgCompat = pkgsForHost.zlib-ng.override {
79+
withZlibCompat = true;
80+
};
81+
82+
# Use zlib-ng and force it to be static-only (we mainly care on Windows)
83+
zlibNgStatic = zlibNgCompat.overrideAttrs (old: {
84+
dontDisableStatic = true;
85+
cmakeFlags = (old.cmakeFlags or [ ]) ++ [
86+
"-DBUILD_SHARED_LIBS=OFF"
87+
];
88+
});
89+
90+
# libpng that uses zlib-ng instead of plain zlib
91+
libpngWithZlibNg = pkgsForHost.libpng.override {
92+
zlib = zlibNgStatic;
93+
};
94+
95+
in
96+
stdenv'.mkDerivation {
5097
pname = "ffmpegthumbnailer";
5198
version = "dev";
5299

@@ -65,6 +112,14 @@
65112
if isStatic || isWindows then
66113
(ffmpeg-headless.override {
67114
withGPL = true;
115+
withShared = false;
116+
withStatic = true;
117+
zlib = zlibNgStatic;
118+
119+
# Disable CUDA/LLVM to avoid compiler-rt dependency
120+
withCuda = false;
121+
withCudaLLVM = false;
122+
withCudaNVCC = false;
68123
buildAvdevice = false;
69124
buildSwresample = false;
70125
buildFfmpeg = false;
@@ -89,6 +144,7 @@
89144
withSpeex = false;
90145
withSoxr = false;
91146
withAmf = false;
147+
withCelt = false;
92148
# Disable X11 for headless
93149
withXcb = false;
94150
withFontconfig = false;
@@ -118,15 +174,15 @@
118174
withDrm = false;
119175
# only needed for dash demuxing
120176
withXml2 = false;
121-
# These deps are only neede for encoding
177+
# These deps are only needed for encoding
122178
withWebp = false;
123179
withTheora = false;
124180
withX264 = false;
125181
withX265 = false;
126182
withXvid = false;
127183
withSvtav1 = false;
128184
# Disable dav1d for static macOS builds
129-
withDav1d = if (isStatic && stdenv.isDarwin) then false else true;
185+
withDav1d = if (isStatic && stdenv'.isDarwin) then false else true;
130186
}).overrideAttrs
131187
(old: {
132188
doCheck = false;
@@ -140,13 +196,61 @@
140196
else
141197
ffmpeg-headless
142198
)
143-
libjpeg
144-
libpng
199+
# Use static versions of libpng and libjpeg for Windows
200+
(
201+
if isWindows then
202+
libjpeg.override {
203+
enableStatic = true;
204+
enableShared = false;
205+
}
206+
else
207+
libjpeg
208+
)
209+
(
210+
if isWindows then
211+
libpngWithZlibNg.overrideAttrs (old: {
212+
dontDisableStatic = true;
213+
configureFlags = (old.configureFlags or [ ]) ++ [
214+
"--enable-static"
215+
"--disable-shared"
216+
];
217+
})
218+
else
219+
libpng
220+
)
221+
145222
]
146223
++ pkgsForHost.lib.optionals isWindows [
147224
# ffmpeg transitive dependencies needed for linking on Windows
148-
bzip2
149-
xz
225+
zlibNgStatic
226+
(xz.overrideAttrs (old: {
227+
dontDisableStatic = true;
228+
configureFlags = (old.configureFlags or [ ]) ++ [
229+
"--enable-static"
230+
"--disable-shared"
231+
];
232+
}))
233+
(bzip2.overrideAttrs (old: {
234+
dontDisableStatic = true;
235+
configureFlags = (old.configureFlags or [ ]) ++ [
236+
"--enable-static"
237+
"--disable-shared"
238+
];
239+
}))
240+
(libiconv.overrideAttrs (old: {
241+
dontDisableStatic = true;
242+
configureFlags = (old.configureFlags or [ ]) ++ [
243+
"--enable-static"
244+
"--disable-shared"
245+
];
246+
}))
247+
(dav1d.overrideAttrs (old: {
248+
mesonFlags = (old.mesonFlags or [ ]) ++ [
249+
"-Ddefault_library=static"
250+
"-Denable_tools=false"
251+
"-Denable_tests=false"
252+
];
253+
}))
150254
]
151255
++ pkgsForHost.lib.optionals (pkgsForHost.stdenv.isLinux && !isStatic) [
152256
glib

0 commit comments

Comments
 (0)