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

# This port is dedicated to PowerPC. This way we can upgrade it
# without any impact on any other systems.
name                        llvm-powerpc

# Based on: https://github.com/iains/LLVM-7-branch
# with additional modifications.

# At the moment we build this against libstdc++.
# TODO: add a variant to support libc++ via libcxx-powerpc.

version                     7.1.1
revision                    1

categories                  lang devel
maintainers                 {@barracuda156 gmail.com:vital.had}
license                     NCSA

description                 LLVM compiler infrastructure
long_description            {*}${description}. This is an experimental port \
                            for PowerPC systems.
platforms                   {darwin < 11}
# This port is not needed on other archs and rather not be used elsewhere.
supported_archs             ppc ppc64

set upstream_ver            7.1.0

homepage                    https://llvm.org
master_sites                https://releases.llvm.org/${upstream_ver}

depends_extract             port:xz-bootstrap
depends_skip_archcheck-append \
                            xz-bootstrap
use_xz                      yes
extract.suffix              .tar.xz

set suffix                  mp-powerpc
set sub_prefix              ${prefix}/libexec/llvm-${version}
worksrcdir                  llvm-${upstream_ver}.src

distfiles                   llvm-${upstream_ver}.src${extract.suffix}

checksums                   llvm-${upstream_ver}.src${extract.suffix} \
                            rmd160  0d972e6d46e8741ae06abbf4125375c1a762f79b \
                            sha256  1bcc9b285074ded87b88faaedddb88e6b5d6c331dfcfb57d7f3393dd622b3764 \
                            size    28313784

configure.cc                ${prefix}/libexec/gcc10-bootstrap/bin/gcc
configure.cxx               ${prefix}/libexec/gcc10-bootstrap/bin/g++
configure.compiler.add_deps no

# 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

depends_build-append        path:bin/pkg-config:pkgconfig

# TODO: does this still need a lib dependency on gcc10-bootstrap?
depends_lib-append          port:gcc10-bootstrap \
                            port:libedit \
                            port:libffi \
                            port:ncurses \
                            port:python27-bootstrap \
                            port:zlib

depends_skip_archcheck-append \
                            python27-bootstrap

depends_run                 path:bin/perl:perl5

patch.pre_args-replace      -p0 -p2

# Macports patches from llvm-7.0 port:
patchfiles \
    0001-MacPorts-Only-Don-t-embed-the-deployment-target-in-t.patch \
    0002-Define-EXC_MASK_CRASH-and-MACH_EXCEPTION_CODES-if-th.patch

# LLVM-relevant patches from https://github.com/iains/LLVM-7-branch
patchfiles-append \
    0001-dsymutil-Avoid-headers-with-block-declarations.patch \
    0002-llvm-DWARF.-CFI-Emit-DW_CFA_restore_extended-when-re.patch \
    0003-llvm-objc-Add-Apple-ObjCMetaData-library-copied-from.patch \
    0004-llvm-Arm-Add-instruction-alias-for-vmov-R-R-l.patch \
    0005-llvm-ARM-Make-.thumb_func-imply-.thumb.patch \
    0006-llvm-Chi-Hua-s-strnlen-patch.patch \
    0007-llvm-Darwin9-More-strncpy.patch \
    0008-llvm-Darwin9-For-earlier-Darwin-round-is-in-the-glob.patch \
    0009-llvm-Darwin9-pthread-on-D9-is-missing-pthread_setnam.patch \
    0010-llvm-Darwin-ppc-Allow-as-PC-value.patch \
    0011-llvm-Darwin-ppc-Use-the-same-separator-char-as-cctoo.patch \
    0012-llvm-PowerPC-Allow-Quoted-symbol-names.patch \
    0013-Initial-Version-on-7.0.0.patch \
    0026-llas-Default-to-DWARF-2-for-now.patch \
    0041-dsymutil-WIP-DEBUG-work-on-GCC-LTO-problem.patch \
    0042-llvm-dsymutil-Mark-debug-stuff-as-debug.patch \
    0043-llvm-ppc-Use-bcl-20-31-picbase_label-instead-of-bl.patch \
    0044-llvm-Darwin-ppc-Improve-support-for-Mach-O-relocs.patch \
    0045-llvm-ppc-Revise-Darwin-Directive-to-MCPU.patch \
    0046-llvm-Darwin-Allow-CPU-SubType-override-from-assemble.patch \
    0047-llvm-Darwin-PPC-Allow-ppc-as-an-internal-arch-name.patch \
    0048-llvm-Add-ppc64-thread-state.patch \
    0049-llvm-Add-ppc64-to-checkThreadCommand.patch \
    0058-llvm-Add-support-for-force_cpusubtype_ALL-and-n.patch \
    0059-llvm-Add-support-for-selective-disable-of-compact_un.patch \
    0061-LLVM-Darwin-ppc-Make-DL-reflect-i1-32b-size-and-alig.patch \
    0062-llvm-darwin-ppc-Fix-up-DL-for-powerpc-alignment-rule.patch \
    0063-llas-Handle-no_compact_unwind.patch \
    0064-llas-Don-t-report-support-for-compressed-debug-disab.patch \
    0072-LLVM-ppc-Initial-impl.-of-check-for-LO14-reloc-valid.patch \
    0073-Backports-for-AVX512-from-LLVM9.patch \
    0074-7.1.1-PowerPC-Darwin-Update-version.patch \
    0075-llvm-Darwin-ppc-Darwin-s-default-relocation-model-is.patch \

