# Maintainer: Johannes Löthberg <johannes@kyriasis.com>
# Contributor: Alexander F Rødseth <xyproto@archlinux.org>
# Contributor: Daniel Micay <danielmicay@gmail.com>
# Contributor: userwithuid <userwithuid@gmail.com>

pkgname=('rust140' 'rust140-docs')
epoch=1
pkgver=1.40.0
pkgrel=1

_llvm_ver=9.0.1

pkgdesc='Systems programming language focused on safety, speed and concurrency'
url='https://www.rust-lang.org/'
arch=(i486 i686 pentium4 'x86_64')
license=('MIT' 'Apache')

makedepends=('rust=1:1.39.0' "llvm=$_llvm_ver" 'libffi' 'perl' 'python' 'curl' 'cmake')
checkdepends=('procps-ng' 'gdb')

options=('!emptydirs')

source=("https://static.rust-lang.org/dist/rustc-$pkgver-src.tar.gz"{,.asc}
        "https://github.com/llvm/llvm-project/releases/download/llvmorg-$_llvm_ver/compiler-rt-$_llvm_ver.src.tar.xz"{,.sig})

sha256sums=('d2e8f931d16a0539faaaacd801e0d92c58df190269014b2360c6ab2a90ee3475'
            'SKIP'
            'c2bfab95c9986318318363d7f371a85a95e333bc0b34fbfa52edbd3f5e3a9077'
            'SKIP')
validpgpkeys=('108F66205EAEB0AAA8DD5E1C85AB96E6FA1BE5FE'  # Rust Language (Tag and Release Signing Key) <rust-key@rust-lang.org>
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard <tstellar@redhat.com>
              'B6C8F98282B944E3B0D5C2530FC3042E345AD05D') # Hans Wennborg <hans@chromium.org>

prepare() {
  cd "rustc-$pkgver-src"

  cat >config.toml <<END
[llvm]
link-shared = true

[build]
target = ["x86_64-unknown-linux-gnu", "i686-unknown-linux-gnu"]
cargo = "/usr/bin/cargo"
rustc = "/usr/bin/rustc"
python = "/usr/bin/python"
extended = true
sanitizers = false
profiler = true
vendor = true

[install]
prefix = "/usr"

[rust]
# LLVM crashes when passing an object through ThinLTO twice.  This is triggered when using rust
# code in cross-language LTO if libstd was built using ThinLTO.
# http://blog.llvm.org/2019/09/closing-gap-cross-language-lto-between.html
# https://github.com/rust-lang/rust/issues/54872
codegen-units-std = 1

debuginfo-level = 2

channel = "stable"

rpath = false

[target.x86_64-unknown-linux-gnu]
llvm-config = "/usr/bin/llvm-config"
END
}

build() {
  cd "rustc-$pkgver-src"

  export RUST_BACKTRACE=1
  export RUST_COMPILER_RT_ROOT="$srcdir/compiler-rt-$_llvm_ver.src"

  python ./x.py dist -j "$(nproc)"
  DESTDIR="$PWD"/dest-rust python ./x.py install -j "$(nproc)"

  # move docs and lib32 libs out of the way for splitting
  mv dest-rust/usr/lib/rustlib/i686-unknown-linux-gnu dest-i686
  mv dest-rust/usr/share/doc dest-doc
}

