1#!/bin/sh 2# Copyright (c) 2017-2021 Petr Vorel <pvorel@suse.cz> 3# Script for CI builds. 4 5set -e 6 7CFLAGS="${CFLAGS:--Wformat -Werror=format-security -Werror=implicit-function-declaration -Werror=return-type -fno-common}" 8CC="${CC:-gcc}" 9 10DEFAULT_PREFIX="$HOME/ltp-install" 11DEFAULT_BUILD="native" 12DEFAULT_TREE="in" 13 14CONFIGURE_OPTS_IN_TREE="--with-open-posix-testsuite --with-realtime-testsuite $CONFIGURE_OPT_EXTRA" 15# TODO: open posix testsuite is currently broken in out-tree-build. Enable it once it's fixed. 16CONFIGURE_OPTS_OUT_TREE="--with-realtime-testsuite $CONFIGURE_OPT_EXTRA" 17 18SRC_DIR="$(cd $(dirname $0); pwd)" 19BUILD_DIR="$SRC_DIR/../ltp-build" 20 21MAKE_OPTS="-j$(getconf _NPROCESSORS_ONLN)" 22MAKE_OPTS_OUT_TREE="$MAKE_OPTS -C $BUILD_DIR -f $SRC_DIR/Makefile top_srcdir=$SRC_DIR top_builddir=$BUILD_DIR" 23 24run_configure() 25{ 26 local configure="$1" 27 shift 28 29 export CC CFLAGS LDFLAGS PKG_CONFIG_LIBDIR 30 echo "CC='$CC' CFLAGS='$CFLAGS' LDFLAGS='$LDFLAGS' PKG_CONFIG_LIBDIR='$PKG_CONFIG_LIBDIR'" 31 32 echo "=== configure $configure $@ ===" 33 if ! $configure $@; then 34 echo "== ERROR: configure failed, config.log ==" 35 cat config.log 36 exit 1 37 fi 38 39 echo "== include/config.h ==" 40 cat include/config.h 41} 42 43configure_in_tree() 44{ 45 run_configure ./configure $CONFIGURE_OPTS_IN_TREE --prefix=$prefix $@ 46} 47 48configure_out_tree() 49{ 50 mkdir -p $BUILD_DIR 51 cd $BUILD_DIR 52 run_configure $SRC_DIR/configure $CONFIGURE_OPTS_OUT_TREE $@ 53} 54 55configure_32() 56{ 57 local tree="$1" 58 local prefix="$2" 59 local arch="$(uname -m)" 60 local dir 61 62 echo "===== 32-bit ${tree}-tree build into $prefix =====" 63 64 if [ -z "$PKG_CONFIG_LIBDIR" ]; then 65 if [ "$arch" != "x86_64" ]; then 66 echo "ERROR: auto-detection not supported platform $arch, export PKG_CONFIG_LIBDIR!" 67 exit 1 68 fi 69 70 for dir in /usr/lib/i386-linux-gnu/pkgconfig \ 71 /usr/lib32/pkgconfig /usr/lib/pkgconfig; do 72 if [ -d "$dir" ]; then 73 PKG_CONFIG_LIBDIR="$dir" 74 break 75 fi 76 done 77 if [ -z "$PKG_CONFIG_LIBDIR" ]; then 78 echo "WARNING: PKG_CONFIG_LIBDIR not found, build might fail" 79 fi 80 fi 81 82 CFLAGS="-m32 $CFLAGS" LDFLAGS="-m32 $LDFLAGS" 83 84 eval configure_${tree}_tree 85} 86 87configure_native() 88{ 89 local tree="$1" 90 local prefix="$2" 91 92 echo "===== native ${tree}-tree build into $prefix =====" 93 eval configure_${tree}_tree 94} 95 96configure_cross() 97{ 98 local tree="$1" 99 local prefix="$2" 100 local host=$(basename "${CC%-gcc}") 101 102 if [ "$host" = "gcc" ]; then 103 echo "Invalid CC variable for cross compilation: $CC (clang not supported)" >&2 104 exit 1 105 fi 106 107 echo "===== cross-compile ${host} ${1}-tree build into $prefix =====" 108 eval configure_${tree}_tree "--host=$host" 109} 110 111build_in_tree() 112{ 113 make $MAKE_OPTS 114} 115 116build_out_tree() 117{ 118 cd $BUILD_DIR 119 make $MAKE_OPTS_OUT_TREE 120} 121 122test_in_tree() 123{ 124 make $1 125} 126 127test_out_tree() 128{ 129 cd $BUILD_DIR 130 make $MAKE_OPTS_OUT_TREE $1 131} 132 133install_in_tree() 134{ 135 make $MAKE_OPTS install 136} 137 138install_out_tree() 139{ 140 cd $BUILD_DIR 141 make $MAKE_OPTS_OUT_TREE DESTDIR="$prefix" SKIP_IDCHECK=1 install 142} 143 144usage() 145{ 146 cat << EOF 147Usage: 148$0 [ -c CC ] [ -i ] [ -o TREE ] [ -p DIR ] [-r RUN ] [ -t TYPE ] 149$0 -h 150 151Options: 152-h Print this help 153-c CC Define compiler (\$CC variable), needed only for configure step 154-i Run 'make install', needed only for install step 155-o TREE Specify build tree, default: $DEFAULT_TREE 156-p DIR Change installation directory. For in-tree build is this value passed 157 to --prefix option of configure script. For out-of-tree build is this 158 value passed to DESTDIR variable (i.e. sysroot) of make install 159 target, which means that LTP will be actually installed into 160 DIR/PREFIX (i.e. DIR/opt/ltp). 161 Default for in-tree build: '$DEFAULT_PREFIX' 162 Default for out-of-tree build: '$DEFAULT_PREFIX/opt/ltp' 163-r RUN Run only certain step (usable for CI), default: all 164-t TYPE Specify build type, default: $DEFAULT_BUILD, only for configure step 165 166TREE: 167in in-tree build 168out out-of-tree build 169 170TYPES: 17132 32-bit build (PKG_CONFIG_LIBDIR auto-detection for x86_64) 172cross cross-compile build (requires set compiler via -c switch) 173native native build 174 175RUN: 176autotools run only 'make autotools' 177configure run only 'configure' 178build run only 'make' 179test run only 'make test' (not supported for cross-compile build) 180test-c run only 'make test-c' (not supported for cross-compile build) 181test-shell run only 'make test-shell' (not supported for cross-compile build) 182install run only 'make install' 183 184Default configure options: 185in-tree: $CONFIGURE_OPTS_IN_TREE 186out-of-tree $CONFIGURE_OPTS_OUT_TREE 187 188configure options can extend the default with \$CONFIGURE_OPT_EXTRA environment variable 189EOF 190} 191 192prefix="$DEFAULT_PREFIX" 193build="$DEFAULT_BUILD" 194tree="$DEFAULT_TREE" 195install= 196run= 197 198while getopts "c:hio:p:r:t:" opt; do 199 case "$opt" in 200 c) CC="$OPTARG";; 201 h) usage; exit 0;; 202 i) install=1;; 203 o) case "$OPTARG" in 204 in|out) tree="$OPTARG";; 205 *) echo "Wrong build tree '$OPTARG'" >&2; usage; exit 1;; 206 esac;; 207 p) prefix="$OPTARG";; 208 r) case "$OPTARG" in 209 autotools|configure|build|test|test-c|test-shell|install) run="$OPTARG";; 210 *) echo "Wrong run type '$OPTARG'" >&2; usage; exit 1;; 211 esac;; 212 t) case "$OPTARG" in 213 32|cross|native) build="$OPTARG";; 214 *) echo "Wrong build type '$OPTARG'" >&2; usage; exit 1;; 215 esac;; 216 ?) usage; exit 1;; 217 esac 218done 219 220cd $SRC_DIR 221 222if [ -z "$run" -o "$run" = "autotools" ]; then 223 make autotools 224fi 225 226if [ -z "$run" -o "$run" = "configure" ]; then 227 eval configure_$build $tree $prefix 228fi 229 230if [ -z "$run" -o "$run" = "build" ]; then 231 echo "=== build ===" 232 eval build_${tree}_tree 233fi 234 235if [ -z "$run" -o "$run" = "test" -o "$run" = "test-c" -o "$run" = "test-shell" ]; then 236 if [ "$build" = "cross" ]; then 237 echo "cross-compile build, skipping running tests" >&2 238 else 239 eval test_${tree}_tree $run 240 fi 241fi 242 243if [ -z "$run" -o "$run" = "install" ]; then 244 if [ "$install" = 1 ]; then 245 eval install_${tree}_tree 246 else 247 echo "make install skipped, use -i to run it" 248 fi 249fi 250 251exit $? 252