Skip to content
Snippets Groups Projects
build 9.3 KiB
Newer Older
TARGET=TNL
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
PREFIX=${HOME}/.local
ROOT_DIR="."
DCMTK_DIR="/usr/include/dcmtk"
BUILD=""
BUILD_JOBS=""
CMAKE="cmake"
CMAKE_ONLY="no"
HELP="no"
VERBOSE=""
OFFLINE_BUILD="no"
WITH_CLANG="no"
WITH_CUDA="yes"
WITH_CUDA_ARCH="auto"
WITH_TESTS="yes"
WITH_TOOLS="yes"
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
INSTANTIATE_LONG_INT="no"
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
INSTANTIATE_LONG_DOUBLE="no"
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
INSTANTIATE_FLOAT="no"
OPTIMIZED_VECTOR_HOST_OPERATIONS="no"

for option in "$@"
do
    case $option in
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
        --prefix=*                       ) PREFIX="${option#*=}" ;;
        --install=*                      ) INSTALL="${option#*=}" ;;
        --root-dir=*                     ) ROOT_DIR="${option#*=}" ;;
        --dcmtk-dir=*                    ) DCMTK_DIR="${option#*=}" ;;
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
        --build=*                        ) BUILD="${option#*=}" ;;
        --build-jobs=*                   ) BUILD_JOBS="${option#*=}" ;;
        --cmake=*                        ) CMAKE="${option#*=}" ;;
        --cmake-only=*                   ) CMAKE_ONLY="${option#*=}" ;;
        --verbose                        ) VERBOSE="VERBOSE=1" ;;
        --help                           ) HELP="yes" ;;
        --offline-build                  ) OFFLINE_BUILD="yes" ;;
        --with-clang=*                   ) WITH_CLANG="${option#*=}" ;;
        --with-mpi=*                     ) WITH_MPI="${option#*=}" ;;
        --with-mic=*                     ) WITH_MIC="${option#*=}" ;;
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
        --with-cuda=*                    ) WITH_CUDA="${option#*=}" ;;
        --with-cuda-arch=*               ) WITH_CUDA_ARCH="${option#*=}";;
        --with-openmp=*                  ) WITH_OPENMP="${option#*=}" ;;
        --with-gmp=*                     ) WITH_GMP="${option#*=}" ;;
        --with-tests=*                   ) WITH_TESTS="${option#*=}" ;;
        --with-coverage=*                ) WITH_COVERAGE="${option#*=}" ;;
        --with-examples=*                ) WITH_EXAMPLES="${option#*=}" ;;
        --with-tools=*                   ) WITH_TOOLS="${option#*=}" ;;
        --with-python=*                  ) WITH_PYTHON="${option#*=}" ;;
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
        --with-templates-instantiation=* ) WITH_TEMPLATE_INSTANTIATION="${option#*=}" ;;
        --instantiate-long-int=*         ) INSTANTIATE_LONG_INT="${option#*=}" ;;
        --instantiate-int=*              ) INSTANTIATE_INT="${option#*=}" ;;
        --instantiate-long-double=*      ) INSTANTIATE_LONG_DOUBLE="${option#*=}" ;;
        --instantiate-double=*           ) INSTANTIATE_DOUBLE="${option#*=}" ;;
        --instantiate-float=*            ) INSTANTIATE_FLOAT="${option#*=}" ;;
        --fast-build                     ) INSTANTIATE_LONG_INT="no"
                                           INSTANTIATE_INT="yes"
                                           INSTANTIATE_LONG_DOUBLE="no"
                                           INSTANTIATE_DOUBLE="yes"
                                           INSTANTIATE_FLOAT="no"
                                           WITH_CUDA_ARCH="auto" ;;
        --optimize-vector-host-operations=* ) OPTIMIZED_VECTOR_HOST_OPERATIONS="yes" ;;
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
        *                                ) 
           echo "Unknown option ${option}. Use --help for more information."
           exit 1 ;;
    esac
done

if [[ ${HELP} == "yes" ]]; then
    echo "TNL build options:"
    echo ""
    echo "   --build=Debug/Release                 Build type."
    echo "   --build-jobs=NUM                      Number of processes to be used for the build. It is set to the number of available CPU cores by default."
    echo "   --prefix=PATH                         Prefix for the installation directory. ${HOME}/local by default."
    echo "   --install=yes/no                      Enables the installation of TNL files."
    echo "   --offline-build=yes/no                Disables online updates during the build. 'no' by default."
    echo "   --with-mpi=yes/no                     Enables MPI. 'yes' by default (OpenMPI required)."
    echo "   --with-mic=yes/no                     Enables MIC (Intel Xeon Phi). 'no' by default (Intel Compiler required)."
    echo "   --with-cuda=yes/no                    Enables CUDA. 'yes' by default (CUDA Toolkit is required)."
    echo "   --with-cuda-arch=all/auto/30/35/...   Chooses CUDA architecture. 'auto' by default."
    echo "   --with-openmp=yes/no                  Enables OpenMP. 'yes' by default."
    echo "   --with-gmp=yes/no                     Enables the wrapper for GNU Multiple Precision Arithmetic Library. 'no' by default."
    echo "   --with-tests=yes/no                   Enables unit tests. 'yes' by default."
    echo "   --with-coverage=yes/no                Enables code coverage reports for unit tests. 'no' by default (lcov is required)."
    echo "   --with-examples=yes/no                Compile the 'examples' directory. 'yes' by default."
