@@ -102,13 +102,14 @@ function reproducible_tar_gzip() {
102102 local dir=$1
103103 local dst=$2
104104 local tar_cmd=tar
105+ local gzip_cmd=gzip
105106
106107 # MacOS has a version of BSD tar which doesn't support setting the --mtime
107108 # flag. We need gnu-tar, or gtar for short to be installed for this script to
108109 # work properly.
109- tar_version=$( tar --version)
110+ tar_version=$( tar --version 2>&1 || true )
110111 if [[ ! " $tar_version " =~ " GNU tar" ]]; then
111- if ! command -v " gtar" ; then
112+ if ! command -v " gtar" > /dev/null 2>&1 ; then
112113 red " GNU tar is required but cannot be found!"
113114 red " On MacOS please run 'brew install gnu-tar' to install gtar."
114115 exit 1
@@ -118,12 +119,26 @@ function reproducible_tar_gzip() {
118119 tar_cmd=gtar
119120 fi
120121
122+ # On MacOS, the default BSD gzip produces a different output than the GNU
123+ # gzip on Linux. To ensure reproducible builds, we need to use GNU gzip.
124+ gzip_version=$( gzip --version 2>&1 || true)
125+ if [[ ! " $gzip_version " =~ " GNU" ]]; then
126+ if ! command -v " ggzip" > /dev/null 2>&1 ; then
127+ red " GNU gzip is required but cannot be found!"
128+ red " On MacOS please run 'brew install gzip' to install ggzip."
129+ exit 1
130+ fi
131+
132+ # We have ggzip installed, use that instead.
133+ gzip_cmd=ggzip
134+ fi
135+
121136 # Pin down the timestamp time zone.
122137 export TZ=UTC
123138
124139 find " ${dir} " -print0 | LC_ALL=C sort -r -z | $tar_cmd \
125140 " --mtime=${BUILD_DATE} " --no-recursion --null --mode=u+rw,go+r-w,a+X \
126- --owner=0 --group=0 --numeric-owner -c -T - | gzip -9n > " $dst "
141+ --owner=0 --group=0 --numeric-owner -c -T - | $gzip_cmd -9n > " $dst "
127142}
128143
129144# reproducible_zip creates a reproducible zip file of a directory. This
0 commit comments