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