Zig's Build System is fundamentally Broken

  • does not support proxy
  • confusing hashes
  • build script incompatible between zig versions

It takes me several hours for fixing build script for ffmpeg, because it’s not an easy task.

download dependencies and put them in right place

https://github.com/soda92/ffmpeg/blob/download/update_zon.py

https://github.com/soda92/ffmpeg/blob/download/update_zon2.py

update .zig.zon files to point to these locations

https://github.com/soda92/ffmpeg/blob/download/update_zon3.py

And it also support github repos…

And the unpack need to match the name

https://github.com/soda92/ffmpeg/blob/download/update_zon4.py

https://github.com/soda92/ffmpeg/blob/download/update_zon5.py

And everything is breaking

https://github.com/soda92/ffmpeg/blob/download/update_zon6.py

fix taken from https://github.com/natecraddock/ziglua/pull/131

Well it only support path relative to build root

https://github.com/soda92/ffmpeg/blob/download/update_zon7.py

It’s so many patches that I need a runnner

import subprocess
from pathlib import Path
import send2trash

if Path("tmp").exists():
    # send2trash.send2trash("tmp")
    pass
if Path("unzip").exists():
    send2trash.send2trash("unzip")
subprocess.run(["git", "restore", "build.zig.zon"])
subprocess.run(["python", "update_zon.py"])
subprocess.run(["python", "update_zon2.py"])
subprocess.run(["python", "update_zon3.py"])
subprocess.run(["python", "update_zon2.py"])
subprocess.run(["python", "update_zon4.py"])
subprocess.run(["python", "update_zon5.py"])
subprocess.run(["python", "update_zon6.py"])
subprocess.run(["python", "update_zon7.py"])
zig build
install
└─ install show_metadata_c
   └─ zig build-exe show_metadata_c Debug native
      └─ zig build-lib ffmpeg Debug native
         └─ run nasm (resample.o)
            └─ zig build-exe nasm ReleaseFast native 79 errors
C:\src\ffmpeg\unzip\github.com\allyourcodebase\nasm\archive\refs\tags\nasm-2.16.1-3\include/compiler.h:101:11: error: 'endian.h' file not found
# include <endian.h>
          ^~~~~~~~~~~
C:\src\ffmpeg\unzip\github.com\allyourcodebase\nasm\archive\refs\tags\nasm-2.16.1-3\nasmlib/alloc.c:38:10: note: in file included from C:\src\ffmpeg\unzip\github.com\allyourcodebase\nasm\archive\refs\tags\nasm-2.16.1-3\nasmlib/alloc.c:38:
#include "compiler.h"
         ^

Later, I tried to undef these macros so the <endian.h> won’t be included:

111 222

But due to comples caching it only works at the first time (change .h before run zig). I have runner script so it’s not a problem.

Then I deleted nasm:

diff --git a/build.zig b/build.zig
index a2cbbeaef0..6d57e9d304 100644
--- a/build.zig
+++ b/build.zig
@@ -843,10 +843,7 @@ pub fn build(b: *std.Build) void {
     };

     @setEvalBranchQuota(2500);
