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

github.setup        csound csound 6.18.1
github.tarball_from archive
revision            4
categories          audio
license             LGPL-2.1+
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer

homepage            https://www.csound.com

description         A language that is used to describe sound synthesizers.

long_description    Csound is a computer programming language for dealing with \
                    sound. It is called Csound because it is written in the C \
                    programming language, as opposed to some of its \
                    predecessors. Csound was written at MIT by Barry Vercoe, \
                    based on an earlier language called Music360.

checksums           rmd160  3702ec791eb5f98f807aed6ec3de7f4eb9deab77 \
                    sha256  b84be8237fa9258ef60fdddf36fae27b20c11665811bf5ed12540da9a9a4414e \
                    size    31925161

depends_build-append \
                    port:gettext \
                    port:flex \
                    port:bison

depends_lib         port:curl \
                    port:gettext-runtime \
                    port:liblo \
                    port:libsndfile

patchfiles          MAC_OS_X_VERSION.patch

# https://github.com/csound/csound/pull/1732
# https://github.com/csound/csound/issues/1733#issuecomment-1668096257
patchfiles-append   0001-Use-correct-clang-options-in-CMakeLists.patch \
                    0002-patch-fix-Altivec.diff

compiler.cxx_standard   2011

configure.args-append   -DBUILD_CXX_INTERFACE=ON \
                        -DBUILD_DSSI_OPCODES=OFF \
                        -DBUILD_JAVA_INTERFACE=OFF \
                        -DBUILD_LUA_INTERFACE=OFF \
                        -DBUILD_TESTS=OFF \
                        -DBUILD_UTILITIES=ON \
                        -DCS_FRAMEWORK_DEST=${frameworks_dir} \
                        -DFAIL_MISSING=ON \
                        -DUSE_ALSA=OFF \
                        -DUSE_CURL=ON \
                        -DUSE_DOUBLE=OFF \
                        -DUSE_GETTEXT=ON \
                        -DUSE_JACK=OFF \
                        -DUSE_PORTAUDIO=OFF \
                        -DUSE_PORTMIDI=OFF \
                        -DUSE_PULSEAUDIO=OFF

# CMake Error at CMakeLists.txt:146 (message): BUILD_TESTS is enabled, but BUILD_STATIC_LIBRARY="OFF"
# Building static lib does not hurt even without tests enabled, since dylib is still built as well.
configure.args-append   -DBUILD_STATIC_LIBRARY=ON

if {${configure.build_arch} ni [list arm i386 ppc]} {
    default_variants-append +double
}

if {${configure.build_arch} in [list ppc ppc64]} {
    # We do not want to add -mcpu= here, that is not safe.
    configure.optflags-append -mtune=native
    # Absence of altivec is not automatically detected.
    if {[catch {sysctl hw.vectorunit} result] || $result == 0} {
        configure.cppflags-append \
                        -DPFFFT_SIMD_DISABLE
    }
    # In either case, enforce a local build.
    archive_sites
}

if [variant_isset double] {
    set libname         CsoundLib64
} else {
    set libname         CsoundLib
}

set cs_framework_path   ${libname}.framework/Versions/6.0
set csoundlib           ${cs_framework_path}/${libname}

post-destroot {
    # While this is not immediately obvious upon install, dylib and binaries are built with wrong paths:
    # /opt/local/lib/CsoundLib64.framework/Versions/6.0/CsoundLib64
    # At the same time this is installed correctly into the framework directory, so the path points to nothing.
    # CMakeLists are a mess, and it is unclear what in particular breaks this. Fix paths in destroot:
    system -W ${destroot}${frameworks_dir} "install_name_tool -id ${frameworks_dir}/${csoundlib} ./${csoundlib}"
    system -W ${destroot}${prefix}/lib "install_name_tool -change ${prefix}/lib/${csoundlib} ${frameworks_dir}/${csoundlib} ./libcsnd6.6.0.dylib"
    foreach bin [glob ${destroot}${prefix}/bin/*] {
        system "install_name_tool -change ${prefix}/lib/${csoundlib} ${frameworks_dir}/${csoundlib} ${bin}"
    }
    # Existing targets also omit headers from /interfaces, which are needed for some dependents:
    # https://github.com/CsoundQt/CsoundQt/issues/392
    xinstall -d ${destroot}${prefix}/include/${name}
    fs-traverse f ${worksrcpath}/interfaces {
        if {[file isfile ${f}] && ([file extension ${f}] == ".h" || [file extension ${f}] == ".hpp")} {
            copy ${f} ${destroot}${prefix}/include/${name}/
        }
    }
}

variant double description "Use double precision" {
    # See: http://www.csounds.com/manual/html/MiscCsound64.html
    configure.args-replace  -DUSE_DOUBLE=OFF \
                            -DUSE_DOUBLE=ON
}

variant portaudio conflicts pulseaudio description "Use PortAudio" {
    depends_build-append    port:portaudio
    configure.args-replace  -DUSE_PORTAUDIO=OFF \
                            -DUSE_PORTAUDIO=ON
}

variant pulseaudio conflicts portaudio description "Use PulseAudio" {
    depends_build-append    port:pulseaudio
    configure.args-replace  -DUSE_PULSEAUDIO=OFF \
                            -DUSE_PULSEAUDIO=ON
}

variant tests description "Build tests" {
    depends_build-append    port:cunit
    configure.args-replace  -DBUILD_TESTS=OFF \
                            -DBUILD_TESTS=ON

    # Otherwise libraries are looked for in the prefix, which fails, if the port has not been installed earlier.
    test.env-append         DYLD_LIBRARY_PATH=${cmake.build_dir}:${cmake.build_dir}/${cs_framework_path}
    test.run                yes
}

# Note: fltk functionality has moved to a separate 'plugins' repo upstream.
# Ports could be created for some or all of those plugins if desired.
