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