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

name                gn-devel
version             20200529
revision            1
categories          devel
platforms           darwin
license             BSD
maintainers         nomaintainer
description         meta-build system for Ninja
long_description    GN is a meta-build system that generates build files for Ninja.
homepage            https://gn.googlesource.com/gn

subport qt5-qtwebengine-gn {
    set hyphen_version 2022-12-09
    version         [string map {"-" ""} ${hyphen_version}]
    revision        0
    maintainers     {gmx.us:chrischavez @chrstphrchvz} openmaintainer
    description     {*}${description} (for building qtwebengine)
    long_description {*}${long_description} This port is for building qt5-qtwebengine \
                    as it contains Qt-specific additions to GN.
}

if {${subport} eq ${name}} {
    PortGroup       compiler_blacklist_versions 1.0

    # https://trac.macports.org/ticket/68164
    platforms       {darwin > 10}

    # git repository information is used for `gn --version` output
    fetch.type      git
    git.url         ${homepage}
    git.branch      b175fa5d6ae1640742aad593185780a08f6ba224

    compiler.cxx_standard 2017
    compiler.blacklist-append {clang < 1001.0.46.4}

    destroot {
        copy ${worksrcpath}/out/gn ${destroot}${prefix}/bin
    }

    test.run        yes
} elseif {${subport} eq "qt5-qtwebengine-gn"} {
    PortGroup       compiler_wrapper 1.0

    # Unless this port is useful for building earlier qtwebengine versions on older macOS...
    if {${os.platform} eq "darwin" && ${os.major} < 17} {
        known_fail  yes
        pre-fetch {
            ui_error "${subport} is for building qt5-qtwebengine, which requires macOS 10.13 or later."
            return -code error "unsupported macOS version"
        }
    }

    # Fetch archives generated by GitHub mirrors
    # Use same ref as qt5-qtwebengine to reuse distfile,
    # even though bundled GN has not been updated since 5.15.10 (2082566)
    # See https://github.com/qt/qtwebengine-chromium/commits/87-based/gn
    set qtwebengine_chromium_ref 85337c28bae7d7d2c79282c4f4c92435cd7649b8
    fetch.type      standard
    dist_subdir     qt5
    distname        qtwebengine-chromium-${qtwebengine_chromium_ref}

    master_sites    https://github.com/qt/qtwebengine-chromium/archive/${qtwebengine_chromium_ref}

    checksums       rmd160  e9c2365e4dab679055f12dd6c1cb04c605fcf86a \
                    sha256  13451d9894ba3cb9e5d15b87eededfc0ced19f297e39086d90c7abfe035eab9b \
                    size    469456308

    # Just extract gn; do not extract all of chromium (~2GB) or ninja
    worksrcdir      ${distname}/gn
    extract.post_args-append ${worksrcdir}

    compiler.cxx_standard 2014

    # see https://github.com/qt/qtwebengine/blob/v5.15.8-lts/src/buildtools/gn.pro#L21
    if {${configure.sdkroot} ne ""} {
        configure.post_args-append --isysroot ${configure.sdkroot}
    }
    pre-configure {
        configure.post_args-append \
            --no-last-commit-position \
            --use-lto \
            --cc [compwrap::wrap_compiler cc] \
            --cxx [compwrap::wrap_compiler cxx] \
            --ld [compwrap::wrap_compiler cxx]
    }

    # Do not conflict with other gn binaries:
    # add suffix and install to Qt5-specific location not in PATH
    post-build {
        move ${worksrcpath}/out/gn ${worksrcpath}/out/gn-qtwebengine5.15
    }
    destroot {
        # see qt_bins_dir definition in PortGroup qt5 1.0
        set qt_bins_dir ${prefix}/libexec/qt5/bin
        xinstall -d -m 0755 ${destroot}${qt_bins_dir}
        copy ${worksrcpath}/out/gn-qtwebengine5.15 ${destroot}${qt_bins_dir}
    }

    # Tests currently do not compile: some misplaced files and compilation errors
    # (reported: https://bugreports.qt.io/browse/QTBUG-99682), as well as dependency on googletest
    # Does not affect building qt5-qtwebengine
    test.run        no

    livecheck.type  none
}

patchfiles-append   fix-invalid-escape-sequence.patch

set python_branch   3.12
set python_version  [string map {"." ""} ${python_branch}]

depends_build-append \
                    port:ninja \
                    port:python${python_version}

use_configure       yes
configure.cmd       ${prefix}/bin/python${python_branch}
configure.args      build/gen.py
configure.pre_args

build.dir           ${worksrcpath}/out
build.cmd           ${prefix}/bin/ninja
build.target        gn
# Build verbosely
build.args          -v

test.target         gn_unittests
test.args           {*}${build.args}
test.post_args      && ${worksrcpath}/out/gn_unittests