# -*- 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               mpi            1.0
PortGroup               linear_algebra 1.0
PortGroup               muniversal     1.1

# Need clock_gettime with CLOCK_MONOTONIC
PortGroup               legacysupport 1.1

legacysupport.newest_darwin_requires_legacy 15

name                    gmsh
version                 4.14.0
revision                0
categories              science
license                 GPL-2+
maintainers             {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description             Finite element mesh generator in 1D, 2D and 3D
long_description        Gmsh is an automatic 3D finite element mesh generator (primarily   \
                        Delaunay) with build-in CAD and post-processing facilities. Its     \
                        design goal is to provide a simple meshing tool for academic test   \
                        cases with parametric input and up to date visualization            \
                        capabilities.

# Source repo is on gitlab.onelab.info, but source download is from
# a different and smaller tarball on the home website.

homepage                https://gmsh.info
master_sites            https://gmsh.info/src/
distname                gmsh-${version}-source
extract.suffix          .tgz

checksums               rmd160  99578b77cd12193242507d2ec19151e6c6b12688 \
                        sha256  db4d7da2062e9a4356a820832138ab99f97af6388bfeb21599a2eadfb0b76a28 \
                        size    18458158

compiler.cxx_standard   2011
compiler.c_standard     1999

mpi.setup
veclibfort              no

depends_lib-append      port:alglib \
                        path:share/pkgconfig/eigen3.pc:eigen3 \
                        path:lib/libfltk.dylib:fltk \
                        port:gmp \
                        port:mathex \
                        port:libmed \
                        port:mmg3d4 \
                        port:hdf5 \
                        port:libpng \
                        path:include/turbojpeg.h:libjpeg-turbo \
                        port:gmm \
                        path:lib/pkgconfig/cairo.pc:cairo \
                        port:libANN \
                        port:cgnslib \
                        port:metis \
                        port:opencascade \
                        port:voroxx \
                        port:szip \
                        port:zlib

# see https://gmsh.info/doc/texinfo/gmsh.html#Compiling-the-source-code

# install static library
# install shared library
# use shared library for Gmsh executable
# use MacPorts libraries
configure.args-append   -DENABLE_BUILD_LIB=ON \
                        -DENABLE_BUILD_SHARED=ON \
                        -DENABLE_BUILD_DYNAMIC=ON \
                        -DENABLE_SYSTEM_CONTRIB=ON

# ACIS is proprietary
# avoid circular dependencies
configure.args-append   -DENABLE_ACIS=OFF \
                        -DENABLE_GETDP=OFF

configure.env-append    CASROOT=${prefix}/libexec/opencascade

pre-configure {
    configure.args-append \
                        -DBLAS_LAPACK_LIBRARIES="-L${prefix}/lib ${linalglib}"
}

if {[mpi_variant_isset]} {
    configure.args-append \
                        -DENABLE_MPI=ON
}

variant docs description {build documentation} {
    build.target-append pdf
    depends_build-append \
                        port:texinfo \
                        port:texlive-latex
}

variant getdp description {Enable GetDP solver} {
    depends_lib-append  port:getdp
    configure.args-delete \
                        -DENABLE_GETDP=OFF
}

variant petsc description {Enable PETSc linear solvers and SLEPc eigensolvers} {
    depends_lib-append  port:petsc \
                        port:slepc

    configure.args-append \
                        -DENABLE_PETSC=ON \
                        -DENABLE_SLEPC=ON \
                        -DPETSC_DIR=${prefix}/lib/petsc \
                        -DSLEPC_DIR=${prefix}/lib/slepc
}

variant oce description {use Community Edition version of Open CASCADE} {
    depends_lib-replace port:opencascade port:oce
    configure.env-replace \
                        CASROOT=${prefix}/libexec/opencascade \
                        CASROOT=${prefix}/libexec/oce
}

variant openmp description {enable support for OpenMP} {
    configure.args-append \
                        -DENABLE_OPENMP=ON
}
if {[variant_isset openmp]} {
    compiler.openmp_version 2.5
}

variant python310 conflicts python311 python312 python313 description {Use Python 3.10} {}
variant python311 conflicts python310 python312 python313 description {Use Python 3.11} {}
variant python312 conflicts python310 python311 python313 description {Use Python 3.12} {}
variant python313 conflicts python310 python311 python312 description {Use Python 3.13} {}

foreach pv {313 312 311 310} {
    if {[variant_isset python${pv}]} {
        set python.version ${pv}
        set python.branch [string index ${python.version} 0].[string range ${python.version} 1 end]

        depends_lib-append  port:py${python.version}-wheel

        post-destroot {
            system  -W ${cmake.build_dir} "${prefix}/bin/python${python.branch} ${worksrcpath}/utils/pypi/sdktowheel.py ${destroot}${prefix} ${os.platform}"
            xinstall -d -m 0755 ${destroot}${frameworks_dir}/Python.framework/Versions/${python.branch}/lib/python${python.branch}
            move    ${destroot}${prefix}/lib/gmsh.py \
                    ${destroot}${frameworks_dir}/Python.framework/Versions/${python.branch}/lib/python${python.branch}
            system  -W ${destroot}${frameworks_dir}/Python.framework/Versions/${python.branch}/lib/python${python.branch} \
                    "${frameworks_dir}/Python.framework/Versions/${python.branch}/bin/python3 -m compileall ."
        }
        break
    }
}

test.run                yes
