# Maintainer:  Carlos Antunes <cmantunes@gmail.com>
# Maintainer:  fauxpark <fauxpark@gmail.com>
# Contributor: nanodude
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_bootstrap=0

_realname=newlib
_target=arm-none-eabi
_gccver=14.2.0

pkgbase=mingw-w64-${_target}-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_target}-${_realname}")
pkgver=4.5.0.20241231
pkgrel=1
pkgdesc='GNU Tools for ARM Embedded Processors - Newlib (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://sourceware.org/newlib/"
license=('spdx:GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-2.1-or-later AND LGPL-3.0-only AND 0BSD AND BSD-2-Clause AND BSD-3-Clause-Clear AND custom')
groups=("${MINGW_PACKAGE_PREFIX}-${_target}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-${_target}-binutils")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gmp"
             "${MINGW_PACKAGE_PREFIX}-mpc"
             "${MINGW_PACKAGE_PREFIX}-mpfr"
             "${MINGW_PACKAGE_PREFIX}-isl"
             $( (( _bootstrap )) || echo "${MINGW_PACKAGE_PREFIX}-${_target}-gcc"))
options=('!strip' '!emptydirs')
source=(
    "https://sourceware.org/pub/newlib/newlib-${pkgver}.tar.gz"
    $( (( !_bootstrap )) || echo "https://ftp.gnu.org/gnu/gcc/gcc-${_gccver}/gcc-${_gccver}.tar.xz")
)
sha256sums=(
    '33f12605e0054965996c25c1382b3e463b0af91799001f5bb8c0630f2ec8c852'
    $( (( !_bootstrap )) || echo 'a7b39bc69cbf9e25826c5a60ab26477001f7c08d85cec04bc0e29cabed6f3cc9')
)
noextract=("newlib-${pkgver}.tar.gz")


prepare() {
    cd "${srcdir}"
    # bsdtar doesn't like the symlinks
    tar -xzf newlib-${pkgver}.tar.gz || true
}

# Newlib is required to build GCC, but GCC is also required to compile newlib
# So we must build a minimal GCC first, to avoid the circular dependency
_build_gcc() {
    cp -a ${MINGW_PREFIX}/${_target} "${srcdir}"/gcc-install-${MSYSTEM}

    if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
        CFLAGS+=" -Wno-int-conversion -Wno-array-bounds -Wno-mismatched-tags"
        CXXFLAGS+=" -Wno-int-conversion -Wno-array-bounds -Wno-mismatched-tags"
    fi

    local _GCC_LDFLAGS="${LDFLAGS} -Wl,--disable-dynamicbase"
    ../gcc-${_gccver}/configure \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --prefix=/ \
        --target=${_target} \
        --enable-languages=c \
        --disable-nls \
        --disable-libssp \
        --disable-shared \
        --disable-threads \
        --disable-libgomp \
        --disable-werror \
        --disable-lto \
        --disable-libffi \
        --disable-decimal-float \
        --disable-libmudflap \
        --disable-libquadmath \
        --disable-libstdcxx-pch \
        --disable-libsanitizer \
        --disable-tls \
        --enable-multilib \
        --enable-interwork \
        --without-headers \
        --with-libelf \
        --with-newlib \
        --with-multilib-list=rmprofile \
        --with-host-libstdcxx="-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm" \
        --with-sysroot=${MINGW_PREFIX} \
        --with-build-sysroot="${srcdir}"/gcc-install-${MSYSTEM} \
        --with-build-time-tools=${MINGW_PREFIX}/${_target}/bin \
        LDFLAGS="${_GCC_LDFLAGS}"

    make all-gcc
    make DESTDIR="${srcdir}"/gcc-install-${MSYSTEM} install-gcc
}

build() {
    if (( _bootstrap )); then
      mkdir -p gcc-install-${MSYSTEM}
      mkdir -p gcc-build-${MSYSTEM} && cd gcc-build-${MSYSTEM}
      _build_gcc
      export PATH="${srcdir}/gcc-install-${MSYSTEM}/bin:${PATH}"
    fi

    mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

    ../newlib-${pkgver}/configure \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${_target} \
        --prefix=${MINGW_PREFIX} \
        --enable-newlib-io-long-long \
        --enable-newlib-io-c99-formats \
        --enable-newlib-register-fini \
        --enable-newlib-retargetable-locking \
        --disable-newlib-supplied-syscalls \
        --disable-nls
    make

    mkdir -p "${srcdir}"/nano-build-${MSYSTEM} && cd "${srcdir}"/nano-build-${MSYSTEM}

    ../newlib-${pkgver}/configure \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${_target} \
        --prefix=${MINGW_PREFIX} \
        --disable-newlib-supplied-syscalls \
        --enable-newlib-reent-small \
        --enable-newlib-retargetable-locking \
        --disable-newlib-fvwrite-in-streamio \
        --disable-newlib-fseek-optimization \
        --disable-newlib-wide-orient \
        --enable-newlib-nano-malloc \
        --disable-newlib-unbuf-stream-opt \
        --enable-lite-exit \
        --enable-newlib-global-atexit \
        --enable-newlib-nano-formatted-io \
        --disable-nls
    make
}

package() {
    cd "${pkgdir}"

    cd "${srcdir}"/nano-build-${MSYSTEM}

    make DESTDIR="${pkgdir}" install

    find "${pkgdir}" -regex ".*/lib\(c\|g\|rdimon\)\.a" -exec rename .a _nano.a '{}' \;
    install -Dm644 -t "${pkgdir}"${MINGW_PREFIX}/${_target}/include/newlib-nano "${pkgdir}"${MINGW_PREFIX}/${_target}/include/newlib.h

    cd "${srcdir}"/build-${MSYSTEM}

    make DESTDIR="${pkgdir}" install

    find "${pkgdir}"${MINGW_PREFIX}/${_target}/lib \( -name "*.a" -or -name "*.o" \) -exec ${_target}-objcopy -R .comment -R .note -R .debug_info -R .debug_aranges -R .debug_pubnames -R .debug_pubtypes -R .debug_abbrev -R .debug_line -R .debug_str -R .debug_ranges -R .debug_loc '{}' \;

    # Avoid conflict with other targets newlib
    mv "${pkgdir}"${MINGW_PREFIX}/share/info/porting.info "${pkgdir}"${MINGW_PREFIX}/share/info/${_target}-porting.info
}
