# -*- 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           xcode_workaround 1.0
PortGroup           github 1.0
PortGroup           legacysupport 1.1
PortGroup           makefile 1.0
PortGroup           muniversal 1.1

set branch          2.1
set version_date    2026-03-30T18:43:18Z
github.setup        LuaJIT LuaJIT 18b087cd2cd4ddc4a79782bf155383a689d5093d
revision            0
checksums           rmd160  984d6a68e473b552e14fd7d789a79d09319b406a \
                    sha256  88a592afa9907d6b0c6e1e7ac9b39982622e3ca086f0646d4ea89b0e4e81f093 \
                    size    1094063

set version_date_s  [clock scan "${version_date}" -format {%Y-%m-%dT%H:%M:%SZ} -timezone :UTC]
version             ${branch}.${version_date_s}

name                luajit
categories          lang
license             MIT
maintainers         nomaintainer

description         a Just-In-Time Compiler for Lua
long_description    LuaJIT is a Just-In-Time Compiler for the Lua programming language.
homepage            https://luajit.org
github.tarball_from archive

conflicts           luajit-openresty

patchfiles          powerpc.patch

post-patch {
    reinplace "s|/usr/local|${prefix}|" ${worksrcpath}/etc/luajit.pc \
                                        ${worksrcpath}/src/luaconf.h \
                                        ${worksrcpath}/src/Makefile

    # Remove -march=i686 from CCOPT_x86
    reinplace -E {s|-march=[a-z0-9_]+||g} ${worksrcpath}/src/Makefile

    # The build process normally expects git and a .git directory to establish
    # the version but it includes a fallback for a `.relver` file
    set relver      [open "${worksrcpath}/.relver" w 0644]
    puts ${relver}  "${version_date_s}"
    close ${relver}
}

compiler.thread_local_storage yes

# crashes at runtime with these compilers
# https://trac.macports.org/ticket/45343
compiler.blacklist  {clang < 700} macports-clang-3.3 macports-clang-3.4

## Fix universal builds, see:
# - https://www.freelists.org/post/luajit/Building-universal-libraries-x86-64-and-arm64-for-osx,4
# - https://git.openembedded.org/meta-openembedded/plain/meta-oe/recipes-devtools/luajit/luajit_git.bb
# Clean automatically set options that apply to both host and target architectures (which we do not want)
build.post_args-append  CCOPT= CCOPT_x86= TARGET_STRIP='@:'
foreach arch ${muniversal.architectures} {
    build.args.${arch}-append  TARGET_FLAGS+="[muniversal::get_archflag cc ${arch}]"
    build.post_args.${arch}-append \
        CFLAGS="${configure.cflags} [configure.cflags.${arch}]" \
        LDFLAGS="${configure.ldflags} [configure.ldflags.${arch}]"
}

# changes to compiler flags must be made before `CFLAGS=...`
xcode_workaround.type append_to_compiler_flags

configure.cflags-append -DLUAJIT_ENABLE_LUA52COMPAT
makefile.override   CC CFLAGS LDFLAGS PREFIX

build.target        amalg
build.args-append   Q=

post-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/share/doc
    copy ${worksrcpath}/doc/ ${destroot}${prefix}/share/doc/${name}
}

livecheck.type      regex
livecheck.url       ${github.homepage}/commits/v${branch}.atom
livecheck.version   ${version_date}
livecheck.regex     {<updated>([:TZ0-9-]+)</updated>}
