xNombre | 232e9ca | 2020-07-03 22:10:22 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | set -e |
| 3 | |
| 4 | # ci_cmake.sh |
| 5 | # Continuously integrate libpng using CMake. |
| 6 | # |
| 7 | # Copyright (c) 2019-2020 Cosmin Truta. |
| 8 | # |
| 9 | # This software is released under the libpng license. |
| 10 | # For conditions of distribution and use, see the disclaimer |
| 11 | # and license in png.h. |
| 12 | |
| 13 | readonly CI_SCRIPTNAME="$(basename "$0")" |
| 14 | readonly CI_SCRIPTDIR="$(cd "$(dirname "$0")" && pwd)" |
| 15 | readonly CI_SRCDIR="$(dirname "$CI_SCRIPTDIR")" |
| 16 | readonly CI_BUILDDIR="$CI_SRCDIR/out/cmake.build" |
| 17 | readonly CI_INSTALLDIR="$CI_SRCDIR/out/cmake.install" |
| 18 | |
| 19 | function ci_info { |
| 20 | printf >&2 "%s: %s\\n" "$CI_SCRIPTNAME" "$*" |
| 21 | } |
| 22 | |
| 23 | function ci_err { |
| 24 | printf >&2 "%s: error: %s\\n" "$CI_SCRIPTNAME" "$*" |
| 25 | exit 2 |
| 26 | } |
| 27 | |
| 28 | function ci_spawn { |
| 29 | printf >&2 "%s: executing:" "$CI_SCRIPTNAME" |
| 30 | printf >&2 " %q" "$@" |
| 31 | printf >&2 "\\n" |
| 32 | "$@" |
| 33 | } |
| 34 | |
| 35 | function ci_init_cmake { |
| 36 | # Initialize the CI_ variables with default values, where applicable. |
| 37 | CI_CMAKE="${CI_CMAKE:-cmake}" |
| 38 | CI_CTEST="${CI_CTEST:-ctest}" |
| 39 | [[ $(uname -s || echo unknown) == Darwin ]] && CI_CC="${CI_CC:-clang}" |
| 40 | CI_CMAKE_BUILD_TYPE="${CI_CMAKE_BUILD_TYPE:-Release}" |
| 41 | # Print the CI_ variables. |
| 42 | ci_info "source directory: $CI_SRCDIR" |
| 43 | ci_info "build directory: $CI_BUILDDIR" |
| 44 | ci_info "install directory: $CI_INSTALLDIR" |
| 45 | ci_info "environment option: \$CI_CMAKE='$CI_CMAKE'" |
| 46 | ci_info "environment option: \$CI_CMAKE_GENERATOR='$CI_CMAKE_GENERATOR'" |
| 47 | ci_info "environment option: \$CI_CMAKE_GENERATOR_PLATFORM='$CI_CMAKE_GENERATOR_PLATFORM'" |
| 48 | ci_info "environment option: \$CI_CMAKE_BUILD_TYPE='$CI_CMAKE_BUILD_TYPE'" |
| 49 | ci_info "environment option: \$CI_CMAKE_BUILD_FLAGS='$CI_CMAKE_BUILD_FLAGS'" |
| 50 | ci_info "environment option: \$CI_CMAKE_VARS='$CI_CMAKE_VARS'" |
| 51 | ci_info "environment option: \$CI_CTEST='$CI_CTEST'" |
| 52 | ci_info "environment option: \$CI_CTEST_FLAGS='$CI_CTEST_FLAGS'" |
| 53 | ci_info "environment option: \$CI_CC='$CI_CC'" |
| 54 | ci_info "environment option: \$CI_CC_FLAGS='$CI_CC_FLAGS'" |
| 55 | ci_info "environment option: \$CI_SANITIZERS='$CI_SANITIZERS'" |
| 56 | ci_info "environment option: \$CI_NO_TEST='$CI_NO_TEST'" |
| 57 | ci_info "environment option: \$CI_NO_INSTALL='$CI_NO_INSTALL'" |
| 58 | ci_info "environment option: \$CI_NO_CLEAN='$CI_NO_CLEAN'" |
| 59 | # Print the CMake/CTest program versions. |
| 60 | ci_spawn "$(command -v "$CI_CMAKE")" --version |
| 61 | ci_spawn "$(command -v "$CI_CTEST")" --version |
| 62 | } |
| 63 | |
| 64 | function ci_build_cmake { |
| 65 | # Initialize ALL_CC_FLAGS as a string. |
| 66 | local ALL_CC_FLAGS="$CI_CC_FLAGS" |
| 67 | [[ $CI_SANITIZERS ]] && ALL_CC_FLAGS="-fsanitize=$CI_SANITIZERS $ALL_CC_FLAGS" |
| 68 | # Initialize ALL_CMAKE_VARS, ALL_CMAKE_BUILD_FLAGS and ALL_CTEST_FLAGS as arrays. |
| 69 | local -a ALL_CMAKE_VARS=() |
| 70 | [[ $CI_CC ]] && ALL_CMAKE_VARS+=(-DCMAKE_C_COMPILER="$CI_CC") |
| 71 | [[ $ALL_CC_FLAGS ]] && ALL_CMAKE_VARS+=(-DCMAKE_C_FLAGS="$ALL_CC_FLAGS") |
| 72 | ALL_CMAKE_VARS+=(-DCMAKE_BUILD_TYPE="$CI_CMAKE_BUILD_TYPE") |
| 73 | ALL_CMAKE_VARS+=(-DCMAKE_INSTALL_PREFIX="$CI_INSTALLDIR") |
| 74 | ALL_CMAKE_VARS+=(-DCMAKE_VERBOSE_MAKEFILE=ON) |
| 75 | [[ $CI_NO_TEST ]] && ALL_CMAKE_VARS+=(-DPNG_TESTS=OFF) |
| 76 | ALL_CMAKE_VARS+=($CI_CMAKE_VARS) |
| 77 | local -a ALL_CMAKE_BUILD_FLAGS=($CI_CMAKE_BUILD_FLAGS) |
| 78 | local -a ALL_CTEST_FLAGS=($CI_CTEST_FLAGS) |
| 79 | # Export the CMake build environment. |
| 80 | [[ $CI_CMAKE_GENERATOR ]] && |
| 81 | ci_spawn export CMAKE_GENERATOR="$CI_CMAKE_GENERATOR" |
| 82 | [[ $CI_CMAKE_GENERATOR_PLATFORM ]] && |
| 83 | ci_spawn export CMAKE_GENERATOR_PLATFORM="$CI_CMAKE_GENERATOR_PLATFORM" |
| 84 | # Build and install. |
| 85 | ci_spawn "$CI_CMAKE" -E remove_directory "$CI_BUILDDIR" |
| 86 | ci_spawn "$CI_CMAKE" -E remove_directory "$CI_INSTALLDIR" |
| 87 | ci_spawn "$CI_CMAKE" -E make_directory "$CI_BUILDDIR" |
| 88 | ci_spawn cd "$CI_BUILDDIR" |
| 89 | ci_spawn "$CI_CMAKE" "${ALL_CMAKE_VARS[@]}" "$CI_SRCDIR" |
| 90 | ci_spawn "$CI_CMAKE" --build . \ |
| 91 | --config "$CI_CMAKE_BUILD_TYPE" \ |
| 92 | "${ALL_CMAKE_BUILD_FLAGS[@]}" |
| 93 | [[ $CI_NO_TEST ]] || |
| 94 | ci_spawn "$CI_CTEST" --build-config "$CI_CMAKE_BUILD_TYPE" \ |
| 95 | "${ALL_CTEST_FLAGS[@]}" |
| 96 | [[ $CI_NO_INSTALL ]] || |
| 97 | ci_spawn "$CI_CMAKE" --build . \ |
| 98 | --config "$CI_CMAKE_BUILD_TYPE" \ |
| 99 | --target install \ |
| 100 | "${ALL_CMAKE_BUILD_FLAGS[@]}" |
| 101 | [[ $CI_NO_CLEAN ]] || |
| 102 | ci_spawn "$CI_CMAKE" --build . \ |
| 103 | --config "$CI_CMAKE_BUILD_TYPE" \ |
| 104 | --target clean \ |
| 105 | "${ALL_CMAKE_BUILD_FLAGS[@]}" |
| 106 | ci_info "success!" |
| 107 | } |
| 108 | |
| 109 | ci_init_cmake |
| 110 | [[ ! $* ]] || { |
| 111 | ci_info "note: this program accepts environment options only" |
| 112 | ci_err "unexpected command arguments: '$*'" |
| 113 | } |
| 114 | ci_build_cmake |