# -*- 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       clang_dependency 1.0

name            icu
set my_name     icu4c
# Don't forget to increase the revision number of the dependents (e.g. boost)
# whenever the library version number changes. Thanks.
# To find dependencies, use:
# port file all | sort -u | xargs grep -El ':icu( |$)' | rev | cut -d / -f 2 | rev | sort -u
version                     67.1
if {${subport} eq "${name}58"} {
version                     58.3
}
categories      devel textproc
platforms       darwin freebsd
maintainers                 nomaintainer
license                     Permissive

description     International Components for Unicode

long_description \
    The International Components for Unicode (ICU) libraries provide robust \
    and full-featured Unicode services on a wide variety of platforms. ICU \
    supports the most current version of the Unicode standard, and they provide \
    support for supplementary Unicode characters (needed for GB 18030 repertoire support).

homepage                    http://site.icu-project.org/
master_sites    https://github.com/unicode-org/icu/releases/download/release-[string map {. -} ${version}]/

distname        ${my_name}-[string map {. _} ${version}]-src
extract.suffix  .tgz
worksrcdir      ${name}/source

checksums                   rmd160  c4a84ef7d2e18d6d1374b5ed6449227b94550e14 \
                            sha256  94a80cd6f251a53bd2a997f6f1b5ac6653fe791dfab66e1eb0227740fb86d5dc \
                            size    24518055

if {${os.major} >= 11 || ${subport} eq "${name}58"} {
    configure.python        /usr/bin/python
} else {
    depends_build-append    port:python27
    license_noconflict      python27
    configure.python        ${prefix}/bin/python2.7
}

subport ${name}58 {
    revision                1
    configure.pre_args      --prefix=${prefix}/libexec/${subport}

    patchfiles-append       CVE-2017-7867-CVE-2017-7868.patch \
                            patch-bootstrap_config-mh-darwin.diff
    checksums               rmd160  0b1ad8ea9bccd5d0f96a89bd0cdc12b5751ed46e \
                            sha256  2680f3c547cd26cba1d7ebd819cd336ff92cf444a270e195fd3b10bfdf22276c \
                            size    22626103
}

if { [vercmp ${version} 59] >= 0 } {
    compiler.cxx_standard   2011
}

set docdir      ${prefix}/share/doc/${name}

configure.args              --disable-layoutex \
                            --disable-samples \
                            --enable-static

subport ${name}-docs {
    revision                0
    conflicts           ${name}-doxygen-docs
    supported_archs     noarch

    description-append  (documentation)
    long_description-append Documentation.

    use_zip             yes
    distname                ${my_name}-[string map {. _} ${version}]-docs
    checksums               rmd160  5132c05255a982017540088fa5a133237f22510e \
                            sha256  d5a2b188a3bd55252d5fbcce75d563ec1ab574e31de626b40dc7b797f03d89b4 \
                            size    7913999

    extract.dir         ${worksrcpath}/doc/html

    use_configure       no

    build {}

    destroot {
        xinstall -m 755 -d ${destroot}${docdir}
        copy ${worksrcpath}/doc/html ${destroot}${docdir}
    }
}

subport ${name}-doxygen-docs {
    revision                0
    conflicts           ${name}-docs
    supported_archs     noarch

    depends_build-append    port:doxygen

    description-append  (build documentation)
    long_description-append Build documentation.

    build.target     doc
    destroot.target  install-doc
}

subport ${name}-lx {
    revision                0
    # http://userguide.icu-project.org/layoutengine/paragraph
    description             ICU paragraph layout library
    long_description        {*}${description}

    depends_build-append    port:pkgconfig
    depends_lib-append      port:icu \
                            port:icu-le-hb

    configure.args-replace  --disable-layoutex --enable-layoutex

    destroot.dir            ${worksrcpath}/layoutex
}

if {${subport} eq ${name}} {
    revision                3
}

