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

name                    sl-ld64
set real_name           ld64
version                 97.17
revision                0
set dyld_version        655.1.1
categories              devel
maintainers             {@barracuda156 macos-powerpc.org:barracuda}
homepage                http://opensource.apple.com/source/${real_name}/
master_sites            https://github.com/apple-oss-distributions/${real_name}/archive/:ld64 \
                        https://github.com/apple-oss-distributions/dyld/archive/:dyld
license                 APSL-2
installs_libs           no
description             ld64 is the new mach-o linker
long_description        ld64 combines several object files and libraries, \
                        resolves references and produces an ouput file.

checksums               dyld-655.1.1.tar.gz \
                        rmd160  3ce28be9a3eb52777443828853040d4c870a5db2 \
                        sha256  67326487dd139a82f0da9f43eb91414041551ee54cacf4763052e5aae606f518 \
                        size    872742

set makefile            "Makefile-97"
set ld64_ver            97

compiler.blacklist-append \
                        *clang* *llvm-gcc-4.2 *gcc-4.0

if {${subport} eq ${name}} {
    checksums-append    ld64-97.17.tar.gz \
                        rmd160  e9e51b2b219fb094a4744b307fe14badac5d74bd \
                        sha256  dc609d295365f8f5853b45e8dbcb44ca85e7dbc7a530e6fb5342f81d3c042db5 \
                        size    418239

    patch_sites         https://distfiles.macports.org/${real_name}/
    checksums-append    ld64-97-standalone-libunwind-headers.patch \
                        rmd160  f6da71e097aa61b1055b3fdc12cd39aafed5f492 \
                        sha256  370d02757ea628b5dd145c099e42fc4eb88cc09cf459a59e32d14bbc9b4a105e \
                        size    141802

    patchfiles-append \
        ld64-version.patch \
        ld64-97-no-LTO.patch \
        ld64-97-ppc-branch-island.patch \
        ld64-97-arm_types_PR38931.patch \
        ld64-97-long-branch-warn.patch \
        ld64-97-standalone-libunwind-headers.patch \
        ld64-97-no-Availability.h.patch \
        ld64-97-BaseAtomImplicitDecl.patch \
        ld64-97-no-ppc-thread_status.patch \
        ld64-97-configured_architectures.patch
}

subport sl-ld64-devel {
    # Xcode 4.2
    version             127.2
    revision            0

    set makefile        "Makefile-127"
    set ld64_ver        127

    checksums-append    ld64-127.2.tar.gz \
                        rmd160  94b1bda67084f52417d3ad465795f4eb26b44b9e \
                        sha256  acfc5c3340a24ad8696ff22027b121cb3807c75fade7dec48b2b52494698e05f \
                        size    492334

    patchfiles-append \
        ld64-version.patch \
        ld64-134-missing-include.patch \
        ld64-127-any-cctools.patch \
        ld64-127-long-branch-warn.patch \
        ld64-127-cxx_initializer_order_and_sim.patch \
        ld64-127-ppc.patch \
        ld64-127-configured_architectures.patch \
        ld64-127-align_file_offset_with_address_layout.patch \
        ld64-127-alignment_padding.patch \
        ld64-127-filter_out_zero_length_functions.patch \
        ld64-127-i386-reloc.patch \
        ld64-127-malformed_object_crash.patch \
        ld64-127-no_ld_classic.patch \
        ld64-127-static_slidable.patch \
        ld64-97-no-Availability.h.patch \
        PR-12249048.patch \
        PR-12057707.patch \
        PR-13282463.patch \
        PR-29117886.patch \
        ld64-ppc-9610466.patch

    # libSystem in 10a190 does not have the symbol this needs.
    # Still valid for 10.6.8 ppc though?
    if  {${os.major} == 10 && ${configure.build_arch} eq "ppc"} {
        patchfiles-append ld64-127-no-compressed-dyldinfo-on-PPCSL.diff
    }
}

variant universal   {}

distfiles           dyld-${dyld_version}.tar.gz:dyld \
                    ${real_name}-${version}.tar.gz:ld64

depends_build       path:include/mach-o/arm/reloc.h:libmacho-headers

configure.cxx_stdlib        libstdc++

worksrcdir          ${workpath}/${real_name}-${version}

post-extract {
    move ${workpath}/ld64-ld64-${version} ${workpath}/ld64-${version}
    move ${workpath}/dyld-dyld-${dyld_version} ${workpath}/dyld-${dyld_version}
    file copy ${filespath}/${makefile} ${worksrcpath}/Makefile
}

post-patch {
    reinplace "s|@@VERSION@@|${version}|g" ${worksrcpath}/src/ld/Options.cpp

    system "cd ${workpath}/dyld-${dyld_version} && patch -p0 < ${filespath}/dyld-655-no-blocks.patch"

    # What is in this patch might be done as reinplaces, as above.
    system "cd ${workpath}/dyld-${dyld_version} && patch -p0 < ${filespath}/dyld-655-availability.diff"

    # Add in some missing target conditionals that error out on newer compilers.
    system "cd ${workpath}/dyld-${dyld_version} && patch -p0 < ${filespath}/dyld-targetconditionals.diff"
}

configure {
    system "cd ${worksrcpath} && ${build.cmd} src/ld/configure.h"
}

set cxx_stdlibflags {}

configure.cppflags-append \
                    -I${workpath}/dyld-${dyld_version}/include

build.args \
        CC="${configure.cc}" \
        CXX="${configure.cxx}" \
        OTHER_CPPFLAGS="${configure.cppflags}" \
        OTHER_CFLAGS="${configure.cflags} [get_canonical_archflags cc]" \
        OTHER_CXXFLAGS="${configure.cxxflags} ${cxx_stdlibflags} [get_canonical_archflags cxx]" \
        OTHER_LDFLAGS="${configure.ldflags} ${cxx_stdlibflags} [get_canonical_archflags ld]"

destroot.args \
        PREFIX=/usr

post-destroot {
    delete {*}[glob ${destroot}/usr/share/man/man1/*]
}

if {"${configure.sdkroot}" eq ""} {
    configure.sdkroot "/"
}

package.flat            no

destroot.violate_mtree  yes

# Prevent direct installation.
known_fail yes
pre-activate {
    ui_error "Do not install this port directly."
    ui_error "It is used to build the installer."
    error "Installation aborted"
}

livecheck.type          none
