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

github.setup        cglogic gn 2234 v
revision            0
categories          devel
license             BSD
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Gn meta build framework – standalone version
long_description    GN is a meta-build system that generates build files for Ninja. \
                    GN is currently used as the build system for Chromium, Fuchsia and related projects.
homepage            https://gn.googlesource.com/gn
checksums           rmd160  f1141ebc1bee5818a90d0b029f9b1c77ab557c35 \
                    sha256  a738b5971837e3ee0456406e429b1365745285a24edc36660a15e3bf76bb1246 \
                    size    1096906
github.tarball_from archive

set py_ver          3.13
set py_ver_nodot    [string map {. {}} ${py_ver}]
depends_build-append \
                    port:ninja \
                    port:python${py_ver_nodot}
depends_run-append  port:python${py_ver_nodot}

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

compiler.cxx_standard 2017

patchfiles          0001-gen.py-do-not-force-random-macOS-version.patch \
                    0002-mac-scoped-do-not-use-objc-types-when-unsupported.patch \
                    0003-Fix-undefined-version-due-to-configure-not-honoring-.patch \
                    0004-build_config.h-sys_info.cc-args.cc-fix-PowerPC-defin.patch \
                    0005-semaphore-use-libdispatch-only-where-it-actually-exi.patch

post-patch {
    reinplace "s|python3|${configure.python}|" ${worksrcpath}/src/gn/exec_process_unittest.cc
    reinplace "s|/usr/bin/env python3|${configure.python}|" ${worksrcpath}/build/full_test.py ${worksrcpath}/build/gen.py
    reinplace "s|@DEPLOYMENT_TARGET@|${macosx_deployment_target}|" ${worksrcpath}/build/gen.py
    reinplace "s|@VERSION@|${version}|" ${worksrcpath}/src/gn/gn_main.cc ${worksrcpath}/src/gn/scope_per_file_provider.cc
}

configure.cmd       ${configure.python} ./build/gen.py

# --prefix is not accepted by the configure script.
configure.pre_args-delete \
                    --prefix=${prefix}

# You would expect this to be honored, but it is not, and the sources include non-existent header unconditionally.
# It is fixed by a patch above.
configure.args      --no-last-commit-position

# Upstream seems to support minimum 10.9. Build fails with -Werror on older systems:
# desc_builder.cc: error: redundant move in return statement [-Werror=redundant-move]
if {${os.platform} eq "darwin" && ${os.major} < 13} {
    configure.args-append \
                    --allow-warnings
}

build.cmd           ${prefix}/bin/ninja
build.dir           ${worksrcpath}/out
build.target
build.env-append    CC=${configure.cc} CXX=${configure.cxx}

destroot {
    copy ${worksrcpath}/out/${name} ${destroot}${prefix}/bin/
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} AUTHORS LICENSE OWNERS README.md ${destroot}${docdir}
    copy ${worksrcpath}/docs ${destroot}${docdir}
}

test.run            yes
test.cmd            ${worksrcpath}/out/gn_unittests
test.target