# Extra patches
# patchfiles-append \
    0082-Fix-for-missing-gethostuuid-on-powerpc.patch

# Unlike with libxml2, libxar of 10.6 is too old to be used.
# We disable support for xar by default and enable it below.
patchfiles-append \
    patch-disable-xar.diff

platform darwin 8 {
    patchfiles-append Only-call-setpriority-PRIO_DARWIN_THREAD-0-PRIO_DARW.patch
}

# Actual compiler is set above to gcc10-bootstrap, do not backlist gcc-4.2
# to avoid issues when no other standard gcc is available.
compiler.blacklist          *clang* *gcc-4.0 macports-gcc-5

cmake.build_type            Release

configure.post_args         ../${worksrcdir}
default configure.dir       {${workpath}/build}
default build.dir           {${workpath}/build}

cmake.install_prefix        ${sub_prefix}

configure.args-delete \
        -DCMAKE_INSTALL_NAME_DIR=${cmake.install_prefix}/lib \
        -DCMAKE_INSTALL_RPATH=${cmake.install_prefix}/lib \
        -DCMAKE_SYSTEM_PREFIX_PATH="${prefix}\;/usr"
configure.args-append \
        -DCMAKE_SYSTEM_PREFIX_PATH="${cmake.install_prefix}\;${prefix}\;/usr"

configure.args-append \
        -DLLVM_LINK_LLVM_DYLIB=ON \
        -DLLVM_ENABLE_ASSERTIONS=OFF \
        -DLLVM_ENABLE_BACKTRACES=OFF \
        -DLLVM_ENABLE_RTTI=ON \
        -DLLVM_INCLUDE_TESTS=OFF \
        -DLLVM_INCLUDE_EXAMPLES=OFF \
        -DLLVM_ENABLE_FFI=ON \
        -DLLVM_BINDINGS_LIST=none \
        -DFFI_INCLUDE_DIR=`pkg-config --cflags-only-I libffi | sed 's/-I//'` \
        -DFFI_LIBRARY_DIR=${prefix}/lib \
        -DPYTHON_EXECUTABLE=${prefix}/libexec/python27-bootstrap/bin/python2.7

# Avoid depending on port:libxml2
configure.args-append \
        -DLIBXML2_INCLUDE_DIR=/usr/include/libxml2 \
        -DLIBXML2_LIBRARIES=/usr/lib/libxml2.dylib \
        -DLIBXML2_XMLLINT_EXECUTABLE=/usr/bin/xmllint
# Unfortunately, using arguments proved unreliable, it may still link
# opportunistically. We do not want to depend on a port which requires C++11.
conflicts_build             libxml2

configure.optflags
# Effects of -Os may be problematic in some cases. We do not want to rebuild LLVM unnecessarily.
# Example: https://github.com/iains/gcc-16-branch/issues/2
# configure.optflags          -Os

# Was needed at least on 10.6:
configure.cxxflags-append   -U__STRICT_ANSI__

# Pick this from clang-11-bootstrap port:
configure.ldflags-append    -static-libstdc++ -static-libgcc

# On systems where libstdc++ is the default, this determines C++ ABI.
# Currently we use the old ABI, and LLVM needs to match that.
compiler.cxx_standard       2011

post-destroot {
    foreach bin [glob ${destroot}${sub_prefix}/bin/*] {
        set bin_filename [string map "${sub_prefix} ${prefix}" ${bin}]-${suffix}
        set exec_path [string map "${destroot}${sub_prefix} ${sub_prefix}" ${bin}]

        xinstall -m 755 "${filespath}/llvm-bin" "${bin_filename}"

        reinplace "s:EXEC_PATH:${exec_path}:" "${bin_filename}"
    }
}

variant emulated_tls description "Enable C11/C++11 thread support via emulated-tls" {
    # Use emulated-tls to support thread_local on systems prior to 10.7
    # Requires linking against macports-libstdc++ or libc++ / libc++abi with cxa_thread_atexit
    # The test for default emulated-tls has been moved into the backend
    patchfiles-append \
        0083-Support-emutls-in-LLVM.patch
}

variant xar description "Enable xar support" {
    depends_lib-append      path:lib/libxar.dylib:xar
    patchfiles-delete       patch-disable-xar.diff
}

default_variants-append     +emulated_tls

livecheck.type              none