-    const config_asm = b.addConfigHeader(.{
-        .style = .nasm,
-        .include_path = "config.asm",
-    }, common_config);
+

     const config_h = b.addConfigHeader(.{
         .style = .blank,
@@ -3129,40 +3126,7 @@ pub fn build(b: *std.Build) void {
     });
     switch (t.cpu.arch) {
         .x86_64, .x86 => {
-            // For x86 there is one additional complication which is compiling .asm
-            // files into object files with NASM.
-            const nasm_dep = b.dependency("nasm", .{
-                .optimize = .ReleaseFast,
-            });
-            const nasm_exe = nasm_dep.artifact("nasm");
-
-            for (all_sources) |input_file| {
-                if (!std.mem.endsWith(u8, input_file, ".asm")) continue;
-
-                const output_basename = basenameNewExtension(b, input_file, ".o");
-                const nasm_run = b.addRunArtifact(nasm_exe);
-
-                // nasm requires a trailing slash on include directories
-                const include_dir = b.fmt("-I{s}/", .{std.fs.path.dirname(input_file).?});       
-
-                nasm_run.addArgs(&.{
-                    "-f",
-                    "elf64",
-                    "-g",
-                    "-F",
-                    "dwarf",
-                    "-I./",
-                    include_dir,
-                });
-
-                nasm_run.addArgs(&.{"--include"});
-                nasm_run.addFileArg(config_asm.getOutput());
-
-                nasm_run.addArgs(&.{"-o"});
-                lib.addObjectFile(nasm_run.addOutputFileArg(output_basename));
-
-                nasm_run.addFileArg(b.path(input_file));
-            }
+
         },
         else => {},
     }
diff --git a/run.py b/run.py
index 79c8f5e494..8d4ecb157c 100644
--- a/run.py
+++ b/run.py
@@ -16,3 +16,4 @@ subprocess.run(["python", "update_zon4.py"])
 subprocess.run(["python", "update_zon5.py"])
 subprocess.run(["python", "update_zon6.py"])
 subprocess.run(["python", "update_zon7.py"])
+subprocess.run(["python", "update_zon8.py"])
diff --git a/update_zon8.py b/update_zon8.py
new file mode 100644
index 0000000000..9282d781a0
--- /dev/null
+++ b/update_zon8.py
@@ -0,0 +1,4 @@
+import shutil
+
+shutil.copy(r"z_patched_files\compiler.h", r"unzip\github.com\allyourcodebase\nasm\archive\refs\tags\nasm-2.16.1-3\include\compiler.h")
+shutil.copy(r"z_patched_files\file.h", r"unzip\github.com\allyourcodebase\nasm\archive\refs\tags\nasm-2.16.1-3\nasmlib\file.h")
\ No newline at end of file

In the end, I only find it does not support Windows:

zig build
install
└─ install show_metadata_c
   └─ zig build-exe show_metadata_c Debug native
      └─ zig build-lib ffmpeg Debug native 449 errors
C:\\src\\ffmpeg/libavutil/thread.h:28:10: error: 'sys/prctl.h' file not found
#include <sys/prctl.h>
         ^~~~~~~~~~~~~~
C:\\src\\ffmpeg\\libavcodec/aactab.c:36:10: note: in file included from C:\\src\\ffmpeg\\libavcodec/aactab.c:36:
#include "libavutil/thread.h"
         ^
C:\\src\\ffmpeg\\libswscale/utils.c:25:9: error: '_DARWIN_C_SOURCE' macro redefined
#define _DARWIN_C_SOURCE // needed for MAP_ANON
        ^
<command line>:9:9: note: previous definition is here
C:\\src\\ffmpeg\\libswscale/utils.c:31:10: error: 'sys/mman.h' file not found
#include <sys/mman.h>
         ^~~~~~~~~~~~~
C:\\src\\ffmpeg/libavutil/thread.h:28:10: error: 'sys/prctl.h' file not found
#include <sys/prctl.h>
         ^~~~~~~~~~~~~~
C:\\src\\ffmpeg\\libavcodec/aacdec.c:260:10: note: in file included from C:\\src\\ffmpeg\\libavcodec/aacdec.c:260:
#include "aacdec_template.c"
         ^
C:\\src\\ffmpeg\\libavcodec/aacdec_template.c:93:10: note: in file included from C:\\src\\ffmpeg\\libavcodec/aacdec_template.c:93:
#include "libavutil/thread.h"
         ^
C:\\src\\ffmpeg/libavutil/thread.h:28:10: error: 'sys/prctl.h' file not found
#include <sys/prctl.h>
         ^~~~~~~~~~~~~~
C:\\src\\ffmpeg\\libavcodec/4xm.c:34:10: note: in file included from C:\\src\\ffmpeg\\libavcodec/4xm.c:34:
#include "libavutil/thread.h"
         ^
C:\\src\\ffmpeg/libavutil/thread.h:28:10: error: 'sys/prctl.h' file not found
#include <sys/prctl.h>
         ^~~~~~~~~~~~~~

wsl

In WSL, I can successfully build it, but other libraries are still missing: skip

 zig cc remux.c -I ../../zig-out/include/ -L../../zig-out/lib -lffmpeg
ld.lld: error: undefined symbol: deflate
>>> referenced by pngenc.c:575 (libavcodec/pngenc.c:575)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/f6bd17677bf017a3f11ac8deec19ad74/pngenc.o:(encode_frame) in archive ../../zig-out/lib/libffmpeg.a
>>> referenced by pngenc.c:360 (libavcodec/pngenc.c:360)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/f6bd17677bf017a3f11ac8deec19ad74/pngenc.o:(png_write_iccp) in archive ../../zig-out/lib/libffmpeg.a
>>> referenced by pngenc.c:285 (libavcodec/pngenc.c:285)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/f6bd17677bf017a3f11ac8deec19ad74/pngenc.o:(png_write_row) in archive ../../zig-out/lib/libffmpeg.a
>>> referenced 6 more times

ld.lld: error: undefined symbol: vorbis_analysis
>>> referenced by libvorbisenc.c:323 (libavcodec/libvorbisenc.c:323)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/37be7e7fd754c225437fd77c2973878b/libvorbisenc.o:(libvorbis_encode_frame) in archive ../../zig-out/lib/libffmpeg.a

ld.lld: error: undefined symbol: vorbis_bitrate_addblock
>>> referenced by libvorbisenc.c:325 (libavcodec/libvorbisenc.c:325)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/37be7e7fd754c225437fd77c2973878b/libvorbisenc.o:(libvorbis_encode_frame) in archive ../../zig-out/lib/libffmpeg.a

ld.lld: error: undefined symbol: vorbis_bitrate_flushpacket
>>> referenced by libvorbisenc.c:329 (libavcodec/libvorbisenc.c:329)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/37be7e7fd754c225437fd77c2973878b/libvorbisenc.o:(libvorbis_encode_frame) in archive ../../zig-out/lib/libffmpeg.a

ld.lld: error: undefined symbol: lame_init
>>> referenced by libmp3lame.c:102 (libavcodec/libmp3lame.c:102)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/f782ff6a63f6fbc56cfd619f4a10734b/libmp3lame.o:(mp3lame_encode_init) in archive ../../zig-out/lib/libffmpeg.a

ld.lld: error: undefined symbol: lame_set_num_channels
>>> referenced by libmp3lame.c:106 (libavcodec/libmp3lame.c:106)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/f782ff6a63f6fbc56cfd619f4a10734b/libmp3lame.o:(mp3lame_encode_init) in archive ../../zig-out/lib/libffmpeg.a

ld.lld: error: undefined symbol: uncompress
>>> referenced by cscd.c:98 (libavcodec/cscd.c:98)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/b092a0b94e6b0ffb723a0d53d3e07bc0/cscd.o:(decode_frame) in archive ../../zig-out/lib/libffmpeg.a
>>> referenced by dxa.c:252 (libavcodec/dxa.c:252)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/d207d5b24fcf278c29bf3d5cab83d101/dxa.o:(decode_frame) in archive ../../zig-out/lib/libffmpeg.a
>>> referenced by exr.c:204 (libavcodec/exr.c:204)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/20f6eb5f10888146f12c2115f7326223/exr.o:(zip_uncompress) in archive ../../zig-out/lib/libffmpeg.a
>>> referenced 13 more times

ld.lld: error: undefined symbol: inflateInit2_
>>> referenced by http.c:852 (libavformat/http.c:852)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/4a5715c1a50261c769a4e1f62f7bfa14/http.o:(parse_content_encoding) in archive ../../zig-out/lib/libffmpeg.a

ld.lld: error: undefined symbol: zlibCompileFlags
>>> referenced by http.c:857 (libavformat/http.c:857)
>>>               /home/soda/src/ffmpeg/.zig-cache/o/4a5715c1a50261c769a4e1f62f7bfa14/http.o:(parse_content_encoding) in archive ../../zig-out/lib/libffmpeg.a

msys2

In fact, build ffmpeg is already a non-trival task on Windows. MSYS2 has 83 packages for dealing with it:

pacman -S ucrt64/mingw-w64-ucrt-x86_64-ffmpeg
resolving dependencies...
looking for conflicting packages...
warning: dependency cycle detected:
warning: mingw-w64-ucrt-x86_64-harfbuzz will be installed before its mingw-w64-ucrt-x86_64-freetype dependency
warning: dependency cycle detected:
warning: mingw-w64-ucrt-x86_64-libwebp will be installed before its mingw-w64-ucrt-x86_64-libtiff dependency

Packages (83) mingw-w64-ucrt-x86_64-aom-3.11.0-1  mingw-w64-ucrt-x86_64-brotli-1.1.0-4  mingw-w64-ucrt-x86_64-cairo-1.18.2-2
              mingw-w64-ucrt-x86_64-dav1d-1.5.0-1  mingw-w64-ucrt-x86_64-fontconfig-2.15.0-2
              mingw-w64-ucrt-x86_64-freetype-2.13.3-1  mingw-w64-ucrt-x86_64-frei0r-plugins-2.3.3-2
              mingw-w64-ucrt-x86_64-fribidi-1.0.16-1  mingw-w64-ucrt-x86_64-gdk-pixbuf2-2.42.12-4
              mingw-w64-ucrt-x86_64-giflib-5.2.2-1  mingw-w64-ucrt-x86_64-glib2-2.82.4-1
              mingw-w64-ucrt-x86_64-glslang-15.1.0-1  mingw-w64-ucrt-x86_64-gnutls-3.8.8-1
              mingw-w64-ucrt-x86_64-graphite2-1.3.14-3  mingw-w64-ucrt-x86_64-gsm-1.0.22-1
              mingw-w64-ucrt-x86_64-harfbuzz-10.2.0-1  mingw-w64-ucrt-x86_64-highway-1.2.0-1
              mingw-w64-ucrt-x86_64-imath-3.1.12-2  mingw-w64-ucrt-x86_64-jbigkit-2.1-5  mingw-w64-ucrt-x86_64-lame-3.100-3
              mingw-w64-ucrt-x86_64-lcms2-2.16-1  mingw-w64-ucrt-x86_64-lerc-4.0.0-1  mingw-w64-ucrt-x86_64-libass-0.17.3-1
              mingw-w64-ucrt-x86_64-libbluray-1.3.4-1  mingw-w64-ucrt-x86_64-libcaca-0.99.beta20-6
              mingw-w64-ucrt-x86_64-libdatrie-0.2.13-3  mingw-w64-ucrt-x86_64-libdeflate-1.23-1
              mingw-w64-ucrt-x86_64-libdovi-3.3.1-1  mingw-w64-ucrt-x86_64-libexif-0.6.24-3
              mingw-w64-ucrt-x86_64-libgme-0.6.3-3  mingw-w64-ucrt-x86_64-libidn2-2.3.7-2
              mingw-w64-ucrt-x86_64-libjpeg-turbo-3.1.0-1  mingw-w64-ucrt-x86_64-libjxl-0.11.1-1
              mingw-w64-ucrt-x86_64-liblc3-1.1.1-2  mingw-w64-ucrt-x86_64-libmodplug-0.8.9.0-4
              mingw-w64-ucrt-x86_64-libmysofa-1.3.1-1  mingw-w64-ucrt-x86_64-libogg-1.3.5-1
              mingw-w64-ucrt-x86_64-libplacebo-7.349.0-1  mingw-w64-ucrt-x86_64-libpng-1.6.45-1
              mingw-w64-ucrt-x86_64-librsvg-2.59.2-1  mingw-w64-ucrt-x86_64-libsoxr-0.1.3-5
              mingw-w64-ucrt-x86_64-libssh-0.11.1-2  mingw-w64-ucrt-x86_64-libtasn1-4.19.0-1
              mingw-w64-ucrt-x86_64-libthai-0.1.29-3  mingw-w64-ucrt-x86_64-libtheora-1.1.1-7
              mingw-w64-ucrt-x86_64-libtiff-4.7.0-1  mingw-w64-ucrt-x86_64-libunibreak-6.1-1
              mingw-w64-ucrt-x86_64-libunistring-1.3-1  mingw-w64-ucrt-x86_64-libva-2.22.0-2
              mingw-w64-ucrt-x86_64-libvorbis-1.3.7-2  mingw-w64-ucrt-x86_64-libvpl-2.14.0-1
              mingw-w64-ucrt-x86_64-libvpx-1.15.0-1  mingw-w64-ucrt-x86_64-libwebp-1.5.0-1
              mingw-w64-ucrt-x86_64-libx264-0.164.r3161.a354f11-3  mingw-w64-ucrt-x86_64-libxml2-2.12.9-2
              mingw-w64-ucrt-x86_64-lzo2-2.10-2  mingw-w64-ucrt-x86_64-nettle-3.10.1-1  mingw-w64-ucrt-x86_64-openal-1.24.2-1
              mingw-w64-ucrt-x86_64-opencore-amr-0.1.6-1  mingw-w64-ucrt-x86_64-openexr-3.3.2-1
              mingw-w64-ucrt-x86_64-openjpeg2-2.5.3-1  mingw-w64-ucrt-x86_64-opus-1.5.2-1
              mingw-w64-ucrt-x86_64-p11-kit-0.25.5-1  mingw-w64-ucrt-x86_64-pango-1.56.0-1  mingw-w64-ucrt-x86_64-pcre2-10.44-2
              mingw-w64-ucrt-x86_64-pixman-0.44.2-1  mingw-w64-ucrt-x86_64-python-packaging-24.2-1
              mingw-w64-ucrt-x86_64-rav1e-0.7.1-4  mingw-w64-ucrt-x86_64-rtmpdump-2.4.r99.f1b83c1-1
              mingw-w64-ucrt-x86_64-shaderc-2024.4-1  mingw-w64-ucrt-x86_64-speex-1.2.1-1
              mingw-w64-ucrt-x86_64-speexdsp-1.2.1-1  mingw-w64-ucrt-x86_64-spirv-cross-1~1.4.304.0-2
              mingw-w64-ucrt-x86_64-spirv-tools-3~1.4.304.0-1  mingw-w64-ucrt-x86_64-srt-1.5.4-1
              mingw-w64-ucrt-x86_64-svt-av1-2.3.0-2  mingw-w64-ucrt-x86_64-vid.stab-1.1.1-1
              mingw-w64-ucrt-x86_64-wineditline-2.208-1  mingw-w64-ucrt-x86_64-x265-4.1-2
              mingw-w64-ucrt-x86_64-xvidcore-1.3.7-4  mingw-w64-ucrt-x86_64-zimg-3.0.5-2
              mingw-w64-ucrt-x86_64-zvbi-0.2.42.r41.gb4cef50-1  mingw-w64-ucrt-x86_64-ffmpeg-7.1-4

Total Installed Size:  800.02 MiB