# -*- 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           boost 1.0
PortGroup           cmake 1.1
PortGroup           compilers 1.0
PortGroup           github 1.0
PortGroup           legacysupport 1.1

boost.version       1.81

github.setup        m-a-d-n-e-s-s madness 69d68dd6b002e527e4392c8927411c5c1e507c65
version             2024.12.06
revision            0
categories          science math
license             GPL-2
maintainers         nomaintainer
description         Multiresolution Adaptive Numerical Environment for Scientific Simulation
long_description    MADNESS provides a high-level environment for the solution \
                    of integral and differential equations in many dimensions \
                    using adaptive, fast methods with guaranteed precision \
                    based on multi-resolution analysis and novel separated representations.
checksums           rmd160  1c6027bd2f2676b6bbae3c7397afa2e7d7bf9d71 \
                    sha256  834a239072f2b8018e1eb5a4c218af5a3c0bcc5768049bf5a3a4e55a32e90a83 \
                    size    36835952
github.tarball_from archive

set py_ver          3.12
set py_ver_nodot    [string map {. {}} ${py_ver}]
depends_build-append \
                    port:python${py_ver_nodot}
configure.python    ${prefix}/bin/python${py_ver}

# On new systems finding OpenBLAS is broken with this port for some reason recently,
# so let them use Accelerate. On non-Darwin OSs there is no Accelerate: OpenBLAS is the only option.
if {(${os.platform} eq "darwin" && ${os.major} < 20) || ${os.platform} ne "darwin"} {
    # For now, using OpenBLAS instead of PG choice, due to this:
    # https://github.com/m-a-d-n-e-s-s/madness/issues/458
    depends_lib-append \
                    path:lib/libopenblas.dylib:OpenBLAS
    require_active_variants \
                    path:lib/libopenblas.dylib:OpenBLAS lapack

    # We need LAPACK_LIBRARIES to be set: https://trac.macports.org/ticket/68721
    configure.pre_args-append \
                    -DLAPACK_LIBRARIES=OpenBLAS
}

patch.pre_args-replace  -p0 -p1
# Temporarily avoid building chemistry part:
# https://github.com/m-a-d-n-e-s-s/madness/issues/459
patchfiles          0001-CMakeLists-support-ppc.patch \
                    0002-Do-not-build-chem-it-is-broken.patch \
                    0003-Do-not-build-apps-they-depend-on-chem.patch

# We do not use these at the moment, but let’s fix them anyway:
post-patch {
    reinplace "s,/usr/bin/env python3,${configure.python}," \
                    {*}[glob ${worksrcpath}/src/apps/cc2/*.py] \
                    {*}[glob ${worksrcpath}/src/apps/cis/*.py] \
                    {*}[glob ${worksrcpath}/src/apps/moldft/*.py] \
                    {*}[glob ${worksrcpath}/src/apps/mp2/*.py] \
                    {*}[glob ${worksrcpath}/src/apps/nemo/*.py] \
                    {*}[glob ${worksrcpath}/src/apps/oep/*.py] \
                    {*}[glob ${worksrcpath}/src/apps/znemo/*.py]
}

compiler.cxx_standard           2017
compiler.thread_local_storage   yes
compilers.setup                 require_fortran

# oneTBB task backend not yet fixed: https://github.com/m-a-d-n-e-s-s/madness/issues/366
# It is unclear whether MPI works, therefore disabling for now.
# Please do not enable without testing, and do not enable by default on PPC.
configure.args-append \
                    -DBUILD_SHARED_LIBS=ON \
                    -DBUILD_TESTING=OFF \
                    -DENABLE_ACML=OFF \
                    -DENABLE_BOOST=ON \
                    -DENABLE_ELEMENTAL=OFF \
                    -DENABLE_GENTENSOR=OFF \
                    -DENABLE_GPERFTOOLS=OFF \
                    -DENABLE_LIBUNWIND=OFF \
                    -DENABLE_LIBXC=OFF \
                    -DENABLE_MKL=OFF \
                    -DENABLE_MPI=OFF \
                    -DENABLE_NEVER_SPIN=OFF \
                    -DENABLE_PAPI=OFF \
                    -DENABLE_PCM=OFF \
                    -DENABLE_SPINLOCKS=ON \
                    -DENABLE_TCMALLOC_MINIMAL=OFF \
                    -DFORTRAN_INTEGER_SIZE=4 \
                    -DMADNESS_TASK_BACKEND="Pthreads" \
                    -DPython3_EXECUTABLE=${configure.python}

legacysupport.newest_darwin_requires_legacy 16
legacysupport.use_mp_libcxx yes

platform darwin {
    # dqueue.h:221:17: error: aligned deallocation function
    # of type 'void (void *, std::align_val_t) noexcept'
    # is only available on macOS 10.13 or newer
    if {${os.major} <= 16} {
        # gcc does not recognize -fno-aligned-allocation
        if {[string match *clang* ${configure.compiler}]} {
            configure.cxxflags-append \
                            -fno-aligned-allocation
        }
    }
    if {[string match *gcc* ${configure.compiler}]} {
        # https://github.com/m-a-d-n-e-s-s/madness/issues/456
        configure.cxxflags-append \
                            -fpermissive
    }
}

# Build auto-detects ccache if it is installed and attempts to write
# to CCACHE_DIR, which is not allowed if configure.ccache=off.
# Set CCACHE_DIR to the build directory instead.
if {![option configure.ccache]} {
    configure.env-append   CCACHE_DIR=${workpath}/.ccache
    build.env-append       CCACHE_DIR=${workpath}/.ccache
    destroot.env-append    CCACHE_DIR=${workpath}/.ccache
}

# Make this a variant due to: https://trac.macports.org/ticket/68775
variant gperftools description "Build with gperftools support" {
    depends_lib-append \
                    port:gperftools
    configure.args-replace \
                    -DENABLE_GPERFTOOLS=OFF -DENABLE_GPERFTOOLS=ON
}

variant tests description "Build and run tests" {
    # Tests 54–65 are expected to fail presently, since they depend on libMADchem, which we disable; see above.
    configure.pre_args-replace \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF
    configure.args-replace \
                    -DBUILD_TESTING=OFF -DBUILD_TESTING=ON
    pre-test {
        # test infrastructure uses /bin/ps, which is forbidden by sandboxing
        append portsandbox_profile " (allow process-exec (literal \"/bin/ps\") (with no-profile))"
    }
    test.run        yes
}
