Skip to content

Commit 1372d76

Browse files
committed
repo: Generate release archive
This adds a script that generates a source archive containing vendored dependencies. This source archive is suitable for building in environments that do not have networking. This archive is bit-for-bit reproducible and will always generate the exact same archive when ran on a given git commit with a given version of system tooling. Signed-off-by: Reilly Brogan <[email protected]>
1 parent 3880a91 commit 1372d76

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ target
44
install
55

66
**/db/**/test.db
7+
8+
# Generated source archives
9+
*.tar.*

justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,6 @@ install-moss:
124124

125125
# Cleanup
126126
rm -rfv $tmpdir
127+
128+
create-release-tar:
129+
scripts/create-release-tar.sh

scripts/create-release-tar.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
# Script to generate a tarball of source code and vendored (downloaded) Rust dependencies
5+
# and the cargo configuration to ensure they are used
6+
7+
# Get the current directory, which we'll use for telling Cargo where to find the sources
8+
wd="$PWD"
9+
10+
# Get the version from Cargo.toml
11+
VERSION=$(yq -oy '.workspace.package.version' Cargo.toml)
12+
13+
# The path where we will output the tar file
14+
path=$wd/moss-$VERSION-vendored.tar.zst
15+
16+
# Clean up stuff we've written before
17+
rm -f "$path"
18+
19+
# Make sure cargo lock files are in sync with cargo.toml
20+
cargo check --locked
21+
22+
PREFIX_TMPDIR=$(mktemp -d)
23+
pushd "$PREFIX_TMPDIR"
24+
25+
# Enable dotglob so we copy over files/folders starting with .
26+
shopt -s dotglob
27+
cp -ra "$wd"/* .
28+
29+
function get_commit_time() {
30+
TZ=UTC0 git log -1 \
31+
--format=tformat:%cd \
32+
--date=format:%Y-%m-%dT%H:%M:%SZ \
33+
"$@"
34+
}
35+
36+
# Set each file mtime to that of it's latest commit
37+
# Set each source file timestamp to that of its latest commit.
38+
git ls-files | while read -r file; do
39+
commit_time=$(get_commit_time "$file") &&
40+
touch -md "$commit_time" "$file"
41+
done
42+
43+
# Set timestamp of each directory under $FILES
44+
# to the latest timestamp of any descendant.
45+
find . -depth -type d -exec sh -c \
46+
'touch -r "$0/$(ls -At "$0" | head -n 1)" "$0"' \
47+
{} ';'
48+
49+
SOURCE_EPOCH=$(get_commit_time)
50+
51+
# Cleanup repo
52+
git reset --hard
53+
git clean -xdf
54+
git clean -df
55+
rm -rf .git
56+
rm -rf serpent-style
57+
58+
# Generate vendored dependencies and the configuration to use them
59+
cargo vendor --manifest-path "$wd/Cargo.toml" >> .cargo/config.toml
60+
61+
# vendoring drags in a lot of Windows dependencies, which makes the resulting tarball enormous
62+
# cargo can't be told only to support a particular platform
63+
# see https://github.com/rust-lang/cargo/issues/7058
64+
# workaround below from https://github.com/rust-lang/cargo/issues/7058#issuecomment-751856262
65+
rm -r vendor/winapi*/lib/*.a
66+
67+
# Reproducible tar flags
68+
TARFLAGS="
69+
--sort=name --format=posix
70+
--pax-option=exthdr.name=%d/PaxHeaders/%f
71+
--pax-option=delete=atime,delete=ctime
72+
--clamp-mtime --mtime=$SOURCE_EPOCH
73+
--numeric-owner --owner=0 --group=0
74+
--mode=go+u,go-w
75+
"
76+
ZSTDFLAGS="-19 -T0"
77+
78+
# shellcheck disable=SC2086
79+
LC_ALL=C tar $TARFLAGS -C $PREFIX_TMPDIR -cf - . |
80+
zstd $ZSTDFLAGS > $path
81+
82+
popd
83+
rm -rf "$PREFIX_TMPDIR"
84+
85+
checksum=$(sha256sum "$path")
86+
echo "Release tar checksum $checksum"

0 commit comments

Comments
 (0)