# -*- 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               legacysupport 1.1
PortGroup               github 1.0
PortGroup               active_variants 1.1
PortGroup               app 1.1
PortGroup               cmake 1.1
PortGroup               perl5 1.0
PortGroup               conflicts_build 1.0

github.setup            darktable-org darktable 4.8.1 release-
name                    darktable-devel
conflicts               darktable
set my_name             darktable
revision                2

categories              graphics
license                 GPL-3+
maintainers             {mcalhoun @MarcusCalhoun-Lopez} {mascguy @mascguy} openmaintainer

description             Virtual lighttable and darkroom for photographers
long_description        Darktable is a virtual lighttable and darkroom for \
                        photographers: it manages your digital negatives in a \
                        database and lets you view them through a zoomable \
                        lighttable. It also enables you to develop raw images \
                        and enhance them.
homepage                https://www.darktable.org/

supported_archs         x86_64 arm64

github.tarball_from     releases
dist_subdir             ${my_name}
use_xz                  yes

checksums               rmd160  8c4f84242041cdc8797333237b4c5645610a9479 \
                        sha256  901b0e2caed36fb8619fdf4c60edfb8d31134b947d3054b5c66fd55c38af5991 \
                        size    6258312

# If lua installed, those headers are found first, rather than lua54
conflicts_build-append  lua

# Enable use of 'macports-libcxx' for macOS 10.12 and earlier, as port uses
# libcxx features normally only available on 10.13 and later.
legacysupport.use_mp_libcxx \
                        yes

# Upstream includes stricter compiler restrictions than are necessary, vis-a-vis
# MacPorts toolchain components. So replace stock versions, with our own dummy
# empty cmake file.
post-patch {
    set cmake_compiler_versions_targets \
        [list \
            cmake/compiler-versions.cmake \
            src/external/rawspeed/cmake/compiler-versions.cmake \
        ]

    foreach f ${cmake_compiler_versions_targets} {
        set ftgt ${worksrcpath}/${f}
        ui_info "Overlaying stock compiler-versions.cmake: ${f}"

        delete ${ftgt}
        copy ${filespath}/cmake-compiler-versions.cmake ${ftgt}
    }
}

# Supported build types: Debug:RelWithDebInfo;Release;Coverage;Sanitize;Tsan;Fuzz
if {[variant_isset debug]} {
    cmake.build_type    RelWithDebInfo
} else {
    cmake.build_type    Release
}

perl5.branches          5.34

# darktable sets its own optimization flags
configure.optflags
# disable deprecation warnings
configure.cppflags-append \
                        -Wno-deprecated-declarations

depends_build-append    \
                        port:cctools \
                        port:gettext \
                        port:intltool \
                        port:perl${perl5.major} \
                        port:pkgconfig \
                        port:po4a

depends_lib-append      \
                        port:atk \
                        path:lib/pkgconfig/cairo.pc:cairo \
                        port:curl \
                        port:desktop-file-utils \
                        port:exiv2 \
                        port:flickcurl \
                        port:GraphicsMagick \
                        path:lib/pkgconfig/gdk-pixbuf-2.0.pc:gdk-pixbuf2 \
                        port:gettext-runtime \
                        path:lib/pkgconfig/glib-2.0.pc:glib2 \
                        port:gmic-lib \
                        path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                        path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                        path:lib/pkgconfig/icu-uc.pc:icu \
                        port:jasper \
                        port:libavif \
                        port:libheif \
                        port:libjxl \
                        port:lua54 \
                        port:imath \
                        port:iso-codes \
                        path:include/turbojpeg.h:libjpeg-turbo \
                        port:json-glib \
                        port:lcms2 \
                        port:lensfun \
                        port:libgphoto2 \
                        port:libpng \
                        path:lib/pkgconfig/librsvg-2.0.pc:librsvg \
                        port:libsecret \
                        path:lib/pkgconfig/libsoup-2.4.pc:libsoup \
                        path:lib/pkgconfig/libusb-1.0.pc:libusb \
                        port:libxml2 \
                        port:openexr \
                        port:openjpeg \
                        port:osm-gps-map \
                        path:lib/pkgconfig/pango.pc:pango \
                        port:portmidi \
                        port:pugixml \
                        port:sqlite3 \
                        port:webp \
                        port:tiff \
                        port:zlib

depends_run-append      \
                        port:adwaita-icon-theme \
                        port:tango-icon-theme \
                        port:exiftool

