#!/bin/env sh

set -x
set -o errexit

# Script meant to be run in the projet's root directory from the 'rules'
# makefile. The steps would have never been extracted to an actuall separate
# script if not for the creation of temp folder and saving its path in a
# variable which would render the makefile recipe very cumbersome.

# Needed for VPP version generation
mv totally_not_a_dot_git .git

# DPAA platform from Layerscape requires a specific DPDK installation layout to
# which the DPDK files installed from the repository don't conform. All the
# headers are there, but in the wrong folders. Modifying the DPDK package is not
# desirable as that would deviate too much from the upstream and wouldn't be of
# any use outside of VPP compilation context. As a solution a temporary
# directory is created in project's root with a curated DPDK installation based
# on the files present in the system and passed to the VPP's build system as
# DPDK's installation path.

dpdk_install_dir="$(realpath $(mktemp -d -p .))/usr"
install -m 777 -d ${dpdk_install_dir}/lib
install -m 777 -d ${dpdk_install_dir}/include
install -m 777 -d ${dpdk_install_dir}/include/generic
cp /usr/include/dpdk/generic/*.h            ${dpdk_install_dir}/include/generic
cp /usr/include/dpdk/*.h                    ${dpdk_install_dir}/include
cp /usr/include/aarch64-linux-gnu/dpdk/*.h  ${dpdk_install_dir}/include

echo "GROUP ( "$(cat debian/librte.list)" )" > ${dpdk_install_dir}/lib/libdpdk.a

mkdir ccache

# OPENSSL_PATH=
export CROSS_SYSROOT=
export CROSS_TOOLCHAIN=
export CROSS_PREFIX=aarch64-linux-gnu
export DPDK_PATH=${dpdk_install_dir}
export PLATFORM=dpaa
export TAG=dpaa

cmake src \
      -G 'Ninja' \
      -DCMAKE_MAKE_PROGRAM=ninja \
      -DDPDK_INCLUDE_DIR=${dpdk_install_dir}/include \
      -DCMAKE_INSTALL_PREFIX:PATH=/usr \
      -DCMAKE_INSTALL_BINDIR:PATH=bin \
      -DCMAKE_INSTALL_SBINDIR:PATH=sbin \
      -DCMAKE_INSTALL_LIBEXECDIR:PATH=libexec \
      -DCMAKE_INSTALL_SYSCONFDIR:PATH=/etc \
      -DCMAKE_INSTALL_SHAREDSTATEDIR:PATH=../com \
      -DCMAKE_INSTALL_LOCALSTATEDIR:PATH=/var \
      -DCMAKE_INSTALL_LIBDIR:PATH=lib \
      -DCMAKE_INSTALL_INCLUDEDIR:PATH=include \
      -DCMAKE_INSTALL_DATAROOTDIR:PATH=share \
      -DCMAKE_INSTALL_SO_NO_EXE=0 \
      -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake \
      -DCMAKE_NO_SYSTEM_FROM_IMPORTED=1 \
      -DCMAKE_C_FLAGS="-g -Ofast -fPIC -march=armv8-a+crc+simd+crypto -ftls-model=local-dynamic -Wno-error=unused-value -DALLOW_EXPERIMENTAL_API -mtune=cortex-A72 -funroll-all-loops -DCLIB_LOG2_CACHE_LINE_BYTES=6" \
      -Wno-dev

      # -DCMAKE_SHARED_LINKER_FLAGS="-Wl,--dynamic-linker=/lib/ld-linux-aarch64.so.1 -Wl,-rpath=/usr/lib64 -Wl,-rpath=./.libs"
