# -*- 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           github 1.0
PortGroup           makefile 1.0

github.setup        crystal-lang crystal 1.16.2
github.tarball_from archive
revision            0
categories          lang
supported_archs     x86_64 arm64
license             Apache-2
maintainers         {makr @mohd-akram} openmaintainer

description         Crystal programming language

long_description    Crystal is a fast, compiled programming language with a \
                    Ruby-inspired syntax.

homepage            https://crystal-lang.org

set llvm_version    20

depends_build       path:bin/pkg-config:pkgconfig

depends_lib         port:boehmgc \
                    port:libffi \
                    port:libiconv \
                    port:llvm-${llvm_version} \
                    port:pcre2

# libraries used by the standard library
# pkg-config is used when compiling user programs
depends_run         port:gmp \
                    port:libevent \
                    port:libxml2 \
                    port:libyaml \
                    path:lib/libssl.dylib:openssl \
                    port:zlib \
                    path:bin/pkg-config:pkgconfig

# dyld: Symbol not found: ____chkstk_darwin
# bootstrap binaries use the libpthread
# functions that were added in macOS 10.15
platforms           {darwin >= 19}

# Use a prebuilt version of Crystal to build itself
set cr_ver          ${version}
set cr_full_ver     ${cr_ver}-1

master_sites-append https://github.com/crystal-lang/${name}/releases/download/${cr_ver}/:bootstrap
distfiles-append    ${name}-${cr_full_ver}-${os.platform}-universal${extract.suffix}:bootstrap

checksums           ${distname}${extract.suffix} \
                    rmd160  a6c7f3264a8630f224c8bc97a7f2d78d0b585ef7 \
                    sha256  4f032761a4018c6fbcf1649b866217d9eb2f3139599c70fcb6dac83714839c5e \
                    size    3719814 \
                    ${name}-${cr_full_ver}-${os.platform}-universal${extract.suffix} \
                    rmd160  d21f750d1c9261c7f196c0be4f34bb2893602f36 \
                    sha256  cfef4c105e6b9e91e5d24024bb2cb3bf66eb1790bb92425dfc890080c696b6b6 \
                    size    59310022

patchfiles          patch-static.diff

post-patch {
    file copy -force ${worksrcpath}/src/lib_c/x86_64-openbsd/c/iconv.cr \
        ${worksrcpath}/src/lib_c/aarch64-darwin/c/iconv.cr
    file copy -force ${worksrcpath}/src/lib_c/x86_64-openbsd/c/iconv.cr \
        ${worksrcpath}/src/lib_c/x86_64-darwin/c/iconv.cr
}

configure.ldflags-append    -Wl,-rpath,${prefix}/libexec/llvm-${llvm_version}/lib

set llvm_config     LLVM_CONFIG=llvm-config-mp-${llvm_version}

compiler.cxx_standard  2014

build.args          release=1 interpreter=1 FLAGS=--no-debug \
                    CRYSTAL_CONFIG_LIBRARY_PATH=${prefix}/lib

build.env           ${llvm_config} \
                    CRYSTAL=${workpath}/${name}-${cr_full_ver}/bin/${name}

post-build {
    system -W ${worksrcpath} "${llvm_config} ${build.cmd} docs"
}

destroot.target-append  install_docs
destroot.args       -o docs
destroot.env        ${llvm_config}

# https://github.com/crystal-lang/crystal/issues/15684
post-destroot {
    move ${destroot}${prefix}/share/docs ${destroot}${prefix}/share/doc/${name}
    move ${destroot}${prefix}/share/examples \
        ${destroot}${prefix}/share/doc/${name}
}

test.run            yes
test.target         spec
test.env            ${llvm_config}
