Skip to content
Snippets Groups Projects
buildscript 12.7 KiB
Newer Older
#!/bin/bash -ex
###############################################################################
# LINUX/MAC SCRIPT TO DRIVE THE JENKINS BUILDS OF MANTID.
# WORKSPACE, JOB_NAME, NODE_LABEL, PACKAGE_SUFFIX, GIT_COMMIT are
# environment variables that are set by Jenkins. The last one
# corresponds to any labels set on a slave.  BUILD_THREADS &
# PARAVIEW_DIR should be set in the configuration of each slave.
###############################################################################
SCRIPT_DIR=$(dirname "$0")

###############################################################################
# System discovery
###############################################################################
if [[ ${NODE_LABELS} == *rhel6* ]]; then
  ON_RHEL6=true
fi
if [[ ${NODE_LABELS} == *rhel7* ]]; then
  ON_RHEL7=true
fi
if [[ ${NODE_LABELS} == *ubuntu* ]]; then
  ON_UBUNTU=true
fi
if [[ ${NODE_LABELS} == *osx* ]]; then
  ON_MACOS=true
fi
###############################################################################
# All nodes currently have PARAVIEW_DIR=5.2.0 and PARAVIEW_NEXT_DIR=5.3.0-RC1
###############################################################################
export PARAVIEW_DIR=${PARAVIEW_NEXT_DIR}
###############################################################################
# Print out the versions of things we are using
###############################################################################
cmake --version

###############################################################################
# Check job requirements from the name and changes
###############################################################################
if [[ ${JOB_NAME} == *clean* ]]; then
  CLEANBUILD=true
fi

if [[ ${JOB_NAME} == *pull_requests* ]]; then
  PRBUILD=true
fi

if [[ ${JOB_NAME} == *debug* ]]; then
  BUILD_CONFIG="Debug"
elif [[ ${JOB_NAME} == *relwithdbg* ]]; then
  BUILD_CONFIG="RelWithDbg"
else
  BUILD_CONFIG="Release"
fi

# For pull requests decide on what to build based on changeset and Jenkins
# parameters.
BUILDPKG=true
SYSTEMTESTS=false
if [[ ${PRBUILD} == true ]]; then
  if [[ -n ${BUILD_PACKAGE} ]]; then
    BUILDPKG=${BUILD_PACKAGE}
  fi
  if [[ ${ON_RHEL7} == true ]]; then
    if ${SCRIPT_DIR}/check_for_changes docs-gui-only; then
      SYSTEMTESTS=false
    else
      BUILDPKG=true
      SYSTEMTESTS=true
    fi
  fi
###############################################################################
# Setup the build directory
# For a clean build the entire thing is removed to guarantee it is clean. All
# other build types are assumed to be incremental and the following items
# are removed to ensure stale build objects don't interfere with each other:
#   - build/bin/**: if libraries are removed from cmake they are not deleted
#                   from bin and can cause random failures
#   - build/ExternalData/**: data files will change over time and removing
#                            the links helps keep it fresh
###############################################################################
BUILD_DIR=$WORKSPACE/build
if [ -z "$BUILD_DIR" ]; then
  echo "Build directory not set. Cannot continue"
  exit 1
fi

if [[ "$CLEANBUILD" == true ]]; then
  rm -rf $BUILD_DIR
fi
if [ -d $BUILD_DIR ]; then
  rm -rf $BUILD_DIR/bin $BUILD_DIR/ExternalData
  if [[ -n ${CLEAN_EXTERNAL_PROJECTS} && "${CLEAN_EXTERNAL_PROJECTS}" == true ]]; then
      rm -rf $BUILD_DIR/eigen-*
Pete Peterson's avatar
Pete Peterson committed
      rm -rf $BUILD_DIR/googletest-*
      rm -fr $BUILD_DIR/python-xmlrunner-*
###############################################################################
# Setup clang
###############################################################################
if [[ ${JOB_NAME} == *clang* ]]; then
  USE_CLANG=true
elif [[ ${ON_MACOS} == true ]] ; then
  if [[ ! $(command -v icpc) ]] ; then
    USE_CLANG=true
  fi
