# -*- 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       compilers 1.0
PortGroup       wxWidgets 1.0

name            plplot
version         5.15.0
revision        7
maintainers     {takeshi @tenomoto} openmaintainer
categories      science
license         LGPL
description     Scientific plotting package, double precision version
checksums       rmd160  ebfca2cb1221e2cafa9e8d002cea421b8123b224 \
                sha256  b92de4d8f626a9b20c84fc94f4f6a9976edd76e33fb1eae44f6804bdcc628c7b \
                size    15380293
master_sites    sourceforge:project/${name}/${name}/${version}%20Source/
homepage        https://plplot.sourceforge.net
set description_base "PLplot is a cross-platform software package for \
creating scientific plots. To help accomplish that task it is organized \
as a core C library, language bindings for that library, and device drivers \
which control how the plots are presented in non-interactive and interactive \
plotting contexts."

long_description    ${description_base} This port uses double precision.

patchfiles-append   patch-src-plmap.c.diff

compilers.choose    fc f90
compilers.setup     default_fortran
compiler.cxx_standard \
                    2011

depends_build-append \
                path:bin/pkg-config:pkgconfig
depends_lib-append \
                port:antigraingeometry \
                port:fontconfig \
                port:freefont-ttf \
                port:freetype \
                port:libharu \
                port:libLASi \
                port:libtool \
                port:qhull \
                port:shapelib \
                port:swig-tcl \
                port:tcl

post-patch {
    reinplace "s|-ObjC|-ObjC -framework AquaTerm|" ${worksrcpath}/cmake/modules/aqt.cmake
    reinplace "s|-framework AquaTerm|-F${frameworks_dir} -framework AquaTerm|" ${worksrcpath}/cmake/modules/aqt.cmake
    if {[variant_isset python27]} {
        reinplace "s|\${CMAKE_INSTALL_EXEC_PREFIX}|${frameworks_dir}/Python.framework/Versions/2.7|" \
                            ${worksrcpath}/cmake/modules/python.cmake
    }
}

configure.cppflags-append   -DUSE_INTERP_RESULT \
                            -DTcl_Import_TCL_DECLARED

configure.env           HOME=${workpath}
configure.args-append   -DBUILD_SHARED_LIBS=ON \
                        -DTCL_INCLUDE_PATH=${prefix}/include \
                        -DTCL_LIBRARY=${prefix}/lib/libtcl.dylib \
                        -DFREETYPE_INCLUDE_DIR_ft2build=${prefix}/include/freetype2 \
                        -DFREETYPE_LIBRARIES=${prefix}/lib/libfreetype.dylib \
                        -DPL_FREETYPE_FONT_PATH=${prefix}/share/fonts/freefont-ttf \
                        -DWITH_FREETYPE=ON \
                        -DQHULL_INCLUDE_DIRS=${prefix}/include/qhull \
                        -DQHULL_LIBRARIES=${prefix}/lib/libqhull.dylib \
                        -DSHAPELIB_INCLUDE_DIR=${prefix}/include \
                        -DSHAPELIB_LIBRARY=${prefix}/lib/libshp.dylib \
                        -DPLD_aqt=OFF \
                        -DPLD_extcairo=ON \
                        -DPLD_memcairo=ON \
                        -DPLD_ps=ON \
                        -DPLD_psttf=ON \
                        -DPLD_pscairo=ON  \
                        -DPLD_pdfcairo=ON \
                        -DPLD_pngcairo=ON \
                        -DPLD_svgcairo=ON \
                        -DPLD_tk=OFF \
                        -DPLD_wxwidgets=OFF \
                        -DPLD_wxpng=OFF \
                        -DPLD_xcairo=OFF  \
                        -DPLD_xwin=OFF \
                        -DPLD_xfig=OFF \
                        -DPLD_tkwin=OFF \
                        -DPLD_bmpqt=OFF \
                        -DPLD_jpgqt=OFF \
                        -DPLD_pngqt=OFF \
                        -DPLD_ppmqt=OFF \
                        -DPLD_epsqt=OFF \
                        -DPLD_pdfqt=OFF \
                        -DPLD_tiffqt=OFF \
                        -DPLD_qtwidget=OFF \
                        -DPLD_svgqt=OFF \
                        -DPLD_extqt=OFF \
                        -DPLD_memqt=OFF \
                        -DPLD_qtwidget=OFF \
                        -DENABLE_ada=OFF \
                        -DENABLE_d=OFF \
                        -DENABLE_fortran=OFF \
                        -DENABLE_itcl=OFF \
                        -DENABLE_java=OFF \
                        -DENABLE_lua=OFF \
                        -DENABLE_ocaml=OFF \
                        -DENABLE_octave=OFF \
                        -DENABLE_pyqt4=OFF \
                        -DENABLE_pyqt5=OFF \
                        -DENABLE_python=OFF \
                        -DENABLE_qt=OFF \
                        -DENABLE_tcl=ON \
                        -DENABLE_tk=OFF \
                        -DENABLE_wxwidgets=OFF \
                        -DENABLE_DYNDRIVERS=OFF \
                        -DBUILD_TEST=OFF \
                        -DHAVE_SHAPELIB=ON \
                        -DHAVE_AGG=ON \
                        -Dhpdf_INCLUDE_DIR=${prefix}/include

