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

name                freeimage
version             3.18.0
checksums           rmd160  b791715fccf49355a3cb27b6250d8ed809c2454e \
                    sha256  f41379682f9ada94ea7b34fe86bf9ee00935a3147be41b6569c9605a53e438fd \
                    size    7415716

set major           [lindex [split ${version} .] 0]
categories          graphics
platforms           macosx
license             {FreeImage-1 GPL-2 GPL-3}

maintainers         nomaintainer

description         Library for FreeImage, a dependency-less graphics library

long_description    FreeImage is a library for developers who would like to \
                    support most popular graphics image formats. Some \
                    highlights are: extremely simple in use, not limited to \
                    the local PC (unique FreeImageIO) and Plugin driven!

homepage            https://freeimage.sourceforge.io
master_sites        sourceforge:project/freeimage/Source%20Distribution/${version}

distname            FreeImage[strsed ${version} {g/\.//}]
use_zip             yes
worksrcdir          FreeImage

patchfiles          patch-c99-fixes.diff
# https://github.com/AcademySoftwareFoundation/openexr/pull/126
patchfiles-append   patch-fix-ImfFastHuf.diff
# https://sourceforge.net/p/freeimage/patches/153
patchfiles-append   patch-fix-PluginBMP.diff \
                    patch-fix-PluginDDS.diff

post-patch {
    set makefiles   "${worksrcpath}/Makefile.fip ${worksrcpath}/Makefile.gnu"

    # Libraries extension is ".dylib" on Darwin, not ".so"
    reinplace {s/\.so/.dylib/} {*}${makefiles}

    # A dot separates the library name from its version on Darwin
    reinplace /^SHAREDLIB/s/-/./ {*}${makefiles}

    # Use libtool(1) instead of ar(1)
    reinplace {s/\$(AR) r/libtool -o/} {*}${makefiles}

    # Do not force installation as root
    reinplace {s/-o root -g root//} {*}${makefiles}

    # Darwin requires different arguments to build dynamic libraries
    reinplace {s|-shared -Wl,-soname,$(VERLIBNAME)|-dynamiclib -install_name $(PREFIX)/lib/$(VERLIBNAME) -compatibility_version $(VER_MAJOR) -current_version $(VER_MAJOR).$(VER_MINOR)|} {*}${makefiles}

    # Introduce HAVE_UNISTD_H to avoid C99 issues in zlib.
    reinplace {s/-D__ANSI__/-D__ANSI__ -DHAVE_UNISTD_H/} {*}${makefiles}

    # Use CXX not CC to link the library, since it is made up of C++ code.
    reinplace {/\$(LIBRARIES)/s/$(CC)/$(CXX)/} {*}${makefiles}

    # Do not hardcode a C++ library name. We set this later in CXX.
    reinplace /^LIBRARIES/s/-lstdc++// {*}${makefiles}

    # Darwin does not have a command ldconfig
    reinplace s/ldconfig// ${worksrcpath}/Makefile.gnu
    # FreeImagePlus doesn't use ldconfig
}

use_configure       no

platform darwin i386 {
    # uses cpuid function
    # https://trac.macports.org/ticket/60624
    compiler.blacklist-append *gcc-3.* *gcc-4.*
}

# In the future, implement in the build phase?
#configure.pipe      no
#configure.ccache    no
#configure.distcc    no

# FreeImage uses -O3, which is set in the Makefiles
configure.optflags-delete -Os

# Not using any MacPorts libraries.
configure.cppflags-delete   -I${prefix}/include
configure.ldflags-delete    -L${prefix}/lib

# Disable ARM optimizations in bundled libpng because the source files
# that contain the ARM optimization code were not included.
if {"arm64" in [get_canonical_archs]} {
    configure.cppflags-append -DPNG_ARM_NEON_OPT=0
}

variant universal {}

# Reimplement parts of portconfigure.tcl
if {[string match *clang* ${configure.cxx}] && ${configure.cxx_stdlib} ne ""} {
    configure.cxxflags-append -stdlib=${configure.cxx_stdlib}
    configure.ldflags-append  -stdlib=${configure.cxx_stdlib}
}

if {${configure.sdkroot} ne ""} {
    configure.ldflags-append -Wl,-syslibroot,${configure.sdkroot}
}

foreach flags {cflags cxxflags} compiler {cc cxx} {
    configure.${flags}-append {*}[get_canonical_archflags ${compiler}]
    
    if {${configure.sdkroot} ne ""} {
        configure.${flags}-append -isysroot${configure.sdkroot}
    }
    
    if {${configure.march} ne "" } {
        configure.${flags}-append -march=${configure.march}
    }
    
    if {${configure.mtune} ne "" } {
        configure.${flags}-append -mtune=${configure.mtune}
    }
}

# running build_main and destroot_main makes use of environment variables like MACOSX_DEPLOYMENT_TARGET

build {
    foreach makefile {Makefile.gnu Makefile.fip} libname {libfreeimage libfreeimageplus} {
        # Makefile overrides CFLAGS and CXXFLAGS, so append options to CC and CXX
        build.args -f ${makefile} \
            PREFIX=${prefix} \
            VERLIBNAME=${libname}.${major}.dylib \
            CC="${configure.cc} ${configure.cppflags} ${configure.cflags}" \
            CXX="${configure.cxx} ${configure.cppflags} ${configure.cxxflags}" \
            LDFLAGS="${configure.ldflags}"
        
        portbuild::build_main
    }
}

destroot {
    foreach makefile {Makefile.gnu Makefile.fip} libname {libfreeimage libfreeimageplus} {
        destroot.args -f ${makefile} \
            PREFIX="${prefix}" \
            VERLIBNAME=${libname}.${major}.dylib \
            INCDIR="${destroot}${prefix}/include" \
            INSTALLDIR="${destroot}${prefix}/lib"
        
        portdestroot::destroot_main
    }
}

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 644 -W ${worksrcpath} Whatsnew.txt license-fi.txt \
        license-gplv2.txt license-gplv3.txt ${destroot}${docdir}
    copy ${worksrcpath}/Examples ${destroot}${docdir}/examples
}

test.run            yes
test.cmd            ${build.cmd} && ./testAPI
test.dir            ${worksrcpath}/TestAPI
test.target

post-patch {
    # Use correct compiler and flags when compiling test
    reinplace "s:g++:${configure.cxx} ${configure.cxxflags}:" ${test.dir}/Makefile
}

livecheck.type      regex
livecheck.regex     {FreeImage (\d+(?:\.\d+)*) released}