-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Factor out reproducibility flags for tar and gzip #6884
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
02f7df7
Factor out tar/gzip reproducibility flags
aherrmann 537b211
use mktgz in package-app
aherrmann 18f0c4c
Bazel managed tar/gzip
aherrmann e62035c
Remove quiet = True
aherrmann af6e813
Build package-app as a sh_binary
aherrmann f390fc6
Avoid file path too long errors
aherrmann abe19ba
Fix readlink -f on MacOS
aherrmann 19f57f0
Document abspath
aherrmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
exports_files([ | ||
"packaging.bzl", | ||
"package-app.sh", | ||
]) | ||
load("@os_info//:os_info.bzl", "is_windows") | ||
|
||
sh_binary( | ||
name = "package-app", | ||
srcs = ["package-app.sh"], | ||
data = [ | ||
"@gzip_dev_env//:gzip", | ||
"@tar_dev_env//:tar", | ||
"//bazel_tools/sh:mktgz", | ||
] + (["@patchelf_nix//:bin/patchelf"] if not is_windows else []), | ||
visibility = ["//visibility:public"], | ||
deps = ["@bazel_tools//tools/bash/runfiles"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,61 @@ | |
# On Windows we only handle statically linked binaries | ||
# (Haskell binaries are linked statically on Windows) so we | ||
# just copy the binary and the resources and create a tarball from that. | ||
|
||
# Copy-pasted from the Bazel Bash runfiles library v2. | ||
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash | ||
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ | ||
source "$0.runfiles/$f" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | ||
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e | ||
# --- end runfiles.bash initialization v2 --- | ||
|
||
# Make sure that runfiles and tools are still found after we change directory. | ||
case "$(uname -s)" in | ||
Darwin) | ||
abspath() { python -c 'import os.path, sys; sys.stdout.write(os.path.abspath(sys.argv[1]))' "$@"; } | ||
canonicalpath() { python -c 'import os.path, sys; sys.stdout.write(os.path.realpath(sys.argv[1]))' "$@"; } | ||
;; | ||
*) | ||
abspath() { realpath -s "$@"; } | ||
canonicalpath() { readlink -f "$@"; } | ||
;; | ||
esac | ||
Comment on lines
+31
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to NixOS/nixpkgs#94222 |
||
|
||
if [[ -n ${RUNFILES_DIR:-} ]]; then | ||
export RUNFILES_DIR=$(abspath $RUNFILES_DIR) | ||
fi | ||
if [[ -n ${RUNFILES_MANIFEST_FILE:-} ]]; then | ||
export RUNFILES_DIR=$(abspath $RUNFILES_MANIFEST_FILE) | ||
fi | ||
|
||
case "$(uname -s)" in | ||
Darwin|Linux) | ||
tar=$(abspath $(rlocation tar_dev_env/tar)) | ||
gzip=$(abspath $(rlocation gzip_dev_env/gzip)) | ||
mktgz=$(abspath $(rlocation com_github_digital_asset_daml/bazel_tools/sh/mktgz)) | ||
patchelf=$(abspath $(rlocation patchelf_nix/bin/patchelf)) | ||
;; | ||
CYGWIN*|MINGW*|MSYS*) | ||
tar=$(abspath $(rlocation tar_dev_env/usr/bin/tar.exe)) | ||
gzip=$(abspath $(rlocation gzip_dev_env/usr/bin/gzip.exe)) | ||
mktgz=$(abspath $(rlocation com_github_digital_asset_daml/bazel_tools/sh/mktgz.exe)) | ||
;; | ||
esac | ||
|
||
set -eou pipefail | ||
|
||
WORKDIR="$(mktemp -d)" | ||
trap "rm -rf $WORKDIR" EXIT | ||
|
||
SRC=$1 | ||
OUT=$2 | ||
SRC=$(abspath $1) | ||
OUT=$(abspath $2) | ||
shift 2 | ||
NAME=$(basename $SRC) | ||
mkdir -p $WORKDIR/$NAME/lib | ||
export ORIGIN=$(dirname $(readlink -f $SRC)) # for rpaths relative to binary | ||
export ORIGIN=$(dirname $(canonicalpath $SRC)) # for rpaths relative to binary | ||
|
||
# Copy in resources, if any. | ||
if [ $# -gt 0 ]; then | ||
|
@@ -35,7 +79,7 @@ if [ $# -gt 0 ]; then | |
if [[ "$res" == *.tar.gz ]]; then | ||
# If a resource is a tarball, e.g., because it originates from another | ||
# rule we extract it. | ||
tar xf "$res" --strip-components=1 -C "$WORKDIR/$NAME/resources" | ||
$tar xf "$res" --strip-components=1 -C "$WORKDIR/$NAME/resources" | ||
else | ||
cp -aL "$res" "$WORKDIR/$NAME/resources" | ||
fi | ||
|
@@ -47,13 +91,13 @@ if [ "$(uname -s)" == "Linux" ]; then | |
binary="$WORKDIR/$NAME/lib/$NAME" | ||
cp $SRC $binary | ||
chmod u+w $binary | ||
rpaths_binary=$(patchelf --print-rpath "$binary"|tr ':' ' ') | ||
rpaths_binary=$($patchelf --print-rpath "$binary"|tr ':' ' ') | ||
function copy_deps { | ||
local from target needed libOK rpaths | ||
from=$1 | ||
target=$2 | ||
needed=$(patchelf --print-needed "$from") | ||
rpaths="$(patchelf --print-rpath "$from"|tr ':' ' ') $rpaths_binary" | ||
needed=$($patchelf --print-needed "$from") | ||
rpaths="$($patchelf --print-rpath "$from"|tr ':' ' ') $rpaths_binary" | ||
|
||
for lib in $needed; do | ||
if [ ! -f "$target/$lib" ]; then | ||
|
@@ -67,7 +111,7 @@ if [ "$(uname -s)" == "Linux" ]; then | |
if [ "$lib" != "ld-linux-x86-64.so.2" ]; then | ||
# clear the old rpaths (silence stderr as it always warns | ||
# with "working around a Linux kernel bug". | ||
patchelf --set-rpath '$ORIGIN' "$target/$lib" 2> /dev/null | ||
$patchelf --set-rpath '$ORIGIN' "$target/$lib" 2> /dev/null | ||
fi | ||
copy_deps "$rpath/$lib" "$target" | ||
break | ||
|
@@ -94,8 +138,8 @@ if [ "$(uname -s)" == "Linux" ]; then | |
done | ||
) | ||
|
||
patchelf --set-rpath '$ORIGIN/lib' "$binary" | ||
patchelf --set-interpreter ld-undefined.so "$binary" | ||
$patchelf --set-rpath '$ORIGIN/lib' "$binary" | ||
$patchelf --set-interpreter ld-undefined.so "$binary" | ||
|
||
# Link resources directory to lib, as that'll be the actual | ||
# binary's location. | ||
|
@@ -116,7 +160,7 @@ elif [[ "$(uname -s)" == "Darwin" ]]; then | |
cp $SRC $WORKDIR/$NAME/$NAME | ||
chmod u+w $WORKDIR/$NAME/$NAME | ||
function copy_deps() { | ||
local from_original=$(readlink -f $1) | ||
local from_original=$(canonicalpath $1) | ||
local from_copied=$2 | ||
local needed="$(/usr/bin/otool -L "$from_copied" | sed -n -e '1d' -e 's/^\s*\([^ ]*\).*$/\1/p')" | ||
# Note that it is crucial that we resolve loader_path relative to from_original instead of from_copied | ||
|
@@ -173,6 +217,4 @@ elif [[ "$(uname -s)" == "Darwin" ]]; then | |
else | ||
cp "$SRC" "$WORKDIR/$NAME/$NAME" | ||
fi | ||
cd $WORKDIR && tar c $NAME \ | ||
--owner=0 --group=0 --numeric-owner --mtime=2000-01-01\ 00:00Z --sort=name \ | ||
| gzip -n > $OUT | ||
cd $WORKDIR && $mktgz $OUT $NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env bash | ||
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# Copy-pasted from the Bazel Bash runfiles library v2. | ||
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash | ||
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ | ||
source "$0.runfiles/$f" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | ||
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e | ||
# --- end runfiles.bash initialization v2 --- | ||
|
||
set -euo pipefail | ||
usage() { | ||
cat >&2 <<'EOF' | ||
usage: mktar OUTPUT ARGS... | ||
|
||
Creates an uncompressed tarball in OUTPUT passing ARGS to tar. The created | ||
tarball is reproducible, i.e. it does not contain any timestamps or similar | ||
non-deterministic inputs. See https://reproducible-builds.org/docs/archives/ | ||
EOF | ||
} | ||
trap usage ERR | ||
|
||
case "$(uname -s)" in | ||
Darwin|Linux) | ||
tar=$(rlocation tar_dev_env/tar) | ||
;; | ||
CYGWIN*|MINGW*|MSYS*) | ||
tar=$(rlocation tar_dev_env/usr/bin/tar.exe) | ||
;; | ||
esac | ||
|
||
$tar cf "$1" "${@:2}" \ | ||
--owner="0" \ | ||
--group="0" \ | ||
--numeric-owner \ | ||
--mtime="2000-01-01 00:00Z" \ | ||
--no-acls \ | ||
--no-xattrs \ | ||
--no-selinux \ | ||
--sort="name" \ | ||
--format=ustar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env bash | ||
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# Copy-pasted from the Bazel Bash runfiles library v2. | ||
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash | ||
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ | ||
source "$0.runfiles/$f" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | ||
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e | ||
# --- end runfiles.bash initialization v2 --- | ||
|
||
set -euo pipefail | ||
usage() { | ||
cat >&2 <<'EOF' | ||
usage: mktgz OUTPUT ARGS... | ||
|
||
Creates a gzip compressed tarball in OUTPUT passing ARGS to tar. The created | ||
tarball is reproducible, i.e. it does not contain any timestamps or similar | ||
non-deterministic inputs. See https://reproducible-builds.org/docs/archives/ | ||
EOF | ||
} | ||
trap usage ERR | ||
|
||
case "$(uname -s)" in | ||
Darwin|Linux) | ||
tar=$(rlocation tar_dev_env/tar) | ||
gzip=$(rlocation gzip_dev_env/gzip) | ||
;; | ||
CYGWIN*|MINGW*|MSYS*) | ||
tar=$(rlocation tar_dev_env/usr/bin/tar.exe) | ||
gzip=$(rlocation gzip_dev_env/usr/bin/gzip.exe) | ||
;; | ||
esac | ||
|
||
$tar c "${@:2}" \ | ||
--owner="0" \ | ||
--group="0" \ | ||
--numeric-owner \ | ||
--mtime="2000-01-01 00:00Z" \ | ||
--no-acls \ | ||
--no-xattrs \ | ||
--no-selinux \ | ||
--sort="name" \ | ||
--format=ustar \ | ||
| $gzip -n > "$1" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are now using Bazel 3.3.1.