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

# The port is super-fragile and prone to obscure breakages.
# Update with care.

name                asymptote
version             3.13
revision            0

categories          graphics
maintainers         nomaintainer
description         Vector graphics language
long_description    Asymptote is a powerful descriptive \
                    vector graphics language that provides a natural \
                    coordinate-based framework for technical drawing. \
                    Labels and equations are typeset with LaTeX, \
                    for high-quality PostScript output.
homepage            https://asymptote.sourceforge.net
license             LGPL-3+
# libpaper is a dependency of ghostscript that's only used at runtime
license_noconflict  libpaper

set python.branch   3.14
set python.version  [join [split ${python.branch} "."] ""]
set python.bin      ${prefix}/bin/python${python.branch}

master_sites        sourceforge:project/${name}/${version}/
extract.suffix      .src.tgz

checksums           rmd160  c51d5b757d5dfaa1e443f164f7628cde869d21a6 \
                    sha256  24b2d2fdfa1e25382c0fe84e5d79466f5ae369d7d9f8d99ee2b9b64fa11dc00c \
                    size    28414977

conflicts_build     glew

patchfiles-append   patch-configure.diff
patchfiles-append   patch-Makefile.diff
patchfiles-append   patch-no-vulkan-really.diff

# https://github.com/vectorgraphics/asymptote/issues/521
patchfiles-append   patch-fix-test_atomic_ops.c.diff

# FileNotFoundError: [Errno 2] No such file or directory: 'ccache /opt/local/bin/g++-mp-14'
configure.ccache    no

# On Lion the default clang fails with
# ../base/graph_splinetype.asy: 13.10: arrays have different lengths: 24 != 25
compiler.blacklist  {clang < 500}
# error: invalid use of template-name 'std::unordered_set' without an argument list
compiler.cxx_standard   2017

configure.args-append \
                    --disable-gl \
                    --disable-lsp \
                    --disable-vulkan \
                    --mandir=${prefix}/share/man \
                    --with-context=${texlive_texmfports}/tex/context/third \
                    --with-latex=${texlive_texmfports}/tex/latex \
                    --with-macos-min-version=${macosx_deployment_target}

configure.cxxflags-append \
                    -std=c++17

if {${os.platform} eq "darwin" && ${os.major} < 13} {
    # Somehow it got broken beyond what legacy-support is supposed to handle:
    # dirent.h:148:36: error: invalid conversion from 'int (*)(const dirent*)' to 'int (*)(dirent*)' [-fpermissive]
    # dirent.h:148:46: error: invalid conversion from 'int (*)(const dirent**, const dirent**)' to 'int (*)(const void*, const void*)' [-fpermissive]
    configure.cxxflags-append \
                    -D_MACPORTS_LEGACY_COMPATIBLE_SCANDIR=1 \
                    -fpermissive
}

# https://github.com/vectorgraphics/asymptote/issues/635
if {[string match *gcc* ${configure.compiler}] && ${configure.build_arch} in [list arm i386 ppc]} {
    configure.ldflags-append \
                    -latomic
}

build.env-append    PYTHON=${python.bin}

# texlive-latex-recommended and texinfo are only used for building the docs
# other relevant packages are:
# - texlive-common
# - texlive-bin
# - texlive-basic
# - texlive-latex
# but they are already a dependency of latex-recommended
# One day we could split that into a separate +docs variant
# or fetch the PDF from somewhere
depends_build-append \
                    port:flex \
                    port:ghostscript \
                    path:bin/pkg-config:pkgconfig \
                    port:texinfo \
                    port:texlive-fonts-recommended \
                    port:texlive-latex-recommended \
                    port:texlive-latex-extra

depends_lib-append  port:curl \
                    port:fftw-3 \
                    path:lib/pkgconfig/glfw3.pc:glfw \
                    port:glm \
                    port:gsl \
                    port:libsigsegv \
                    port:ncurses \
                    port:readline \
                    port:zlib

# Runtime dependencies really depend on user preferences.
# Asy can draw graphics without any TeX or without LaTeX,
# so one might just as well remove all deps (or add more)

# The hierarchy of dependencies goes as:
# - texlive-latex-recommended [common, bin, basic, latex]
# - texlive-latex             [common, bin, basic]
# - texlive-basic             [common, bin]

# But then asy also supports luatex, context etc.
depends_run-append  port:ghostscript \
                    port:texlive-basic \
                    port:py${python.version}-pycson

depends_skip_archcheck-append \
                    port:ghostscript \
                    port:texinfo \
                    port:texlive-basic \
                    port:texlive-bin \
                    port:texlive-latex-recommended

# FIXME: GL is optional. At least on 10.6 ppc it is broken at the moment due to:
# https://forums.macrumors.com/threads/10-6-snow-leopard-powerpc-development.2439769/page-20?post=33912236#post-33912236
variant opengl description "Enable OpenGL" {
    configure.args-delete \
                    --disable-gl
}

post-destroot {
    reinplace "s|#!/usr/bin/env python.*|#!${python.bin}|" \
        {*}[glob -directory ${destroot}${prefix}/share/${name}/GUI *.py]
}

post-activate {
    # run `mktexlsr` to make sure the asymptote files are found:
    system "mktexlsr"
}

post-deactivate {
    system "mktexlsr"
}

notes "If you want to use xasy, install py${python.version}-pyqt5."