Jakub Klinkovský's avatar
Jakub Klinkovský committed
    echo "   --with-tools=yes/no                   Compile the 'src/Tools' directory. 'yes' by default."
    echo "   --with-python=yes/no                  Compile the Python bindings. 'yes' by default."
    echo "   --with-templates-instantiation=yes/no Precompiles some TNL templates during the build. 'no' by default."
    echo "   --cmake=CMAKE                         Path to cmake. 'cmake' by default."
    echo "   --verbose                             It enables verbose build."
    echo "   --root-dir=PATH                       Path to the TNL source code root dir."
    echo "   --dcmtk-dir=PATH                      Path to the DCMTK (Dicom Toolkit) root dir."
    echo "   --help                                Write this help."
    exit 1
fi

if [[ ${WITH_CLANG} == "yes" ]]; then
   export CXX=clang++
   export CC=clang
fi

if [[ ${WITH_MPI} == "yes" ]]; then
    if [[ ! -x  "$(command -v mpic++)" ]]; then
       echo "Warning:mpic++ is not installed on this system. MPI support is turned off." 
    else
       # instruct OpenMPI to use the original compiler
       # reference: https://www.open-mpi.org/faq/?category=mpi-apps#override-wrappers-after-v1.0
       # FIXME: this does not work with CUDA_HOST_COMPILER=mpic++
#       if [ -n "$CXX" ]; then
#          export OMPI_CXX="$CXX"
#       fi
       export CXX=mpic++
       export CUDA_HOST_COMPILER=mpic++
    fi
    if [[ ! -x  "$(command -v mpicc)" ]]; then
       echo "Warning: mpicc is not installed on this system." 
    else
       # instruct OpenMPI to use the original compiler
       # reference: https://www.open-mpi.org/faq/?category=mpi-apps#override-wrappers-after-v1.0
#       if [ -n "$CC" ]; then
#          export OMPI_CC="$CC"
#       fi
if hash ninja 2>/dev/null; then
   generator=Ninja
   make=ninja
   check_file="build.ninja"
else
   generator="Unix Makefiles"
   make=make
   check_file="Makefile"
fi
Jakub Klinkovský's avatar
Jakub Klinkovský committed
cmake_command=(
   ${CMAKE} ${ROOT_DIR}
         -G "${generator}"
         -DCMAKE_BUILD_TYPE=${BUILD}
         -DCMAKE_INSTALL_PREFIX=${PREFIX}
         -DOFFLINE_BUILD=${OFFLINE_BUILD}
         -DWITH_MIC=${WITH_MIC}
         -DWITH_CUDA=${WITH_CUDA}
         -DWITH_CUDA_ARCH=${WITH_CUDA_ARCH}
         -DWITH_OPENMP=${WITH_OPENMP}
         -DWITH_GMP=${WITH_GMP}
         -DWITH_TESTS=${WITH_TESTS}
         -DWITH_COVERAGE=${WITH_COVERAGE}
         -DWITH_EXAMPLES=${WITH_EXAMPLES}
         -DWITH_TOOLS=${WITH_TOOLS}
         -DWITH_PYTHON=${WITH_PYTHON}
         -DDCMTK_DIR=${DCMTK_DIR}
         -DWITH_TEMPLATE_INSTANTIATION=${WITH_TEMPLATE_INSTANTIATION}
         -DINSTANTIATE_FLOAT=${INSTANTIATE_FLOAT}
         -DINSTANTIATE_DOUBLE=${INSTANTIATE_DOUBLE}
         -DINSTANTIATE_LONG_DOUBLE=${INSTANTIATE_LONG_DOUBLE}
         -DINSTANTIATE_INT=${INSTANTIATE_INT}
         -DINSTANTIATE_LONG_INT=${INSTANTIATE_LONG_INT}
         -DOPTIMIZED_VECTOR_HOST_OPERATIONS=${OPTIMIZED_VECTOR_HOST_OPERATIONS}
)
# Skip running cmake if it was already run and the cmake command is the same.
# The build system (e.g. make) will call it automatically if necessary (e.g.
# when some CMakeLists.txt changes).
if [[ -f ".cmake_command" ]]; then
   last_cmake_command=$(cat ".cmake_command" 2>/dev/null)
else
   last_cmake_command=""
fi
Jakub Klinkovský's avatar
Jakub Klinkovský committed
if [[ ! -f "$check_file" ]] || [[ "$last_cmake_command" != "${cmake_command[@]}" ]]; then
   echo "Configuring ${BUILD} $TARGET ..."
Jakub Klinkovský's avatar
Jakub Klinkovský committed
   "${cmake_command[@]}"
   echo -n "${cmake_command[@]}" > ".cmake_command"
if [[ ${CMAKE_ONLY} == "yes" ]]; then
   exit 0
if [[ "$make" == "make" ]]; then
   if [[ -n ${BUILD_JOBS} ]]; then
      # override $MAKEFLAGS from parent environment
      export MAKEFLAGS=-j${BUILD_JOBS}
   elif [[ -z ${MAKEFLAGS} ]]; then
      # $BUILD_JOBS and $MAKEFLAGS are not set => set default value
      BUILD_JOBS=$(grep "core id" /proc/cpuinfo | sort -u | wc -l)
      export MAKEFLAGS=-j${BUILD_JOBS}
   fi
else
   if [[ -z ${BUILD_JOBS} ]]; then
      BUILD_JOBS=$(grep "core id" /proc/cpuinfo | sort -u | wc -l)
   fi
   make="$make -j$BUILD_JOBS"
   echo "Building ${BUILD} $TARGET using $BUILD_JOBS processors ..."
   # number of processors is unknown - it is encoded in $MAKEFLAGS from parent environment
   echo "Building ${BUILD} $TARGET ..."
if [[ "$INSTALL" == "yes" ]]; then
   # install implies all
   make_target="install"
else
   make_target="all"
fi

$make ${VERBOSE} $make_target
if [[ ${WITH_TESTS} == "yes" ]]; then
   CTEST_OUTPUT_ON_FAILURE=1 $make test