build 8.32 KB
Newer Older
1 2
#!/bin/bash

3 4
set -e

5
TARGET=TNL
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
6
PREFIX=${HOME}/.local
7
INSTALL="no"
8 9 10 11 12 13 14 15
ROOT_DIR="."
DCMTK_DIR="/usr/include/dcmtk"
BUILD=""
BUILD_JOBS=""
CMAKE="cmake"
CMAKE_ONLY="no"
HELP="no"
VERBOSE=""
16
OFFLINE_BUILD="no"
17

18
WITH_CLANG="no"
19
WITH_MPI="yes"
20
WITH_CUDA="yes"
21
WITH_CUDA_ARCH="auto"
22
WITH_OPENMP="yes"
23
WITH_GMP="no"
24
WITH_TESTS="yes"
25
RUN_TESTS="yes"   # whether to run tests if they were compiled (coverage script sets it to no)
26
TESTS_JOBS="4"
27
WITH_PROFILING="no"
28
WITH_COVERAGE="no"
29
WITH_DOC="yes"
30
WITH_EXAMPLES="yes"
31
WITH_PYTHON="yes"
32
WITH_TOOLS="yes"
33
WITH_BENCHMARKS="yes"
34

35 36 37
for option in "$@"
do
    case $option in
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
38
        --prefix=*                       ) PREFIX="${option#*=}" ;;
39
        --install=*                      ) INSTALL="${option#*=}" ;;
40 41
        --root-dir=*                     ) ROOT_DIR="${option#*=}" ;;
        --dcmtk-dir=*                    ) DCMTK_DIR="${option#*=}" ;;
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
42
        --build=*                        ) BUILD="${option#*=}" ;;
43 44 45 46 47
        --build-jobs=*                   ) BUILD_JOBS="${option#*=}" ;;
        --cmake=*                        ) CMAKE="${option#*=}" ;;
        --cmake-only=*                   ) CMAKE_ONLY="${option#*=}" ;;
        --verbose                        ) VERBOSE="VERBOSE=1" ;;
        --help                           ) HELP="yes" ;;
48
        --offline-build                  ) OFFLINE_BUILD="yes" ;;
49
        --with-clang=*                   ) WITH_CLANG="${option#*=}" ;;
50
        --with-mpi=*                     ) WITH_MPI="${option#*=}" ;;
Tomáš Oberhuber's avatar
Tomáš Oberhuber committed
51 52
        --with-cuda=*                    ) WITH_CUDA="${option#*=}" ;;
        --with-cuda-arch=*               ) WITH_CUDA_ARCH="${option#*=}";;
53
        --with-openmp=*                  ) WITH_OPENMP="${option#*=}" ;;
54
        --with-gmp=*                     ) WITH_GMP="${option#*=}" ;;
55
        --with-tests=*                   ) WITH_TESTS="${option#*=}" ;;
56
        --run-tests=*                    ) RUN_TESTS="${option#*=}" ;;
57
        --tests-jobs=*                   ) TESTS_JOBS="${option#*=}" ;;
58
        --with-profiling=*               ) WITH_PROFILING="${option#*=}" ;;
59
        --with-coverage=*                ) WITH_COVERAGE="${option#*=}" ;;
60
        --with-doc=*                     ) WITH_DOC="${option#*=}" ;;
61
        --with-examples=*                ) WITH_EXAMPLES="${option#*=}" ;;
62
        --with-tools=*                   ) WITH_TOOLS="${option#*=}" ;;
63
        --with-benchmarks=*              ) WITH_BENCHMARKS="${option#*=}" ;;
64
        --with-python=*                  ) WITH_PYTHON="${option#*=}" ;;
65
        *                                )
66 67 68 69 70
           echo "Unknown option ${option}. Use --help for more information."
           exit 1 ;;
    esac
done

71
if [[ ${HELP} == "yes" ]]; then
72 73 74
    echo "TNL build options:"
    echo ""
    echo "   --build=Debug/Release                 Build type."
75
    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."
76 77
    echo "   --prefix=PATH                         Prefix for the installation directory. ${HOME}/local by default."
    echo "   --install=yes/no                      Enables the installation of TNL files."
78
    echo "   --offline-build=yes/no                Disables online updates during the build. 'no' by default."
79
    echo "   --with-mpi=yes/no                     Enables MPI. 'yes' by default (OpenMPI required)."
80
    echo "   --with-cuda=yes/no                    Enables CUDA. 'yes' by default (CUDA Toolkit is required)."
81
    echo "   --with-cuda-arch=all/auto/3.0/3.5/... Chooses CUDA architecture. 'auto' by default."
82
    echo "   --with-openmp=yes/no                  Enables OpenMP. 'yes' by default."
83
    echo "   --with-gmp=yes/no                     Enables the wrapper for GNU Multiple Precision Arithmetic Library. 'no' by default."
84 85
    echo "   --with-tests=yes/no                   Enables compilation of unit tests. 'yes' by default."
    echo "   --run-tests=yes/no                    Runs unit tests if they were compiled. 'yes' by default."
86
    echo "   --tests-jobs=NUM                      Number of processes to be used for the unit tests. It is 4 by default."
87
    echo "   --with-profiling=yes/no               Enables code profiling compiler falgs. 'no' by default."
88
    echo "   --with-coverage=yes/no                Enables code coverage reports for unit tests. 'no' by default (lcov is required)."
89
    echo "   --with-doc=yes/no                     Build documentation. 'yes' by default."
