xref: /aosp_15_r20/external/pytorch/scripts/build_host_protoc.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker#!/bin/bash
2*da0073e9SAndroid Build Coastguard Worker##############################################################################
3*da0073e9SAndroid Build Coastguard Worker# Build script to build the protoc compiler for the host platform.
4*da0073e9SAndroid Build Coastguard Worker##############################################################################
5*da0073e9SAndroid Build Coastguard Worker# This script builds the protoc compiler for the host platform, which is needed
6*da0073e9SAndroid Build Coastguard Worker# for any cross-compilation as we will need to convert the protobuf source
7*da0073e9SAndroid Build Coastguard Worker# files to cc files.
8*da0073e9SAndroid Build Coastguard Worker#
9*da0073e9SAndroid Build Coastguard Worker# --other-flags accepts flags that should be passed to cmake. Optional.
10*da0073e9SAndroid Build Coastguard Worker#
11*da0073e9SAndroid Build Coastguard Worker# After the execution of the file, one should be able to find the host protoc
12*da0073e9SAndroid Build Coastguard Worker# binary at build_host_protoc/bin/protoc.
13*da0073e9SAndroid Build Coastguard Worker
14*da0073e9SAndroid Build Coastguard Workerset -e
15*da0073e9SAndroid Build Coastguard Worker
16*da0073e9SAndroid Build Coastguard WorkerCAFFE2_ROOT="$( cd "$(dirname -- "$0")"/.. ; pwd -P)"
17*da0073e9SAndroid Build Coastguard WorkerBUILD_ROOT=${BUILD_ROOT:-"$CAFFE2_ROOT/build_host_protoc"}
18*da0073e9SAndroid Build Coastguard Workermkdir -p $BUILD_ROOT/build
19*da0073e9SAndroid Build Coastguard Workercd $BUILD_ROOT/build
20*da0073e9SAndroid Build Coastguard Worker
21*da0073e9SAndroid Build Coastguard WorkerCMAKE_ARGS=()
22*da0073e9SAndroid Build Coastguard WorkerCMAKE_ARGS+=("-DCMAKE_INSTALL_PREFIX=$BUILD_ROOT")
23*da0073e9SAndroid Build Coastguard WorkerCMAKE_ARGS+=("-Dprotobuf_BUILD_TESTS=OFF")
24*da0073e9SAndroid Build Coastguard Worker
25*da0073e9SAndroid Build Coastguard Worker# If Ninja is installed, prefer it to Make
26*da0073e9SAndroid Build Coastguard Workerif [ -x "$(command -v ninja)" ]; then
27*da0073e9SAndroid Build Coastguard Worker  CMAKE_ARGS+=("-GNinja")
28*da0073e9SAndroid Build Coastguard Workerfi
29*da0073e9SAndroid Build Coastguard Worker
30*da0073e9SAndroid Build Coastguard Workerwhile true; do
31*da0073e9SAndroid Build Coastguard Worker    case "$1" in
32*da0073e9SAndroid Build Coastguard Worker        --other-flags)
33*da0073e9SAndroid Build Coastguard Worker            shift;
34*da0073e9SAndroid Build Coastguard Worker            CMAKE_ARGS+=("$@")
35*da0073e9SAndroid Build Coastguard Worker            break ;;
36*da0073e9SAndroid Build Coastguard Worker        "")
37*da0073e9SAndroid Build Coastguard Worker            break ;;
38*da0073e9SAndroid Build Coastguard Worker        *)
39*da0073e9SAndroid Build Coastguard Worker            echo "Unknown option passed as argument: $1"
40*da0073e9SAndroid Build Coastguard Worker            break ;;
41*da0073e9SAndroid Build Coastguard Worker    esac
42*da0073e9SAndroid Build Coastguard Workerdone
43*da0073e9SAndroid Build Coastguard Worker
44*da0073e9SAndroid Build Coastguard Worker# Use ccache if available (this path is where Homebrew installs ccache symlinks)
45*da0073e9SAndroid Build Coastguard Workerif [ "$(uname)" == 'Darwin' ] && [ -d /usr/local/opt/ccache/libexec ]; then
46*da0073e9SAndroid Build Coastguard Worker  CMAKE_ARGS+=("-DCMAKE_C_COMPILER=/usr/local/opt/ccache/libexec/gcc")
47*da0073e9SAndroid Build Coastguard Worker  CMAKE_ARGS+=("-DCMAKE_CXX_COMPILER=/usr/local/opt/ccache/libexec/g++")
48*da0073e9SAndroid Build Coastguard Workerfi
49*da0073e9SAndroid Build Coastguard Worker
50*da0073e9SAndroid Build Coastguard Workercmake "$CAFFE2_ROOT/third_party/protobuf/cmake" ${CMAKE_ARGS[@]}
51*da0073e9SAndroid Build Coastguard Worker
52*da0073e9SAndroid Build Coastguard Workerif [ -z "$MAX_JOBS" ]; then
53*da0073e9SAndroid Build Coastguard Worker  if [ "$(uname)" == 'Darwin' ]; then
54*da0073e9SAndroid Build Coastguard Worker    MAX_JOBS=$(sysctl -n hw.ncpu)
55*da0073e9SAndroid Build Coastguard Worker  else
56*da0073e9SAndroid Build Coastguard Worker    MAX_JOBS=$(nproc)
57*da0073e9SAndroid Build Coastguard Worker  fi
58*da0073e9SAndroid Build Coastguard Workerfi
59*da0073e9SAndroid Build Coastguard Workercmake --build . -- "-j${MAX_JOBS}" install
60