f32b5784f492c62208b3894be9001eb59990ead4
[dpdk.git] / devtools / test-meson-builds.sh
1 #! /bin/sh -e
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2018 Intel Corporation
4
5 # Run meson to auto-configure the various builds.
6 # * all builds get put in a directory whose name starts with "build-"
7 # * if a build-directory already exists we assume it was properly configured
8 # Run ninja after configuration is done.
9
10 # set pipefail option if possible
11 PIPEFAIL=""
12 set -o | grep -q pipefail && set -o pipefail && PIPEFAIL=1
13
14 srcdir=$(dirname $(readlink -f $0))/..
15 . $srcdir/devtools/load-devel-config
16
17 MESON=${MESON:-meson}
18 use_shared="--default-library=shared"
19 builds_dir=${DPDK_BUILD_TEST_DIR:-.}
20
21 if command -v gmake >/dev/null 2>&1 ; then
22         MAKE=gmake
23 else
24         MAKE=make
25 fi
26 if command -v ninja >/dev/null 2>&1 ; then
27         ninja_cmd=ninja
28 elif command -v ninja-build >/dev/null 2>&1 ; then
29         ninja_cmd=ninja-build
30 else
31         echo "ERROR: ninja is not found" >&2
32         exit 1
33 fi
34 if command -v ccache >/dev/null 2>&1 ; then
35         CCACHE=ccache
36 else
37         CCACHE=
38 fi
39
40 default_path=$PATH
41 default_cppflags=$CPPFLAGS
42 default_cflags=$CFLAGS
43 default_ldflags=$LDFLAGS
44 default_meson_options=$DPDK_MESON_OPTIONS
45
46 check_cc_flags () # <flag to check> <flag2> ...
47 {
48         echo 'int main(void) { return 0; }' |
49                 cc $@ -x c - -o /dev/null 2> /dev/null
50 }
51
52 load_env () # <target compiler>
53 {
54         targetcc=$1
55         # reset variables before target-specific config
56         export PATH=$default_path
57         unset PKG_CONFIG_PATH # global default makes no sense
58         export CPPFLAGS=$default_cppflags
59         export CFLAGS=$default_cflags
60         export LDFLAGS=$default_ldflags
61         export DPDK_MESON_OPTIONS=$default_meson_options
62         # set target hint for use in the loaded config file
63         if [ -n "$target_override" ] ; then
64                 DPDK_TARGET=$target_override
65         elif command -v $targetcc >/dev/null 2>&1 ; then
66                 DPDK_TARGET=$($targetcc -v 2>&1 | sed -n 's,^Target: ,,p')
67         else # toolchain not yet in PATH: its name should be enough
68                 DPDK_TARGET=$targetcc
69         fi
70         echo "Using DPDK_TARGET $DPDK_TARGET"
71         # config input: $DPDK_TARGET
72         . $srcdir/devtools/load-devel-config
73         # config output: $DPDK_MESON_OPTIONS, $PATH, $PKG_CONFIG_PATH, etc
74         command -v $targetcc >/dev/null 2>&1 || return 1
75 }
76
77 config () # <dir> <builddir> <meson options>
78 {
79         dir=$1
80         shift
81         builddir=$1
82         shift
83         if [ -f "$builddir/build.ninja" ] ; then
84                 # for existing environments, switch to debugoptimized if unset
85                 # so that ABI checks can run
86                 if ! $MESON configure $builddir |
87                                 awk '$1=="buildtype" {print $2}' |
88                                 grep -qw debugoptimized; then
89                         $MESON configure --buildtype=debugoptimized $builddir
90                 fi
91                 return
92         fi
93         options=
94         if echo $* | grep -qw -- '--default-library=shared' ; then
95                 options="$options -Dexamples=all"
96         else
97                 options="$options -Dexamples=l3fwd" # save disk space
98         fi
99         options="$options --buildtype=debugoptimized"
100         for option in $DPDK_MESON_OPTIONS ; do
101                 options="$options -D$option"
102         done
103         options="$options $*"
104         echo "$MESON $options $dir $builddir"
105         $MESON $options $dir $builddir
106 }
107
108 compile () # <builddir>
109 {
110         builddir=$1
111         if [ -n "$TEST_MESON_BUILD_VERY_VERBOSE" ] ; then
112                 # for full output from ninja use "-v"
113                 echo "$ninja_cmd -v -C $builddir"
114                 $ninja_cmd -v -C $builddir
115         elif [ -n "$TEST_MESON_BUILD_VERBOSE" ] ; then
116                 # for keeping the history of short cmds, pipe through cat
117                 echo "$ninja_cmd -C $builddir | cat"
118                 $ninja_cmd -C $builddir | cat
119         else
120                 echo "$ninja_cmd -C $builddir"
121                 $ninja_cmd -C $builddir
122         fi
123 }
124
125 install_target () # <builddir> <installdir>
126 {
127         rm -rf $2
128         if [ -n "$TEST_MESON_BUILD_VERY_VERBOSE$TEST_MESON_BUILD_VERBOSE" ]; then
129                 echo "DESTDIR=$2 $ninja_cmd -C $1 install"
130                 DESTDIR=$2 $ninja_cmd -C $1 install
131         else
132                 echo "DESTDIR=$2 $ninja_cmd -C $1 install >/dev/null"
133                 DESTDIR=$2 $ninja_cmd -C $1 install >/dev/null
134         fi
135 }
136
137 build () # <directory> <target compiler | cross file> <meson options>
138 {
139         targetdir=$1
140         shift
141         crossfile=
142         [ -r $1 ] && crossfile=$1 || targetcc=$1
143         shift
144         # skip build if compiler not available
145         command -v ${CC##* } >/dev/null 2>&1 || return 0
146         if [ -n "$crossfile" ] ; then
147                 cross="--cross-file $crossfile"
148                 targetcc=$(sed -n 's,^c[[:space:]]*=[[:space:]]*,,p' \
149                         $crossfile | tr -d "'" | tr -d '"')
150         else
151                 cross=
152         fi
153         load_env $targetcc || return 0
154         config $srcdir $builds_dir/$targetdir $cross --werror $*
155         compile $builds_dir/$targetdir
156         if [ -n "$DPDK_ABI_REF_VERSION" ]; then
157                 abirefdir=${DPDK_ABI_REF_DIR:-reference}/$DPDK_ABI_REF_VERSION
158                 if [ ! -d $abirefdir/$targetdir ]; then
159                         # clone current sources
160                         if [ ! -d $abirefdir/src ]; then
161                                 git clone --local --no-hardlinks \
162                                         --single-branch \
163                                         -b $DPDK_ABI_REF_VERSION \
164                                         $srcdir $abirefdir/src
165                         fi
166
167                         rm -rf $abirefdir/build
168                         config $abirefdir/src $abirefdir/build $cross \
169                                 -Dexamples= $*
170                         compile $abirefdir/build
171                         install_target $abirefdir/build $abirefdir/$targetdir
172                         $srcdir/devtools/gen-abi.sh $abirefdir/$targetdir
173
174                         # save disk space by removing static libs and apps
175                         find $abirefdir/$targetdir/usr/local -name '*.a' -delete
176                         rm -rf $abirefdir/$targetdir/usr/local/bin
177                         rm -rf $abirefdir/$targetdir/usr/local/share
178                 fi
179
180                 install_target $builds_dir/$targetdir \
181                         $(readlink -f $builds_dir/$targetdir/install)
182                 $srcdir/devtools/gen-abi.sh \
183                         $(readlink -f $builds_dir/$targetdir/install)
184                 $srcdir/devtools/check-abi.sh $abirefdir/$targetdir \
185                         $(readlink -f $builds_dir/$targetdir/install)
186         fi
187 }
188
189 if [ "$1" = "-vv" ] ; then
190         TEST_MESON_BUILD_VERY_VERBOSE=1
191 elif [ "$1" = "-v" ] ; then
192         TEST_MESON_BUILD_VERBOSE=1
193 fi
194 # we can't use plain verbose when we don't have pipefail option so up-level
195 if [ -z "$PIPEFAIL" -a -n "$TEST_MESON_BUILD_VERBOSE" ] ; then
196         echo "# Missing pipefail shell option, changing VERBOSE to VERY_VERBOSE"
197         TEST_MESON_BUILD_VERY_VERBOSE=1
198 fi
199
200 # shared and static linked builds with gcc and clang
201 for c in gcc clang ; do
202         command -v $c >/dev/null 2>&1 || continue
203         for s in static shared ; do
204                 export CC="$CCACHE $c"
205                 build build-$c-$s $c --default-library=$s
206                 unset CC
207         done
208 done
209
210 # test compilation with minimal x86 instruction set
211 # Set the install path for libraries to "lib" explicitly to prevent problems
212 # with pkg-config prefixes if installed in "lib/x86_64-linux-gnu" later.
213 default_machine='nehalem'
214 if ! check_cc_flags "-march=$default_machine" ; then
215         default_machine='corei7'
216 fi
217 build build-x86-default cc -Dlibdir=lib -Dmachine=$default_machine $use_shared
218
219 # 32-bit with default compiler
220 if check_cc_flags '-m32' ; then
221         if [ -d '/usr/lib/i386-linux-gnu' ] ; then
222                 # 32-bit pkgconfig on Debian/Ubuntu
223                 export PKG_CONFIG_LIBDIR='/usr/lib/i386-linux-gnu/pkgconfig'
224         elif [ -d '/usr/lib32' ] ; then
225                 # 32-bit pkgconfig on Arch
226                 export PKG_CONFIG_LIBDIR='/usr/lib32/pkgconfig'
227         else
228                 # 32-bit pkgconfig on RHEL/Fedora (lib vs lib64)
229                 export PKG_CONFIG_LIBDIR='/usr/lib/pkgconfig'
230         fi
231         target_override='i386-pc-linux-gnu'
232         build build-32b cc -Dc_args='-m32' -Dc_link_args='-m32'
233         target_override=
234         unset PKG_CONFIG_LIBDIR
235 fi
236
237 # x86 MinGW
238 build build-x86-mingw $srcdir/config/x86/cross-mingw -Dexamples=helloworld
239
240 # generic armv8a with clang as host compiler
241 f=$srcdir/config/arm/arm64_armv8_linux_gcc
242 export CC="clang"
243 build build-arm64-host-clang $f $use_shared
244 unset CC
245 # some gcc/arm configurations
246 for f in $srcdir/config/arm/arm64_[bdo]*gcc ; do
247         export CC="$CCACHE gcc"
248         build build-$(basename $f | tr '_' '-' | cut -d'-' -f-2) $f $use_shared
249         unset CC
250 done
251
252 # ppc configurations
253 for f in $srcdir/config/ppc/ppc* ; do
254         build build-$(basename $f | cut -d'-' -f-2) $f $use_shared
255 done
256
257 # Test installation of the x86-default target, to be used for checking
258 # the sample apps build using the pkg-config file for cflags and libs
259 build_path=$(readlink -f $builds_dir/build-x86-default)
260 export DESTDIR=$build_path/install
261 # No need to reinstall if ABI checks are enabled
262 if [ -z "$DPDK_ABI_REF_VERSION" ]; then
263         install_target $build_path $DESTDIR
264 fi
265
266 load_env cc
267 pc_file=$(find $DESTDIR -name libdpdk.pc)
268 export PKG_CONFIG_PATH=$(dirname $pc_file):$PKG_CONFIG_PATH
269
270 # if pkg-config defines the necessary flags, test building some examples
271 if pkg-config --define-prefix libdpdk >/dev/null 2>&1; then
272         export PKGCONF="pkg-config --define-prefix"
273         for example in cmdline helloworld l2fwd l3fwd skeleton timer; do
274                 echo "## Building $example"
275                 $MAKE -C $DESTDIR/usr/local/share/dpdk/examples/$example clean shared static
276         done
277 fi