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

name                        opencascade
github.setup                Open-Cascade-SAS OCCT 7.9.2 V
github.tarball_from         archive
revision                    0

categories                  science
license                     LGPL-2
maintainers                 {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description                 Development platform for CAD/CAE, \
                            3D surface/solid modeling and data exchange
long_description            ${description}
homepage                    https://www.opencascade.com/content/open-source-core-technology

set git_version             [join [split ${version} .] _]

# Workarounds for URL bug in github portgroup.
# https://trac.macports.org/ticket/70652
# https://trac.macports.org/ticket/71377
github.master_sites         ${github.homepage}/archive/refs/tags
distname                    [join ${github.tag_prefix}]${git_version}

checksums                   rmd160  f2125195f917687533f874542823a2f9ceca129e \
                            sha256  3cd080d3fc33ba0c6c157e110afe3e015859524c4694dbb09812ec9d61595639 \
                            size    48588929

# Add support for custom CMAKE_BUILD_TYPE = MacPorts, set by cmake portgroup.
# Note, build_type = "None" DOES NOT WORK CORRECTLY for this port.
# https://trac.macports.org/ticket/49108
patchfiles-append           patch-CMakeLists.txt.diff

compiler.cxx_standard       2017
configure.args-append       -DCMAKE_CXX_STANDARD=17 \
                            -DCMAKE_CXX_STANDARD_REQUIRED=ON

depends_build-append        path:lib/pkgconfig/RapidJSON.pc:rapidjson

depends_lib-append          port:freeimage \
                            port:freetype \
                            port:onetbb \
                            port:tcl \
                            port:tk

# install it into libexec to avoid conflict with oce port
cmake.install_prefix ${prefix}/libexec/${name}

configure.args-append       -DUSE_FREEIMAGE=ON \
                            -DUSE_RAPIDJSON=ON \
                            -DUSE_TBB=ON \
                            -DBUILD_DOC_Overview=OFF \
                            -D3RDPARTY_DIR=${prefix}

# see https://trac.macports.org/ticket/59917
if {${os.major} < 12} {
    configure.cxxflags-append \
                            -F/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks
}

if {[string match *gcc* ${configure.compiler}]} {
    # OpenGl_GlFunctions.cxx: error: invalid conversion from 'void (*)(GLenum, GLint, GLenum, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*)'
    # {aka 'void (*)(unsigned int, int, unsigned int, int, int, int, unsigned int, unsigned int, const void*)'} to 'OpenGl_GlFunctions::glTexImage2D_t'
    # {aka 'void (*)(unsigned int, int, int, int, int, int, unsigned int, unsigned int, const void*)'} [-fpermissive]
    configure.cxxflags-append \
                            -fpermissive
    if {${configure.build_arch} in [list arm i386 ppc]} {
        # Undefined symbols: "___atomic_fetch_add_8"
        configure.ldflags-append \
                            -latomic
    }
}

variant docs description {Build and install documentation} {
    depends_build-append    path:bin/doxygen:doxygen
    configure.args-delete   -DBUILD_DOC_Overview=OFF
}

if {${universal_possible} && [variant_isset universal]} {
    set merger_dont_diff ${prefix}/bin/custom.sh
    post-destroot {
        file delete ${destroot}${prefix}/bin/custom.sh
        system -W ${destroot}${prefix}/bin/ "echo \"#!/bin/bash\" > custom.sh"
        foreach arch ${configure.universal_archs} {
            reinplace "s|^#!/bin/bash||" ${destroot}${prefix}/bin/${arch}-custom.sh
            system -W ${destroot}${prefix}/bin/ "cat ${arch}-custom.sh >> custom.sh"
            file delete ${destroot}${prefix}/bin/${arch}-custom.sh
        }
    }
}

# keep this port at prefix's cmake's lib
post-destroot {
    xinstall -d -m 0755 ${destroot}${prefix}/lib/cmake
    foreach cmakedir [glob -type d -nocomplain -tails -directory ${destroot}${cmake.install_prefix}/lib/cmake *] {
        xinstall -d -m 0755 ${destroot}${prefix}/lib/cmake/${cmakedir}
        foreach cmakefile [glob -tails -directory ${destroot}${cmake.install_prefix}/lib/cmake/${cmakedir} *.cmake] {
            ln -s ${cmake.install_prefix}/lib/cmake/${cmakedir}/${cmakefile} \
                ${destroot}${prefix}/lib/cmake/${cmakedir}/
            reinplace -q "s|\\\${CMAKE_CURRENT_LIST_DIR}|${cmake.install_prefix}/lib/cmake/${cmakedir}|g" \
                 ${destroot}${cmake.install_prefix}/lib/cmake/${cmakedir}/${cmakefile}
            reinplace -q "s|\\\${CMAKE_CURRENT_LIST_FILE}|${cmake.install_prefix}/lib/cmake/${cmakedir}/${cmakefile}|g" \
                 ${destroot}${cmake.install_prefix}/lib/cmake/${cmakedir}/${cmakefile}
        }
    }
}

# Avoid release candidates
github.livecheck.regex  {([0-9._]+)}
livecheck.version       ${git_version}
