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

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

github.setup        vlang v 0.4.3
github.tarball_from archive
revision            0

name                vlang
# Remove weekly from version number
#version             [string range ${version} 7 end]
homepage            https://vlang.io/

description         Simple, fast, safe, compiled language for developing maintainable software
long_description    {*}${description}. Compiles itself in <1s with zero library dependencies. This port \
                    provides weekly builds of Vlang.

license             MIT
categories          lang
maintainers         {harens @harens} openmaintainer

# Install the V compiler as a separate distfile.
# Based on https://github.com/macports/macports-ports/blob/master/games/minetest/Portfile
set vc_commit       5e691a82c01957870b451e06216a9fb3a4e83a18
set main_distfile   ${distfiles}
set vc_distfile     ${vc_commit}${extract.suffix}
set vc_mastersite   https://github.com/vlang/vc/archive

distfiles           ${main_distfile}:main \
                    ${vc_distfile}:vc

master_sites        ${github.master_sites}:main \
                    ${vc_mastersite}:vc

checksums           ${main_distfile} \
                    rmd160  d6ba79bb2034084de8b5c94f1cc1217ec3d80cc0 \
                    sha256  79bbe201fe6f7b98b2f80e405ce1d914b4d28931372bf7f9d30cf9b356e4d4f1 \
                    size    7110780 \
                    ${vc_distfile} \
                    rmd160  84185a2c2dc90e0aeb7ae6b70152f557f46472a5 \
                    sha256  2e8612b9328b66b80ebac41176c7df26c5980ad37adf3ef20bbcaa5f0195c265 \
                    size    1798998

depends_lib         port:boehmgc

# https://trac.macports.org/ticket/64913
if {${build_arch} in "ppc ppc64"} {
    configure.ldflags-append -latomic
}

# Path to V compiler
set vc_path ${worksrcpath}/vc

pre-patch {
    # Allows us to patch vc
    move ${workpath}/vc-${vc_commit} ${vc_path}
}

# ./thirdparty/stdatomic/nix/atomic.h:12:10: fatal error: 'stdatomic.h' file not found
# Based on cmus port: compatible compilers are Xcode > Xcode 7, clang > 3.7, and gcc > 4.8
compiler.c_standard 2011
compiler.blacklist-append {clang < 700} macports-clang-3.*

# Stop V from looking in Homebrew directories, and remove hardcoded MacPorts prefix.
# V has built-in font rendering, but users can optionally use freetype.
patchfiles          patch-boehmgc-prefix.diff \
                    patch-freetype2-prefix.diff \
                    patch-min-macos.diff \
                    patch-openssl-prefix.diff \
                    patch-pkgconfig-prefix.diff \
                    patch-postgresql-prefix.diff \
                    patch-ptrace.diff

# See https://github.com/vlang/v/issues/6605
# Header file <sys/errno.h>, needed for module `sync` was not found
if { ${os.major} <= [option legacysupport.newest_darwin_requires_legacy] } {
    # Use LegacySupport with the time module
    patchfiles-append patch-clock-gettime.diff \
                      patch-legacysupport-makefile.diff

    # Header file <sys/errno.h>, needed for module `sync` was not found
    if { ${os.major} <= 9 } {
        patchfiles-append patch-errno-header.diff
    }
}

post-patch {
    # https://github.com/vlang/v/issues/10796 - Set correct deployment target.
    reinplace "s|@MACOS_VERSION@|${macosx_deployment_target}|" ${worksrcpath}/vlib/v/pref/pref.v
    reinplace "s|macosx_version_min = _SLIT(\"10.7\")|macosx_version_min = _SLIT(\"${macosx_deployment_target}\")|" \
    ${vc_path}/v.c

    # Disable vlang self update feature.
    copy -force ${filespath}/vup.v ${worksrcpath}/cmd/tools

    foreach f [list net/openssl/openssl.c.v fontstash/a_d_use_freetype.v db/pg/pg.c.v v/pkgconfig/pkgconfig.v builtin/builtin_d_gcboehm.c.v] {
        reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" vlib/${f}
    }

    if { ${os.major} <= [option legacysupport.newest_darwin_requires_legacy] } {
        reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/vlib/time/time_nix.c.v
    }
}

# -cg, -showcc and -show-c-output allows for better error debugging
# local prevents fetching the V compiler during the build
build.args-append   local=true \
                    VFLAGS="-v -cg -showcc -show-c-output -prod -cc ${configure.cc} -cflags [shellescape ${configure.cflags}]" \
                    VC=${vc_path}

# See https://trac.macports.org/ticket/64913
# In case _developer group doesn't exist
add_users vlang group=_developer

destroot {
    set library_path ${destroot}${prefix}/lib/${name}
    set examples_path ${destroot}${prefix}/share/examples

    xinstall -d ${examples_path}
    move ${worksrcpath}/examples ${examples_path}/${name}

    xinstall -d ${library_path}
    foreach f {cmd thirdparty v v.mod vlib} {
        move ${worksrcpath}/${f} ${library_path}/${f}
    }

    ln -s ${prefix}/lib/${name}/v ${destroot}${prefix}/bin

    # Vlang compiles the subcommands in the tools dir on first runtime usage (e.g. v doctor).
    # Allow running Vlang to those in the _developer group, rather than running sudo each time.
    # See https://github.com/vlang/v/issues/10324

    # Majority of subcommands only require ${library_path}/cmd/tools to be writable but v self requires
    # all of ${library_path}

    fs-traverse item ${library_path} {
        file attributes ${item} -group _developer -permissions g+w
    }
}

# Delete leftover subcommands compiled during runtime
post-deactivate {
    delete ${prefix}/lib/${name}
}

# Based on various Android ports - don't make files world-writable.
# e.g. https://github.com/macports/macports-ports/blob/master/java/android/Portfile
notes "
The Vlang library directory is group _developer writable. You need to be a member of the
_developer group to use Vlang. If you are not, run:

sudo dscl . append /Groups/_developer GroupMembership <username>

Optionally, to use the V PostgreSQL module, install PostgreSQL and set the POSTGRESQL_VERSION
environment variable. For PostgreSQL 14:

export POSTGRESQL_VERSION=14
"

# Check for weekly versions only
#livecheck.version ${version}
#livecheck.regex weekly.(\[0-9.\]+\[0-9\])
