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

name            lib2geom
conflicts       lib2geom-devel
set my_name     lib2geom
gitlab.setup    inkscape ${my_name} 1.3
revision        2

categories      graphics
license         {LGPL-2.1 MPL-1.1}
maintainers     {devans @dbevans} {mascguy @mascguy} openmaintainer

description     2Geom is a computational geometry library intended for use with \
                2D vector graphics.
long_description \
                ${description} It concentrates on high-level algorithms, such as \
                computing the length of a curve or Boolean operations on paths.

checksums       rmd160  c4b55dae72b49fb5dfd687f747b0a2227bbb5d9d \
                sha256  bd07f2a1322fcad16a55a1901207faa55dffdecebc56a00eabd4575f62e043c9 \
                size    1275751

dist_subdir     ${my_name}

# this port only uses boost headers during build
# and does not link with any boost libraries
# no need to rebuild due to boost library ABI changes
boost.depends_type build
boost.version      1.81

# Patch to expose formal options for: Cython, GTK3
# Otherwise, those will be opportunistically pulled in, which we don't want.
patchfiles-append \
                patch-cmake-options.diff

depends_build-append \
                port:gettext \
                port:pkgconfig

depends_lib-append \
                path:lib/pkgconfig/cairo.pc:cairo \
                port:double-conversion \
                port:gettext-runtime \
                path:lib/pkgconfig/glib-2.0.pc:glib2 \
                port:gsl \
                port:ragel

# C++17 is required
compiler.cxx_standard 2017

#--------------------------------------------------------------------------------------------------
# Xcode Clang 10.x is based on LLVM 6.x. And it does not include an upstream fix, for the
# following (valid) scenario involving templates:
#
#   MyTemplate(some_arg);
#
# It can be sidestepped, by explicitly declaring a variable of that template type:
#
#   MyTemplate myVar(some_arg);
#
# But since this is an LLVM 6.x bug - and one that was ultimately fixed, after Xcode 10.x was
# released - it arguably shouldn't be necessary.
#
# The specific compilation error for this scenario is:
#   error: cannot use parentheses when declaring variable with deduced class template specialization type
#
# See: https://trac.macports.org/ticket/68036
#--------------------------------------------------------------------------------------------------
compiler.blacklist-append \
                {clang >= 1000 < 1100}

# Need no-invalid-constexpr for v1.3. Remove if/when fixed in future.
if { [string match *clang* ${configure.compiler}] } {
    configure.cxxflags-append \
                -Wno-invalid-constexpr \
                -Wno-error=unknown-warning-option \
                -Wno-unknown-warning-option
}

configure.args-append \
                -D2GEOM_BUILD_SHARED:BOOL=ON \
                -D2GEOM_TESTING:BOOL=OFF \
                -D2GEOM_USE_GPL_CODE:BOOL=OFF \
                -DWITH_CYTHON:BOOL=OFF \
                -DWITH_GTK3:BOOL=OFF

# Our Inkscape port, v1.2.2 - revisions 0 and 1 - install a public copy of lib2geom.
# So we must forcibly deactivate Inkscape first, and prompt the user to upgrade.
# This logic added on 2023-04-27; leave in place for at least 6 months.
pre-activate {
    set inkscape_conflict_ver_min "1.2.2_0"
    set inkscape_conflict_ver_max "1.2.2_1"

    if { ![catch {set inkscape_ver_info [lindex [registry_active inkscape] 0]}] } {
        set inkscape_ver [lindex ${inkscape_ver_info} 1]_[lindex ${inkscape_ver_info} 2]
        ui_info "Inkscape active version: ${inkscape_ver}"

        if { [vercmp ${inkscape_ver} ${inkscape_conflict_ver_min}] >= 0
            && [vercmp ${inkscape_ver} ${inkscape_conflict_ver_max}] <= 0 } {

            ui_msg "NOTE: Your currently-installed version of Inkscape conflicts."
            ui_msg "  Manually run 'sudo port -N upgrade inkscape' to upgrade to a non-conflicting version."

            registry_deactivate_composite inkscape "" [list ports_nodepcheck 1]
        } else {
            ui_info "Inkscape version doesn't conflict"
        }
    } else {
        ui_info "Inkscape not installed"
    }
}

variant apidocs description {Generate and install HTML API documentation} {
    depends_build-append \
                path:bin/doxygen:doxygen \
                path:bin/dot:graphviz

    post-build {
        system -W ${worksrcpath} "${prefix}/bin/doxygen"
    }

    post-destroot {
        set docdir ${prefix}/share/doc/${name}/html

        xinstall -d ${destroot}${docdir}
        xinstall -m 644 \
            {*}[glob ${worksrcpath}/doc/html/*] \
            ${destroot}${docdir}
    }
}
