# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           cmake 1.1
PortGroup           github 1.0

name                glfw
categories          graphics
platforms           darwin macosx
license             zlib
maintainers         {michaelld @michaelld} \
                    {@barracuda156 macos-powerpc.org:barracuda} \
                    openmaintainer
homepage            https://www.glfw.org
description         GLFW is a free, open-source, portable library for OpenGL \
                    and OpenGL ES application development

# Special build on Mac OS X < 10.6
if {${os.platform} eq "darwin" && ${os.major} < 10} {
    # Mac OS X 10.5 and earlier: use the latest commit supporting those OS versions
    github.setup    glfw glfw a94a84b507b0d6d11e8a3f257cb21f4bd6553516
    # Change github.tarball_from to 'releases' or 'archive' next update

    version         3.1.2-20151024
    revision        1
    checksums       rmd160  1c07a75a88f272653ca246aa1212a208a83ecb30 \
                    sha256  ff4745264f92b740c50ecc90370f102c86c439f2b6108f1495bd60c87e5d6a83 \
                    size    366040
    github.tarball_from tarball

    # Drop this if version gets updated:
    platform darwin 10 powerpc {
        incr revision
    }

    # Bump the epoch because I moved the version from 20151012 to 3.1.2
    epoch           1

    long_description ${description}. This version of GLFW is the latest to provide support \
        for Mac OS X 10.6 and prior, and it will not be updated. It is provided in the hope \
        that it allows ports depending on GLFW to build on these older Mac OS X installs.

    # Patch to fix install directory for cmake files and docs
    patchfiles-append \
                    patch-CMakeLists.txt.legacy.diff

    # Backports:
    patchfiles-append \
                    0001-Cocoa-backport-c156b50e4c61f8d9d32d137f252190ead4d6f.patch \
                    0002-Cocoa-backport-9e56099eddf28b87dfb622dede6b84f8723dd.patch \
                    0003-Backport-a90ee65f7b0264a672634fc4f5891a2ab57a47ae.patch \
                    0004-Cocoa-backport-370eac3c48d00facd44ac0dc61c651232e417.patch \
                    0005-Cocoa-backport-a10caa46313d181b34d38be7bdded738e19ba.patch \
                    0006-Cocoa-cherry-pick-from-6570d0c4b7a1868ad8af4e61dd5f5.patch

    # Do not unnecessarily die on launch.
    patchfiles-append \
                    0007-Do-not-fail-on-non-standard-GL.patch

    # Do not lie about the version:
    post-patch {
        reinplace "s|GLFW_VERSION_MINOR \"2\"|GLFW_VERSION_MINOR \"1\"|" ${worksrcpath}/CMakeLists.txt
        reinplace "s|define GLFW_VERSION_MINOR          2|define GLFW_VERSION_MINOR          1|" ${worksrcpath}/include/GLFW/glfw3.h
    }

    # Cocoa does not build on 10.5 due to CoreGraphics references, use X11.
    # TODO: consider using a newer version with X11 backend. Or make it a variant?
    if {${os.major} < 10} {
        depends_lib-append \
                    port:xorg-libX11 \
                    port:xorg-libXcursor \
                    port:xorg-libXinerama \
                    port:xorg-libXrandr \
                    port:xorg-libXxf86vm
        patchfiles-append \
                    patch-use-x11.diff
    }

} elseif {${os.platform} eq "darwin" && (${os.major} == 11 || ${os.major} == 12)} {
    # Mac OS X 10.6-8: use the latest commit supporting this OS version
    github.setup    glfw glfw 3.2.1
    revision        1
    checksums       rmd160  5fb5988736a0a2812426907ab8d64b94eeacc010 \
                    sha256  34bc25f8111501eec35a52fd39fa50336a0c2e812d4a14454c7c946458ab015c \
                    size    472539
    github.tarball_from tarball

    long_description ${description}. This version of GLFW is the latest to provide support \
        for Mac OS X 10.6–10.8, and it will not be updated. It is provided in the hope \
        that it allows ports depending on GLFW to build on these older Mac OS X installs.

    patchfiles      patch-CMakeLists.txt.321.1.diff \
                    patch-CMakeLists.txt.321.2.diff

    # Backports:
    patchfiles-append \
                    0001-Cocoa-backport-c156b50e4c61f8d9d32d137f252190ead4d6f.patch \
                    0002-Cocoa-backport-9e56099eddf28b87dfb622dede6b84f8723dd.patch \
                    0003-Backport-a90ee65f7b0264a672634fc4f5891a2ab57a47ae.patch \
                    0004-Cocoa-backport-370eac3c48d00facd44ac0dc61c651232e417.patch

    # Special patches
    patchfiles-append \
                    0005-fix-Cocoa-for-10.6.patch \
                    0006-fix-segfault-on-launch.patch

    configure.args-append \
                    -DGLFW_USE_RETINA=off
} else {
    # Mac OS X 10.9 and newer: release and devel
    # Requires c11 support as of 82ca58da (20190305) for
    # <stdatomic.h>; 3.3 was released shortly after this change,
    # so this requirement is now for all modern GLFW.
    compiler.c_standard   2011
    # <stdatomic.h> support was introduced in Xcode 7.0
    compiler.blacklist-append {clang < 700}

    compiler.cxx_standard 2011

    configure.args-append -DCMAKE_C_STANDARD=11 -DCMAKE_C_EXTENSIONS=OFF

    subport glfw-devel {}

    if {${subport} eq ${name}} {
        # release
        github.setup glfw glfw 3.4
        revision    0
        # bump the epoch because I moved the version from 20151012 to 3.1.2
        epoch       1
        checksums   rmd160  17386aca3759e361fa888c4bea85e355f87a2129 \
                    sha256  c038d34200234d071fae9345bc455e4a8f2f544ab60150765d7704e08f3dac01 \
                    size    936948
        github.tarball_from archive

        conflicts   glfw-devel
        long_description ${description}: \
            This port follows the release version of GLFW, which is typically updated every 6 months. If for some reason this port does not build or function as desired, try the ${name}-devel port.

        # patch to fix install directory for cmake files and docs
        patchfiles-append patch-CMakeLists.txt.release.diff

        # Lost-cost update for 10.6 powerpc without large-scale changes to portfile.
        # TODO: once confirmed no regressions, 3.2.1 will be removed.
        platform darwin 10 {
            patchfiles-append \
                    0001-Cocoa-NSGL-Port-backend-to-build-on-Mac-OS-X-10.6-wi.patch \
                    0002-Cocoa-Guard-convertRectToScreen-for-the-10.6-SDK.patch \
                    0003-Cocoa-Guard-setRestorable-for-the-10.6-SDK.patch \
                    0004-Cocoa-Guard-loadNibNamed-owner-topLevelObjects-for-t.patch
        }
    } else {
        # devel
        github.setup glfw glfw ac10768495837eb98da27d01fe706073d6d251c2
        version     20250718-[string range ${github.version} 0 7]
        revision    0
        # because I messed up a version as "202105623-52d8347d_0", which
        # because of the messed up date is always greater than a correctly
        # formatted date, which means livecheck will never show a newer version.
        epoch       1
        checksums   rmd160  c9160504f83216a7bc5246c87c0d1306d8e5ded1 \
                    sha256  19ca71b6bc738cefd89cb3cb67c4eb779aa4fed17c4f7e04a37cd9fe15d87645 \
                    size    910215
        # Change github.tarball_from to 'releases' or 'archive' next update
        github.tarball_from tarball

        conflicts   glfw
        long_description ${description}: \
            This port follows the GIT master version of GLFW, which is typically updated every few days to weeks.

        # patch to fix install directory for cmake files and docs
        patchfiles-append patch-CMakeLists.txt.devel.diff
    }
}

# configuration arguments for MacPorts build

configure.args-append \
    -DBUILD_SHARED_LIBS=on \
    -DGLFW_BUILD_EXAMPLES=off \
    -DGLFW_BUILD_TESTS=off \
    -DGLFW_CMAKE_CONFIG_PATH=share \
    -DBUILD_BUILD_DOCS=off

# remove top-level library path, such that internal libraries are used
# instead of any already-installed ones.

configure.ldflags-delete -L${prefix}/lib

variant docs description {build documentation} {
    # Default value is ON
    configure.args-delete -DBUILD_BUILD_DOCS=off
}

if {${build_arch} ni [list ppc ppc64]} {
    # Doxygen fails on PPC with Bus error due to malloc issues.
    # See: https://github.com/iains/darwin-toolchains-start-here/discussions/20
    default_variants    +docs
}

if {[variant_isset docs]} {
    depends_build-append  path:bin/doxygen:doxygen
    configure.args-append -DDOXYGEN_EXECUTABLE=${prefix}/bin/doxygen
} else {
    configure.args-append -DDOXYGEN_EXECUTABLE=
}
