#!/bin/mkpkg
# description: EFI application development toolkit
# url: https://sourceforge.net/projects/gnu-efi/

name=gnu-efi
version=3.0.18
release=1
depends=()
makedeps=()
source=(https://downloads.sourceforge.net/sourceforge/gnu-efi/gnu-efi-$version.tar.bz2
        https://ftp.gnu.org/gnu/binutils/binutils-2.46.0.tar.xz
        gnu-efi-3.0.18-clang.patch
        gnu-efi-3.0.18-remove-linux-headers.patch
        gnu-efi-3.0.19-fix-binutils-2.46.patch)
sha256sums=(
    "7f212c96ee66547eeefb531267b641e5473d7d8529f0bd8ccdefd33cf7413f5c"
    "d75a94f4d73e7a4086f7513e67e439e8fcdcbb726ffe63f4661744e6256b2cf2"
    "cbd8f6af3b8f522fc65c65a96a589602a9a072a501740954f8617990029ae984"
    "0996a5a167567066d7289cc4f9d576602cc61a3ea67608e76544b58bb0a47e94"
    "173191018e118048025e6a5acc8e4fd9a401e58fa485bfab04dd09b5c24e4a4f"
)

build() {
    # Build private GNU binutils — need objcopy with EFI/pei- target support
    cd $SRC/binutils-2.46.0
    CONFIG_SHELL=/bin/bash ./configure \
        --prefix=$SRC/binutils-local \
        --disable-nls \
        --disable-multilib \
        --disable-werror \
        --disable-gdb \
        --disable-gdbserver \
        --disable-gold \
        --disable-gprofng \
        --disable-sim \
        --disable-libdecnumber \
        --disable-readline \
        --enable-ld \
        --enable-targets=x86_64-linux,i686-linux,x86_64-pep \
        --with-system-zlib \
        CC=clang CXX=clang++
    make $MKPKG_MAKEFLAGS
    make install
    export PATH="$SRC/binutils-local/bin:$PATH"

    cd $SRC/gnu-efi-$version

    # apply patches
    patch -N -p1 < $SRC/gnu-efi-3.0.18-clang.patch || true
    patch -N -p1 < $SRC/gnu-efi-3.0.18-remove-linux-headers.patch || true
    patch -N -p1 < $SRC/gnu-efi-3.0.19-fix-binutils-2.46.patch || true

    # remove -Werror
    sed -i 's/-Werror//g' Make.defaults

    # fix CHAR16 typedef for clang — wchar_t causes pointer mismatch
    sed -i 's/typedef wchar_t CHAR16/typedef uint16_t CHAR16/' \
        inc/x86_64/efibind.h

    # musl: include clang's own headers first, then system
    CPPINCLUDEDIR=$(clang -print-resource-dir 2>/dev/null)/include
    export CFLAGS="-nostdinc -isystem $CPPINCLUDEDIR -isystem /usr/include"

    mkdir -p $SRC/gnu-efi-$version/x86_64/lib/runtime
    mkdir -p $SRC/gnu-efi-$version/x86_64/lib/x86_64
    mkdir -p $SRC/gnu-efi-$version/x86_64/gnuefi
    mkdir -p $SRC/gnu-efi-$version/x86_64/inc

    make -j1 ARCH=x86_64 \
        CC=clang \
        AS="$SRC/binutils-local/bin/as" \
        LD="$SRC/binutils-local/bin/ld" \
        AR=llvm-ar \
        RANLIB=llvm-ranlib \
        OBJCOPY="$SRC/binutils-local/bin/objcopy" \
        SUBDIRS="lib gnuefi inc" \
        all

    make -j1 ARCH=x86_64 \
        CC=clang \
        AS="$SRC/binutils-local/bin/as" \
        LD="$SRC/binutils-local/bin/ld" \
        AR=llvm-ar \
        RANLIB=llvm-ranlib \
        OBJCOPY="$SRC/binutils-local/bin/objcopy" \
        PREFIX=/usr \
        LIBDIR=/usr/lib \
        INSTALLROOT=$PKG \
        SUBDIRS="lib gnuefi inc" \
        install

    # xen and other tools expect efi/pe.h directly
    cp $PKG/usr/include/efi/x86_64/pe.h $PKG/usr/include/efi/pe.h
}
signify() {
    untrusted comment: public key
    RWTZ9IduCSQ/mL8337TEUinPwT92xFEUpD92hkS7IxcOnzTt9QdpohT3
}

# vim: filetype=sh