if [[ $USE_CLANG ]]; then
  # Assuming we are using the clang compiler
  echo "Using clang/llvm compiler."
  clang --version
  export CC=clang
  export CXX=clang++
  # check if this is also a clang-tidy build
  if [[ ${JOB_NAME} == *clang_tidy* ]]; then
      CLANGTIDYVAR="-DENABLE_CLANG_TIDY=ON"
  fi
  #check if CMakeCache.txt exists and if so that the cxx compiler is clang++
  #only needed with incremental builds. Clean builds delete this directory in a later step.
  if [[ -e $BUILD_DIR/CMakeCache.txt ]] && [[ ${JOB_NAME} != *clean* ]]; then
    COMPILERFILEPATH=`grep 'CMAKE_CXX_COMPILER:FILEPATH' $BUILD_DIR/CMakeCache.txt`
    if [[ $COMPILERFILEPATH != *clang++* ]]; then
      # Removing the build directory entirely guarantees clang is used.
#for openmp support on OS X run
# `brew install llvm`
# `ln -s /usr/local/opt/llvm/lib/libomp.dylib /usr/local/lib/libomp.dylib`
if [[ ${ON_MACOS} == true ]] ; then
  if [[ ${JOB_NAME} == *openmp* ]]; then
    export CC=/usr/local/opt/llvm/bin/clang
    export CXX=/usr/local/opt/llvm/bin/clang++
  fi
fi

###############################################################################
# Set up the location for the local object store outside of the build and
# source tree, which can be shared by multiple builds.
# It defaults to a MantidExternalData directory within the HOME directory.
# It can be overridden by setting the MANTID_DATA_STORE environment variable.
###############################################################################
if [ -z "$MANTID_DATA_STORE" ]; then
  export MANTID_DATA_STORE=$HOME/MantidExternalData
###############################################################################
# RHEL6 setup steps - nodes must have a "rhel6" label set (in lowercase)
###############################################################################
DIST_FLAGS="-DSTATIC_LIBSTDCXX=OFF"
if [[ "${ON_RHEL6}" == true ]]; then
  SCL_ON_RHEL6="scl enable mantidlibs34 devtoolset-2"
###############################################################################
# Check if this is a Python 3 build and set CMake arguments.
###############################################################################
if [[ ${JOB_NAME} == *python3* ]]; then
  PYTHON3_EXECUTABLE=`which python3`
  DIST_FLAGS="${DIST_FLAGS} -DPYTHON_EXECUTABLE=$PYTHON3_EXECUTABLE"
  PARAVIEW_DIR="${PARAVIEW_DIR}-python3"
###############################################################################
# Packaging options
###############################################################################
if [[ "$BUILDPKG" == true ]]; then
  PACKAGINGVARS="-DPACKAGE_DOCS=ON"
  # Set some variables relating to the linux packages
  if [[ ${ON_MACOS} != true ]]; then
    # Use different suffix for linux builds
    if [[ ${JOB_NAME} == *release* ]]; then
      echo "Performing release build" # do nothing
    elif [[ ${JOB_NAME} == *master* ]]; then
      PACKAGE_SUFFIX="nightly"
    elif [[ ${JOB_NAME} == *pvnext* ]]; then
      PACKAGE_SUFFIX="mantidunstable-pvnext"
      PACKAGE_SUFFIX="unstable"
    fi

    # Add '-python3' to package name and install path
    if [[ ${JOB_NAME} == *python3* ]]; then
	PACKAGE_SUFFIX=${PACKAGE_SUFFIX}-python3
    fi

    # Only unsuffixed release builds create envvars scripts
    if [[ ${JOB_NAME} == *release* && -z "$PACKAGE_SUFFIX" ]]; then
      PACKAGINGVARS="${PACKAGINGVARS} -DENVVARS_ON_INSTALL=True -DCPACK_SET_DESTDIR=ON"
    else
      PACKAGINGVARS="${PACKAGINGVARS} -DENVVARS_ON_INSTALL=False -DCPACK_SET_DESTDIR=OFF"
    fi

    if [ ! -z "$PACKAGE_SUFFIX" ]; then
      PACKAGINGVARS="${PACKAGINGVARS} -DCMAKE_INSTALL_PREFIX=/opt/mantid${PACKAGE_SUFFIX} -DCPACK_PACKAGE_SUFFIX=${PACKAGE_SUFFIX}"
###############################################################################
# Generator
###############################################################################
if [ $(command -v ninja) ]; then
  CMAKE_GENERATOR="-G Ninja"
elif [ $(command -v ninja-build) ]; then
  CMAKE_GENERATOR="-G Ninja"
fi
if [ -e $BUILD_DIR/CMakeCache.txt ]; then
  CMAKE_GENERATOR=""
fi

###############################################################################
# Work in the build directory
###############################################################################

