# Packager = Tuxcore <dev@tuxcore.org> 
pkgbase=intel-media-sdk
pkgname=('intel-media-sdk' 'libmfx')
pkgver=23.2.2
pkgrel=3
pkgdesc='Legacy API for hardware video acceleration on Intel GPUs (Broadwell to Rocket Lake)'
arch=('x86_64')
url='https://software.intel.com/en-us/media-sdk/'
license=('MIT')
makedepends=('libdrm' 'libva' 'wayland' 'intel-media-driver'
             'cmake' 'libpciaccess' 'libx11' 'libxcb' 'python')
source=("https://github.com/Intel-Media-SDK/MediaSDK/archive/intel-mediasdk-${pkgver}.tar.gz"
        '010-intel-media-sdk-fix-reproducible-build.patch'::'https://github.com/Intel-Media-SDK/MediaSDK/commit/f6925886f27a39eed2e43c5b7b6c342d00f7a970.patch'
        '020-intel-media-sdk-gcc13-fix.patch'
        '030-intel-media-libcttmetrics-static-only.patch')
sha256sums=('12f23a78104edc1c9bfa755effd2723866d107ad752f72d3839fcc8db0503cec'
            'f1d8a4edf953cfec1516f1a8383c5ee033245aba16cfae0bc79b7de1a6365fcc'
            '38fc06cdc31372d26f1dc2a7e5b1ea57c22f8f83fbf84e0af5638a8040aa7f4e'
            'a473fd7dbd3c7240a3511471cfad6a39dc462b102c88cd23c35e25f2e8ec0246')

prepare() {
    patch -d "MediaSDK-intel-mediasdk-${pkgver}" -Np1 -i "${srcdir}/010-intel-media-sdk-fix-reproducible-build.patch"
    patch -d "MediaSDK-intel-mediasdk-${pkgver}" -Np1 -i "${srcdir}/020-intel-media-sdk-gcc13-fix.patch"
    patch -d "MediaSDK-intel-mediasdk-${pkgver}" -Np1 -i "${srcdir}/030-intel-media-libcttmetrics-static-only.patch"
}

build() {
    export CFLAGS+=' -DNDEBUG -ffat-lto-objects'
    export CXXFLAGS+=' -DNDEBUG -ffat-lto-objects'
    cmake -B build -S "MediaSDK-intel-mediasdk-${pkgver}" \
        -G 'Unix Makefiles' \
        -DBUILD_ALL:BOOL='ON' \
        -DBUILD_TOOLS:BOOL='ON' \
        -DCMAKE_BUILD_TYPE:STRING='None' \
        -DCMAKE_INSTALL_PREFIX:PATH='/usr' \
        -DENABLE_ITT:BOOL='OFF' \
        -DENABLE_OPENCL:BOOL='OFF' \
        -DENABLE_WAYLAND:BOOL='ON' \
        -DENABLE_X11_DRI3:BOOL='ON' \
        -DMFX_APPS_DIR='/usr/lib/mfx' \
        -Wno-dev
    cmake --build build
}

check() {
    ctest --test-dir build --output-on-failure
}

package_intel-media-sdk() {
    depends=('libdrm' 'libva' 'wayland' "libmfx=${pkgver}" 'intel-media-driver')
    options=('!emptydirs')
    provides=('vpl-runtime')
    
    DESTDIR="$pkgdir" cmake --install build
    mv "${pkgdir}/usr/lib/libcttmetrics.a" "${pkgdir}/usr/lib/mfx/samples"
    install -D -m644 "MediaSDK-intel-mediasdk-${pkgver}/LICENSE" -t "${pkgdir}/usr/share/licenses/${pkgname}"
    
    [ -d 'libmfx' ] && rm -rf libmfx
    mkdir -p libmfx/usr/{include,lib/pkgconfig}
    mv "${pkgdir}/usr/include/mfx" libmfx/usr/include
    mv "${pkgdir}/usr/lib"/libmfx.so* libmfx/usr/lib
    mv "${pkgdir}/usr/lib/pkgconfig"/{,lib}mfx.pc libmfx/usr/lib/pkgconfig
}

package_libmfx() {
    pkgdesc='Intel Media SDK dispatcher library'
    depends=('gcc-libs')
    
    mv -T libmfx "$pkgdir"
    install -D -m644 "MediaSDK-intel-mediasdk-${pkgver}/LICENSE" -t "${pkgdir}/usr/share/licenses/${pkgname}"
}
