1#!/bin/bash 2# 3# Build protoc 4set -evux -o pipefail 5 6PROTOBUF_VERSION=3.5.1 7 8# ARCH is 64 bit unless otherwise specified. 9ARCH="${ARCH:-64}" 10DOWNLOAD_DIR=/tmp/source 11INSTALL_DIR="/tmp/protobuf-cache/$PROTOBUF_VERSION/$(uname -s)-$(uname -p)-x86_$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 wget -O - 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 ./configure CFLAGS=-m"$ARCH" CXXFLAGS=-m"$ARCH" --disable-shared \ 35 --prefix="$INSTALL_DIR" 36 # the same source dir is used for 32 and 64 bit builds, so we need to clean stale data first 37 make clean 38 make -j$NUM_CPU 39 make install 40 popd 41fi 42 43# If /tmp/protobuf exists then we just assume it's a symlink created by us. 44# It may be that it points to the wrong arch, so we idempotently set it now. 45if [[ -L /tmp/protobuf ]]; then 46 rm /tmp/protobuf 47fi 48ln -s "$INSTALL_DIR" /tmp/protobuf 49 50cat <<EOF 51To compile with the build dependencies: 52 53export LDFLAGS=-L/tmp/protobuf/lib 54export CXXFLAGS=-I/tmp/protobuf/include 55export LD_LIBRARY_PATH=/tmp/protobuf/lib 56EOF 57