makedepends=(cmake llvm libssh2)

description="The Rust programming language is designed to be a safe, concurrent, practical language."
url="http://static.rust-lang.org/"

packager="Great-OS Team"
maintainer="Great-OS Team"

name=rustc
version=1.95.0
release=1

source=(https://static.rust-lang.org/dist/$name-$version-src.tar.gz)  

prepare() {
  cd $name-$version-src

  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 = 148671

[llvm]
# 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 = "for Great-OS dev"

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

# Only install these extended tools. Cargo, clippy, rustdoc, and rustfmt
# are installed by a default rustup installation, and rust-src is needed
# to build the Rust code in Linux kernel (in case you need such a kernel
# feature).
tools = ["cargo", "clippy", "rustdoc", "rustfmt", "src"]

[install]
prefix = "/usr"
docdir = "share/doc/rustc-$version"

[rust]
channel = "stable"

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

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

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

[target.i686-unknown-linux-gnu]
llvm-config = "/usr/bin/llvm-config"
EOF

wget https://github.com/lfs-book/rust-openssl/archive/v0.10.78/rust-openssl-v0.10.78.tar.gz
tar xf rust-openssl-v0.10.78.tar.gz

cat >> src/tools/cargo/Cargo.toml << EOF
[patch.crates-io]
openssl = { path = "../../../rust-openssl-0.10.78/openssl" }
openssl-sys = { path = "../../../rust-openssl-0.10.78/openssl-sys" }
EOF

sed -ri src/tools/cargo/Cargo.lock \
    -e '/name = "openssl-sys"/,/^$/{/source|checksum/d;s/0.9.111/0.9.114/}' \
    -e '/name = "openssl"/,/^$/{/source|checksum/d;s/0.10.75/0.10.78/}'     \
    -e '/name = "openssl-macros"/,/^$/{/source|checksum/d}'
}

build() {
  cd $name-$version-src

  export LIBSSH2_SYS_USE_PKG_CONFIG=1
  export LIBSQLITE3_SYS_USE_PKG_CONFIG=1

  python3 ./x.py build

  DESTDIR=$PKG  python3 ./x.py install
}