if { ${subport} ne "${name}-docs" } {
    if { ${supported_archs} ne "noarch" } {
        PortGroup       muniversal 1.0
    }

    if { [vercmp ${version} 59] >= 0 } {
        patchfiles-append   patch-config-mh-darwin.diff

        patchfiles-append patch-declspec-conflict.patch
        # https://trac.macports.org/ticket/60325

        patchfiles-append patch-i18n-formatted_string_builder.h.diff
    }

    # ICU has three mechanisms to aid other projects in building properly
    #     1) .pc files for pkg-config
    #     2) shell script icu-config that can output compiler flags
    #     3) Makefile include files Makefile.inc and pkgdata.inc
    # For a time, all three assumed other projects would want to be
    #     built as ICU had been built, regardless of whether it was appropriate.
    # For the most part, the extraneous flags have been removed.
    #     1) They have been removed completely from the .pc files.
    #        pkg-config is the recommended way of building against ICU.
    #     2) icu-config does not print them out, but they are still contained in the script itself.
    #     3) They still exist in the Makefile include files.
    #
    # for descriptions of the problem, see
    #     r101578
    #     #40069
    #     https://bugs.gentoo.org/show_bug.cgi?id=202059
    #
    # for upstream reports and discussion, see
    #    http://bugs.icu-project.org/trac/ticket/10308
    #    http://bugs.icu-project.org/trac/ticket/6102
    if {${supported_archs} ne "noarch"} {
        post-build {
            if {[variant_exists universal] && [variant_isset universal]} {
                set dirs {}
                foreach arch ${universal_archs_to_use} {
                    lappend dirs ${worksrcpath}-${arch}
                }
            } else {
                set dirs ${worksrcpath}
            }
            foreach dir ${dirs} {
                # Removing architecture-specific differences is necessary to facilitate merging.
                # This should not be a problem for projects using icu-config because we do not
                # believe icu-config uses SIZEOF_VOID_P anyway. Projects using the Makefile
                # include files may experience problems. The developers of ICU suggest using
                # pkg-config over both icu-config and the Makefile includes.
                reinplace {s| -DSIZEOF_VOID_P=[48]||g} \
                    ${dir}/config/Makefile.inc \
                    ${dir}/config/icu-config
                reinplace -E {s| -arch +[^ ]+||g} \
                    ${dir}/config/pkgdata.inc

                # remove architecture-specific host information
                # calling icu-config for the host information is not mentioned in any
                # icu man pages as an option, differs between architectures, and can't be merged
                # https://trac.macports.org/ticket/45268
                reinplace {s|host=\".*\"|host=\"\"|g} \
                    ${dir}/config/icu-config
                reinplace {s|host_alias=\".*\"|host_alias=\"\"|g} \
                    ${dir}/config/icu-config
                reinplace {s|host_cpu=\".*\"|host_cpu=\"\"|g} \
                    ${dir}/config/icu-config
            }
        }
    }

    set platform [switch ${os.platform} {darwin {format MacOSX} freebsd {format FreeBSD}}]
    configure.cmd   ./runConfigureICU ${platform}

    configure.universal_args-delete --disable-dependency-tracking
    if {[variant_isset universal]} {
        # ICU detects cross compiling by trying to run binaries.
        # r73849 and #29904 indicate that cross compiling is a problem.
        # Of the solutions tried (r79660, r73865, and r73849), simply disallowing cross compiling is by far the simplest.
        # Note that #29904 should not show up again because universal now better recognizes 32/64-bit cross compiling (r79658)
        set merger_must_run_binaries "yes"
    }

    # Fix bug #11981 that prevents ICU from building when upgrading.
    # The default configure flags causes utilisation of outdated ICU
    # headers/libs instead of the right ones.
    configure.cppflags
    configure.ldflags

    build.type      gnu
    build.args      VERBOSE=1

    test.run        yes
    test.target     check
    test.args       VERBOSE=1

    if { ${name} eq ${subport} } {
        post-destroot {
            xinstall -d ${destroot}${docdir}
            xinstall -m 0644 {*}[glob ${worksrcpath}/../*.{css,html,txt}] ${destroot}${docdir}
        }
    }

    platform freebsd {
        build.env       MAKE=/usr/local/bin/gmake
        destroot.env    MAKE=/usr/local/bin/gmake
    }

    platform darwin 8 {
        if { [vercmp ${version} 59] < 0 } {
            patchfiles-append patch-bootstrap_common-putil.cpp.diff
        } else {
        patchfiles-append patch-common-putil.cpp.diff
    }
    }

    if {${build_arch} eq "ppc" || ${build_arch} eq "ppc64"} {
        # see https://trac.macports.org/ticket/59514
        post-extract {
            xinstall -d -m 0755 ${worksrcpath}/data/out
        }
    }
}

if {${subport} eq ${name}} {
    livecheck.url       http://site.icu-project.org/download
    livecheck.regex     "ICU4C-Download\">(\[0-9.\]+)"
} else {
    livecheck.type      none
}
