pkgname=x265
pkgver=3.6
pkgdesc="Library for encoding video streams into the H.265/HEVC format"
homepage="https://www.x265.org/ https://bitbucket.org/multicoreware/x265_git/"
licenses=("GPL-2")
depends=(">=dev-lang/nasm-2.13")
_patches=(
  "${pkgname}-3.5-r5-cpp-std.patch"
  "${pkgname}-3.5-r5-gcc15.patch"
  "${pkgname}-3.6-cmake-cleanup.patch"
  "${pkgname}-3.6-code-cleanup.patch"
  "${pkgname}-3.6-code-cleanup_2.patch"
  "${pkgname}-3.6-code-cleanup_3.patch"
  "${pkgname}-3.6-code-cleanup_4.patch"
  "${pkgname}-cmake-min-version-3.28.patch"
)
_patch_sums=("2ce2c6fa598db565bd5a6acddd0ec9095b68fde28584a23c5a398a16b44a5514b6d4ec2f9b8e4ab9a871320c694f4723958eca55588d90b3fdb5a94244c08729"
  "ed87e6919f0bcd6a719411716c4dbd0016e4d7c23beca4cf7d93d0beca4c7af275c8734e103098f7f358161917fed0a6a0d1301e1eb8de8744a8b4e6b288519f"
  "43d4c8420e8722337b9787ad434f00747bb97d7fe71dfd4934e1a8b36525ee7cc96ba2dd1dde3b7621452a59b2db2dbcd887732c8bfd828449e48e955e9c7f9b"
  "a4131ca9ccf7f8594f27506982ec2c0c00b026227343845aad8afd5e9d5d7a8722bfd2a5f365655fd4db52e5ce952258fd4fc89b3939b222b5e75d2f31c330db"
  "b67f809161759bff76c5958c2a34b09d79066917424960d4654d5922bfabea3d2bde3028f0ddd7d0c408516d75978634b12505a12d45cb018edf6cdfa79bab76"
  "37d327d59438b8b88db8de05d5a056c8baffbc46ea93d155c6ab5ffdded97f3ef5d621eb6c6a024f9022a8ff23b882663050cdaad99dd5cb8f236f1e9c4a1dae"
  "6c88ffa5c008dcf2ca394a6f6edc5cd859343119a589b1ee6497fe84727f92f25ccff24818a347ee3b92516f574e8cba182d1c26f86df41dbd9ddb62ca8dcd7c"
  "bef91e0d206c7e173c01ac775358e771c136da00afb7eb6ee27f1538ac6a1588556ed4c21ad93b2637f0c27c3ff50dd65e0b3c7266e62651416a603558a831c0"
)
sources=("${pkgname}-${pkgver}.tar.gz"
  ${_patches[@]}
)
urls=("https://bitbucket.org/multicoreware/x265_git/downloads/${pkgname}_${pkgver}.tar.gz"
  ${_patches[@]}
)
sha512sums=("570429c3f0b560b2f946ac9baba97d66d7d6485e46eb695b17003b5913028f62d35822477633e911cd3ea60e6ce0ecc7d9d82de7d8257272f9ca7b43761438be"
  ${_patch_sums[@]}
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  cd "source"
  for _patch in ${_patches[@]}; do
    patch -p1 -i ${filedir}/${_patch}
  done
}

src_build() {
  local common_args=(
    -S "source"
    -D CMAKE_INSTALL_PREFIX=/usr
    -D ENABLE_PIC=ON
    -D ENABLE_LIBNUMA="no"
    -D ENABLE_SVT_HEVC="no" # missing
    -D ENABLE_VTUNE="no" # missing
    -D GIT_ARCHETYPE=1 #814116
  )
  local hdr_args=(
    "${common_args[@]}"
    -D HIGH_BIT_DEPTH=ON
    -D EXPORT_C_API=OFF
    -D ENABLE_SHARED=OFF
    -D ENABLE_CLI=OFF
  )
  local final_args=(
    "${common_args[@]}"
    -D ENABLE_SHARED=TRUE
    -D EXTRA_LIB='libx265_main10.a;libx265_main12.a'
    -D EXTRA_LINK_FLAGS='-L .'
    -D LINKED_10BIT=TRUE
    -D LINKED_12BIT=TRUE
  )

  cmake -B build-10 "${hdr_args[@]}" -D ENABLE_HDR10_PLUS=ON
  cmake --build build-10
  cmake -B build-12 "${hdr_args[@]}" -D MAIN12=ON
  cmake --build build-12

  cmake -B build "${final_args[@]}"
  ln -sr build-10/libx265.a build/libx265_main10.a
  ln -sr build-12/libx265.a build/libx265_main12.a
  cmake --build build
}

src_install() {
  DESTDIR="${pkgdir}" cmake --install build
  find "${pkgdir}" -name "*.a" -delete
}

# vim:ft=sh syn=sh et sw=2:
