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