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

PortSystem                  1.0
PortGroup                   github 1.0

github.setup                axelang axe 0.0.17 v
revision                    0
categories                  lang
maintainers                 {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
license                     GPL-3

description                 Axe programming language
long_description            {*}${description}. This port is experimental.
homepage                    https://axelang.org

# 2026.03.18
set boot_hash               69ced228e3c3cbf0ecd5a63a3415e60bc5336c74
master_sites                https://github.com/axelang/${name}/archive/v${version}/:main \
                            https://github.com/axelang/${name}-bootstrap/archive/${boot_hash}/:boot
distfiles                   v${version}${extract.suffix}:main \
                            axe-bootstrap-${boot_hash}${extract.suffix}:boot
checksums                   v${version}${extract.suffix} \
                            rmd160  f320cb6f5eabb2b23b94f2c1e7a16261d5da90a9 \
                            sha256  e29fe353fdec4434efae6c62d6846a96aca1c8ce048fea1ded81461c729adda8 \
                            size    8129319 \
                            ${name}-bootstrap-${boot_hash}${extract.suffix} \
                            rmd160  2e4152530627881d260e5808aedec046f07a28b6 \
                            sha256  08e4b08c7534e0c1bea1455c9749d95f4a62e42aedc3ea6036d39002c11e02ce \
                            size    133562

# TODO: perhaps rather patch axc.c and builds.axe to point to correct libs directly. 
# https://github.com/axelang/axe/issues/5
post-extract {
    move ${workpath}/${name}-${version} ${workpath}/${name}
    move ${workpath}/axe-bootstrap-${boot_hash} ${workpath}/axe-bootstrap

    delete -force ${workpath}/axe/source/compiler/external/lib-macos
    ln -s ${prefix}/lib ${workpath}/axe/source/compiler/external/lib-macos
}

depends_lib-append          port:curl \
                            port:pcre \
                            port:yyjson \
                            port:zlib

patch.dir                   ${workpath}

# https://github.com/axelang/axe/issues/4
post-patch {
    reinplace "s|\"cc\"|\"${configure.cc}\"|g" \
                            ${workpath}/${name}/source/compiler/builds.axe \
                            ${workpath}/${name}-bootstrap/axc.c
    reinplace "s|-Wno-everything|-Wno-error=int-conversion -Wno-error=incompatible-pointer-types -Wno-error=implicit-int -Wno-error=return-mismatch|g" \
                            ${workpath}/${name}/source/compiler/builds.axe \
                            ${workpath}/${name}-bootstrap/axc.c
}

# At least for type redefinitions:
compiler.c_standard         2011

universal_variant           no

use_configure               no

pre-build {
    # https://github.com/axelang/axe-bootstrap/issues/1
    configure.cflags-append -Wno-error=int-conversion -Wno-error=incompatible-pointer-types -Wno-error=implicit-int

    system -W ${workpath}/axe-bootstrap "${configure.cc} axc.c -o axe ${configure.cflags}"

    # https://github.com/axelang/axe/commit/6dc6fac82f79730dc0ceaa48b76887f6ffb778bc
    copy ${workpath}/${name}/source/compiler/std ${workpath}/axe-bootstrap/std
}

build {
    # FIXME: 32-bit builds are broken: https://github.com/axelang/axe/issues/9
    # As a temporary hack, we just use the bootstrap compiler.
    if {${configure.build_arch} in [list i386 ppc]} {
        copy ${workpath}/axe-bootstrap/axe ${workpath}/${name}/source/compiler/
    } else {
        system -W ${workpath}/${name}/source/compiler "${workpath}/axe-bootstrap/axe axc -o axe -I${prefix}/include -L${prefix}/lib --loud --release"
    }
}

destroot {
    set axe_root ${prefix}/libexec/${name}
    xinstall -d ${destroot}${axe_root}
    copy ${workpath}/${name}/source/compiler/axe ${destroot}${axe_root}/
    copy ${workpath}/${name}/source/compiler/std ${destroot}${axe_root}/
}
