# -*- 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

name            ngspice
version         43
revision        1
license         BSD
categories      science cad
maintainers     {gmail.com:degnan.68k @bpdegnan} \
                {mark @markemer} \
                openmaintainer

description     Circuit simulator based on Spice 3f5, XSpice and others
long_description \
                Ngspice is a mixed-level/mixed-signal circuit simulator. Its \
                code is based on three open source software packages: Spice3f5, \
                Cider1b1 and Xspice. Ngspice is part of gEDA project, a full \
                suite of Electronic Design Automation tools.

homepage        http://ngspice.sourceforge.net/
master_sites    sourceforge:project/ngspice/ng-spice-rework/${version}

checksums       rmd160  577a26e18e70fdf1fceb87ae3c6a783c3cf00d8e \
                sha256  14dd6a6f08531f2051c13ae63790a45708bd43f3e77886a6a84898c297b13699 \
                size    10279606

set docdir      ${prefix}/share/doc/${name}

# checking whether C compiler accepts -std=gnu11... no
# C compiler cannot compile C11 code, etc.
compiler.c_standard     2011
compiler.cxx_standard   2011

if {${name} eq ${subport}} {

    # freetype2 headers are not found by default
    configure.cppflags-prepend -I${prefix}/include/freetype2

    # TASK_BASIC_INFO_COUNT and friends were renamed in 10.8
    patchfiles-append   patch-ngspice-older-MACH-defines.diff

    # https://trac.macports.org/ticket/70175
    patchfiles-append   patch-fix-linking.diff

    depends_lib-append  port:readline \
                        port:libtool \
                        port:xorg-libX11 \
                        port:xorg-libXaw \
                        port:xorg-libXext

    configure.args      --enable-cider \
                        --enable-xspice \
                        --enable-pss \
                        --disable-openmp \
                        --disable-silent-rules \
                        --with-readline=${prefix} \
                        --with-x

    # https://trac.macports.org/ticket/70665
    # https://github.com/macports/macports-ports/commit/e485b0a0c8125cb9d8bcc6241a9c2016514188c8
    if {${os.platform} eq "darwin" && ${os.major} <= 10} {
        configure.cppflags-append \
                                -D__NOEXTENSIONS__

        # Do not let an arbitrary compiler be set as CC_FOR_BUILD,
        # do not link C++ code with gcc.
        patchfiles-append       patch-use-cxx-to-link-cxx-code.diff
        configure.env-append    CC_FOR_BUILD=${configure.cc}
    }

    # Set CFLAGS and LDFLAGS
    configure.ldflags-append -L${prefix}/lib

    build.args          VERBOSE=1
    post-destroot {
        xinstall -d ${destroot}${docdir}
        xinstall -m 644 -W ${worksrcpath} \
            ANALYSES \
            AUTHORS \
            BUGS \
            COPYING \
            ChangeLog \
            DEVICES \
            FAQ \
            NEWS \
            README \
            Stuarts_Poly_Notes \
            ${destroot}${docdir}
    }

    variant manual description {Legacy compatibility variant} {
        depends_run-append      port:ngspice-docs
    }

    variant lib description {Enable dynamic lib} {
        depends_run-append      port:ngspice-lib
    }

    default_variants    +manual +lib

    livecheck.regex     ${name}-(\[0-9.\]+)${extract.suffix}

    # running the tests from MacPorts errors early, probably due to a permissions
    # issue when calling the X11 display subsystem. Running the tests manually from the
    # worksrcdir works fine, however
    # test.run            yes
    # test.target         check

} else {
    livecheck.type      none
}

subport ngspice-docs {
    supported_archs     noarch
    description         PDF manual for ngspice
    long_description    {*}${description}

    distname            ${name}-${version}-manual.pdf
    extract.suffix
    extract.only

    checksums           rmd160  af261765d31bb74434eb66f1e13b67c116223b04 \
                        sha256  ae1d8db376df224e0d82e46d34a9d7ada068a69d685144f43b5efe5a2d06f0e8 \
                        size    2559027

    use_configure       no

    build {}

    destroot {
        xinstall -d ${destroot}${docdir}
        xinstall -m 644 ${distpath}/${distname} ${destroot}${docdir}
    }
}

subport ngspice-lib {
    description         ngspice dynamic library
    long_description    {*}${description}

    # freetype2 headers are not found by default
    configure.cppflags-prepend -I${prefix}/include/freetype2

    # TASK_BASIC_INFO_COUNT and friends were renamed in 10.8
    patchfiles-append   patch-ngspice-older-MACH-defines.diff

    depends_lib-append  port:autoconf \
                        port:automake \
                        port:bison \
                        port:flex \
                        port:gawk \
                        port:readline \
                        port:libtool

    configure.args      --enable-cider \
                        --enable-xspice \
                        --enable-pss \
                        --enable-relpath \
                        --disable-openmp \
                        --with-readline=${prefix} \
                        --with-ngshared

    # https://trac.macports.org/ticket/70665
    # https://github.com/macports/macports-ports/commit/e485b0a0c8125cb9d8bcc6241a9c2016514188c8
    if {${os.platform} eq "darwin" && ${os.major} <= 10} {
        configure.cppflags-append \
                                -D__NOEXTENSIONS__
    }

    # Set CFLAGS and LDFLAGS
    configure.cflags-append     -Wall -I${prefix}/include/freetype2

    # Use the correct prefix
    use_configure       yes
    destroot {
        xinstall -d ${destroot}${prefix}/lib
        xinstall -m 755 -W ${worksrcpath}/src/.libs \
            libngspice.0.dylib \
            ${destroot}${prefix}/lib
        ln -s ${prefix}/lib/libngspice.0.dylib \
            ${destroot}${prefix}/lib/libngspice.dylib
        xinstall -d ${destroot}${prefix}/lib/pkgconfig
        xinstall -m 644 -W ${worksrcpath} \
            ngspice.pc \
            ${destroot}${prefix}/lib/pkgconfig
        xinstall -d ${destroot}${prefix}/include/ngspice
        xinstall -m 644 -W ${worksrcpath} \
            src/include/ngspice/sharedspice.h \
            ${destroot}${prefix}/include/ngspice
    }
}
