# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           cargo   1.0
PortGroup           github  1.0

github.setup        PerryTS perry 0.5.1665 v
github.tarball_from archive
revision            0

homepage            https://perryts.com/

categories          lang devel
installs_libs       no
license             MIT
maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

description         Native TypeScript compiler written in Rust
long_description    Perry compiles TypeScript directly to native \
                    executables using SWC and LLVM, without requiring \
                    Node.js, Electron, or a browser engine at runtime.

checksums           ${distname}${extract.suffix} \
                    rmd160  d11d63ff15414c7e40158e08894c4b795253850e \
                    sha256  079dfdf68aa5051c5ad072484cf6c1bf37240ed0ad7e50e7a2f1f2ff8df5e2e7 \
                    size    246497814

# Perry generates code through inkwell, which links LLVM 22 by way
# of llvm-sys 221.
set llvm_version    22
# llvm-sys links LLVM's system libraries into perry directly.
depends_lib-append  port:libffi \
                    port:libiconv \
                    port:libxml2 \
                    port:llvm-${llvm_version} \
                    port:zlib
build.env-append    LLVM_SYS_${llvm_version}1_PREFIX=${prefix}/libexec/llvm-${llvm_version}

# Since 0.5.1665 perry builds with a pinned nightly: perry-runtime uses
# unstable features, and .cargo/config.toml passes -Z use-sync-unwind and
# rebuilds std from source (build-std). RUSTC_BOOTSTRAP=1 unlocks these on
# MacPorts' stable rustc, as devel/bun does, and rust-src supplies the
# library sources. Fetch crates during the build so the standard library
# dependencies match the installed rust-src when Rust is updated.
depends_build-append \
                    port:rust-src
build.env-append    RUSTC_BOOTSTRAP=1

# The Rust standard library dependency versions follow rust-src.
cargo.offline_cmd

# libz-ng-sys builds zlib-ng with cmake. perry-runtime compiles an LLVM IR
# file (perry_trap.ll) with opaque pointers, which older Xcode clang (e.g.
# on macOS 12) rejects; use the clang matching the LLVM perry links.
depends_build-append \
                    port:cmake \
                    port:clang-${llvm_version}
build.env-append    PERRY_TRAP_CLANG=${prefix}/bin/clang-mp-${llvm_version}

# The runtime and stdlib archives perry links programs against come from
# their -static wrapper crates.
build.args-append   --package ${name} \
                    --package ${name}-runtime-static \
                    --package ${name}-stdlib-static

destroot {
    xinstall -m 0755 \
        ${worksrcpath}/target/[cargo.rust_platform]/release/${name} \
        ${destroot}${prefix}/bin/

    xinstall -m 0644 \
        ${worksrcpath}/target/[cargo.rust_platform]/release/lib${name}_runtime.a \
        ${worksrcpath}/target/[cargo.rust_platform]/release/lib${name}_stdlib.a \
        ${destroot}${prefix}/lib/
}

notes               "Perry invokes clang when compiling TypeScript. The \
                    Xcode command line tools provide /usr/bin/clang; to use \
                    a MacPorts LLVM toolchain instead, set PERRY_LLVM_CLANG \
                    to the desired clang executable."

github.livecheck.regex \
                    {([0-9.]+)}

