xref: /aosp_15_r20/external/pytorch/scripts/build_local.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1#!/bin/bash
2#
3##############################################################################
4# Example command to build Caffe2
5##############################################################################
6#
7
8set -ex
9
10CAFFE2_ROOT="$( cd "$(dirname "$0")"/.. ; pwd -P)"
11
12CMAKE_ARGS=()
13
14# If Ninja is installed, prefer it to Make
15if [ -x "$(command -v ninja)" ]; then
16  CMAKE_ARGS+=("-GNinja")
17fi
18
19# Use ccache if available (this path is where Homebrew installs ccache symlinks)
20if [ "$(uname)" == 'Darwin' ]; then
21  if [ -n "${CCACHE_WRAPPER_PATH:-}"]; then
22    CCACHE_WRAPPER_PATH=/usr/local/opt/ccache/libexec
23  fi
24  if [ -d "$CCACHE_WRAPPER_PATH" ]; then
25    CMAKE_ARGS+=("-DCMAKE_C_COMPILER=$CCACHE_WRAPPER_PATH/gcc")
26    CMAKE_ARGS+=("-DCMAKE_CXX_COMPILER=$CCACHE_WRAPPER_PATH/g++")
27  fi
28fi
29
30# Use special install script with Anaconda
31if [ -n "${USE_ANACONDA}" ]; then
32  export SKIP_CONDA_TESTS=1
33  export CONDA_INSTALL_LOCALLY=1
34  "${ROOT_DIR}/scripts/build_anaconda.sh" "$@"
35else
36  # Make sure that pyyaml is installed for the codegen of building Aten to work
37  if [[ -n "$(python -c 'import yaml' 2>&1)" ]]; then
38    echo "Installing pyyaml with pip at $(which pip)"
39    pip install --user pyyaml
40  fi
41
42  # Make sure that typing is installed for the codegen of building Aten to work
43  if [[ -n "$(python -c 'import typing' 2>&1)" ]]; then
44    echo "Installing typing with pip at $(which pip)"
45    pip install --user typing
46  fi
47
48  # Build protobuf compiler from third_party if configured to do so
49  if [ -n "${USE_HOST_PROTOC:-}" ]; then
50    echo "USE_HOST_PROTOC is set; building protoc before building Caffe2..."
51    "$CAFFE2_ROOT/scripts/build_host_protoc.sh"
52    CUSTOM_PROTOC_EXECUTABLE="$CAFFE2_ROOT/build_host_protoc/bin/protoc"
53    echo "Built protoc $("$CUSTOM_PROTOC_EXECUTABLE" --version)"
54    CMAKE_ARGS+=("-DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=$CUSTOM_PROTOC_EXECUTABLE")
55  fi
56
57  # We are going to build the target into build.
58  BUILD_ROOT=${BUILD_ROOT:-"$CAFFE2_ROOT/build"}
59  mkdir -p "$BUILD_ROOT"
60  cd "$BUILD_ROOT"
61  echo "Building Caffe2 in: $BUILD_ROOT"
62
63  cmake "$CAFFE2_ROOT" \
64        -DCMAKE_BUILD_TYPE=Release \
65        "${CMAKE_ARGS[@]}" \
66        "$@"
67
68  # Determine the number of CPUs to build with.
69  # If the `CAFFE_MAKE_NCPUS` variable is not specified, use them all.
70  if [ -n "${MAX_JOBS}" ]; then
71      CAFFE_MAKE_NCPUS="$MAX_JOBS"
72  elif [ -n "${CAFFE_MAKE_NCPUS}" ]; then
73      CAFFE_MAKE_NCPUS="$CAFFE_MAKE_NCPUS"
74  elif [ "$(uname)" == 'Darwin' ]; then
75      CAFFE_MAKE_NCPUS="$(sysctl -n hw.ncpu)"
76  else
77      CAFFE_MAKE_NCPUS="$(nproc)"
78  fi
79
80  # Now, actually build the target.
81  cmake --build . -- "-j$CAFFE_MAKE_NCPUS"
82fi
83