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

name                libgit2
conflicts           libgit2-devel
set my_name         libgit2

# don't forget to update py-pygit2 and libgit2-glib as well
github.setup        libgit2 libgit2 1.9.0 v
github.tarball_from archive
revision            1
epoch               1

categories          devel
maintainers         {devans @dbevans} {mascguy @mascguy}
license             {GPL-2 Permissive}

description         A portable, pure C implementation of the Git core methods
long_description    libgit2 is a portable, pure C implementation of the \
                    Git core methods provided as a re-entrant linkable \
                    library with a solid API, allowing you to write native \
                    speed custom Git applications in any language which \
                    supports C bindings.
homepage            https://libgit2.org/

dist_subdir         ${my_name}

checksums           rmd160  cf1c29dbeaf0e0651bdee529af06ecc10c35aab7 \
                    sha256  75b27d4d6df44bd34e2f70663cfd998f5ec41e680e1e593238bbe517a84c7ed2 \
                    size    7660744

depends_build-append \
                    path:bin/pkg-config:pkgconfig

depends_lib-append \
                    port:curl \
                    port:libiconv \
                    port:libssh2 \
                    path:lib/libssl.dylib:openssl \
                    port:pcre2 \
                    port:zlib

# Fix for error: parameter '...' not found in the function declaration
# See: https://trac.macports.org/ticket/65585
patchfiles-append   patch-tests-no-error.diff

post-patch {
    # Upstream compiles with C standard set to 90, which isn't sufficient for
    # legacy-support. So patch all CMake files, and raise that to C 99.
    # See: https://trac.macports.org/ticket/68148
    fs-traverse f ${worksrcpath} {
        set fname [file tail ${f}]
        if {[string match CMakeLists.txt ${fname}]} {
            ui_info "Patching c_standard for cmake file: ${f}"
            reinplace -q "s|PROPERTIES C_STANDARD 90|PROPERTIES C_STANDARD 99|g" ${f}
        }
    }
}

compiler.c_standard  1999

# Force cmake to pass `-std=c99`, otherwise `-std=c90` is used
# See: https://trac.macports.org/ticket/72428
cmake.set_c_standard yes

# Clear optflags; controlled by project, via cmake build type
configure.optflags

if {[variant_isset debug]} {
    cmake.build_type Debug
} else {
    cmake.build_type RelWithDebInfo
}

configure.args-append \
                    -DREGEX_BACKEND:STRING=pcre2 \
                    -DUSE_ICONV:BOOL=ON \
                    -DUSE_SSH:BOOL=ON \
                    -DUSE_THREADS:BOOL=OFF

variant threadsafe description {Build with threadsafe option} {
    configure.args-replace \
                    -DUSE_THREADS:BOOL=OFF \
                    -DUSE_THREADS:BOOL=ON
}

default_variants    +threadsafe

platform darwin 8 {
    # Fix unsetenv return value
    configure.cflags-append \
                    -D__DARWIN_UNIX03

    # Copy Apple's copyfile.h for use on Mac OS X 10.4 and higher.
    # See APPLE_LICENSE.txt for license and copying information.
    post-patch {
        file copy ${filespath}/copyfile.h ${worksrcpath}
    }
    configure.cflags-append \
                    "-I${worksrcpath}"
}

# customize regex to avoid release candidates, alpha, beta, etc
github.livecheck.regex (\\d+(?:\\.\\d+)*)