90
    echo "   --with-examples=yes/no                Compile the 'examples' directory. 'yes' by default."
Jakub Klinkovský's avatar
Jakub Klinkovský committed
91 92
    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."
93
    echo "   --with-benchmarks=yes/no              Compile the 'src/Benchmarks' directory. 'yes' by default."
94
    echo "   --cmake=CMAKE                         Path to cmake. 'cmake' by default."
95 96
    echo "   --verbose                             It enables verbose build."
    echo "   --root-dir=PATH                       Path to the TNL source code root dir."
97
    echo "   --dcmtk-dir=PATH                      Path to the DCMTK (Dicom Toolkit) root dir."
98 99 100 101
    echo "   --help                                Write this help."
    exit 1
fi

102
if [[ ${WITH_CLANG} == "yes" ]]; then
103 104
   export CXX=clang++
   export CC=clang
105
   export CUDA_HOST_COMPILER=clang++
106 107 108
else
   export CXX=g++
   export CC=gcc
109 110
fi

111 112 113 114 115 116
if [[ ! $(command -v cmake) ]]; then
   echo "Error: cmake is not installed. See http://www.cmake.org/download/" >&2
   exit 1
fi

if [[ $(command -v ninja) ]]; then
117 118 119 120 121 122 123 124
   generator=Ninja
   make=ninja
   check_file="build.ninja"
else
   generator="Unix Makefiles"
   make=make
   check_file="Makefile"
fi
125

Jakub Klinkovský's avatar
Jakub Klinkovský committed
126 127 128 129 130 131 132 133 134
cmake_command=(
   ${CMAKE} ${ROOT_DIR}
         -G "${generator}"
         -DCMAKE_BUILD_TYPE=${BUILD}
         -DCMAKE_INSTALL_PREFIX=${PREFIX}
         -DOFFLINE_BUILD=${OFFLINE_BUILD}
         -DWITH_CUDA=${WITH_CUDA}
         -DWITH_CUDA_ARCH=${WITH_CUDA_ARCH}
         -DWITH_OPENMP=${WITH_OPENMP}
135
         -DWITH_MPI=${WITH_MPI}
Jakub Klinkovský's avatar
Jakub Klinkovský committed
136 137
         -DWITH_GMP=${WITH_GMP}
         -DWITH_TESTS=${WITH_TESTS}
138
         -DWITH_PROFILING=${WITH_PROFILING}
Jakub Klinkovský's avatar
Jakub Klinkovský committed
139
         -DWITH_COVERAGE=${WITH_COVERAGE}
140
         -DWITH_DOC=${WITH_DOC}
Jakub Klinkovský's avatar
Jakub Klinkovský committed
141 142 143
         -DWITH_EXAMPLES=${WITH_EXAMPLES}
         -DWITH_TOOLS=${WITH_TOOLS}
         -DWITH_PYTHON=${WITH_PYTHON}
144
         -DWITH_BENCHMARKS=${WITH_BENCHMARKS}
Jakub Klinkovský's avatar
Jakub Klinkovský committed
145 146
         -DDCMTK_DIR=${DCMTK_DIR}
)
147

148 149 150 151 152 153 154 155
# 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
156
if [[ ! -f "$check_file" ]] || [[ "$last_cmake_command" != "${cmake_command[@]}" ]]; then
157
   echo "Configuring ${BUILD} $TARGET ..."
Jakub Klinkovský's avatar
Jakub Klinkovský committed
158 159
   "${cmake_command[@]}"
   echo -n "${cmake_command[@]}" > ".cmake_command"
160 161
fi

162 163
if [[ ${CMAKE_ONLY} == "yes" ]]; then
   exit 0
164 165
fi

166 167 168 169
# get the number of physical cores present on the system, even with multiple NUMA nodes
# see https://unix.stackexchange.com/a/279354
SYSTEM_CORES=$(lscpu --all --parse=CORE,SOCKET | grep -Ev "^#" | sort -u | wc -l)

170 171 172 173 174 175
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
176
      BUILD_JOBS=$SYSTEM_CORES
177 178 179 180
      export MAKEFLAGS=-j${BUILD_JOBS}
   fi
else
   if [[ -z ${BUILD_JOBS} ]]; then
181
      BUILD_JOBS=$SYSTEM_CORES
182 183
   fi
   make="$make -j$BUILD_JOBS"
184 185 186
fi

if [[ -n ${BUILD_JOBS} ]]; then
187
   echo "Building ${BUILD} $TARGET using $BUILD_JOBS processors ..."
188
else
189 190
   # number of processors is unknown - it is encoded in $MAKEFLAGS from parent environment
   echo "Building ${BUILD} $TARGET ..."
191
fi
192

193 194 195 196 197 198 199
if [[ "$INSTALL" == "yes" ]]; then
   # install implies all
   make_target="install"
else
   make_target="all"
fi

200 201 202 203 204
# make expects VERBOSE=1, ninja expects -v
if [[ "$make" != "make" ]] && [[ "$VERBOSE" ]]; then
   VERBOSE="-v"
fi

205
$make ${VERBOSE} $make_target
206

207 208 209 210
if [[ ${WITH_DOC} == "yes" ]]; then
   "$ROOT_DIR/Documentation/build" --prefix="$PREFIX"
fi

211
if [[ ${WITH_TESTS} == "yes" ]] && [[ ${RUN_TESTS} == "yes" ]]; then
212
   CTEST_PARALLEL_LEVEL=${TESTS_JOBS} CTEST_OUTPUT_ON_FAILURE=1 $make test
213
fi