# -*- 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           active_variants 1.1
PortGroup           github 1.0
PortGroup           boost 1.0

categories          science
maintainers         {mmoll @mamoll}
description         The Open Motion Planning Library (OMPL)
long_description    The Open Motion Planning Library (OMPL) consists of a set \
                    of sampling-based motion planning algorithms.
github.setup        ompl ompl 1.7.0
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0
homepage            http://ompl.kavrakilab.org
platforms           darwin
license             BSD
checksums           rmd160  1ba68e1fa3395c13758c32f877e386ceb419c15c \
                    sha256  581a5e90b4843a54abad5b61087290d00b113253adbc58b2b548ec3bb0950f2c \
                    size    19677306
depends_build-append port:pkgconfig
depends_lib-append   port:flann port:triangle port:eigen3

# older versions don't have a cmake module for Boost.Numpy. This can be removed once that is fixed.
boost.version 1.87
compiler.cxx_standard   2017
configure.args-append   -DOMPL_BUILD_DEMOS=OFF \
                        -DOMPL_BUILD_TESTS=OFF \
                        -DOMPL_REGISTRATION=OFF \
                        -DCMAKE_DISABLE_FIND_PACKAGE_spot=ON \
                        -DCMAKE_DISABLE_FIND_PACKAGE_Doxygen=ON

# generate the extra C++ code needed for Python bindings. This code seems
# somewhat dependent on compiler and/or boost version, so we can't
# pre-generate them and make them available as an extra download.
post-configure {
    if {[variant_isset python310] || [variant_isset python311] || [variant_isset python312] || [variant_isset python313]} {
        # enable parallel build on at most 2 cores. Generating the bindings
        # uses large amounts of memory, so don't use more cores.
        if { ${use_parallel_build} } { set cj "-j 2" } else { set cj "" }
        system -W ${cmake.build_dir} "${build.cmd} ${cj} update_bindings"
    }
}

# The +app variant includes all the content of the regular OMPL version, but
# adds a simple GUI and an extra library with bindings to the assimp and fcl
# libraries. The cascade of dependencies due to pyqt5 is rather large, so by
# default the +app variant is not enabled. Also, the additions to OMPL in
# this variant are distributed under a slightly different license than the
# regular OMPL version.
variant app description {Include GUI and extra demo code} {
    license             noncommercial
    pre-extract {
        ui_warn "The +app variant causes some extra code to be compiled that is
distributed under the Rice University Software Distribution License. For
details, please read:
http://ompl.kavrakilab.org/license.html#ricelicense"
    }
    depends_lib-append  port:assimp port:fcl port:redis
    # ompl needs some help finding assimp for some reason
    configure.args-append \
        -DCMAKE_DISABLE_FIND_PACKAGE_PQP=ON \
        -DOMPLAPP_RESOURCE_DIR=${prefix}/share/ompl/resources \
        -DASSIMP_LIBRARIES=assimp -DASSIMP_INCLUDE_DIRS=${prefix}/include
    github.setup        ompl omplapp 1.7.0
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    master_sites        https://github.com/ompl/omplapp/releases/download/${version}
    distname            omplapp-${version}-Source
    checksums           rmd160  f659fb724a332623996bab8dcae1aef7f122ed2b \
                        sha256  4fcbd98ea8ca377ff9c563c1a1b546bde94c151ee1f520de7dd8e46493f52101 \
                        size    34790680
    notes "
    If you intend to use ompl_webapp, you need to first launch the redis server and set the following environment variables:

        sudo port load redis
        export OMPL_BROKER_URL='redis://localhost:6379'
        export OMPL_BACKEND_URL='redis://localhost:6379'
    "
}

variant spot description {Include support for automaton generation from LTL specifications using Spot} {
    configure.args-delete -DCMAKE_DISABLE_FIND_PACKAGE_spot=ON
    depends_lib-append    port:spot
}

set pythons_versions {3.10 3.11 3.12 3.13}

set pythons_ports {}
foreach v ${pythons_versions} {
    lappend pythons_ports python[string map {. {}} ${v}]
}

proc python_dir {} {
    global pythons_versions
    foreach p ${pythons_versions} {
        set s [string map {. {}} ${p}]
        if {[variant_isset python${s}]} {
            return [file normalize [exec python${p} -c "import sys; print(sys.prefix)"]/lib/${p}/site-packages]
        }
    }
    error "Python support not enabled."
}

foreach v ${pythons_versions} {
    set s [string map {. {}} ${v}]
    set p python${s}
    set vnodots [string index ${s} 0][string index ${s} 1]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Use Python ${v} for python bindings" "
        depends_build-append    port:py${s}-pyplusplus
        depends_lib-append      port:${p}
        configure.args-append   -DPYTHON_EXEC=${prefix}/bin/python${v}
        boost.require_numpy     yes

        # the python bindings rely on Boost.Python, so make sure it is installed.
        require_active_variants [boost::depends_portname]-numpy ${p}

        if { [variant_isset app] } {
            depends_lib-append \
                port:py${s}-pyqt5 \
                port:py${s}-opengl \
                port:py${s}-flask \
                port:py${s}-celery \
                port:py${s}-redis \
                port:redis
            post-destroot {
                # extra copy of libs not needed in site_packages and causes problems with rpaths
                set site_packages \[python_dir\]
                delete ${destroot}\${site_packages}/ompl/util/libompl.dylib \
                       ${destroot}\${site_packages}/ompl/app/libompl_app.dylib \
                       ${destroot}\${site_packages}/ompl/app/libompl_app_base.dylib \
                       ${destroot}${prefix}/share/ompl/webapp/static/problem_files/2D \
                       ${destroot}${prefix}/share/ompl/webapp/static/problem_files/3D
                file mkdir ${destroot}${prefix}/share/ompl/webapp/static/problem_files
                ln -sf ${prefix}/share/ompl/resources/2D ${destroot}${prefix}/share/ompl/webapp/static/problem_files/2D
                ln -sf ${prefix}/share/ompl/resources/3D ${destroot}${prefix}/share/ompl/webapp/static/problem_files/3D
            }
        } else {
            post-destroot {
                # extra copy of libs not needed in site_packages and causes problems with rpaths
                set site_packages \[python_dir\]
                delete ${destroot}\${site_packages}/ompl/util/libompl.dylib
            }
        }
    "
}

# enable one python variant so that the python bindings can be compiled
set selected_python python312
foreach v ${pythons_versions} {
    set s [string map {. {}} ${v}]
    if {[variant_isset python${s}]} {
        set selected_python python${s}
    }
}
default_variants +${selected_python}

livecheck.type  regex
livecheck.url   http://ompl.kavrakilab.org/download.html
livecheck.regex (\[0-9.\]+), released
