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

github.setup            lcompilers lpython 0.20.0 v
revision                0
categories              lang python
maintainers             nomaintainer
license                 BSD
description             High-performance typed Python compiler
long_description        LPython aggressively optimizes type-annotated \
                        Python code. It has several backends, including LLVM, \
                        C, C++ and WASM. LPython’s primary tenet is speed.
homepage                https://lpython.org

checksums               rmd160  01cdab5767af01d017339f79355c34e9ab2ec8d7 \
                        sha256  741fb64ddf70528eb713103d5b6e2068344b5768b546b0c21145d5a1507635a0 \
                        size    1196647
github.tarball_from     archive

set port_libfmt         libfmt10
cmake.prefix_path-append \
                        ${prefix}/lib/${port_libfmt}/cmake

depends_build-append    port:bash \
                        port:bison \
                        port:re2c
depends_lib-append      path:lib/pkgconfig/RapidJSON.pc:rapidjson \
                        port:zlib \
                        port:${port_libfmt}

if {${os.platform} eq "darwin" \
    && ${configure.build_arch} in [list ppc ppc64]} {
    set py_ver          3.11
} else {
    set py_ver          3.12
}
set py_ver_nodot        [string map {. {}} ${py_ver}]
depends_lib-append      port:python${py_ver_nodot}

patchfiles-append       0001-stacktrace.cpp-include-syslimits.h-for-PATH_MAX.patch \
                        0002-UserOverride.cmake-fix-native-optflag-for-PowerPC.patch \
                        0003-Install-libs-into-a-sane-location.patch \
                        0004-parser_stype.h-allow-building-for-ppc.patch \
                        0005-patch-build0.sh.patch \
                        0006-patch-version.patch

configure.python        ${prefix}/bin/python${py_ver}

post-patch {
    reinplace "s|@VERSION@|${version}|" ${worksrcpath}/ci/version.sh
    reinplace "s|@PYTHON@|${configure.python}|g" ${worksrcpath}/build0.sh
    reinplace "s|/usr/bin/env python|${configure.python}|" ${worksrcpath}/run_tests.py
}

compiler.cxx_standard   2017

# TODO: add support for LLVM for x86 and aarch64, as it is done for lfortran,
# once it gets fixed: https://github.com/lcompilers/lpython/issues/2462

configure.args-append   -DLPYTHON_BUILD_ALL=YES \
                        -DLPYTHON_STATIC_BIN=NO \
                        -DWITH_FMT=YES \
                        -DWITH_INTRINSIC_MODULES=NO \
                        -DWITH_JSON=YES \
                        -DWITH_LCOMPILERS_FAST_ALLOC=YES \
                        -DWITH_LLVM=NO \
                        -DWITH_MACHO=YES \
                        -DWITH_RUNTIME_LIBRARY=YES \
                        -DWITH_STACKTRACE=NO \
                        -DWITH_UNWIND=NO \
                        -DWITH_ZLIB=YES

pre-configure {
    system -W ${worksrcpath} "sh ./build0.sh"
}

# CMake Error at CMakeLists.txt:23 (message): CMAKE_BUILD_TYPE
# must be one of: Debug, Release (current value: 'MacPorts')
cmake.build_type        Release

platform powerpc {
    # Since no buildbot for PowerPC, we use -mtune=native,
    # and just in case force a local build:
    archive_sites
}

test.run                yes
