# -*- 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
PortGroup                       legacysupport                 1.1

github.setup                    symengine symengine 0.13.0 v
revision                        0
categories                      math science
license                         MIT
maintainers                     {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description                     a fast symbolic manipulation library
long_description                ${name} is {*}${description}, written in C++.

checksums                       rmd160  9604d49f062fd0abd1aef9f19671a3fc80ff8ac5 \
                                sha256  f46bcf037529cd1a422369327bf360ad4c7d2b02d0f607a62a5b09c74a55bb59 \
                                size    942757
github.tarball_from             archive

# LLVM_MINIMUM_REQUIRED_VERSION is set to 3.8
#     see https://github.com/symengine/symengine/blob/master/CMakeLists.txt
# mimic LLVM requirements uses by MacPorts base
#     see https://github.com/macports/macports-ports/blob/master/_resources/port1.0/compilers/clang_compilers.tcl
if {${os.major} >= 12 || ${os.platform} ne "darwin"} {
    set llvm_version            18
} elseif {${os.major} >= 10} {
    set llvm_version            11
} elseif {${os.major} >= 9} {
    set llvm_version            7.0
} else {
    set llvm_version            none
}
if {${configure.build_arch} in [list ppc ppc64]} {
    # PowerPC systems prefer GCC compilers
    set llvm_version            none
}

compiler.cxx_standard           2014

depends_lib-append              port:gmp \
                                port:mpfr \
                                port:libmpc \
                                port:flint
if {${llvm_version} ne "none"} {
    depends_lib-append          port:llvm-${llvm_version}
}

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

# symengine sets its own optimization flags
configure.optflags

configure.args-append           -DBUILD_SHARED_LIBS=ON \
                                -DBUILD_FOR_DISTRIBUTION=ON \
                                -DBUILD_TESTS=OFF \
                                -DBUILD_BENCHMARKS=OFF

# see https://github.com/symengine/symengine/blob/master/README.md#recommended-options-to-build
configure.args-append           -DWITH_GMP=ON \
                                -DWITH_MPFR=ON \
                                -DWITH_MPC=ON \
                                -DINTEGER_CLASS=flint
if {${llvm_version} ne "none"} {
    configure.args-append       -DWITH_LLVM=ON
} else {
    configure.args-append       -DWITH_LLVM=OFF
}
configure.args-append           -DWITH_SYMENGINE_THREAD_SAFE=ON

# see https://github.com/symengine/symengine/issues/1671
configure.args-append           -DWITH_COTIRE=OFF

# use MP's clang based on above LLVM version for consistency with used includes/libs
if {${llvm_version} ne "none"} {
    compiler.blacklist          clang *gcc*
    compiler.fallback           macports-clang-${llvm_version}
    compiler.whitelist          macports-clang-${llvm_version}
    cmake.module_path-append    ${prefix}/libexec/llvm-${llvm_version}/lib/cmake/llvm
}

# mirrors setings in llvm-11
legacysupport.use_static                     yes
# futimens
legacysupport.newest_darwin_requires_legacy  16

if {[string match *gcc* ${configure.compiler}]} {
    # ___atomic_store_8
    configure.ldflags-append    -latomic
}

variant tests description {Enable tests} {
    test.run                    yes
    configure.args-replace      -DBUILD_TESTS=OFF \
                                -DBUILD_TESTS=ON
    test.args-append            DYLD_LIBRARY_PATH=${cmake.build_dir}/symengine
}
