diff --git a/scripts/build_host_protoc.sh b/scripts/build_host_protoc.sh index 1c741647e70..cd704a8fce4 100755 --- a/scripts/build_host_protoc.sh +++ b/scripts/build_host_protoc.sh @@ -11,22 +11,22 @@ # After the execution of the file, one should be able to find the host protoc # binary at build_host_protoc/bin/protoc. +set -e + CAFFE2_ROOT="$( cd "$(dirname -- "$0")"/.. ; pwd -P)" BUILD_ROOT=$CAFFE2_ROOT/build_host_protoc mkdir -p $BUILD_ROOT/build - cd $BUILD_ROOT/build -CMAKE=$(which cmake || which /usr/bin/cmake || which /usr/local/bin/cmake) -SHARED="$CAFFE2_ROOT/third_party/protobuf/cmake -DCMAKE_INSTALL_PREFIX=$BUILD_ROOT -Dprotobuf_BUILD_TESTS=OFF " -OTHER_FLAGS="" +CMAKE_ARGS=() +CMAKE_ARGS+=("-DCMAKE_INSTALL_PREFIX=$BUILD_ROOT") +CMAKE_ARGS+=("-Dprotobuf_BUILD_TESTS=OFF") while true; do case "$1" in --other-flags) shift; - echo "Other flags passed to cmake: $@"; - OTHER_FLAGS=" $@ "; + CMAKE_ARGS+=("$@") break ;; "") break ;; @@ -36,7 +36,16 @@ while true; do esac done +# Use ccache if available (this path is where Homebrew installs ccache symlinks) +if [ "$(uname)" == 'Darwin' ] && [ -d /usr/local/opt/ccache/libexec ]; then + CMAKE_ARGS+=("-DCMAKE_C_COMPILER=/usr/local/opt/ccache/libexec/gcc") + CMAKE_ARGS+=("-DCMAKE_CXX_COMPILER=/usr/local/opt/ccache/libexec/g++") +fi -$CMAKE $SHARED $OTHER_FLAGS || exit 1 -make -j 4 || exit 1 -make install || exit 1 +cmake "$CAFFE2_ROOT/third_party/protobuf/cmake" ${CMAKE_ARGS[@]} + +if [ "$(uname)" == 'Darwin' ]; then + make "-j$(sysctl -n hw.ncpu)" install +else + make "-j$(nproc)" install +fi diff --git a/scripts/build_ios.sh b/scripts/build_ios.sh index 692153e509b..b8c23fc98d6 100755 --- a/scripts/build_ios.sh +++ b/scripts/build_ios.sh @@ -2,54 +2,80 @@ ############################################################################## # Example command to build the iOS target. ############################################################################## -# +# # This script shows how one can build a Caffe2 binary for the iOS platform # using ios-cmake. This is very similar to the android-cmake - see # build_android.sh for more details. +set -e + CAFFE2_ROOT="$( cd "$(dirname "$0")"/.. ; pwd -P)" -echo "Caffe2 codebase root is: $CAFFE2_ROOT" -# We are going to build the target into build_ios. -BUILD_ROOT=$CAFFE2_ROOT/build_ios -mkdir -p $BUILD_ROOT -echo "Build Caffe2 ios into: $BUILD_ROOT" # Build protobuf from third_party so we have a host protoc binary. echo "Building protoc" BITCODE_FLAGS="-DCMAKE_C_FLAGS=-fembed-bitcode -DCMAKE_CXX_FLAGS=-fembed-bitcode " -$CAFFE2_ROOT/scripts/build_host_protoc.sh --other-flags $BITCODE_FLAGS || exit 1 +$CAFFE2_ROOT/scripts/build_host_protoc.sh --other-flags $BITCODE_FLAGS # Now, actually build the iOS target. -echo "Building caffe2" +BUILD_ROOT=$CAFFE2_ROOT/build_ios +mkdir -p $BUILD_ROOT cd $BUILD_ROOT -if [ -z ${IOS_PLATFORM+x} ]; then - # IOS_PLATFORM is not set, in which case we will default to OS, which - # builds iOS. - IOS_PLATFORM=OS +CMAKE_ARGS=() + +# Use locally built protoc because we'll build libprotobuf for the +# target architecture and need an exact version match. +CMAKE_ARGS+=("-DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=$CAFFE2_ROOT/build_host_protoc/bin/protoc") + +# Use ios-cmake to build iOS project from CMake. +# This projects sets CMAKE_C_COMPILER to /usr/bin/gcc and +# CMAKE_CXX_COMPILER to /usr/bin/g++. In order to use ccache (if it is available) we +# must override these variables via CMake arguments. +CMAKE_ARGS+=("-DCMAKE_TOOLCHAIN_FILE=$CAFFE2_ROOT/third_party/ios-cmake/toolchain/iOS.cmake") +CCACHE_WRAPPER_PATH=/usr/local/opt/ccache/libexec +if [ -d "$CCACHE_WRAPPER_PATH" ]; then + CMAKE_ARGS+=("-DCMAKE_C_COMPILER=$CCACHE_WRAPPER_PATH/gcc") + CMAKE_ARGS+=("-DCMAKE_CXX_COMPILER=$CCACHE_WRAPPER_PATH/g++") fi +# IOS_PLATFORM controls type of iOS platform (see ios-cmake) +if [ -n "${IOS_PLATFORM:-}" ]; then + CMAKE_ARGS+=("-DIOS_PLATFORM=${IOS_PLATFORM}") +else + # IOS_PLATFORM is not set, default to OS, which builds iOS. + CMAKE_ARGS+=("-DIOS_PLATFORM=OS") +fi + +# Don't build binaries or tests (only the library) +CMAKE_ARGS+=("-DBUILD_TEST=OFF") +CMAKE_ARGS+=("-DBUILD_BINARY=OFF") +CMAKE_ARGS+=("-DBUILD_PYTHON=OFF") + +# Disable unused dependencies +CMAKE_ARGS+=("-DUSE_CUDA=OFF") +CMAKE_ARGS+=("-DUSE_OPENCV=OFF") +CMAKE_ARGS+=("-DUSE_LMDB=OFF") +CMAKE_ARGS+=("-DUSE_LEVELDB=OFF") +CMAKE_ARGS+=("-DUSE_MPI=OFF") + +# pthreads +CMAKE_ARGS+=("-DCMAKE_THREAD_LIBS_INIT=-lpthread") +CMAKE_ARGS+=("-DCMAKE_HAVE_THREADS_LIBRARY=1") +CMAKE_ARGS+=("-DCMAKE_USE_PTHREADS_INIT=1") + +# Only toggle if VERBOSE=1 +if [ "${VERBOSE:-}" == '1' ]; then + CMAKE_ARGS+=("-DCMAKE_VERBOSE_MAKEFILE=1") +fi + +CMAKE_ARGS+=("-DCMAKE_C_FLAGS=-fembed-bitcode") +CMAKE_ARGS+=("-DCMAKE_CXX_FLAGS=-fembed-bitcode") + cmake .. \ - -DCMAKE_TOOLCHAIN_FILE=$CAFFE2_ROOT/third_party/ios-cmake/toolchain/iOS.cmake\ -DCMAKE_INSTALL_PREFIX=../install \ -DCMAKE_BUILD_TYPE=Release \ - -DIOS_PLATFORM=${IOS_PLATFORM} \ - -DUSE_CUDA=OFF \ - -DUSE_OPENCV=OFF \ - -DBUILD_TEST=OFF \ - -DBUILD_BINARY=OFF \ - -DCMAKE_C_FLAGS=-fembed-bitcode \ - -DCMAKE_CXX_FLAGS=-fembed-bitcode \ - -DUSE_LMDB=OFF \ - -DUSE_LEVELDB=OFF \ - -DBUILD_PYTHON=OFF \ - -DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=$CAFFE2_ROOT/build_host_protoc/bin/protoc \ - -DCMAKE_VERBOSE_MAKEFILE=1 \ - -DUSE_MPI=OFF \ -DBUILD_SHARED_LIBS=OFF \ - -DCMAKE_THREAD_LIBS_INIT=-lpthread \ - -DCMAKE_HAVE_THREADS_LIBRARY=1 \ - -DCMAKE_USE_PTHREADS_INIT=1 \ - $@ \ - || exit 1 + ${CMAKE_ARGS[@]} \ + $@ + cmake --build . -- "-j$(sysctl -n hw.ncpu)" diff --git a/scripts/build_local.sh b/scripts/build_local.sh index 474e34b9cff..97d33ca8d3e 100755 --- a/scripts/build_local.sh +++ b/scripts/build_local.sh @@ -26,8 +26,13 @@ cd "$BUILD_ROOT" set -x cmake .. ${CMAKE_ARGS} "$@" || exit 1 -if [ "$(uname)" = 'Darwin' ]; then - cmake --build . -- "-j$(sysctl -n hw.ncpu)" +if [ "$(uname)" == 'Darwin' ]; then + # Use ccache if available (this path is where Homebrew installs ccache symlinks) + if [ -d /usr/local/opt/ccache/libexec ]; then + export PATH="/usr/local/opt/ccache/libexec:$PATH" + fi + + cmake --build . -- "-j$(sysctl -n hw.ncpu)" else - cmake --build . -- "-j$(nproc)" + cmake --build . -- "-j$(nproc)" fi