#!/bin/sh
# Copyright (c) Red Hat Inc. 2017-
# Arnaldo Carvalho de Melo <acme@redhat.com>
# cross build Dockerfiles must have ENV lines for ARCH and CROSS_COMPILE

export PATH=$PATH:$EXTRA_PATH

if [ -z "$1" ] ; then
	export PERF_TARBALL=http://localhost/perf/perf-latest.tar.xz
else
	export PERF_TARBALL=$1
fi

cd /git
echo "Downloading $PERF_TARBALL..."
curl -OL $PERF_TARBALL || wget $PERF_TARBALL
xzcat `basename $PERF_TARBALL` | tar xf - && \
mv perf-*/ linux && \

cat /git/linux/HEAD

# print the version for dm to harvest and put in the status line
${CROSS_COMPILE}gcc -v

make $EXTRA_MAKE_ARGS ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE EXTRA_CFLAGS="$EXTRA_CFLAGS" -C /git/linux/tools/perf O=/tmp/build/perf || exit 1
rm -rf /tmp/build/perf/{.[^.]*,*}
make $EXTRA_MAKE_ARGS ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE EXTRA_CFLAGS="$EXTRA_CFLAGS" NO_LIBELF=1 -C /git/linux/tools/perf O=/tmp/build/perf || exit 1

# Allow for explicitely asking to avoid building with clang even if present
# ClearLinux had this problem at some point
[ $NO_CLANG ] && exit 0

# Bail ou if we don't have clang, print the version for dm to harvest and put in the status line
${CROSS_COMPILE}clang -v || exit 0

rm -rf /tmp/build/perf/{.[^.]*,*}
make $EXTRA_MAKE_ARGS ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE EXTRA_CFLAGS="$EXTRA_CFLAGS" -C /git/linux/tools/perf O=/tmp/build/perf CC=clang || exit 1
rm -rf /tmp/build/perf/{.[^.]*,*}
make $EXTRA_MAKE_ARGS ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE EXTRA_CFLAGS="$EXTRA_CFLAGS" NO_LIBELF=1 -C /git/linux/tools/perf O=/tmp/build/perf CC=clang || exit 1
rm -rf /tmp/build/perf/{.[^.]*,*}
make $EXTRA_MAKE_ARGS ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE EXTRA_CFLAGS="$EXTRA_CFLAGS" LIBCLANGLLVM=1 -C /git/linux/tools/perf O=/tmp/build/perf CC=clang || exit 1
rm -rf /tmp/build/perf/{.[^.]*,*}
make $EXTRA_MAKE_ARGS ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE EXTRA_CFLAGS="$EXTRA_CFLAGS" LIBCLANGLLVM=1 -C /git/linux/tools/perf O=/tmp/build/perf || exit 1

# Need tp create a MANIFEST on tools/objtool and/or make sure it uses
# only stuff in tools/include/, etc.
#make -C /git/linux/tools/objtool O=/tmp/build/objtool && \
#
# We now use just a detached tarball, not the whole kernel sources
# accessed via NFS, so...
#make -C /git/linux O=/tmp/build/linux defconfig && \
#make -C /git/linux O=/tmp/build/linux headers_install && \
#make -C /git/linux O=/tmp/build/linux samples/bpf/
