File indexing completed on 2024-07-21 04:35:18

0001 #!/bin/bash
0002 
0003 # SPDX-FileCopyrightText: 2015-2016 Collabora Ltd.
0004 # SPDX-FileCopyrightText: 2020-2024 Ralf Habacker ralf.habacker @freenet.de
0005 #
0006 # SPDX-License-Identifier: MIT
0007 
0008 set -euo pipefail
0009 set -x
0010 
0011 # kill kde and x session
0012 function cleanup() {
0013     touch $builddir/finished
0014     if test "$ci_in_docker" = yes; then
0015         if test "$ci_host" = native; then
0016             ${start_kde_session}_shutdown
0017             kill -s 9 $DBUS_SESSION_BUS_PID
0018         else
0019             $wrapper $start_kde_session --terminate
0020         fi
0021         killall -s 9 xvfb-run
0022         sleep 1
0023         killall -s 9 Xvfb
0024     fi
0025 }
0026 
0027 # start xvfb session - will restart in case of crashes
0028 function start_x_session() {
0029     rm -f $builddir/finished
0030     (
0031         while ! test -f $builddir/finished; do
0032             xvfb-run -s "+extension GLX +render" -a -n 99 openbox 2>&1 >/dev/null
0033         done
0034     ) &
0035     export DISPLAY=:99
0036     sleep 2
0037 }
0038 
0039 # start dbus-daemon and kde background processes
0040 function start_kde_session() {
0041     if test "$ci_host" = native; then
0042         # start new dbus session, which is required by kio
0043         # and identified by $DBUS_SESSION_BUS_PID
0044         eval `dbus-launch --sh-syntax`
0045     fi
0046     $wrapper $start_kde_session --verbose
0047 }
0048 
0049 function start_session() {
0050     if test "$ci_variant" = kf5 && test -v dep_prefix; then
0051         # setup qt5.conf
0052         qtconf="$dep_prefix/bin/qt5.conf"
0053         sed "s,Prefix.*$,Prefix=$dep_prefix,g" "$dep_prefix/bin/qt5.conf" > "$builddir/bin/qt5.conf"
0054     fi
0055 
0056     if test "$ci_in_docker" = yes; then
0057         start_x_session
0058         start_kde_session
0059     fi
0060 }
0061 
0062 # missing wrapper for associated rpm macro
0063 function cmake-kde4() {
0064     NAME="cmake_kde4"
0065     ${RPM_OPT_FLAGS:=}
0066     ${LDFLAGS:=}
0067     ${icerun:=}
0068     eval "`rpm --eval "%${NAME} $(printf " %q" "${@}")"`"
0069 }
0070 
0071 # missing wrapper for associated rpm macro
0072 function cmake-kf5() {
0073     NAME="cmake_kf5"
0074     ${RPM_OPT_FLAGS:=}
0075     ${LDFLAGS:=}
0076     ${icerun:=}
0077     eval "`rpm --eval "%${NAME} $(printf " %q" "${@}")"`"
0078 }
0079 
0080 ##
0081 ## initialize support to run cross compiled executables
0082 ##
0083 # syntax: init_wine <path1> [<path2> ... [<pathn>]]
0084 # @param  path1..n  pathes for adding to wine executable search path
0085 #
0086 # The function exits the shell script in case of errors
0087 #
0088 init_wine() {
0089     if ! command -v wineboot >/dev/null; then
0090         echo "wineboot not found"
0091         exit 1
0092     fi
0093 
0094     # run without X11 display to avoid that wineboot shows dialogs
0095     wineboot -fi
0096 
0097     # add local paths to wine user path
0098     local addpath="" d="" i
0099     for i in "$@"; do
0100         local wb=$(winepath -w "$i")
0101         addpath="$addpath$d$wb"
0102         d=";"
0103     done
0104 
0105     # create registry file from template
0106     local wineaddpath=$(echo "$addpath" | sed 's,\\,\\\\\\\\,g')
0107     r=$(realpath $0)
0108     r=$(dirname $r)
0109     sed "s,@PATH@,$wineaddpath,g" "$r/user-path.reg.in" > user-path.reg
0110 
0111     # add path to registry
0112     wine regedit /C user-path.reg
0113 
0114     # check if path(s) has been set and break if not
0115     local o=$(wine cmd /C "echo %PATH%")
0116     case "$o" in
0117         (*z:* | *Z:*)
0118             # OK
0119             ;;
0120         (*)
0121             echo "Failed to add Unix paths '$*' to path: Wine %PATH% = $o" >&2
0122             exit 1
0123             ;;
0124     esac
0125 }
0126 
0127 #
0128 # prepare running cross compile executables
0129 #
0130 # @param prefix   host prefix to build for e.g. i686-w64-mingw32
0131 # @param binpath  path for executables in build root
0132 #
0133 init_cross_runtime() {
0134     local _prefix="$1"
0135     local binpath="$2"
0136     # CFLAGS and CXXFLAGS does do work, checked with cmake 3.15
0137     export LDFLAGS="-${ci_runtime}-libgcc"
0138     # enable tests if supported
0139     if [ "$ci_test" = yes ]; then
0140         sysroot=$("${_prefix}-gcc" --print-sysroot)
0141         # check if the prefix is a subdir of sysroot (e.g. openSUSE)
0142         if [ -d "${sysroot}/${_prefix}" ]; then
0143             dep_prefix="${sysroot}/${_prefix}"
0144         else
0145             # fallback: assume the dependency libraries were built with --prefix=/${ci_host}
0146             dep_prefix="/${_prefix}"
0147         fi
0148         # choose correct wine architecture
0149         if [ "${_prefix%%-*}" = x86_64 ]; then
0150             export WINEARCH=win64
0151             export WINEPREFIX=${HOME}/.wine64
0152         else
0153             export WINEARCH=win32
0154             export WINEPREFIX=${HOME}/.wine32
0155         fi
0156         export WINEDEBUG=fixme-all
0157         # clean wine prefix
0158         rm -rf ${WINEPREFIX}
0159         libgcc_path=
0160         if [ "$ci_runtime" = "shared" ]; then
0161             libgcc_path=$(dirname "$("${_prefix}-gcc" -print-libgcc-file-name)")
0162         fi
0163 
0164         init_wine "${dep_prefix}/bin" "$2" ${libgcc_path:+"$libgcc_path"}
0165     fi
0166 }
0167 
0168 # ci_build:
0169 # used for debugging
0170 : "${ci_build:=yes}"
0171 
0172 # ci_clean:
0173 # used for debugging
0174 : "${ci_clean:=yes}"
0175 
0176 # ci_host:
0177 # See ci-install.sh
0178 : "${ci_host:=native}"
0179 
0180 # ci_in_docker:
0181 # flags to indicate that we are running in docker
0182 : "${ci_in_docker:=auto}"
0183 
0184 # ci_jobs:
0185 # number of jobs
0186 : "${ci_jobs:=5}"
0187 
0188 # ci_runtime:
0189 # One of static, shared; used for windows cross builds
0190 : "${ci_runtime:=shared}"
0191 
0192 # ci_test:
0193 # If yes, run tests; if no, just build
0194 : "${ci_test:=yes}"
0195 
0196 # ci_variant:
0197 # One of kf5, kf4
0198 : "${ci_variant:=kf5}"
0199 
0200 # specify build dir
0201 srcdir="$(pwd)"
0202 builddir=${srcdir}/ci-build-${ci_variant}-${ci_host}
0203 
0204 # check and setup if running in docker
0205 if [ $ci_in_docker = auto ]; then
0206     if [ -f /.dockerenv ]; then
0207         ci_in_docker=yes
0208     else
0209         ci_in_docker=no
0210     fi
0211 fi
0212 
0213 # enable sudo if running in docker
0214 sudo=
0215 if [ -f /.dockerenv ] && [ -n `getent passwd | grep ^user` ]; then
0216     sudo=sudo
0217 fi
0218 
0219 # common cmake options
0220 cmake_options="-DBUILD_WITH_QTNETWORK=1"
0221 
0222 # settings for build variants
0223 case "$ci_variant" in
0224     (kf5*)
0225         cmake_options+=" -DBUILD_APPLETS=0 -DBUILD_TESTING=1 -DENABLE_CLIENT_PACKAGE_TEST=1"
0226         cmake_suffix="kf5"
0227         export QT_LOGGING_RULES="*=true"
0228         export QT_FORCE_STDERR_LOGGING=1
0229         export QT_ASSUME_STDERR_HAS_CONSOLE=1
0230         start_kde_session=kdeinit5
0231         ;;
0232 
0233     (kf4)
0234         cmake_options+=" -DBUILD_QT4=1 -DBUILD_TESTING=1"
0235         cmake_suffix="kde4"
0236         start_kde_session=kdeinit4
0237         ;;
0238 esac
0239 
0240 # for building
0241 cmake=cmake
0242 
0243 # settings for platforms
0244 case "$ci_host" in
0245     (mingw32)
0246         cmake_options+=" -DQT_MOC_EXECUTABLE=/usr/i686-w64-mingw32/bin/moc"
0247         cmake_configure="$ci_host-cmake-$cmake_suffix"
0248         init_cross_runtime i686-w64-mingw32 $builddir/bin
0249         wrapper=/usr/bin/wine
0250         ;;
0251     (mingw64)
0252         cmake_options+=" -DAUTOMOC_EXECUTABLE=/usr/bin/x86_64-w64-mingw32-moc"
0253         cmake_configure="$ci_host-cmake-$cmake_suffix"
0254         init_cross_runtime x86_64-w64-mingw32 $builddir/bin
0255         wrapper=/usr/bin/wine
0256         ;;
0257     (*)
0258         cmake_options+=" -DCMAKE_CXX_FLAGS=-fPIC"
0259         cmake_configure="cmake-$cmake_suffix"
0260         export LD_LIBRARY_PATH=${builddir}/bin
0261         wrapper=
0262         ;;
0263 esac
0264 
0265 # custom settings
0266 case "$ci_variant" in
0267     (kf5)
0268         cmake_options+=" -DBUILD_WITH_WEBKIT=0 -DBUILD_WITH_WEBENGINE=0"
0269         ;;
0270     (kf5-webkit)
0271         cmake_options+=" -DBUILD_WITH_WEBKIT=1"
0272         ;;
0273     (kf5-webengine)
0274         cmake_options+=" -DBUILD_WITH_WEBENGINE=1"
0275         ;;
0276 esac
0277 
0278 # setup vars
0279 srcdir="$(pwd)"
0280 builddir=${srcdir}/ci-build-${ci_variant}-${ci_host}
0281 
0282 
0283 # create subdirs
0284 if test "$ci_clean" = yes; then
0285     rm -rf ${builddir}
0286     $sudo mkdir -p ${builddir}
0287     $sudo chmod a+wrx ${builddir}
0288 fi
0289 
0290 # configure and build
0291 if test "$ci_build" = yes; then
0292     $cmake_configure -- -S ${srcdir} -B ${builddir} $cmake_options
0293     $cmake --build ${builddir} -j$ci_jobs
0294 fi
0295 
0296 # run tests
0297 echo "checking for running tests = $ci_test"
0298 if test "$ci_test" = yes; then
0299     trap cleanup EXIT
0300 
0301     start_session
0302 
0303     # run tests
0304     ctest --test-dir ${builddir} --output-on-failure --timeout 60 --jobs $ci_jobs
0305 
0306     # show screenshot in case of errors
0307     if test $? -ne 0; then
0308         xwd -root -silent | convert xwd:- png:/tmp/screenshot.png
0309         cat /tmp/screenshot.png | uuencode screenshot
0310     fi
0311 fi
0312 
0313 # run install
0314 $cmake --build ${builddir} -t install DESTDIR=$PWD/tmp