post-configure {
    reinplace "s|-ObjC ||" ${workpath}/build/pkgcfg/plplot.pc
    if {[variant_isset dmd]} {
        reinplace /Requires.private/d ${workpath}/build/pkgcfg/plplot-d.pc
        reinplace {s|Libs: -L"${libdir}" -lplplotdmd|Libs: -L=-L"${libdir}" -L=-lplplotdmd -L=-lplplot|} ${workpath}/build/pkgcfg/plplot-d.pc
    }
}

if {${universal_possible} && [variant_isset universal]} {
    configure.args-append -DCMAKE_OSX_ARCHITECTURES=\"[strsed ${configure.universal_archs} "g| |;|"]\" -DENABLE_fortran:BOOL=OFF
} else {
# Adhoc fix
# cmake passes -arch to Fortran compiler
# g95 ignores -arch but gfortran fails with it
    if {[variant_isset g95]} {
        configure.args-append -DCMAKE_OSX_ARCHITECTURES=\"[strsed ${build_arch} "g| |;|"]\"
    }
}

#platform i386 {
#    configure.args-append   -DCMAKE_MODULE_LINKER_FLAGS=\"-undefined dynamic_lookup\" \
#                            -DCMAKE_EXE_LINKER_FLAGS=\"-undefined dynamic_lookup\"
#}

# https://trac.macports.org/ticket/70087
if {${os.platform} eq "darwin" && [string match *clang* ${configure.compiler}]} {
    default_variants    +aquaterm
}
default_variants-append +x11

subport ${name}-single {
    description     Scientific plotting package, single precision version
    long_description    ${description_base} This port uses single precision.

    configure.pre_args      -DCMAKE_INSTALL_PREFIX=${prefix}/lib/${subport}
    configure.args-append   -DPL_DOUBLE=OFF
}

if {[variant_isset py27_pyqt4]} {
    default_variants +qt4
}

# variant gcw is deleted since it has been deprecated
# gd driver (gif, jpeg, png, svg) has been deprecated

# Fortran notes:
# * In build/language_tests/Fortran, FC and LDFLAGS are used.
# * CMAKE_Fortran_COMPILER is used later in build.
# * To avoid -arch ${build_arch} to be passed configure.ld_archflags is unset.

if {[fortran_variant_isset]} {
    configure.ld_archflags  ""
    configure.args-replace  -DENABLE_fortran=OFF -DENABLE_fortran=ON
    configure.args-append   -DCMAKE_Fortran_COMPILER=\"${configure.fc}\" \
                            -DF95_MOD_DIR=${prefix}/include/plplot
    if {[variant_isset g95]} {
        configure.args-append   -DCMAKE_Fortran_FLAGS=\"${configure.fflags} -fno-second-underscore\"
    } else {
        configure.args-append   -DCMAKE_Fortran_FLAGS=\"${configure.fflags}\"
    }
    macosx_deployment_target
}

variant octave description {Add support for Octave} {
    depends_lib-append      port:swig-octave
    configure.args-replace  -DENABLE_octave=OFF -DENABLE_octave=ON
    configure.cxxflags-append \
                            -std=c++11
    license-append          GPL-2+
}

variant java description {Add support for Java} {
    depends_lib-append      port:swig-java
    configure.args-replace  -DENABLE_java=OFF -DENABLE_java=ON
}

variant ocaml description {Add support for OCaml} {
    patchfiles-append       patch-bindings-ocaml-plplot_core.idl.diff \
                            patch-examples-ocaml.diff
    depends_lib-append      port:ocaml \
                            port:swig-ocaml \
                            port:ocaml-camlidl \
                            port:ocaml-findlib
    configure.args-replace  -DENABLE_ocaml=OFF -DENABLE_ocaml=ON
    configure.args-append   -DOCAML_HAS_CAIRO=OFF \
                            -DOCAML_HAS_GTK=OFF
}

variant python27 description {Add support for python27} {
    depends_lib-append      port:python27 \
                            port:py27-numpy \
                            port:swig-python
    configure.args-replace  -DENABLE_python=OFF -DENABLE_python=ON
    configure.args-append   -DHAVE_NUMPY:BOOL=ON \
                            -DPYTHON_INCLUDE_PATH=${frameworks_dir}/Python.framework/Versions/2.7/Headers
}

