1#!/usr/bin/env bash
2
3usage () {
4cat <<EOI
5Usage: $0 [ options... ]
6Options:
7    -a : autoconf+cscope
8    -c : CMake debug build
9    -d : dependencies check
10    -g : prefer GdkPixbuf rendering
11    -i : prefer Imlib2 rendering
12    -r : CMake release build
13    -t : test configure options
14    -j# : number of gmake procs
15    --prefix=... : install prefix
16    --with-xterm=... : set terminal
17EOI
18exit 0
19}
20
21unset ACONF DBGCM DEPEN RELCM TESTC GDK IM2 jobs prefix xterm
22prefix=/usr
23xterm=urxvt
24GDK=ON
25IM2=OFF
26
27if ! command -v gmake >/dev/null ; then
28    function gmake () {
29        make "$@"
30    }
31fi
32
33# parse argv
34for x
35do
36    case $x in
37        (-a) ACONF=1 ;;  # enable autoconf+cscope
38        (-c) DBGCM=1 ;;  # enable CMake debug build
39        (-d) DEPEN=1 ;;  # check the dependencies
40        (-r) RELCM=1 ;;  # enable CMake release build
41        (-t) TESTC=1 ;;  # test all combinations of configure options
42        (-j*) jobs=$x ;; # number of gmake procs
43        (-i) IM2=ON ; GDK=OFF ;; # enable Imlib2
44        (-g) GDK=ON ; IM2=OFF ;; # enable gdkpixbuf
45        (--prefix=*) prefix=${x#*=} ;; # install prefix
46        (--with-xterm=*) xterm=${x#*=} ;; # set terminal
47        (-h|--help|-?) usage ;;
48        (*) echo "$0: Unknown option '$x'." >&2 ; exit 1 ;;
49    esac
50done
51
52# set default
53[[ -v ACONF || -v DBGCM || -v DEPEN || -v RELCM || -v TESTC ]] || ACONF=1
54
55# gmake jobs
56[[ -v jobs ]] || jobs=-j$(($(nproc 2>/dev/null||echo 4)<<1))
57
58# check all POTFILES exist
59if [[ -f po/POTFILES.in ]]; then
60    fail=
61    for file in $(< po/POTFILES.in ); do
62        if [[ ! -f $file ]]; then
63            echo "Missing $file is still in po/POTFILES.in!" >&2
64            fail+="$file "
65        fi
66    done
67    if [[ -n $fail ]]; then
68        echo "Please update po/POTFILES.in for $fail!" >&2
69        set -x
70        exit 99
71    fi
72fi
73
74if [[ -v ACONF ]]; then
75
76    rm -f cscope.*
77    ./autogen.sh
78    ./configure.sh
79    gmake clean
80    gmake cscope
81    cscope -b
82    gmake "$jobs" clean all README
83
84fi
85
86if [[ -v DBGCM ]]; then
87
88    # cmake cheat sheet... with Debian-style configuration
89    mkdir -p builddir-debug || rm -rf builddir-debug/CMake*
90    cd builddir-debug &&
91    cmake .. \
92        -DCONFIG_GDK_PIXBUF_XLIB=$GDK \
93        -DCONFIG_IMLIB2=$IM2 \
94        -DCONFIG_LIBRSVG=ON \
95        -DCONFIG_XRANDR=ON \
96        -DCONFIG_XPM=ON \
97        -DCMAKE_CXX_COMPILER_ID=clang \
98        -DCMAKE_CXX_COMPILER=clang++ \
99        -DCMAKE_INSTALL_PREFIX="$prefix" \
100        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
101        -DICEHELPIDX="$prefix/share/doc/icewm-common/html/icewm.html" \
102        -DCFGDIR=/etc/X11/icewm \
103        -DVERSION=10.9.8-debug \
104        -DDOCDIR="$prefix/share/doc/icewm-common" \
105        -DCMAKE_VERBOSE_MAKEFILE=ON \
106        -DXTERMCMD="$xterm" &&
107    gmake "$jobs"
108fi
109
110if [[ -v RELCM ]]; then
111
112    # cmake release configuration
113    [[ -d build ]] && rm -rf build
114    mkdir -m 0700 build &&
115    cd build &&
116    cmake .. \
117        -DCMAKE_BUILD_TYPE=MinSizeRel \
118        -DCMAKE_INSTALL_PREFIX="$prefix" \
119        -DCONFIG_GDK_PIXBUF_XLIB=$GDK \
120        -DCONFIG_IMLIB2=$IM2 \
121        -DCONFIG_LIBRSVG=ON \
122        -DCONFIG_XPM=ON \
123        -DCMAKE_VERBOSE_MAKEFILE=ON \
124        -DCONFIG_XRANDR=ON \
125        -DENABLE_LTO=ON \
126        -DXTERMCMD=$xterm &&
127    gmake "$jobs"
128fi
129
130# check the dependencies
131if [[ -v DEPEN ]]; then
132    for p in \
133        x11 xext xcomposite xdamage xfixes \
134        xrender xrandr xinerama xft \
135        fontconfig sm ice \
136        sndfile alsa ao \
137        gio-2.0 gio-unix-2.0 \
138        gdk-pixbuf-xlib-2.0 librsvg-2.0 \
139        imlib2 xpm libpng libjpeg
140    do
141        printf "%-20s: " "$p"
142        pkg-config --modversion --print-errors --errors-to-stdout $p
143    done
144fi
145
146# test combinations of configure options
147if [[ -v TESTC ]]; then
148    rm -f -- rebuild.log rebuild.err
149    for i in {001..050} :; do
150        rm -f -- rebuild.tmp
151        gmake "$jobs" clean &>>rebuild.log
152        # pick five configure options randomly:
153        able=$(./configure --help=short |
154              sed -e 's|\[.*||' |
155              awk '{print $1}' |
156              grep -e --enable- -e --disable- |
157              grep -v -e -FEATURE -e -checking -e -tracking -e -maintainer |
158              grep -v -e -rules -e -install -e -libtool -e -static -e -shared |
159              shuf -n 5)
160        echo "# $i: $(date +%T): ./configure $able" | tee rebuild.tmp
161        ./configure $able &>>rebuild.tmp &&
162        gmake "$jobs" &>>rebuild.tmp ||
163        { echo "FAILED for $able !" |
164            tee -a rebuild.err;
165            mv -fv rebuild.tmp rebuild-$i.err
166        }
167    done |& tee -a rebuild.log
168fi
169
170