Reproducible Builds
A build is reproducible when the same source, built with the same declared toolchain, produces bit-for-bit identical output, on any machine, at any time, in any directory. It sounds like a nicety. It is actually what lets you:
- verify a binary: anyone can rebuild a release from its tagged source and compare hashes; a mismatch means the binary was not built from that source (the core defence against a compromised build server, see Software Supply Chain Security);
- cache by content: identical outputs mean downstream steps keep hitting the cache (Content-Addressed Caches);
- diff releases: when two builds differ, the difference is a real change, not noise.
Debian, Arch Linux, NixOS, Guix, Tor Browser and Bitcoin Core all check reproducibility routinely; reproducible-builds.org documents the techniques.
The test: build twice, compare
The method is always the same: build twice while varying something that shouldn't matter (the time, the directory, the user), then compare the outputs byte by byte. This session defines a same helper that compares SHA-256 hashes and prints the verdict:
same() { [ "$(sha256sum < "$1")" = "$(sha256sum < "$2")" ] && echo "identical" || echo "different"; }
mkdir -p r && cd r
printf '#include <stdio.h>\nint main(void) { puts("release 1.0"); return 0; }\n' > app.c
gcc -O2 app.c -o build1
sleep 1
gcc -O2 app.c -o build2
printf 'two builds, one second apart: '; same build1 build2two builds, one second apart: identical
A plain GCC build of simple code is already reproducible: the compiler and linker are deterministic, and the build ID that ld embeds (readelf -n shows it) is a hash of the content, not something random. Problems come from three places: time, paths and archive metadata.
Time: DATE, TIME and SOURCE_DATE_EPOCH
Version banners often use the __DATE__ and __TIME__ macros, which expand to the moment of compilation, so every build differs. GCC and Clang honour the SOURCE_DATE_EPOCH convention: if the environment variable is set (a Unix timestamp, usually the time of the last commit), they use it instead of the clock. This machine sets it to 1767225600 (2026-01-01 00:00 UTC):
same() { [ "$(sha256sum < "$1")" = "$(sha256sum < "$2")" ] && echo "identical" || echo "different"; }
mkdir -p r && cd r
printf '#include <stdio.h>\nint main(void) { printf("built %%s %%s\\n", __DATE__, __TIME__); return 0; }\n' > banner.c
echo "SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH"
gcc -O2 banner.c -o b1 && ./b1
sleep 1
gcc -O2 banner.c -o b2
printf 'same epoch, rebuilt later: '; same b1 b2
SOURCE_DATE_EPOCH=$(git log -1 --format=%ct 2>/dev/null || echo 1767312000) gcc -O2 banner.c -o b3 && ./b3
printf 'different epoch: '; same b1 b3SOURCE_DATE_EPOCH=1767225600
built Jan 1 2026 00:00:00
same epoch, rebuilt later: identical
built Jan 2 2026 00:00:00
different epoch: different
(In a real repository, git log -1 --format=%ct prints the last commit's timestamp, which is the usual value for SOURCE_DATE_EPOCH; this folder is not a Git repository, so the fallback value was used.) Better still, don't embed build times at all: embed the version and commit, which identify the build exactly.
Paths: -ffile-prefix-map
Debug information, __FILE__ (used by assert) and some error messages contain the absolute path of the source file. Two developers with different home folders, or two CI jobs in different work directories, get different binaries:
same() { [ "$(sha256sum < "$1")" = "$(sha256sum < "$2")" ] && echo "identical" || echo "different"; }
mkdir -p checkout-a/src checkout-b/src
cat > checkout-a/src/app.c <<'SRC'
#include <assert.h>
#include <stdio.h>
int main(int argc, char **argv) {
(void)argv;
assert(argc > 0);
printf("compiled from %s\n", __FILE__);
return 0;
}
SRC
cp checkout-a/src/app.c checkout-b/src/
for d in checkout-a checkout-b; do (cd $d && gcc -g -O2 "$PWD/src/app.c" -o app); done
printf 'plain -g build in two folders: '; same checkout-a/app checkout-b/app
checkout-a/app
strings -a checkout-a/app | grep -c /home/learner/checkout-a
for d in checkout-a checkout-b; do (cd $d && gcc -g -O2 -ffile-prefix-map="$PWD"=. "$PWD/src/app.c" -o app); done
printf 'with -ffile-prefix-map=$PWD=.: '; same checkout-a/app checkout-b/app
checkout-a/appplain -g build in two folders: different
compiled from /home/learner/checkout-a/src/app.c
4
with -ffile-prefix-map=$PWD=.: identical
compiled from ./src/app.c
The strings | grep -c line counts four copies of the absolute folder name inside the first binary. -ffile-prefix-map=OLD=NEW rewrites every path that starts with OLD, in debug info (-fdebug-prefix-map) and in macros like __FILE__ (-fmacro-prefix-map), in one flag. CMake projects usually pass -ffile-prefix-map=${CMAKE_SOURCE_DIR}=.; Cargo has --remap-path-prefix, Go has -trimpath.
Archives: order, owners and timestamps
tar stores each file's modification time, owner and the order in which the directory was read; gzip stores the original file name and its time. Packaging steps therefore need flags too:
same() { [ "$(sha256sum < "$1")" = "$(sha256sum < "$2")" ] && echo "identical" || echo "different"; }
mkdir -p pkg/bin pkg/doc && echo "binary" > pkg/bin/app && echo "readme" > pkg/doc/README
tar -cf r1.tar pkg
sleep 1 && touch pkg/bin/app
tar -cf r2.tar pkg
printf 'plain tar after a touch: '; same r1.tar r2.tar
REPRO="--sort=name --mtime=@$SOURCE_DATE_EPOCH --owner=0 --group=0 --numeric-owner"
tar $REPRO -cf r1.tar pkg
sleep 1 && touch pkg/bin/app
tar $REPRO -cf r2.tar pkg
printf 'tar with fixed order/time/owner: '; same r1.tar r2.tar
gzip -c r1.tar > a.tar.gz; sleep 1; touch r1.tar; gzip -c r1.tar > b.tar.gz
printf 'gzip: '; same a.tar.gz b.tar.gz
gzip -nc r1.tar > a.tar.gz; sleep 1; touch r1.tar; gzip -nc r1.tar > b.tar.gz
printf 'gzip -n (no name, no time): '; same a.tar.gz b.tar.gzplain tar after a touch: different
tar with fixed order/time/owner: identical
gzip: different
gzip -n (no name, no time): identical
ar already runs in deterministic mode on modern distributions (Static Libraries); zip needs -X and fixed file times; Python wheels and JAR files have their own SOURCE_DATE_EPOCH support.
A checklist
| Source of difference | Fix |
|---|---|
build time in the binary (__DATE__, version banners) |
SOURCE_DATE_EPOCH, or embed the commit instead |
| absolute paths | -ffile-prefix-map, -trimpath, --remap-path-prefix |
file order (readdir, globbing, hash-map iteration in build scripts) |
sort explicitly (tar --sort=name, sorted source lists) |
| timestamps, owners in archives | tar --mtime --owner --group --numeric-owner, gzip -n, zip -X |
| locale and time zone | LC_ALL=C, TZ=UTC in the build environment |
| randomness (generated names, unseeded random numbers in code generators) | fixed seeds (GCC: -frandom-seed=<string>), deterministic tools |
| different toolchains | pin the toolchain version (container image, Nix, Bazel toolchains) and record it |
When two builds still differ, diffoscope (from reproducible-builds.org) compares them recursively, unpacking archives and disassembling binaries, and shows exactly which bytes differ and why.
Test yourself
What does GCC use for __DATE__ and __TIME__ when SOURCE_DATE_EPOCH is set?
- The current clock
- The timestamp in
SOURCE_DATE_EPOCH - The file's mtime
- Nothing, they become empty
Why were the two -g builds in different folders different?
- Different compilers
- Debug info and
__FILE__contained each folder's absolute path - The clock changed
- Random build IDs
Which tar options make an archive independent of file order, times and owners?
-czf--sort=name --mtime=@$SOURCE_DATE_EPOCH --owner=0 --group=0 --numeric-owner--verbose--append