variant py27_pyqt4 requires python27 description {Add support for pyQT4 using python27} {
    depends_lib-append      port:py27-pyqt4
    configure.args-replace  -DENABLE_pyqt4=OFF -DENABLE_pyqt4=ON
}

variant gdc description {Use dmd variant instead} {
    default_variants-append dmd
}

variant dmd description {Add support for D} {
    depends_lib-append      port:dmd
    patchfiles-append       patch-cmake-modules-language_support-cmake-CMakeTestDCompiler.cmake.diff \
                            patch-cmake-modules-language_support-cmake-Platform-Darwin-dmd.cmake.diff \
                            patch-examples-d.diff \
                            patch-bindings-d-plplot.d.diff \
                            patch-cmake-modules-language_support-cmake-CMakeDInformation.cmake.diff
    configure.args-replace  -DENABLE_d=OFF -DENABLE_d=ON
    configure.env-append    D_PATH=${prefix}

    post-patch {
        # see https://trac.macports.org/wiki/UsingTheRightCompiler
        reinplace "s|__MACPORTS_LINKER__|${configure.cc}|g" ${worksrcpath}/cmake/modules/language_support/cmake/CMakeDInformation.cmake
    }
}

variant lua description {Add support for Lua} {
    depends_lib-append      port:swig-lua
    configure.args-replace  -DENABLE_lua=OFF -DENABLE_lua=ON
}

variant deprecated description {Allow build of deprecated API} {
    configure.args-append   -DPL_DEPRECATED=ON
}

variant aquaterm description {Add support for Aquaterm} {
    depends_lib-append      port:aquaterm
    configure.args-replace  -DPLD_aqt=OFF -DPLD_aqt=ON
    configure.args-append   -DAQT_FRAMEWORK=${frameworks_dir}/AquaTerm.framework
}

variant qt4 description {Add support for Qt4} {
    depends_lib-append      bin:qmake:qt4-mac
    configure.args-replace  -DENABLE_qt=OFF -DENABLE_qt=ON \
                            -DPLD_bmpqt=OFF -DPLD_bmpqt=ON \
                            -DPLD_epsqt=OFF -DPLD_epsqt=ON \
                            -DPLD_extqt=OFF -DPLD_extqt=ON \
                            -DPLD_jpgqt=OFF -DPLD_jpgqt=ON \
                            -DPLD_memqt=OFF -DPLD_memqt=ON \
                            -DPLD_pdfqt=OFF -DPLD_pdfqt=ON \
                            -DPLD_pngqt=OFF -DPLD_pngqt=ON \
                            -DPLD_ppmqt=OFF -DPLD_ppmqt=ON \
                            -DPLD_qtwidget=OFF -DPLD_qtwidget=ON \
                            -DPLD_svgqt=OFF -DPLD_svgqt=ON \
                            -DPLD_tiffqt=OFF -DPLD_tiffqt=ON \
    configure.args-append   -DQT_QMAKE_EXECUTABLE=${prefix}/libexec/qt4/bin/qmake
}

variant wxwidgets description {Add support for wxWidgets} {
    # It is needed to use C++ subports, otherwise build succeeds but binaries fail to launch:
    # Fatal Error: Mismatch between the program and library build versions detected.
    # The library used 3.0 (wchar_t,compiler with C++ ABI 1002,wx containers,compatible with 2.8),
    # and your program used 3.0 (wchar_t,compiler with C++ ABI 1018,wx containers,compatible with 2.8).
    # Also, wxWidgets-3.0 does not work correctly on early macOS versions; use wxGTK there.
    if {${os.platform} eq "darwin" && ${os.major} > 10} {
        wxWidgets.use       wxWidgets-3.0-cxx11
    } else {
        wxWidgets.use       wxGTK-3.0-cxx11
        # FIXME: wxPLViewer may fail to launch:
        # wxPlViewerApp::OnInit: error when creating wxPlFrame instance
        # Same error reported here: https://bugs.mageia.org/show_bug.cgi?id=29848#c60
        # For now, use the old API:
        configure.args-append \
                            -DOLD_WXWIDGETS=ON
    }
    depends_lib-append      port:${wxWidgets.port}
    configure.args-replace  -DPLD_wxwidgets=OFF -DPLD_wxwidgets=ON \
                            -DENABLE_wxwidgets=OFF -DENABLE_wxwidgets=ON
    configure.args-append   -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig}
}

# X11 notes: Tk binding fails in configure
variant x11 {
    depends_lib-append      path:lib/pkgconfig/pango.pc:pango
    configure.args-replace  -DPLD_xcairo=OFF -DPLD_xcairo=ON \
                            -DPLD_xfig=OFF -DPLD_xfig=ON \
                            -DPLD_xwin=OFF -DPLD_xwin=ON
}

livecheck.type              regex
livecheck.url               http://sourceforge.net/p/${name}/news/
livecheck.regex             {PLplot-([0-9]+\.[0-9]+\.[0-9]+)}
