# -*- 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               codeberg 1.0

set base_name           zig
codeberg.setup          ziglang ${base_name}-bootstrap 0.16.0
revision                0
categories              lang
license                 MIT
maintainers             {i0ntempest @i0ntempest} openmaintainer
homepage                https://ziglang.org/

# https://ziglang.org/download/0.16.0/release-notes.html#OS-Version-Requirements
platforms               {darwin >= 22}

description             Zig programming language (bootstrap build with no runtime dependencies)
long_description        Zig is a general-purpose programming language designed for robustness, \
                        optimality, and maintainability. This is the bootstrap build version with \
                        no external runtime dependencies.

checksums               rmd160  474f5ba6be808a5c6413727e9682cd25dffeeb2f \
                        sha256  a3d5867c25cb2046568b12e373219c35e49d02a7fabd21a4e8acc527fc4521c4 \
                        size    84043297

# sterilize MacPorts build environment
compiler.cpath
compiler.library_path

configure.cxx_stdlib

configure.cflags
configure.cxxflags
configure.cppflags
configure.optflags
configure.ldflags

configure.universal_cflags
configure.universal_cxxflags
configure.universal_cppflags
configure.universal_ldflags
configure.universal_args

configure.ccache        no
configure.distcc        no

# only supply cmake; use make, python3, and clang(++) from the system/CLT.
compiler.whitelist      clang
depends_build-append    port:cmake

if {${build_arch} eq "x86_64"} {
    set arch_filename   ${build_arch}
} else {
    # file name is "aarch64" for arm64
    set arch_filename   aarch64
}

extract.rename          yes

patchfiles-append       patch-parallel.diff
post-patch {
    reinplace -E "s|^(\[\[:space:\]\]*)cmake(\[\[:space:\]\]+)|\\1${prefix}/bin/cmake\\2|" ${worksrcpath}/build
    # macOS github runner has only 8GB of memory
    reinplace "s|max_rss = 8_000_000_000|max_rss = 7_500_000_000|g" ${worksrcpath}/zig/build.zig
}

set abi                 none
set mcpu                baseline
use_configure           no
build.env               PATH=/usr/bin:/bin:/usr/sbin:/sbin \
                        CC=${configure.cc} \
                        CXX=${configure.cxx}
build.cmd               ./build
build.target            ${arch_filename}-macos-${abi}
build.args              ${mcpu} ${build.jobs}

destroot {
    xinstall -d ${destroot}${prefix}/libexec/${name}
    xinstall -m 0755 ${worksrcpath}/out/${base_name}-${arch_filename}-macos-${abi}-${mcpu}/${base_name} ${destroot}${prefix}/libexec/${name}/
    copy ${worksrcpath}/out/${base_name}-${arch_filename}-macos-${abi}-${mcpu}/lib ${destroot}${prefix}/libexec/${name}/
    set wrapper ${destroot}${prefix}/bin/${name}
    set fh [open ${wrapper} w]
    puts ${fh} "#!/bin/sh"
    puts ${fh} "export ZIG_LIB_DIR=${prefix}/libexec/${name}/lib"
    puts ${fh} "exec ${prefix}/libexec/${name}/${base_name} \"\$@\""
    close ${fh}
    file attributes ${wrapper} -permissions 0755
}
