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

github.setup        KhronosGroup OpenCOLLADA 1.6.68 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                opencollada
categories          graphics
license             MIT
maintainers         @jasonliu-- openmaintainer
homepage            http://www.opencollada.org
description         stream-based library for reading and writing \
                    COLLADA files
long_description    OpenCOLLADA is a library that allows computer \
                    graphics software applications to read and write \
                    COLLADA (COLLAborative Design Activity) files. \
                    COLLADA files are typically XML-based documents \
                    identified with a .dae (digital asset exchange) \
                    filename extension.

checksums           rmd160  afc58cdd5ff73f8f11ae4f2da87d43817df9974d \
                    sha256  70af2194a3a411ccc05102daa3faaa5f1ea1758120171f7f635a0e488985b2ad \
                    size    12098976

depends_build-append \
                    port:pkgconfig
depends_lib-append  port:libxml2

compiler.cxx_standard 2011

# DAEValidator/library/src/Dae.cpp:307:23: error: loop variable 'node' is always a copy because the range of type 'const opencollada::XmlNodeSet' does not return a reference [-Werror,-Wrange-loop-analysis]
#                                for (const auto & node : nodes)
#                                                  ^
# To be removed once fixed upstream
patchfiles-append   fix-loop-variable-type.patch \
                    patch-no_Werroro.diff

post-patch {
    # A MacPorts build is basically similar to a Unix build process
    reinplace -E "s|\(NOT UNIX\)|\\1 AND NOT APPLE|g" \
        ${worksrcpath}/CMakeLists.txt
    reinplace -E "s|\(UNIX\)|\\1 OR APPLE|g" \
        ${worksrcpath}/OpenCOLLADAConfig.cmake.in

    # The version of the PCRE library that is delivered with OpenCOLLADA
    # differs from the one that is available through MacPorts, so we
    # modify the CMake file to build using the provided PCRE library.
    # (Oddly, the SConstruct file already includes this option.)
    reinplace "/option.USE_EXPAT/a\\
option(PCRENATIVE \"Set to build using the systems native pcre lib instead of the delivered lib\" ON)\\
" \
        ${worksrcpath}/CMakeLists.txt
    reinplace -E "s|\(if.*PCRE_FOUND\)|\\1 AND PCRENATIVE|" \
        ${worksrcpath}/CMakeLists.txt

    # OpenCOLLADA puts its CMake package config files in the wrong place
   reinplace -E "s|\(set.OPENCOLLADA_INST_CMAKECONFIG \).*cmake|\\1\${CMAKE_INSTALL_PREFIX}/lib/cmake/opencollada|" \
       ${worksrcpath}/CMakeLists.txt

    # I have no idea why OpenCOLLADA's source code installs all of
    # its lib files into ${prefix}/lib/opencollada, and yet during
    # the compile process, it tries to link object files to
    # ${prefix}/lib/libUTF.dylib (even though libUTF.dylib, like
    # all other libs compiled by OpenCOLLADA, gets placed into
    # lib/opencollada!). Since this rpath is established during link
    # time, moving libUTF.dylib or creating a symbolic link in
    # ${prefix}/lib during the destroot phase won't fix the issue.
    set regexes [list \
        "s|\(EXECUTABLE_OUTPUT_PATH.*bin\)|\\1/opencollada|" \
        "s|\(LIBRARY_OUTPUT_PATH.*lib\)|\\1/opencollada|" \
        "s|\(PROPERTIES\)\( OUTPUT_NAME\)|\\1 INSTALL_NAME_DIR \${OPENCOLLADA_INST_LIBRARY}\\2|g" \
    ]
    foreach re $regexes {
        reinplace -E $re ${worksrcpath}/CMakeLists.txt
    }

    # Fix "no member named 'isnan' in namespace 'std'" errors.
    # See: https://github.com/KhronosGroup/OpenCOLLADA/issues/594
    reinplace "/include.*math\.h/a\\
#include <cmath>\\
" \
        ${worksrcpath}/GeneratedSaxParser/src/GeneratedSaxParserUtils.cpp
}

configure.args-append   -DUSE_SHARED=ON \
                        -DPCRENATIVE=OFF
