#!/bin/bash

# Slackware build script for openvino

# Copyright 2024  Johannes Schoepfer, Germany
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=openvino
VERSION=${VERSION:-2024.4.1}
VERSION_FLATBUFFERS=24.3.25
VERSION_ONNX=1.16.0
VERSION_ITTAPI=3.24.4
VERSION_PROTOBUF=21.12
COMMIT_ONEDNN=c8ae8d96e963bd04214858319fa334968e5e73c9
COMMIT_ONEDNN_GPU=4ccd07e3a10e1c08075cf824ac14708245fbc334
COMMIT_MLAS=d1bc25ec4660cddd87804fcf03b2411b5dfb2e94
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "arm" ]; then
  SLKCFLAGS="-O2 -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=hard"
  LIBDIRSUFFIX=""
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.?z
cd $PRGNAM-$VERSION

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} + -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} +

patch -p1 < $CWD/010-openvino-disable-werror.patch
patch -p1 < $CWD/020-openvino-use-protobuf-shared-libs.patch

# Flatbuffers from SBo provide broken cmake files(static-lib removed but static-lib-cmake-files not removed)
(
cd thirdparty/flatbuffers/
rmdir flatbuffers
tar vxf $CWD/flatbuffers-$VERSION_FLATBUFFERS.tar.gz
ln -s flatbuffers-$VERSION_FLATBUFFERS flatbuffers
)
# onnx frontend
(
cd thirdparty/onnx/
rmdir onnx
tar vxf $CWD/onnx-$VERSION_ONNX.tar.gz
ln -s onnx-$VERSION_ONNX onnx
)
# ittapi
(
cd thirdparty/ittapi/
rmdir ittapi
tar vxf $CWD/ittapi-$VERSION_ITTAPI.tar.gz
ln -s ittapi-$VERSION_ITTAPI ittapi
)
# protobuf
(
cd thirdparty/protobuf/
rmdir protobuf
tar vxf $CWD/protobuf-all-$VERSION_PROTOBUF.tar.gz
ln -s protobuf-$VERSION_PROTOBUF protobuf
)
# mlas for cpu inference device
(
cd src/plugins/intel_cpu/thirdparty
rmdir mlas
tar vxf $CWD/mlas-$COMMIT_MLAS.tar.gz
ln -s mlas-$COMMIT_MLAS mlas
)
# oneDNN for cpu inference device
(
cd src/plugins/intel_cpu/thirdparty
rmdir onednn
tar vxf $CWD/oneDNN-$COMMIT_ONEDNN.tar.gz
ln -s oneDNN-$COMMIT_ONEDNN onednn
)
# oneDNN for gpu inference device
(
cd src/plugins/intel_gpu/thirdparty
rmdir onednn_gpu
tar vxf $CWD/oneDNN-$COMMIT_ONEDNN_GPU.tar.gz
ln -s oneDNN-$COMMIT_ONEDNN_GPU onednn_gpu
)

# npu plugin needs avx and does not build with the default x86_64 arch
mkdir -p build
cd build
  unshare -n cmake \
    -G Ninja \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_STANDARD=17 \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -DCMAKE_INSTALL_PREFIX=/usr/share/openvino \
    -DLIB_SUFFIX=${LIBDIRSUFFIX} \
    -DENABLE_AVX2=OFF \
    -DENABLE_AVX512F=OFF \
    -DENABLE_CPPLINT=OFF \
    -DENABLE_SYSTEM_FLATBUFFERS=OFF \
    -DENABLE_SYSTEM_PUGIXML=ON \
    -DENABLE_SYSTEM_SNAPPY=ON \
    -DENABLE_SYSTEM_TBB=ON \
    -DENABLE_PROFILING_ITT=ON \
    -DENABLE_TBBBIND_2_5=OFF \
    -DENABLE_JS=OFF \
    -DENABLE_SAMPLES=OFF \
    -DENABLE_INTEL_CPU=ON \
    -DENABLE_INTEL_GPU=ON \
    -DENABLE_INTEL_NPU=OFF \
    -DCMAKE_BUILD_TYPE=Release ..
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install/strip
cd ..

# We don't build samples, so we remove samples stub files
rm -rf $PKG/usr/share/openvino/samples

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENSE README.md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