package_rust140() {
  depends=('gcc-libs' 'llvm-libs' 'curl' 'libssh2')
  optdepends=('lldb: rust-lldb script'
              'gdb: rust-gdb script')
  provides=('cargo' 'rustfmt')
  conflicts=('cargo' 'rustfmt')
  replaces=('cargo' 'rustfmt')

  cd "rustc-$pkgver-src"

  mv dest-rust/* "$pkgdir"
  install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE*

  # delete unnecesary files, e.g. components and manifest files only used for the uninstall script
  cd "$pkgdir"/usr/lib/rustlib
  rm components install.log manifest-* rust-installer-version uninstall.sh

  # rustbuild always installs copies of the shared libraries to /usr/lib,
  # overwrite them with symlinks to the per-architecture versions
  ln -srft "$pkgdir"/usr/lib x86_64-unknown-linux-gnu/lib/*.so

  install -d "$pkgdir"/usr/share/bash-completion
  mv "$pkgdir"/etc/bash_completion.d/ "$pkgdir"/usr/share/bash-completion/completions/
}

package_lib32-rust-libs() {
  descriptino=('32-bit libraries for Rust')
  depends=('lib32-gcc-libs')
  provides=('lib32-rust')
  conflicts=('lib32-rust')
  replaces=('lib32-rust')

  cd "rustc-$pkgver-src"
  install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE*

  install -d "$pkgdir"/usr/lib/rustlib/ "$pkgdir"/usr/lib32/
  mv dest-i686 "$pkgdir"/usr/lib/rustlib/i686-unknown-linux-gnu
  ln -srft "$pkgdir"/usr/lib32 "$pkgdir"/usr/lib/rustlib/i686-unknown-linux-gnu/lib/*.so
}

package_rust140-docs() {
  description=('Documentation for the Rust programming language')

  cd "rustc-$pkgver-src"
  install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE*

  install -d "$pkgdir"/usr/share/doc
  mv dest-doc/* "$pkgdir"/usr/share/doc
}

# vim:set ts=2 sw=2 et:
# enable i686-unknown-linux-gnu target
# and replace pentium4 (SSE2) with pentium3 (no SSE2) in the compiler spec file
#
# Also trying to reduce memory usage by building with
# -debuginfo-level-std=1
# see https://github.com/rust-lang/rust/issues/60294

source+=(config.toml.patch)
sha256sums+=('42e861ba90335d0e2e666e10b12d9ff85ea1bdb711176fb3c09e92375e193660')

eval "$(
  declare -f prepare | \
    sed '
      $ i patch -p1 -i "$srcdir/config.toml.patch"
    '
)"

# stop insanity of nproc (see FS#62952)
eval "$(
  declare -f build | \
    sed '
      s/-j"$(nproc)"/-j16/
    '
)"

if [ "${CARCH}" != 'pentium4' ]; then
  eval "$(
    declare -f prepare | \
      sed '
        $ i sed -i "s/pentium4/pentium3/g" "$srcdir/rustc-$pkgver-src/vendor/rustc-ap-rustc_target/spec/i686_unknown_linux_gnu.rs"
        $ i sed -i "s/pentium4/pentium3/g" "$srcdir/rustc-$pkgver-src/src/librustc_target/spec/i686_unknown_linux_gnu.rs"
        $ i sed -i "s/a75a6025d7e3424edf9baf3039056c0f8eea157631a175d00ac5a218aa54b510/484bf8be15015b330fa9a97b6dabb8c7627e59d5cddb2dd0e83478749f8aabad/g" "$srcdir/rustc-$pkgver-src/vendor/rustc-ap-rustc_target/.cargo-checksum.json"
      '
  )"
fi

# Don't move lib32 support into rust32-lib, we are on 32-bit in the end
# and don't draw a symlink to 64-bit libraries (but to 32-bit instead)

pkgname=(
  $(
    printf '%s\n' "${pkgname[@]}" | \
      grep -v '^lib32-rust-libs'
  )
)

eval "$(
  declare -f package_rust140 | \
    sed '
      /mv.*i686-unknown-linux-gnu.*/d
      /ln /s/x86_64-unknown-linux-gnu/i686-unknown-linux-gnu/g
    '
)"

eval "$(
  declare -f build | \
    sed '
      /mv.*i686-unknown-linux-gnu.*/d
      /ln /s/x86_64-unknown-linux-gnu/i686-unknown-linux-gnu/g
    '
)"

# watch for files changing which need patching during the x.py craziness!
# currently this is duplicate U1024 and P1024 types (this is currently needed
# for i686 and 1.38.0)
source+=('watcher.sh')
sha256sums+=('c91b55a428ef463fc1529724da0c7f3930bd9d4b32b4596a14be77cc3f1e0594')

if [ "${CARCH}" = "i686" ]; then
  makedepends+=('inotify-tools' 'procps-ng')
  
  eval "$(
    declare -f build | \
      sed '
        /\.\/x.py/ i \
          chmod u+x "$srcdir/watcher.sh" \
          "$srcdir/watcher.sh" "$srcdir" & \
          _kill_pid=$! \
           sleep 10
        /\.\/x.py/ a \
          kill $_kill_pid || true
      '
  )"
fi

# Enable on demand, if your previous rust is broken, see also build-support and AUR
# for rust-bin.
#makedepends=(${makedepends[@]//rust/})
#makedepends+=('rust-bin')
sha256sums=('dd97005578defc10a482bff3e4e728350d2099c60ffcf1f5e189540c39a549ad'
            'SKIP'
            'c2bfab95c9986318318363d7f371a85a95e333bc0b34fbfa52edbd3f5e3a9077'
            'SKIP'
            '42e861ba90335d0e2e666e10b12d9ff85ea1bdb711176fb3c09e92375e193660'
            'c91b55a428ef463fc1529724da0c7f3930bd9d4b32b4596a14be77cc3f1e0594')
groups+=(build-shims)