1#!/bin/bash 2# 3# Build protoc 4set -evux -o pipefail 5 6PROTOBUF_VERSION=21.7 7 8# ARCH is x86_64 bit unless otherwise specified. 9ARCH="${ARCH:-x86_64}" 10DOWNLOAD_DIR=/tmp/source 11INSTALL_DIR="/tmp/protobuf-cache/$PROTOBUF_VERSION/$(uname -s)-$ARCH" 12mkdir -p $DOWNLOAD_DIR 13 14# Start with a sane default 15NUM_CPU=4 16if [[ $(uname) == 'Linux' ]]; then 17 NUM_CPU=$(nproc) 18fi 19if [[ $(uname) == 'Darwin' ]]; then 20 NUM_CPU=$(sysctl -n hw.ncpu) 21fi 22 23# Make protoc 24# Can't check for presence of directory as cache auto-creates it. 25if [ -f ${INSTALL_DIR}/bin/protoc ]; then 26 echo "Not building protobuf. Already built" 27# TODO(ejona): swap to `brew install --devel protobuf` once it is up-to-date 28else 29 if [[ ! -d "$DOWNLOAD_DIR"/protobuf-"${PROTOBUF_VERSION}" ]]; then 30 curl -Ls https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/protobuf-all-${PROTOBUF_VERSION}.tar.gz | tar xz -C $DOWNLOAD_DIR 31 fi 32 pushd $DOWNLOAD_DIR/protobuf-${PROTOBUF_VERSION} 33 # install here so we don't need sudo 34 if [[ "$ARCH" == x86* ]]; then 35 ./configure CFLAGS=-m${ARCH#*_} CXXFLAGS=-m${ARCH#*_} --disable-shared \ 36 --prefix="$INSTALL_DIR" 37 elif [[ "$ARCH" == aarch* ]]; then 38 ./configure --disable-shared --host=aarch64-linux-gnu --prefix="$INSTALL_DIR" 39 elif [[ "$ARCH" == ppc* ]]; then 40 ./configure --disable-shared --host=powerpc64le-linux-gnu --prefix="$INSTALL_DIR" 41 elif [[ "$ARCH" == s390* ]]; then 42 ./configure --disable-shared --host=s390x-linux-gnu --prefix="$INSTALL_DIR" 43 elif [[ "$ARCH" == loongarch* ]]; then 44 ./configure --disable-shared --host=loongarch64-unknown-linux-gnu --prefix="$INSTALL_DIR" 45 fi 46 # the same source dir is used for 32 and 64 bit builds, so we need to clean stale data first 47 make clean 48 make V=0 -j$NUM_CPU 49 make install 50 popd 51fi 52 53# If /tmp/protobuf exists then we just assume it's a symlink created by us. 54# It may be that it points to the wrong arch, so we idempotently set it now. 55if [[ -L /tmp/protobuf ]]; then 56 rm /tmp/protobuf 57fi 58ln -s "$INSTALL_DIR" /tmp/protobuf 59 60cat <<EOF 61To compile with the build dependencies: 62 63export LDFLAGS=-L/tmp/protobuf/lib 64export CXXFLAGS=-I/tmp/protobuf/include 65export LD_LIBRARY_PATH=/tmp/protobuf/lib 66EOF 67