# Reduce optimizations, which cause some versions of MacPorts Clang to crash during compilation
patchfiles-append       patch-cmake-opt-flags.diff

# Enable Cxx 2011 deprecated features, like std::auto_ptr, necessary for dep 'exiv2' v0.27.x.
# Remove once 'exiv2' has been updated to 0.28.x or later.
patchfiles-append       patch-cmake-cxx-2011-compat.diff
configure.args-append   -DENABLE_CXX11_COMPAT=ON

# sets march optimization to generic
configure.args-append   -DBINARY_PACKAGE_BUILD=ON

configure.args-append   -DBUILD_CMSTEST=OFF \
                        -DBUILD_CURVE_TOOLS=ON \
                        -DBUILD_NOISE_TOOLS=ON \
                        -DTESTBUILD_OPENCL_PROGRAMS=OFF \
                        -DUSE_COLORD=OFF \
                        -DUSE_KWALLET=OFF \
                        -DUSE_OPENMP=OFF \
                        -DUSE_UNITY=OFF \
                        -DDONT_USE_INTERNAL_LUA=OFF \
                        -Dperl_BIN=${perl5.bin}

# For ARM build, we must disable Intel instruction set use.
if {${configure.build_arch} eq "arm64"} {
    configure.args-append \
                        -DBUILD_SSE2_CODEPATHS=OFF
}

post-build {
    set wrap [open "${workpath}/darktable-app" w 0755]
    puts ${wrap} "#!/bin/sh"
    puts ${wrap} ""
    puts ${wrap} "export XDG_DATA_DIRS=${prefix}/share:\$XDG_DATA_DIRS"
    puts ${wrap} "exec ${prefix}/bin/darktable --moduledir ${prefix}/lib/darktable"
    puts ${wrap} ""
    close ${wrap}
}

post-destroot {
    # need to determine why lib is not going where expected, until then...
    move ${destroot}${prefix}/lib/darktable/libdarktable.dylib ${destroot}${prefix}/lib/

    # create symlinks for tools
    set port_tools_dir ${prefix}/libexec/darktable/tools
    foreach f [glob ${destroot}${port_tools_dir}/darktable-*] {
        set fn [file tail ${f}]
        ln -s ${port_tools_dir}/${fn} ${destroot}${prefix}/bin/${fn}
    }
}

post-activate {
    system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
    system "${prefix}/bin/glib-compile-schemas ${prefix}/share/glib-2.0/schemas"

    # Note: mime db update now handled automatically by shared-mime-info
    # system "env XDG_DATA_DIRS=${prefix}/share ${prefix}/bin/update-mime-database -V ${prefix}/share/mime"
}

app.name                ${my_name}
app.executable          ${workpath}/darktable-app
app.icon                packaging/macosx/Icons.icns

universal_variant       no

compiler.cxx_standard   2020

# compiling src/osx/osx.mm with GCC results in a cascade of errors
compiler.blacklist-append      *gcc*

# NOTE: Now requires a minimum of Xcode Clang 10.0.1, as of v3.6.x
# However, runtime crashes occur with Xcode Clang 10.x and 11.x, so be sure to avoid both
# See: https://trac.macports.org/ticket/64252
compiler.blacklist-append {clang < 1200}

#--------------------------------------------------------------------------------------------------
# Blacklist MacPorts Clang 14 and newer (up to 19) for now, due to compilation crashes.
# See: https://trac.macports.org/ticket/65246
#--------------------------------------------------------------------------------------------------
#compiler.blacklist-append {macports-clang-1[4-9]}

variant x11 conflicts quartz {
    require_active_variants gtk3 x11 quartz
    configure.args-append   -DUSE_MAC_INTEGRATION=OFF
}

variant quartz conflicts x11 {
    require_active_variants gtk3 quartz x11
    depends_lib-append      port:gtk-osx-application-gtk3
}

if {![variant_isset quartz]} {
    default_variants-append +x11
}

variant openmp description {enable support for OpenMP} {
    configure.args-replace  -DUSE_OPENMP=OFF -DUSE_OPENMP=ON
}
if {[variant_isset openmp]} {
    compiler.openmp_version 4.5
    compiler.log_verbose_output no
}

livecheck.url       https://github.com/darktable-org/darktable/releases
livecheck.regex     {release (\d+(?:\.\d+)*)}
