pkgname=rust
pkgver=1.89.0
pkgdesc="Systems programming language focused on safety, speed and concurrency"
homepage=https://www.rust-lang.org/
arch=(x86_64)
license=("Apache-2.0 OR MIT")
sources=("rustc-${pkgver}-src.tar.xz")
urls=("https://static.rust-lang.org/dist/${sources[0]}")
md5sums=("982f087479302e6d91432efc81852b00")

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  local clangdir
  clangdir="$(clang -print-resource-dir)"
  cat << EOF > bootstrap.toml
# See bootstrap.toml.example for more possible options,
# and see src/bootstrap/defaults/bootstrap.dist.toml for a few options
# automatically set when building from a release tarball
# (unfortunately, we have to override many of them).

# Tell x.py the editors have reviewed the content of this file
# and updated it to follow the major changes of the building system,
# so x.py will not warn us to do such a review.
change-id = 142379

profile = "dist"

[llvm]
#from gentoo
download-ci-llvm = false
optimize = true
release-debuginfo = false

# When using system llvm prefer shared libraries
link-shared = true

# If building the shipped LLVM source, only enable the x86 target
# instead of all the targets supported by LLVM.
targets = "X86"

[build]
description = "LFS 12.4-systemd ${pkgname}-${pkgver}"

# Omit docs to save time and space (default is to build them).
docs = false

# Do not query new versions of dependencies online.
locked-deps = true

# Specify which extended tools (those from the default install).
tools = ["cargo", "clippy", "rustdoc", "rustfmt"]

sanitizers = true
profiler = true


[install]
prefix = "/usr"

[rust]
channel = "stable"

# Enable the same optimizations as the official upstream build.
lto = "thin"
codegen-units = 1

# Don't build lld which does not belong to this package and seems not
# so useful for BLFS.  Even if it turns out to be really useful we'd build
# it as a part of the LLVM package instead.
lld = false

# Don't build llvm-bitcode-linker which is only useful for the NVPTX
# backend that we don't enable.
llvm-bitcode-linker = false

[dist]
compression-formats = ["gz"]
compression-profile = "fast"

[target.x86_64-unknown-linux-gnu]
cc = "/usr/bin/gcc"
cxx = "/usr/bin/g++"
ar = "/usr/bin/gcc-ar"
ranlib = "/usr/bin/gcc-ranlib"
llvm-config = "/usr/bin/llvm-config"
#optimized-compiler-builtins = "$clangdir/lib/linux/libclang_rt.builtins-x86_64.a"

EOF
}

src_build() {
  leaf_clear_flags
  export LIBSQLITE3_SYS_USE_PKG_CONFIG=1
  ./x.py build
}

src_check() {
  leaf_record_message "Some tests are known to fail. See BLFS handbook."
  set +e
  ./x.py test --verbose --no-fail-fast | tee rustc-testlog
  local -a ps=( "${PIPESTATUS[@]}" )
  local rc="${ps[0]}"
  grep '^test result:' rustc-testlog | awk '{sum1 += $4; sum2 += $6} END { print sum1 " passed; " sum2 " failed" }'
  set -e
  return "$rc"
}

src_install() {
  export LIBSQLITE3_SYS_USE_PKG_CONFIG=1
  DESTDIR="${pkgdir}" python ./x.py install -j "${PARALLEL_JOBS}"

  cd "${pkgdir}"
  rm -v usr/lib/rustlib/{components,install.log,rust-installer-version,uninstall.sh}
  rm -v usr/lib/rustlib/manifest-*

  install -dm755 usr/share/bash-completion/completions
  mv -v etc/bash_completion.d/cargo usr/share/bash-completion/completions
  rm -rfv etc/

  # licenses for main rust package
  local ldir="usr/share/licenses/rust" f d
  mkdir -p "$ldir"
  for f in usr/share/doc/*/{COPYRIGHT,LICENSE}*; do
    d="$(dirname "$f")"
    case $f in
      */LICENSE-APACHE) rm -v "$f" ;;
      *) mv -v "$f" "$ldir/${f##*/}.${d##*/}" ;;
    esac
    rmdir -p --ignore-fail-on-non-empty "$d"
  done
}
