Skip to content

Commit

Permalink
Install specific protoc version when generating protobufs
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Dec 18, 2024
1 parent aa7c048 commit 0a54794
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 14 deletions.
1 change: 1 addition & 0 deletions scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protobuf-bin/
78 changes: 78 additions & 0 deletions scripts/download-protoc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bash
set -eou pipefail

# Specify the protobuf release version
PROTOBUF_VERSION="29.2"

# Define SHA-256 hashes for each supported platform
# Update these hashes by running ./print-protoc-hashes.sh
declare -A SHA256_HASHES=(
["linux-aarch64"]="0019dfc4b32d63c1392aa264aed2253c1e0c2fb09216f8e2cc269bbfb8bb49b5"
["linux-x86_64"]="52e9e7ece55c7e30e7e8bbd254b4b21b408a5309bca826763c7124b696a132e9"
["darwin-aarch64"]="0e153a38d6da19594c980e7f7cd3ea0ddd52c9da1068c03c0d8533369fbfeb20"
)

# Determine the platform
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
[[ "${ARCH}" == "arm64" ]] && ARCH="aarch64"

PLATFORM="${OS}-${ARCH}"

# Set the download URL based on the platform
case "${PLATFORM}" in
linux-x86_64)
PROTOC_ZIP="protoc-${PROTOBUF_VERSION}-linux-x86_64.zip"
;;
linux-aarch64)
PROTOC_ZIP="protoc-${PROTOBUF_VERSION}-linux-aarch64.zip"
;;
darwin-aarch64)
PROTOC_ZIP="protoc-${PROTOBUF_VERSION}-osx-aarch_64.zip"
;;
*)
echo "Unsupported platform: ${PLATFORM}" >&2
exit 1
;;
esac

# Download the specified version of protobuf
DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_ZIP}"
echo "Downloading from: ${DOWNLOAD_URL}" >&2
curl -LO "${DOWNLOAD_URL}"

# Verify checksum
EXPECTED_SHA256="${SHA256_HASHES[${PLATFORM}]}"
if command -v shasum >/dev/null 2>&1; then
ACTUAL_SHA256=$(shasum -a 256 "${PROTOC_ZIP}" | cut -d' ' -f1)
else
ACTUAL_SHA256=$(sha256sum "${PROTOC_ZIP}" | cut -d' ' -f1)
fi

if [[ "${ACTUAL_SHA256}" != "${EXPECTED_SHA256}" ]]; then
echo "Checksum verification failed!" >&2
echo "Expected: ${EXPECTED_SHA256}" >&2
echo "Got: ${ACTUAL_SHA256}" >&2
rm "${PROTOC_ZIP}"
exit 1
fi

echo "Checksum verified successfully" >&2

# Create a directory for extraction
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_DIR="$SCRIPT_DIR/protobuf-bin/protoc-${PROTOBUF_VERSION}"
mkdir -p "${INSTALL_DIR}"

# Unzip the downloaded file
unzip -q -o "${PROTOC_ZIP}" -d "${INSTALL_DIR}"

# Clean up the zip file
rm "${PROTOC_ZIP}"

# Return a new PATH with the protobuf binary
PROTOC_BIN="${INSTALL_DIR}/bin"
echo "Installed protoc ${PROTOBUF_VERSION} to ${INSTALL_DIR}" >&2

# Return the protoc bin path to stdout
printf "${PROTOC_BIN}"
32 changes: 18 additions & 14 deletions scripts/gen-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ set -eou pipefail
root=$1

proto_array=(
core/crypto/pb/crypto.proto
core/record/pb/envelope.proto
core/peer/pb/peer_record.proto
core/sec/insecure/pb/plaintext.proto
p2p/host/autonat/pb/autonat.proto
p2p/security/noise/pb/payload.proto
p2p/transport/webrtc/pb/message.proto
p2p/protocol/identify/pb/identify.proto
p2p/protocol/circuitv2/pb/circuit.proto
p2p/protocol/circuitv2/pb/voucher.proto
p2p/protocol/autonatv2/pb/autonatv2.proto
p2p/protocol/holepunch/pb/holepunch.proto
p2p/host/peerstore/pstoreds/pb/pstore.proto
core/crypto/pb/crypto.proto
core/record/pb/envelope.proto
core/peer/pb/peer_record.proto
core/sec/insecure/pb/plaintext.proto
p2p/host/autonat/pb/autonat.proto
p2p/security/noise/pb/payload.proto
p2p/transport/webrtc/pb/message.proto
p2p/protocol/identify/pb/identify.proto
p2p/protocol/circuitv2/pb/circuit.proto
p2p/protocol/circuitv2/pb/voucher.proto
p2p/protocol/autonatv2/pb/autonatv2.proto
p2p/protocol/holepunch/pb/holepunch.proto
p2p/host/peerstore/pstoreds/pb/pstore.proto
)

proto_paths=""
for path in "${proto_array[@]}"; do
proto_paths+="$path "
proto_paths+="$path "
done

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROTOC_BIN_PATH="$("${SCRIPT_DIR}/download-protoc.sh")"
export PATH="$PROTOC_BIN_PATH:$PATH"

echo protoc --version $(protoc --version)
protoc --proto_path=$root --go_out=$root --go_opt=paths=source_relative $proto_paths
62 changes: 62 additions & 0 deletions scripts/print-protoc-hashes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash
set -eou pipefail

# Specify the protobuf release version
PROTOBUF_VERSION="29.2"

# Define the platforms
PLATFORMS=("linux-x86_64" "linux-aarch64" "darwin-aarch64")

# Array to store the hashes
declare -A HASHES

# Function to download and calculate the SHA-256 hash
calculate_hash() {
local platform=$1
local protoc_zip

case "${platform}" in
linux-x86_64)
protoc_zip="protoc-${PROTOBUF_VERSION}-linux-x86_64.zip"
;;
linux-aarch64)
protoc_zip="protoc-${PROTOBUF_VERSION}-linux-aarch64.zip"
;;
darwin-aarch64)
protoc_zip="protoc-${PROTOBUF_VERSION}-osx-aarch_64.zip"
;;
*)
echo "Unsupported platform: ${platform}"
exit 1
;;
esac

# Download the specified version of protobuf
download_url="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOBUF_VERSION}/${protoc_zip}"
echo "Downloading from: ${download_url}"
curl -LO "${download_url}"

# Calculate the SHA-256 hash
if command -v shasum >/dev/null 2>&1; then
sha256_hash=$(shasum -a 256 "${protoc_zip}" | cut -d' ' -f1)
else
sha256_hash=$(sha256sum "${protoc_zip}" | cut -d' ' -f1)
fi

# Store the hash in the array
HASHES["${platform}"]="${sha256_hash}"

# Clean up the zip file
rm "${protoc_zip}"
}

# Iterate over the platforms and calculate the hashes
for platform in "${PLATFORMS[@]}"; do
calculate_hash "${platform}"
done

# Print all the hashes together at the end
echo "Expected SHA-256 hashes for protobuf ${PROTOBUF_VERSION}:"
for platform in "${!HASHES[@]}"; do
echo "[\"${platform}\"]=\"${HASHES[${platform}]}\""
done

0 comments on commit 0a54794

Please sign in to comment.