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

# NOTE: For any version number change, also
# NOTE:   Update py-protobuf3 version's number to match
# NOTE: For a minor or major version number change, also
# NOTE:   Revbump et, protobuf-c, mosh and py-onnx
set release_version \
                21.12

name            protobuf3-cpp
github.setup    protocolbuffers protobuf 3.${release_version} v
git.branch      v${release_version}
revision        4

# remove this stealth update code with next version update
if {[vercmp ${version} == 3.21.12]} {
    dist_subdir ${name}/${version}_1
}

categories      devel
maintainers     {mascguy @mascguy} openmaintainer
license         BSD

conflicts       protobuf-cpp

description     Encode data in an efficient yet extensible format.
long_description \
                Google Protocol Buffers are a flexible, efficient, \
                automated mechanism for serializing structured data -- \
                think XML, but smaller, faster, and simpler.  You \
                define how you want your data to be structured once, \
                then you can use special generated source code to \
                easily write and read your structured data to and from \
                a variety of data streams and using a variety of \
                languages.  You can even update your data structure \
                without breaking deployed programs that are compiled \
                against the "old" format.  You specify how you want \
                the information you're serializing to be structured by \
                defining protocol buffer message types in .proto \
                files.  Each protocol buffer message is a small \
                logical record of information, containing a series of \
                name-value pairs.
homepage        https://protobuf.dev

checksums       rmd160  f7c2879e427160c2a5055a2ab5d5ab231304fd31 \
                sha256  4eab9b524aa5913c6fffb20b2a8abf5ef7f95a80bc0701f3a6dbb4c607f73460 \
                size    4842303

github.tarball_from releases
distname        protobuf-cpp-${version}
worksrcdir      protobuf-${version}

patchfiles-append max_align_t.patch

# Upstream adds zlib include - which is ${prefix}/include - before search path
# of 3rd-party components, like gtest, gmock, etc. That causes the external
# versions of those to be pulled in, and the build fails.
# So don't let the project cmake add zlib; already added (last) by base.
patchfiles-append cmake-zlib-include.diff

compiler.cxx_standard   2011
compiler.thread_local_storage   yes
# error: constexpr constructor never produces a constant expression [-Winvalid-constexpr]
compiler.blacklist {clang < 900}

if { [string match *clang* ${configure.compiler}] } {
    # Quiet deprecation warnings
    configure.cxxflags-append \
                -Wno-deprecated-declarations \
                -Wno-error=unknown-warning-option \
                -Wno-unknown-warning-option
}

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

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

depends_lib-append \
                port:zlib

configure.args-append \
                -DBUILD_SHARED_LIBS:BOOL=ON \
                -Dprotobuf_BUILD_PROTOC_BINARIES:BOOL=ON \
                -Dprotobuf_BUILD_TESTS:BOOL=OFF

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

    xinstall -d -m 755 ${docdir}

    foreach f {CHANGES.txt CONTRIBUTORS.txt LICENSE README.md editors examples} {
        file copy ${worksrcpath}/${f} ${docdir}
    }
}

proc port_test_ver_check {p_name p_ver p_rev} {
    if { [catch {set port_ver_info [lindex [registry_active ${p_name}] 0]}] } {
        error "Tests require that ${p_name} be active; install, then re-run tests"
    } else {
        set test_ver ${p_ver}_${p_rev}
        set port_ver [lindex ${port_ver_info} 1]_[lindex ${port_ver_info} 2]
        ui_info "port_test_ver_check: ${p_name}: test_ver: ${test_ver}; port_ver: ${port_ver}"

        if { [vercmp ${port_ver} ${test_ver}] != 0 } {
            error "Tests require installed version of ${p_name} to match port; update, then re-run tests"
        }
    }
}

variant tests description {Build with tests enabled} {
    pre-configure {
        port_test_ver_check ${subport} ${version} ${revision}
    }

    configure.args-replace \
                -Dprotobuf_BUILD_TESTS:BOOL=OFF \
                -Dprotobuf_BUILD_TESTS:BOOL=ON

    test.run    yes
    test.target check
}

livecheck.type  none
