roneocibinarieslinuxbuild-archives.sh

ee892817f152551895f8482d4db15cefa51ef865

At commit ee89281

176 lines · 6.2 KiBShellRaw
  1. #!/usr/bin/env bash
  2. # Build static Linux musl release archives (`linux-amd64`, `linux-arm64`) for
  3. # Roneo and write an OCI image layout matching docs/PUBLIC-FORGE-RELEASES.md §4.
  4. #
  5. # Usage:
  6. # ci/binaries/linux/build-archives.sh [commit-sha]
  7. #
  8. # Output:
  9. # release/linux/oci OCI image layout containing one image index with two
  10. # archive manifests:
  11. # - entry `linux-amd64` -> roneo-<version>-linux-amd64.tar.gz
  12. # - entry `linux-arm64` -> roneo-<version>-linux-arm64.tar.gz
  13. set -euo pipefail
  14. binary_name="roneo"
  15. layout_dir="${RONEO_RELEASE_LAYOUT:-release/linux/oci}"
  16. index_type="application/vnd.oci.image.index.v1+json"
  17. manifest_type="application/vnd.oci.image.manifest.v1+json"
  18. archive_type="application/vnd.forge.archive.v1"
  19. empty_type="application/vnd.oci.empty.v1+json"
  20. gzip_type="application/gzip"
  21. commit="${1:-}"
  22. if [ -n "$commit" ] && ! [[ $commit =~ ^[0-9a-f]{40}$ ]]; then
  23. echo "build-archives.sh: $commit is not a full 40-hex commit SHA" >&2
  24. exit 2
  25. fi
  26. # Extract workspace package version from Cargo.toml (no build metadata '+').
  27. read_version() {
  28. awk '
  29. /^\[workspace\.package\]/ { in_pkg = 1; next }
  30. /^\[/ { in_pkg = 0 }
  31. in_pkg && /^version[[:space:]]*=/ {
  32. gsub(/^[^"]*"/, "")
  33. gsub(/".*$/, "")
  34. print
  35. exit
  36. }
  37. ' Cargo.toml
  38. }
  39. version=$(read_version)
  40. if ! [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
  41. echo "build-archives.sh: invalid Cargo.toml workspace version: '${version}'" >&2
  42. exit 1
  43. fi
  44. sha256_of() {
  45. local sum
  46. sum=$(sha256sum "$1")
  47. printf '%s' "${sum%% *}"
  48. }
  49. oci_put_file() {
  50. local blobs=$1 media_type=$2 src=$3 hex size
  51. hex=$(sha256_of "$src")
  52. size=$(wc -c <"$src")
  53. size=${size//[[:space:]]/}
  54. [ -e "$blobs/$hex" ] || cp "$src" "$blobs/$hex"
  55. printf '{"digest":"sha256:%s","mediaType":"%s","size":%s}' "$hex" "$media_type" "$size"
  56. }
  57. oci_put_json() {
  58. local blobs=$1 media_type=$2 payload=$3 hex size
  59. hex=$(printf '%s' "$payload" | sha256sum)
  60. hex=${hex%% *}
  61. printf '%s' "$payload" >"$blobs/$hex"
  62. size=$(wc -c <"$blobs/$hex")
  63. size=${size//[[:space:]]/}
  64. printf '{"digest":"sha256:%s","mediaType":"%s","size":%s}' "$hex" "$media_type" "$size"
  65. }
  66. build_target() {
  67. local triple=$1 arch=$2 out_archive=$3
  68. local bin stage
  69. echo "build-archives.sh: building ${binary_name} ${version} for ${triple} (linux/${arch})"
  70. RUSTFLAGS="--remap-path-prefix=${HOME}=/build --remap-path-prefix=$(pwd)=/src -C linker=rust-lld -C link-self-contained=yes -C target-feature=+crt-static -C link-arg=-s -C link-arg=-z -C link-arg=relro -C link-arg=-z -C link-arg=now -C link-arg=-z -C link-arg=noexecstack" \
  71. cargo build --release --locked --target "$triple" -p roneo-cli --bin "$binary_name"
  72. bin="target/${triple}/release/${binary_name}"
  73. test -f "$bin"
  74. # Verify the binary runs on matching host architecture.
  75. local host_arch
  76. host_arch=$(uname -m)
  77. if { [ "$arch" = "amd64" ] && [ "$host_arch" = "x86_64" ]; } ||
  78. { [ "$arch" = "arm64" ] && [ "$host_arch" = "aarch64" ]; }; then
  79. local said
  80. said=$("$bin" --version)
  81. if [ "$said" != "${binary_name} ${version}" ]; then
  82. echo "build-archives.sh: ${triple} binary said '${said}', expected '${binary_name} ${version}'" >&2
  83. return 1
  84. fi
  85. echo "build-archives.sh: verified ${triple} executable: ${said}"
  86. fi
  87. stage="${work}/stage-${arch}"
  88. mkdir -p "$stage"
  89. cp "$bin" "${stage}/${binary_name}"
  90. chmod 0755 "${stage}/${binary_name}"
  91. cp LICENSE "${stage}/LICENSE"
  92. chmod 0644 "${stage}/LICENSE"
  93. # Deterministic tarball with binary at root (`roneo`) for direct extraction
  94. # by both `install.sh` and the `mise` forge backend plugin.
  95. tar -C "$stage" \
  96. --owner=0 --group=0 --numeric-owner \
  97. --sort=name --mtime='@0' \
  98. -cf - "$binary_name" LICENSE | gzip -n -9 >"$out_archive"
  99. }
  100. write_oci_layout() {
  101. local out=$1
  102. shift
  103. local blobs config layer manifest entry entries="" index pair ref_name file title
  104. if [ -e "$out" ] || [ -L "$out" ]; then
  105. echo "build-archives.sh: $out already exists; layout must start clean" >&2
  106. return 1
  107. fi
  108. blobs="${out}/blobs/sha256"
  109. mkdir -p "$blobs"
  110. config=$(oci_put_json "$blobs" "$empty_type" '{}')
  111. for pair in "$@"; do
  112. ref_name=${pair%%=*}
  113. file=${pair#*=}
  114. if [ ! -f "$file" ] || [ -L "$file" ]; then
  115. echo "build-archives.sh: $file is not a regular file" >&2
  116. return 1
  117. fi
  118. title=${file##*/}
  119. if ! [[ $title =~ ^[A-Za-z0-9._+-]+$ ]]; then
  120. echo "build-archives.sh: $title is not a plain file name" >&2
  121. return 1
  122. fi
  123. if [[ $title != "${binary_name}-${version}-linux-"* ]]; then
  124. echo "build-archives.sh: $title does not start with ${binary_name}-${version}-linux-" >&2
  125. return 1
  126. fi
  127. layer=$(oci_put_file "$blobs" "$gzip_type" "$file")
  128. manifest=$(printf '{"artifactType":"%s","config":%s,"layers":[{"annotations":{"org.opencontainers.image.title":"%s"},%s],"mediaType":"%s","schemaVersion":2}' \
  129. "$archive_type" "$config" "$title" "${layer#\{}" "$manifest_type")
  130. entry=$(oci_put_json "$blobs" "$manifest_type" "$manifest")
  131. entries+=${entries:+,}$(printf '{"annotations":{"org.opencontainers.image.ref.name":"%s"},"artifactType":"%s",%s' \
  132. "$ref_name" "$archive_type" "${entry#\{}")
  133. done
  134. index=$(oci_put_json "$blobs" "$index_type" \
  135. "$(printf '{"manifests":[%s],"mediaType":"%s","schemaVersion":2}' "$entries" "$index_type")")
  136. printf '{"manifests":[%s],"mediaType":"%s","schemaVersion":2}' "$index" "$index_type" >"${out}/index.json"
  137. printf '{"imageLayoutVersion":"1.0.0"}' >"${out}/oci-layout"
  138. }
  139. work=$(mktemp -d "${TMPDIR:-/tmp}/roneo-release-linux.XXXXXX")
  140. trap 'rm -rf "$work"' EXIT INT TERM
  141. rustup target add x86_64-unknown-linux-musl aarch64-unknown-linux-musl
  142. amd64_archive="${work}/${binary_name}-${version}-linux-amd64.tar.gz"
  143. arm64_archive="${work}/${binary_name}-${version}-linux-arm64.tar.gz"
  144. build_target x86_64-unknown-linux-musl amd64 "$amd64_archive"
  145. build_target aarch64-unknown-linux-musl arm64 "$arm64_archive"
  146. rm -rf "$layout_dir"
  147. write_oci_layout "$layout_dir" \
  148. "linux-amd64=${amd64_archive}" \
  149. "linux-arm64=${arm64_archive}"
  150. echo "build-archives.sh: wrote OCI layout at ${layout_dir}"
  151. ls -la "${layout_dir}" "${layout_dir}/blobs/sha256"