# Maintainer: Maxime Gauduin <alucryd@archlinux.org>
# Contributor: Kyle Keen <keenerd@gmail.com>
# Contributor: Mihai Militaru <mihai militaru at xmpp dot ro>
# Contributor: carstene1ns <arch carsten-teibes.de>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - disable setting MBEDTLS_HAVE_SSE2
#  - patch to fix FTBFS with gcc 14: https://github.com/Mbed-TLS/mbedtls/issues/9003

pkgname=mbedtls
pkgver=3.6.1
pkgrel=1
pkgdesc='An open source, portable, easy to use, readable and flexible TLS library'
arch=(x86_64)
url=https://tls.mbed.org
license=(Apache-2.0)
depends=(
  glibc
  sh
)
checkdepends=(python)
makedepends=(
  cmake
  git
  ninja
  python
)
provides=(
  libmbedcrypto.so
  libmbedtls.so
  libmbedx509.so
  polarssl
)
replaces=(polarssl)
conflicts=(polarssl)
options=(staticlibs)
_tag=b727a6982c8f4d97dc8a979d7af417a08a551e5e
source=(
  git+https://github.com/Mbed-TLS/mbedtls.git#tag=${_tag}
  git+https://github.com/Mbed-TLS/mbedtls-framework.git
  gcc14.patch
)
b2sums=('9a38000d9a88443d9b48f43c811eb5ffc7b03a779c283482f6025235729a7e72474fe71e0293ee48a61e7621e03e0b89462e02c64fc16461bae0e30ff4f8a357'
        'SKIP'
        'b1f1ac09a40350c508d6019a4f937b73ff5322528285a71ce341eddacc09ac06392c4d91d754786250c724174947d3f2e9fca8f242275212f9adb4d9ecd65bed')

prepare() {
  cd mbedtls
  git submodule init framework
  git config submodule.framework.url "${srcdir}"/mbedtls-framework
  git -c protocol.file.allow=always submodule update framework
  #scripts/config.py set MBEDTLS_HAVE_SSE2
  scripts/config.py set MBEDTLS_THREADING_C
  scripts/config.py set MBEDTLS_THREADING_PTHREAD
  patch -p1 -i ../gcc14.patch
}

pkgver() {
  cd mbedtls
  git describe --tags | sed 's/^v//; s/^mbedtls-//'
}

build() {
  export CFLAGS+=' -ffat-lto-objects'
  cmake -S mbedtls -B build -G Ninja \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_SKIP_RPATH=ON \
    -DUSE_SHARED_MBEDTLS_LIBRARY=ON \
    -DUSE_STATIC_MBEDTLS_LIBRARY=ON \
    -Wno-dev
  cmake --build build
}

check() {
  LD_LIBRARY_PATH="${srcdir}"/build/library ctest --test-dir build
}

package() {
  DESTDIR="${pkgdir}" cmake --install build

  # rename generic utils
  local _prog _baseprog
  for _prog in "${pkgdir}"/usr/bin/*; do
  _baseprog=$(basename "$_prog")
    mv -v "$_prog" "${_prog//$_baseprog/mbedtls_$_baseprog}"
  done

  # fixup static lib permissions
  chmod 644 "$pkgdir"/usr/lib/*.a
}

# vim: ts=2 sw=2 et: