# -*- 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
PortGroup                   muniversal 1.0

if {${os.arch} eq "powerpc"} {
    set xtools_version      6f6c04e8b25497851f6a5979a6e96023fffe22df
    github.setup            iains darwin-xtools ${xtools_version}
    version                 2.2.4
    revision                1
    epoch                   1

    checksums               ${name}-${xtools_version}.tar.gz \
                            rmd160  07dd50453b93f90ea4806fd8c12be3acb91c2fbd \
                            sha256  7568104068614b8e8b1cb72fcc01219f78f90a0d14d95c255be78e2c0a899d62 \
                            size    2932788
} else {
    set xtools_version      6446947f3e99db52a40b30a38f36b9ae33492aea
    github.setup            iains darwin-xtools ${xtools_version}
    version                 3.3.0
    revision                1
    epoch                   0

    checksums               ${name}-${xtools_version}.tar.gz \
                            rmd160  18d99473a012f4fa48464aadd078921a25bdc322 \
                            sha256  9167b67e128f616cef079bbeda4b116fe0d41fe54784fff85826420daae23f5f \
                            size    2958759
}

set libyaml_version         0.2.5
set libyaml_distfile        ${libyaml_version}.tar.gz
distfiles-append            ${libyaml_distfile}:libyaml
checksums-append            ${libyaml_distfile} \
                            rmd160  7fe42b7d7bb0dd07aedee6c775c43fd35bb5cf3e \
                            sha256  fa240dbf262be053f3898006d502d514936c818e422afdcf33921c63bed9bf2e \
                            size    85055

github.tarball_from         archive

platforms                   darwin
categories                  devel
maintainers                 nomaintainer

description                 Aset of modifications to ctools and ld64 that enable support for older systems with up-to-date-tools.
long_description            {*}${description}
license                     {APSL-2 Apache-2}

homepage                    https://github.com/iains/darwin-xtools

master_sites-append         https://github.com/yaml/libyaml/archive/refs/tags:libyaml

patchfiles-append           0001-ld64-unwinddump-include-stdlib-for-exit-3.patch \
                            0002-ld64-archive_file-fix-compilation-without-LTO_SUPPOR.patch \
                            0003-cctools-cmake-as-fix-compilation-on-powerpc-apple-da.patch \
                            0004-cctools-otool-fix-duplicate-symbol-in-arm-i386-_disa.patch \
                            0005-cctools-cmake-cbtlibs-link-against-libstuff-for-vari.patch \
                            0006-ld64-cmake-build-libprunetrie-as-static-archive.patch \
                            0007-tapilite-add-TAPI-emulation-library.patch \
                            0008-ld64-cmake-use-normal-cmake-trickery-to-link-against.patch \
                            0009-tapilite-bunch-of-fixes-and-changes.patch \
                            0010-tapilite-cmake-explitly-build-static-library.patch \
                            0011-ld64-fix-compilation-on-arm64.patch \
                            0012-cctools-fix-build-on-macOS-monterey.patch \
                            0013-libyaml-add-the-way-to-include-it.patch \
                            0014-cctools-removed-redundant-implementation-of-get_toc_.patch \
                            0015-tapilite-fix-build-by-clang.patch

platform darwin powerpc {
    patchfiles-append       0016-Minor-fix-ups-for-ppc.patch
}

post-extract {
    ln -s ${workpath}/libyaml-${libyaml_version} ${worksrcpath}/libyaml
}

# Use cmake-bootstrap
depends_build-replace       path:bin/cmake:cmake port:cmake-bootstrap
depends_skip_archcheck-append \
                            cmake-bootstrap
configure.cmd               ${prefix}/libexec/cmake-bootstrap/bin/cmake

# prevent findng or linking against any other ports
compiler.cpath
compiler.library_path
configure.cppflags-delete -I${prefix}/include
configure.ldflags-delete  -L${prefix}/lib

compiler.cxx_standard       2011

merger_arch_flag            yes
merger_arch_compiler        yes
merger_must_run_binaries    yes

if {${os.platform} eq "darwin" && ${os.major} < 12} {
    depends_build-append    port:gcc10-bootstrap
    depends_skip_archcheck-append \
                            gcc10-bootstrap

    # Leopard is the first macOS which supports Roseta
    # See: https://trac.macports.org/ticket/67284
    if {${os.major} > 8} {
        pre-configure {
            file mkdir ${workpath}/bins

            set gcc [open "${workpath}/bins/gcc" w 0755]
            puts ${gcc} "#!/bin/sh"
            puts ${gcc} "arch -arch $\{BUILD_ARCH:-${build_arch}\} ${prefix}/libexec/gcc10-bootstrap/bin/gcc \"\$@\""
            close ${gcc}

            set gxx [open "${workpath}/bins/g++" w 0755]
            puts ${gxx} "#!/bin/sh"
            puts ${gxx} "arch -arch $\{BUILD_ARCH:-${build_arch}\} ${prefix}/libexec/gcc10-bootstrap/bin/g++ \"\$@\""
            close ${gxx}
        }

        configure.cc        ${workpath}/bins/gcc
        configure.cxx       ${workpath}/bins/g++

        if {[variant_exists universal] && [variant_isset universal]} {
            foreach arch ${universal_archs_supported} {
                lappend merger_configure_env(${arch}) BUILD_ARCH=${arch}
                lappend merger_build_env(${arch}) BUILD_ARCH=${arch}
            }
        }
    } else {
        configure.cc        ${prefix}/libexec/gcc10-bootstrap/bin/gcc
        configure.cxx       ${prefix}/libexec/gcc10-bootstrap/bin/g++
    }

    # prevent it from linking against gcc's libstdc++.6.dylib and libgcc_s.1.1.dylib
    configure.ldflags-append \
                            -static-libstdc++ -static-libgcc

    configure.compiler.add_deps no
}

cmake.install_prefix        ${prefix}/libexec/${name}

cmake.build_type            Release
cmake.generator             {Unix Makefiles}

configure.args-append       -DCCTOOLS_LD_CLASSIC=OFF \
                            -DXTOOLS_LIBYAML_PATH=${worksrcpath}/libyaml \
                            -DXTOOLS_USE_TAPILITE=ON

# 64bit LD requires futures patches
if {${build_arch} ni [list i386 ppc]} {
    configure.args-append   -DXTOOLS_HOST_IS_64B=ON
} else {
    configure.args-append   -DXTOOLS_HOST_IS_64B=OFF
}

post-destroot {
    file delete -force {*}[glob -directory ${destroot}${cmake.install_prefix} \
                           cmake/yaml* include/yaml.h lib/libyaml*]
}

github.livecheck.branch     darwin-xtools-[string map {. -} ${version}]