###############################################################################
# Clean up any artifacts from last build so that if it fails
# they don't get archived again
###############################################################################
rm -f *.dmg *.rpm *.deb *.tar.gz

###############################################################################
# CMake configuration
###############################################################################
$SCL_ON_RHEL6 "cmake ${CMAKE_GENERATOR} -DCMAKE_BUILD_TYPE=${BUILD_CONFIG} -DENABLE_CPACK=ON -DMAKE_VATES=ON -DParaView_DIR=${PARAVIEW_DIR} -DMANTID_DATA_STORE=${MANTID_DATA_STORE} -DDOCS_HTML=ON -DENABLE_CONDA=ON -DENABLE_FILE_LOGGING=OFF ${DIST_FLAGS} ${PACKAGINGVARS} ${CLANGTIDYVAR} .."
###############################################################################
# Coverity build should exit early
###############################################################################
if [[ ${JOB_NAME} == *coverity_build_and_submit* ]]; then
  ${COVERITY_DIR}/cov-build --dir cov-int cmake --build . -- -j ${BUILD_THREADS}
  tar czvf mantid.tgz cov-int
  status=$(curl --form token=$COVERITY_TOKEN --form email=mantidproject@gmail.com \
       --form file=@mantid.tgz --form version=$GIT_COMMIT \
       https://scan.coverity.com/builds?project=mantidproject%2Fmantid)
  status=$(echo ${status} | sed -e 's/^ *//' -e 's/ *$//')
  if [[ -z $status ]]; then
  else
    echo "$status"
    exit 1
###############################################################################
# Build step
###############################################################################
$SCL_ON_RHEL6 "cmake --build . -- -j$BUILD_THREADS"
$SCL_ON_RHEL6 "cmake --build . --target AllTests -- -j$BUILD_THREADS"
###############################################################################
# static analysis builds stop here
###############################################################################
if [[ $USE_CLANG ]] && [[ ${JOB_NAME} == *clang_tidy* ]]; then
  exit 0
fi


###############################################################################
###############################################################################
# Remove any Mantid.user.properties file
userprops=~/.mantid/Mantid.user.properties
rm -f $userprops
$SCL_ON_RHEL6 "ctest -j$BUILD_THREADS --schedule-random --output-on-failure"
###############################################################################
# Run the documentation tests on Ubuntu when doing a pull request build but not for python 3.
###############################################################################
if [[ ${ON_UBUNTU} == true ]] && [[ ${PRBUILD} == true ]]; then
  # Remove doctrees directory so it forces a full reparse. It seems that
  # without this newly added doctests are not executed
  if [ -d $BUILD_DIR/docs/doctrees ]; then
    rm -rf $BUILD_DIR/docs/doctrees/*
  fi
  $SCL_ON_RHEL6 "cmake --build . --target docs-test"
fi

###############################################################################
# Create the install kit if required. This includes building the Qt help
# documentation
###############################################################################
if [[ ${BUILDPKG} == true ]]; then
  # Workaround so that the target can find the properties file
  # CMake doesn't easily allow environment variables on custom targets
Martyn Gigg's avatar
Martyn Gigg committed
  if [[ ${ON_MACOS} == true ]]; then
  $SCL_ON_RHEL6 "cmake --build . --target docs-qthelp"
  $SCL_ON_RHEL6 "cpack"
  # Source tarball on clean build (arbitrarily choose rhel7)
  # Also, parcel up the documentation into a tar file that is easier to move around
  # and labelled by the commit id it was built with. This assumes the Jenkins git plugin
  # has set the GIT_COMMIT environment variable
  if [[ ${CLEANBUILD} == true && ${ON_RHEL7} == true ]]; then
    $SCL_ON_RHEL6 "cmake --build . --target docs-html"
    tar -cjf mantiddocs-g${GIT_COMMIT:0:7}.tar.bz2 --exclude='*.buildinfo' --exclude="MantidProject.q*" docs/html
    # The ..._PREFIX argument avoids opt/Mantid directories at the top of the tree
    $SCL_ON_RHEL6 "cpack --config CPackSourceConfig.cmake -D CPACK_PACKAGING_INSTALL_PREFIX="
  fi

###############################################################################
# Run the system tests if required. Run from a package to have at least one
# Linux checks it install okay
###############################################################################
if [[ ${SYSTEMTESTS} == true ]]; then
  if [[ ${PRBUILD} == true ]]; then
    EXTRA_ARGS="--exclude-in-pull-requests" $SCRIPT_DIR/systemtests
  else
    $SCRIPT_DIR/systemtests
  fi