File PKGBUILD of Package cmake-git

# $Id$
# Maintainer: Piotr Gorski <lucjan.lucjanov@gmail.com>
# Contributor: Ysblokje <ysblokje at gmail dot com>
# Contributor: Felix Yan <felixonmars@archlinux.org>
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Contributor: Pierre Schmitz <pierre@archlinux.de>
# Contributor: Christoph Viebig <oss@christoph-viebig.de>
#
# This PKGBUILD is based on the official Arch cmake package.

pkgname=cmake-git-god
pkgver=4.2.1.737.g824f2a7a20
pkgrel=4
pkgdesc='A cross-platform open-source make system (God Mode Build with GCC 16, CLI Only)'
arch=('x86_64')
url="http://www.cmake.org/"
license=('custom')
conflicts=('cmake' 'cmake-git')
provides=('cmake' 'cmake-git')
depends=('curl' 'libarchive' 'jsoncpp' 'libjsoncpp.so' 'libuv' 'rhash' 'cppdap')
makedepends=('python-sphinx' 'emacs' 'nlohmann-json' 'git' 'gcc-git-god' 'llvm-god-mlgo')
source=(  "cmake.tar.gz"
  "cmake-cppflags.patch"
)
md5sums=(         'SKIP'
         'd7316e540d07e0a7ebce75951a7b2697'
)



pkgver() {
  local _ver _ts
  if [[ -f "${srcdir}/cmake.obsinfo" ]]; then
    _ver=$(sed -n 's/^version: //p' "${srcdir}/cmake.obsinfo" | head -n1)
  fi
  if [[ -z "$_ver" && -f "${srcdir}/._servicedata" ]]; then
    _ver=$(sed -n 's/.*<param name="version">\(.*\)<\/param>.*/\1/p' "${srcdir}/._servicedata" | head -n1)
  fi
  if [[ -z "$_ver" && -d "${srcdir}/cmake" ]]; then
    _ts=$(find "${srcdir}/cmake" -type f -printf '%T@\n' 2>/dev/null | sort -n | tail -n1 | cut -d. -f1)
    if [[ -n "$_ts" ]]; then
      _ver=$(date -u -d "@${_ts}" +%Y%m%d.%H%M%S 2>/dev/null)
    fi
  fi
  if [[ -z "$_ver" ]]; then
    _ver="${pkgver}"
  fi
  echo "${_ver//-/_}"
}


prepare() {
    cd "$srcdir/cmake"
    patch -p1 -i ../cmake-cppflags.patch # Honor CPPFLAGS https://gitlab.kitware.com/cmake/cmake/issues/12928
}

build() {
  cd "$srcdir/cmake"

  # --- MISAKA GOD MODE INJECTION ---
  msg2 "Injecting God Mode Compiler and Flags..."
  if [ ! -x /opt/gcc-git-god/bin/gcc ]; then
    echo "gcc-git-god missing in /opt/gcc-git-god"
    return 1
  fi
  export CC="/opt/gcc-git-god/bin/gcc"
  export CXX="/opt/gcc-git-god/bin/g++"
  
  # Keep BOLT profiling runnable on generic OBS builders.
  export GOD_FLAGS="-O3 -march=x86-64 -mtune=generic -flto=auto -fgraphite-identity -floop-nest-optimize -pipe -fno-semantic-interposition"
  export GOD_LDFLAGS="-L/opt/gcc-git-god/lib64 -Wl,-rpath,/opt/gcc-git-god/lib64 -flto=auto -Wl,--emit-relocs"

  export CFLAGS="$GOD_FLAGS"
  export CXXFLAGS="$GOD_FLAGS"
  export LDFLAGS="$GOD_LDFLAGS"
  export LD_LIBRARY_PATH="/opt/gcc-git-god/lib64:$LD_LIBRARY_PATH"

  msg2 "Bootstrapping God Mode CLI (No Qt, No Compromise)..."
  ./bootstrap --prefix=/usr \
    --mandir=/share/man \
    --docdir=/share/doc/cmake \
    --datadir=/share/cmake \
    --sphinx-man \
    --sphinx-html \
    --no-system-libs \
    --parallel=$(/usr/bin/getconf _NPROCESSORS_ONLN) -- \
    -DBUILD_QtDialog=OFF \
    -DCMAKE_USE_OPENSSL=ON
  
  msg2 "Main build starting..."
  make
}

