Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amanbolat/4d4e9aeec2bb7baefc1fbea8bf67a026 to your computer and use it in GitHub Desktop.
Save amanbolat/4d4e9aeec2bb7baefc1fbea8bf67a026 to your computer and use it in GitHub Desktop.
Makefile with pinned version of protoc and protoc-gen-go

Makefile with pinned version of protoc

This gist includes two files, Makefile and the script.

Script downloads the protoc for a given os and arch, unzips the files to .bin directory. It won't download the zip file, if it was already downloaded.

Makefile provides gen-prot entry point that ensures that protoc exists and generates the code from given proto files.

This setup ensures that the protoc and plugins versions are pinned and there is no need to donwload those tools manually.

export BIN := $(PWD)/.bin
export PROTOC_BIN := $(PWD)/.bin/protocd/bin
export PATH := $(BIN):$(PROTOC_BIN):$(PATH)
PROTOC_VER := 25.3
PROTOC_CHECKSUM := d0fcd6d3b3ef6f22f1c47cc30a80c06727e1eccdddcaf0f4a3be47c070ffd3fe
gen-proto:
# install protoc
PROTOC_VER=${PROTOC_VER} PROTOC_CHECKSUM=${PROTOC_CHECKSUM} ./install-protoc.sh
go install google.golang.org/protobuf/cmd/[email protected]
protoc --proto_path=. --go_out=. --go-grpc_out=. \
--go_opt=module=github.com/user/repo \
path/to/proto_files/*.proto
#!/bin/bash
# This script downloads and installs the protoc binary for the current platform.
# Only Linux and macOS are supported.
#
# Required environment variables:
# - PROTOC_VER: the version of protoc to install (e.g. 25.3)
# - PROTOC_CHECKSUM: the sha256 checksum of the protoc binary (e.g. d0fcd6d3b3ef6f22f1c47cc30a80c06727e1eccdddcaf0f4a3be47c070ffd3fe)
#
# To get the checksum run the command below:
# curl -L "https://github.com/protocolbuffers/protobuf/releases/download/v25.3/protoc-25.3-osx-aarch_64.zip" | sha256sum
set -ex
if [[ -z "$PROTOC_VER" ]]; then
echo "PROTOC_VER environment variable not set"
exit 1
fi
if [[ -z "$PROTOC_CHECKSUM" ]]; then
echo "PROTOC_CHECKSUM environment variable not set"
exit 1
fi
os_name="$(uname -o)"
case $os_name in
"GNU/Linux")
os_name="linux"
;;
"Darwin")
os_name="osx"
;;
*)
echo "unsupported OS: $os_name"
exit 1
;;
esac
os_arch="$(uname -m)"
case $os_arch in
"x86_64")
os_arch="x86_64"
;;
"arm64")
os_arch="aarch_64"
;;
*)
echo "unsupported architecture: $os_arch"
exit 1
;;
esac
if [ -f ".bin/protoc.zip" ]; then
ACTUAL_CHECKSUM=$(sha256sum .bin/protoc.zip | awk '{ print $1 }')
if [ "$ACTUAL_CHECKSUM" == "$PROTOC_CHECKSUM" ]; then
echo "Checksums match, no need to download"
exit 0
fi
fi
curl -sSL \
"https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VER}/protoc-${PROTOC_VER}-${os_name}-${os_arch}.zip" \
-o .bin/protoc.zip
rm -rf .bin/protocd
rm -rf .bin/protoc
mkdir -p .bin/protocd
unzip .bin/protoc.zip -d .bin/protocd
chmod +x .bin/protocd/bin/protoc
MIT License
Copyright (c) 2024 Amanbolat Balabekov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment