1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
#!/bin/bash --noprofile
# (C) Copyright 2011- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.
set -eua
CMAKE_MIN_REQUIRED=3.11
CMAKE_BUILD_VERSION=3.18.3
usage()
{
echo "Usage: ecbuild [--help] [--version]"
exit $1
}
help()
{
cat <<EOF
USAGE:
ecbuild [--help] [--version]
ecbuild [option...] [--] [cmake-argument...] <path-to-source>
ecbuild [option...] [--] [cmake-argument...] <path-to-existing-build>
DESCRIPTION:
ecbuild is a build system based on CMake, but providing a lot of macro's
to make it easier to work with. Upon execution,
the equivalent cmake command is printed.
ecbuild/cmake must be called from an out-of-source build directory and
forbids in-source builds.
SYNOPSIS:
--help Display this help
--version Display ecbuild version
Available values for "option":
--cmakebin=<path>
Set which cmake binary to use. Default is 'cmake'
--prefix=<prefix>
Set the install path to <prefix>.
Equivalent to cmake argument "-DCMAKE_INSTALL_PREFIX=<prefix>"
--build=<build-type>
Set the build-type to <build-type>.
Equivalent to cmake argument "-DCMAKE_BUILD_TYPE=<build-type>"
<build-type> can be any of:
- debug : Lowest optimization level, useful for debugging
- release : Highest optimization level, for best performance
- bit : Highest optimization level while staying bit-reproducible
- ...others depending on project
--log=<log-level>
Set the ecbuild log-level
Equivalent to "-DECBUILD_LOG_LEVEL=<log-level>"
<log-level> can be any of:
- DEBUG
- INFO
- WARN
- ERROR
- CRITICAL
- OFF
Every choice outputs also the log-levels listed below itself
--static
Build static libraries.
Equivalent to "-DBUILD_SHARED_LIBS=OFF"
--dynamic, --shared
Build dynamic libraries (usually the default).
Equivalent to "-DBUILD_SHARED_LIBS=ON"
--config=<config>
Configuration file using CMake syntax that gets included
Equivalent to cmake argument "-DECBUILD_CONFIG=<config-file>"
--toolchain=<toolchain>
Use a platform specific toolchain, containing settings such
as compilation flags, locations of commonly used dependencies.
<toolchain> should be the path to a custom toolchain file.
Equivalent to cmake argument "-DCMAKE_TOOLCHAIN_FILE=<toolchain-file>"
--cache=<ecbuild-cache-file> (advanced)
A file called "ecbuild-cache.cmake" is generated during configuration.
This file can be moved to a safe location, and specified for future
builds to speed up checking of compiler/platform capabilities. Note
that this is only accelerating fresh builds, as cmake internally
caches also. Therefore this option is *not* recommended.
--get-cmake[=<prefix>]
Automatically download CMake binaries from version $CMAKE_BUILD_VERSION.
Requires an internet connection. If no prefix is given, install into
$PWD/.local/.
--build-cmake[=<prefix>]
Automatically download and build CMake version $CMAKE_BUILD_VERSION.
Requires an internet connection and may take a while. If no prefix
is given, install into $PWD/.local/.
--dryrun
Don't actually execute the cmake call, just print what would have
been executed.
Available values for "cmake-argument":
Any value that can be usually passed to cmake to (re)configure the build.
Typically these values start with "-D".
example: -DENABLE_TESTS=ON -DENABLE_MPI=OFF -DECKIT_PATH=...
They can be explicitly separated from [option...] with a "--", for the case
there is a conflicting option with the "cmake" executable, and the latter's
option is requested.
------------------------------------------------------------------------
NOTE: When reconfiguring a build, it is only necessary to change the relevant
options, as everything stays cached. For example:
> ecbuild --prefix=PREFIX .
> ecbuild -DENABLE_TESTS=ON .
------------------------------------------------------------------------
Compiling:
To compile the project with <N> threads:
> make -j<N>
To get verbose compilation/linking output:
> make VERBOSE=1
Testing:
To run the project's tests
> ctest
Also check the ctest manual/help for more options on running tests
Installing:
To install the project in location PREFIX with
"--prefix=PREFIX" or
"-DCMAKE_INSTALL_PREFIX=PREFIX"
> make install
------------------------------------------------------------------------
ECMWF"
EOF
exit $1
}
INSTALL_DIR="$( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd -P )"
export ecbuild_ROOT="$( cd "$INSTALL_DIR/.." && pwd -P )"
export ecbuild_DIR=$ecbuild_ROOT # for versions of CMake < 3.12
ECBUILD_MODULE_PATH=""
# If there is a directory share/ecbuild/cmake relative to the parent directory
# (as in an install tree), add it to CMAKE_MODULE_PATH
if [ -d $INSTALL_DIR/../share/ecbuild/cmake ]; then
ECBUILD_MODULE_PATH="$( cd "$INSTALL_DIR/../share/ecbuild/cmake" && pwd -P )"
# If there is a cmake subdirectory relative to the script directory (as in a
# tarball), add it to CMAKE_MODULE_PATH
elif [ -d $INSTALL_DIR/../cmake ]; then
ECBUILD_MODULE_PATH="$( cd "$INSTALL_DIR/../cmake" && pwd -P )"
fi
# Fail if we couldn't find ecBuild modules
if [ ! -f "$ECBUILD_MODULE_PATH/ecbuild_system.cmake" ]; then
echo "FATAL: ecBuild modules could not be found in either $INSTALL_DIR/../share/ecbuild/cmake or $INSTALL_DIR/../cmake" >&2
exit 1
fi
ADD_ECBUILD_OPTIONS="-DCMAKE_MODULE_PATH=$ECBUILD_MODULE_PATH"
version()
{
ecbuild_version=$(cat ${ECBUILD_MODULE_PATH}/VERSION)
echo "ecbuild version ${ecbuild_version}"
command -v cmake >/dev/null 2>&1 || { exit 0; }
cmake --version | head -1
exit 0
}
log()
{
log_level=$(tr "[a-z]" "[A-Z]" <<< "$1")
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DECBUILD_LOG_LEVEL=${log_level}"
}
prefix()
{
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DCMAKE_INSTALL_PREFIX=${1/#\~\//$HOME/}"
}
config()
{
arg=${1/#\~\//$HOME/}
if [ -f $arg ]; then
config_file=$arg
config_file="$( cd $( dirname "${config_file}" ) && pwd -P )/$( basename ${config_file} )"
else
echo "Error:"
echo " Config file [$arg] is not found or is not a file."
exit 1
fi
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DECBUILD_CONFIG=${config_file}"
}
toolchain()
{
arg=${1/#\~\//$HOME/}
if [ -f $arg ]; then
toolchain_file=$arg
fi
if [ -z ${toolchain_file+x} ]; then
echo "Error toolchain $arg is not valid"
exit 1
else
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DCMAKE_TOOLCHAIN_FILE=${toolchain_file}"
fi
}
cache()
{
arg=$1
if [ -f $arg ]; then
cache_file=$arg
cache_file="$( cd $( dirname "${cache_file}" ) && pwd -P )/$( basename ${cache_file} )"
else
echo "Error:"
echo " Cache file [$arg] is not found or is not a file."
exit 1
fi
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DECBUILD_CACHE=${cache_file}"
}
if test $# -eq 0; then
usage 1
fi
while test $# -gt 0; do
# Split --option=value in $opt="--option" and $val="value"
opt=""
val=""
case "$1" in
--*=*)
opt=`echo "$1" | sed 's/=.*//'`
val=`echo "$1" | sed 's/--[_a-zA-Z0-9-]*=//'`
;;
--*)
opt=$1
;;
# -D*)
# ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS $1"
# ;;
*)
break
;;
esac
# echo "debug opt: $opt $val"
# Parse options
case "$opt" in
--help)
help 0
;;
--version)
version
;;
--dryrun)
dryrun="yes"
;;
--cmakebin)
cmakebin="$val"
;;
--prefix)
prefix "$val"
;;
--build)
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DCMAKE_BUILD_TYPE=$val"
;;
--log)
log $val
;;
--static)
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DBUILD_SHARED_LIBS=OFF"
;;
--dynamic)
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DBUILD_SHARED_LIBS=ON"
;;
--shared)
ADD_ECBUILD_OPTIONS="$ADD_ECBUILD_OPTIONS -DBUILD_SHARED_LIBS=ON"
;;
--toolchain)
toolchain $val
;;
--config)
config $val
;;
--cache)
cache $val
;;
--get-cmake)
get_cmake="bin"
if [[ -n $val ]]; then
cmake_prefix="$val"
fi
;;
--build-cmake)
get_cmake="src"
if [[ -n $val ]]; then
cmake_prefix="$val"
fi
;;
--)
shift
break
;;
*)
echo "unknown option: $opt"
usage 1
;;
esac
shift
done
# If no arguments remain, set srcARG to "."
if [ $# -eq 0 ]; then
srcARG="."
fi
if [ -z ${toolchain_file+x} ]; then
if [ -z ${ECBUILD_TOOLCHAIN+x} ]; then :
else
toolchain ${ECBUILD_TOOLCHAIN}
echo "ecbuild toolchain set using environment variable ECBUILD_TOOLCHAIN"
fi
fi
src=${srcARG:=""}
cmake=${cmakebin:=cmake}
dryrun=${dryrun:=no}
get_cmake=${get_cmake:=""}
cmake_prefix=${cmake_prefix:=$PWD/.local}
cmake_found=""
cmake_version_sufficient=""
# Check that version $1 satisfies $2
# CMake versions have no more than 4 fields
# Version sort (sort -V) is not available on all platforms
version_gte() {
[ "$2" = "$(echo -e "$1\n$2" | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -k 4,4 -g | head -n1)" ]
}
# Download a CMake tarball
# $1: version
# $2: suffix (optional)
download_cmake() {
tarball=cmake-$1${2:-""}.tar.gz
if [[ ! -r $tarball ]]; then
shortver=$(echo $1 | cut -d. -f1-2)
url=http://www.cmake.org/files/v$shortver/$tarball
# -N Download only if the remote version of the file is newer
# --continue Continue an interrupted download
# -T 60 Time out a download attempt after 60 seconds
# -t 3 Only make 3 download attempts
wget -N --continue -T 60 -t 3 $url || {
echo "Failed to download CMake release $1." >&2
echo "Please download from $url" >&2
echo "and place $tarball in $PWD" >&2
exit 1
}
fi
echo $tarball
}
# Use already built CMake if any
if [[ -x "${cmake_prefix}/bin/cmake" ]]; then
echo "Using already built CMake in ${cmake_prefix}/bin/cmake" >&2
cmake="${cmake_prefix}/bin/cmake"
# Get a CMake binary if requested and no sufficient version found
elif [[ $get_cmake = "bin" ]]; then
plat=$(uname -s)
arch=$(uname -m)
if [[ "${plat}" != "Linux" ]] && [[ "${plat}" != "Darwin" ]] ; then
echo "Cannot download CMake binaries for this platform." >&2
echo "Please use --build-cmake to build from source." >&2
exit 1
fi
if [[ "${arch}" != "x86_64" ]] ; then
echo "Cannot download CMake binaries for this architecture." >&2
echo "Please use --build-cmake to build from source." >&2
exit 1
fi
echo "Downloading CMake version ${CMAKE_BUILD_VERSION} binaries and installing into ${cmake_prefix} ..." >&2
tarball=$(download_cmake "${CMAKE_BUILD_VERSION}" "-${plat}-${arch}")
mkdir -p "${cmake_prefix}"
tar xzf $tarball -C "${cmake_prefix}" --strip-components=1
cmake="${cmake_prefix}/bin/cmake"
# Build CMake from source if requested and no sufficient version found
elif [[ $get_cmake = "src" ]]; then
echo "Building CMake version ${CMAKE_BUILD_VERSION} and installing into ${cmake_prefix} ..." >&2
tarball=$(download_cmake "${CMAKE_BUILD_VERSION}")
tar xzf $tarball
(
mkdir -p build_cmake
cd build_cmake
../cmake-${CMAKE_BUILD_VERSION}/bootstrap --prefix="${cmake_prefix}" && make && make install
)
cmake="${cmake_prefix}/bin/cmake"
fi
# Check if the cmake version is sufficient
if $(command -v $cmake >/dev/null 2>&1); then
cmake_found="yes"
cmake_version=$($cmake --version | head -n1 | awk '{ print $3 }')
echo "Found CMake version $cmake_version" >& 2
if version_gte $cmake_version $CMAKE_MIN_REQUIRED; then
cmake_version_sufficient="yes"
fi
fi
# Fail if we don't have a sufficient CMake
if [[ ! $cmake_version_sufficient ]]; then
if [[ ! $cmake_found ]]; then
echo "CMake is required and cannot be found in the PATH." >&2
else
echo "CMake version $CMAKE_MIN_REQUIRED is required but only $cmake_version was found." >&2
fi
echo "" >&2
echo " Try 'module load cmake', specify a CMake binary with --cmakebin=/path/to/cmake" >&2
echo " or let ecbuild download and build CMake with the --build-cmake option." >&2
exit 1
fi
echo ""
echo "$cmake ${ADD_ECBUILD_OPTIONS} $@ $src"
echo ""
if [ ${dryrun} == "yes" ]; then
echo "[DRYRUN] -- not executing"
exit 0
fi
$cmake ${ADD_ECBUILD_OPTIONS} "$@" $src
|