blob: d380debc9684fa4f064f43870fdb130159674ecb [file] [log] [blame]
#!/bin/bash
# NOTE: All parameters to this function are forwared directly to conda-build
# and so will never be seen by the build.sh
# TODO change arguments to go to cmake by default
# TODO handle setting flags in build.sh too
set -ex
# portable_sed: A wrapper around sed that works on both mac and linux, used to
# alter conda-build files such as the meta.yaml. It always adds the inplace
# flag
# portable_sed <full regex string> <file>
portable_sed () {
if [ "$(uname)" == 'Darwin' ]; then
sed -i '' "$1" "$2"
else
sed -i "$1" "$2"
fi
}
# remove_package: Given a package name, removes any line that mentions that
# file from the meta.yaml
remove_package () {
portable_sed "/$1/d" "${META_YAML}"
}
# add_package: Takes a package name and a version and finagles the
# meta.yaml to ask for that version specifically.
# NOTE: this assumes that $META_YAML has already been set
add_package () {
remove_package $1
# This magic string _M_STR is in the requirements sections of the meta.yaml
# The \\"$'\n' is a properly escaped new line
# Those 4 spaces are there to properly indent the comment
local _M_STR='# other packages here'
portable_sed "s/$_M_STR/- ${1} ${2}\\"$'\n'" $_M_STR/" "${META_YAML}"
}
CAFFE2_ROOT="$( cd "$(dirname "$0")"/.. ; pwd -P)"
CONDA_BUILD_ARGS=()
CMAKE_BUILD_ARGS=()
# Reinitialize submodules
git submodule update --init
#
# Read python and gcc version
#
# Read the gcc version to see what ABI to build for
if [[ $BUILD_ENVIRONMENT == *gcc4.8* ]]; then
GCC_USE_C11=0
GCC_VERSION='4.8'
fi
if [ "$(uname)" != 'Darwin' -a -z "${GCC_USE_C11}" ]; then
GCC_VERSION="$(gcc --version | grep --only-matching '[0-9]\.[0-9]\.[0-9]*' | head -1)"
if [[ "$GCC_VERSION" == 4* ]]; then
GCC_USE_C11=0
else
GCC_USE_C11=1
fi
fi
# Read the python version
# Specifically 3.6 because the latest Anaconda version is 3.6, and so it's site
# packages have 3.6 in the name
PYTHON_VERSION="$(python --version 2>&1 | grep --only-matching '[0-9]\.[0-9]\.[0-9]*')"
if [[ "$PYTHON_VERSION" == 3.6* ]]; then
# This is needed or else conda tries to move packages to python3/site-packages
# isntead of python3.6/site-packages
CONDA_BUILD_ARGS+=(" --python 3.6")
fi
#
# Pick the correct conda-build folder
#
CAFFE2_CONDA_BUILD_DIR="${CAFFE2_ROOT}/conda/caffe2"
if [[ "${BUILD_ENVIRONMENT}" == *full* ]]; then
CAFFE2_CONDA_BUILD_DIR="${CAFFE2_CONDA_BUILD_DIR}/cuda_full"
elif [[ "${BUILD_ENVIRONMENT}" == *cuda* ]]; then
CAFFE2_CONDA_BUILD_DIR="${CAFFE2_CONDA_BUILD_DIR}/cuda"
else
CAFFE2_CONDA_BUILD_DIR="${CAFFE2_CONDA_BUILD_DIR}/no_cuda"
fi
META_YAML="${CAFFE2_CONDA_BUILD_DIR}/meta.yaml"
CONDA_BUILD_CONFIG_YAML="${CAFFE2_CONDA_BUILD_DIR}/conda_build_config.yaml"
#
# Build the name of the package depending on CUDA and gcc
#
CAFFE2_PACKAGE_NAME="caffe2"
if [[ $BUILD_ENVIRONMENT == *cuda* ]]; then
# CUDA 9.0 and 9.1 are not in conda, and cuDNN is not in conda, so instead of
# pinning CUDA and cuDNN versions in the conda_build_config and then setting
# the package name in meta.yaml based off of these values, we let Caffe2
# take the CUDA and cuDNN versions that it finds in the build environment,
# and manually set the package name ourself.
CAFFE2_PACKAGE_NAME="${CAFFE2_PACKAGE_NAME}-cuda${CAFFE2_CUDA_VERSION}-cudnn${CAFFE2_CUDNN_VERSION}"
fi
if [[ "$(uname)" != 'Darwin' ]]; then
if [[ $GCC_USE_C11 -eq 0 ]]; then
# gcc compatibility is not tracked by conda-forge, so we track it ourselves
CAFFE2_PACKAGE_NAME="${CAFFE2_PACKAGE_NAME}-gcc${GCC_VERSION:0:3}"
fi
fi
if [[ $BUILD_ENVIRONMENT == *full* ]]; then
CAFFE2_PACKAGE_NAME="${CAFFE2_PACKAGE_NAME}-full"
fi
portable_sed "s/name: caffe2.*\$/name: ${CAFFE2_PACKAGE_NAME}/" "${META_YAML}"
#
# Handle skipping tests and uploading
#
# If skipping tests, remove the test related lines from the meta.yaml and don't
# upload to Anaconda.org
if [ -n "$SKIP_CONDA_TESTS" ]; then
portable_sed '/test:/d' "${META_YAML}"
portable_sed '/imports:/d' "${META_YAML}"
portable_sed '/caffe2.python.core/d' "${META_YAML}"
elif [ -n "$UPLOAD_TO_CONDA" ]; then
# Upload to Anaconda.org if needed. This is only allowed if testing is
# enabled
CONDA_BUILD_ARGS+=(" --user ${ANACONDA_USERNAME}")
CONDA_BUILD_ARGS+=(" --token ${CAFFE2_ANACONDA_ORG_ACCESS_TOKEN}")
fi
#
# Change flags based on target gcc ABI
#
if [[ "$(uname)" != 'Darwin' ]]; then
if [ "$GCC_USE_C11" -eq 0 ]; then
CMAKE_BUILD_ARGS+=("-DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=0")
# Default conda channels use gcc 7.2 (for recent packages), conda-forge uses
# gcc 4.8.5
CAFFE2_CONDA_CHANNEL='-c conda-forge'
# opencv 3.3.1 in conda-forge doesn't have imgcodecs
add_package 'opencv' '==3.1.0'
if [[ "$PYTHON_VERSION" == 3.* ]]; then
# opencv 3.1.0 for python 3 requires numpy 1.12
add_package 'numpy' '>1.11'
fi
fi
fi
# Build Caffe2 with conda-build
#
# If --user and --token are set, then this will also upload the built package
# to Anaconda.org, provided there were no failures and all the tests passed
CONDA_CMAKE_BUILD_ARGS="$CMAKE_BUILD_ARGS" conda build "${CAFFE2_CONDA_BUILD_DIR}" $CAFFE2_CONDA_CHANNEL ${CONDA_BUILD_ARGS[@]} "$@"
# Install Caffe2 from the built package into the local conda environment
if [ -n "$CONDA_INSTALL_LOCALLY" ]; then
conda install -y $CAFFE2_CONDA_CHANNEL "${CAFFE2_PACKAGE_NAME}" --use-local
fi