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

# faccessat
legacysupport.newest_darwin_requires_legacy 13

github.setup        ocaml opam 2.5.2
revision            0
categories          ocaml sysutils
license             LGPL-2
maintainers         {@pmetzger pmetzger} openmaintainer
homepage            http://opam.ocaml.org
description         OPAM is a package manager for OCaml
long_description    OPAM is a source-based package manager for OCaml. \
                    It supports multiple simultaneous compiler installations, \
                    flexible package constraints, and a Git-friendly \
                    development workflow.

github.tarball_from releases
distname            opam-full-${version}
checksums           rmd160  d376e8cf5b15fa5f849f844bbb4ecf783419c6b6 \
                    sha256  b3623809567f19ed6b5d679b8c7bbc0bdec9418bff4a875ff0799d446d8555c3 \
                    size    14474162

depends_build       port:ocaml
# Opam upgrade uses patch to update the package repository,
# this can silently fail with macos patch when files are
# deleted upstream.
depends_lib         port:gpatch

# This prevents configure from trying to use a program to fetch dependencies.
# We don't need or want one.
configure.env-append  FETCH=no

# * patch-stop-fetch-check.diff dikes out a test that will prevent us
#   from disabling fetch. Otherwise FETCH=no will fail and we'll need to
#   depend on curl or wget.
# * patch-makefile.diff is a kludge to put documentation files into
#   share/doc/opam-installer – without this, they'd end up in
#   doc/opam-installer, and there is no /opt/local/doc.
patchfiles          patch-stop-fetch-check.diff \
                    patch-makefile.diff

# Patch updated due to https://github.com/ocaml/opam/issues/6822
post-extract {
    xinstall -d ${worksrcpath}/src_ext/patches/dune-local
    copy ${filespath}/fix-bundled-dune.patch \
                    ${worksrcpath}/src_ext/patches/dune-local
}

use_parallel_build  no
build.target        all
configure.args-append --with-vendored-deps

# Match OCaml settings:
compiler.blacklist  *gcc-4.0 *gcc-4.2 {clang < 400}

if {[string match *gcc* ${configure.compiler}]} {
    # Temporary fix for:
    # https://github.com/ocaml-opam/ocaml-mccs/issues/51
    # https://github.com/ocaml/dune/issues/949
    configure.args-append \
                    --without-mccs
}

platform darwin {
    if {${os.major} < 14} {
        patchfiles-append \
                    patch-link-legacysupport.diff

        post-patch {
            reinplace "s|@PREFIX@|${prefix}|" ${worksrcpath}/src/core/dune
        }
    }

    if {${os.major} < 11} {
        # Matching ocaml 1.1 PG setting.
        # See: https://github.com/ocaml/dune/issues/8941
        build.env-append \
                    DUNE_CONFIG__COPY_FILE=portable
    }

    # FIXME: 2.5.0 segfaults during dune build.
    platform darwin 9 {
        github.setup        ocaml opam-full 2.3.0
        revision            0
        checksums           rmd160  af863ac8afccbdd3e825e3fe1fffcdf66d128993 \
                            sha256  506ba76865dc315b67df9aa89e7abd5c1a897a7f0a92d7b2694974fdc532b346 \
                            size    12860518
    }

    # 2.3 is the last version that works with the version of dune that doesn't use libproc.h
    platform darwin 8 {
        github.setup        ocaml opam-full 2.3.0
        revision            1
        checksums           rmd160  af863ac8afccbdd3e825e3fe1fffcdf66d128993 \
                            sha256  506ba76865dc315b67df9aa89e7abd5c1a897a7f0a92d7b2694974fdc532b346 \
                            size    12860518

        depends_build-append    port:gmake
        build.cmd               ${prefix}/bin/gmake
    }
}