package() {
  cd "$srcdir/cmake"
  make DESTDIR="${pkgdir}" install

  # --- MISAKA GOD MODE INJECTION: BOLT POST-INSTALL ---
  BOLT_BIN="$(command -v llvm-bolt || true)"
  MERGE_FDATA_BIN="$(command -v merge-fdata || true)"
  for d in /opt/llvm-god/bin /opt/llvm-god /usr/lib/llvm*/bin /usr/lib/llvm*; do
    if [ -z "$BOLT_BIN" ] && [ -x "$d/llvm-bolt" ]; then
      BOLT_BIN="$d/llvm-bolt"
    fi
    if [ -z "$MERGE_FDATA_BIN" ] && [ -x "$d/merge-fdata" ]; then
      MERGE_FDATA_BIN="$d/merge-fdata"
    fi
  done
  if [[ "${GOD_ENABLE_BOLT:-0}" != "1" ]]; then
    msg2 "GOD_ENABLE_BOLT is not enabled; skipping BOLT optimization."
  elif [ ! -x "$BOLT_BIN" ] || [ ! -x "$MERGE_FDATA_BIN" ]; then
    msg2 "BOLT tools missing (llvm-bolt/merge-fdata); skipping BOLT optimization."
  elif ! "$BOLT_BIN" --help >/dev/null 2>&1 || ! "$MERGE_FDATA_BIN" --help >/dev/null 2>&1; then
    msg2 "BOLT tools are present but not runnable on this builder; skipping BOLT optimization."
  else
    msg2 "Initiating BOLT Post-Link Optimization on installed binaries..."
    
    mkdir -p bolt_profile_run
    bolt_rt=""
    for d in /opt/llvm-god/lib /opt/llvm-god/lib64 /usr/lib /usr/lib/llvm*/lib /usr/lib/llvm*/lib64; do
      if [[ -f "$d/libbolt_rt_instr.a" ]]; then
        bolt_rt="$d/libbolt_rt_instr.a"
        break
      fi
    done
    if [[ -z "$bolt_rt" ]]; then
      msg2 "BOLT runtime not found (libbolt_rt_instr.a); skipping BOLT optimization."
    else
      "$BOLT_BIN" ./bin/cmake -o bolt_profile_run/cmake.inst \
        -instrument \
        -instrumentation-file="$PWD/bolt_profile_run/cmake.fdata" \
        -instrumentation-file-append-pid
      LD_LIBRARY_PATH="/opt/gcc-git-god/lib64" bolt_profile_run/cmake.inst .. -DCMAKE_BUILD_TYPE=Release > /dev/null 2>&1 || true

      if ls bolt_profile_run/cmake.fdata.* &>/dev/null; then
        "$MERGE_FDATA_BIN" bolt_profile_run/cmake.fdata.* > bolt_profile_run/cmake.fdata
      fi
      if [ ! -s bolt_profile_run/cmake.fdata ]; then
        msg2 "No BOLT profile data collected; keeping non-BOLT binaries."
      else
        msg2 "Applying BOLT to cmake..."
        "$BOLT_BIN" ./bin/cmake -o ./bin/cmake.bolt -data bolt_profile_run/cmake.fdata -reorder-blocks=ext-tsp -reorder-functions=cdsort -split-functions -split-all-cold -split-eh -dyno-stats
        cp ./bin/cmake.bolt "$pkgdir"/usr/bin/cmake
        
        msg2 "Applying BOLT to ccmake..."
        "$BOLT_BIN" ./bin/ccmake -o ./bin/ccmake.bolt -data bolt_profile_run/cmake.fdata -reorder-blocks=ext-tsp -reorder-functions=cdsort -split-functions -split-all-cold -split-eh
        cp ./bin/ccmake.bolt "$pkgdir"/usr/bin/ccmake
      fi
    fi
  fi
  # --- END BOLT ---

  rm -rf "$pkgdir"/usr/share/doc/cmake/html/_sources
  emacs -batch -f batch-byte-compile "${pkgdir}"/usr/share/emacs/site-lisp/cmake-mode.el
  install -Dm644 LICENSE.rst -t "${pkgdir}"/usr/share/licenses/$pkgname
}
openSUSE Build Service is sponsored by