# -*- 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
maintainers         {michaelld @michaelld} openmaintainer
description         GLFW is a free, open-source, portable library for OpenGL and OpenGL ES application development
license             zlib
platforms           darwin macosx

# special build on Mac OS X <= 10.6
if {${os.platform} eq "darwin" && ${os.major} < 11} {

    # Mac OS X 10.6 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
    github.tarball_from tarball
    version         3.1.2-20151024
    checksums       rmd160  1c07a75a88f272653ca246aa1212a208a83ecb30 \
                    sha256  ff4745264f92b740c50ecc90370f102c86c439f2b6108f1495bd60c87e5d6a83 \
                    size    366040
    revision        0

    # 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

    # Cocoa does not build on 10.5 and powerpc due to CoreGraphics references, use X11.
    # TODO: consider using a newer version with X11 backend.
    if {${os.major} < 10 || ${configure.build_arch} eq "ppc"} {
        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.7-8: use the latest commit supporting this OS version
    github.setup    glfw glfw 3.2.1
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    checksums       rmd160  5fb5988736a0a2812426907ab8d64b94eeacc010 \
                    sha256  34bc25f8111501eec35a52fd39fa50336a0c2e812d4a14454c7c946458ab015c \
                    size    472539

    long_description ${description}. This version of GLFW is the latest to provide support for \
        Mac OS X 10.7 and 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

} 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
        github.tarball_from archive
        checksums   rmd160  17386aca3759e361fa888c4bea85e355f87a2129 \
                    sha256  c038d34200234d071fae9345bc455e4a8f2f544ab60150765d7704e08f3dac01 \
                    size    936948
        revision    0

        # bump the epoch because I moved the version from 20151012 to 3.1.2

        epoch           1

        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

    } else {

        # devel

        github.setup glfw glfw ac10768495837eb98da27d01fe706073d6d251c2
        # Change github.tarball_from to 'releases' or 'archive' next update
        github.tarball_from tarball
        version     20250718-[string range ${github.version} 0 7]
        checksums   rmd160  c9160504f83216a7bc5246c87c0d1306d8e5ded1 \
                    sha256  19ca71b6bc738cefd89cb3cb67c4eb779aa4fed17c4f7e04a37cd9fe15d87645 \
                    size    910215
        revision    0

        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

        # 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

    }
}

# use the real home page, not github's

homepage            https://www.glfw.org

# do VPATH (out of source tree) build

cmake.out_of_source yes

# 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=
}
