# Build recipe for musl.
#
# Copyright (c) 2015-2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=musl
version=1.2.2
release=3

# Define a category for the output of the package name
pkgcategory=libs

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=https://www.musl-libc.org/releases/$tarname
#fetch="
#  https://rsync.dragora.org/current/sources/$tarname
#  https://mirror.cedia.org.ec/dragora/current/sources/$tarname
#"

description="
A powerful standard C/POSIX library.

Musl is a new standard library to power a new generation of Linux-based
devices.  Musl is lightweight, fast, simple, free, and strives to be
correct in the sense of standards-conformance and safety.
"

homepage=https://www.musl-libc.org
license="MIT Expat variant"

# Source documentation
docs="COPYRIGHT README VERSION WHATSNEW"
docsdir="${docdir}/${program}"

# Limit package name to the program name
full_pkgname=$program

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    #patch -Np1 -i "${worktree}/patches/musl/branch-updates.diff"
    patch -Np0 -i "${worktree}/patches/musl/musl-utmp_path.diff"

    # Do not use the compiler runtime library from the temporary system,
    # the toolchain must be adjusted until the native GCC take place
    if cc -print-libgcc-file-name | grep -q '^/tools'
    then
        patch -Np1 -i "${worktree}/patches/musl/musl-nolibcc_stage1.diff"
    fi

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --syslibdir=/lib \
     --enable-shared \
     --enable-static \
     --enable-optimize

    make -j${jobs}
    make -j${jobs} install DESTDIR="$destdir"

    # Provide minimal libssp_nonshared.a

    cc -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
       -o __stack_chk_fail_local.o
    ar rc libssp_nonshared.a __stack_chk_fail_local.o
    ranlib libssp_nonshared.a
    cp -p libssp_nonshared.a "${destdir}/usr/lib${libSuffix}"
    strip -g "${destdir}/usr/lib${libSuffix}/libssp_nonshared.a"

    # To print shared library dependencies
    mkdir -p "${destdir}/usr/bin"
    ln -sf /usr/lib${libSuffix}/libc.so "${destdir}/usr/bin/ldd"

    # Create dynamic linker runtime file taking -$(ARCH) as reference
    for file in "${destdir}"/lib/ld-musl-*.so.1
    do
        ld_path="${file##*/}"			# Basename
        ld_path="${ld_path%%.so.1}.path"	# Get the rid of .so.1
        export ld_path
        break;
    done
    unset -v file

    if test -n "$ld_path"
    then
        mkdir -p "${destdir}/etc"
        cat << EOF > "${destdir}"/etc/$ld_path
/lib
/usr/local/lib
/usr/lib
EOF
        # Include path for 'libSuffix'
        if test -n "$libSuffix"
        then
            cat << EOF >> "${destdir}"/etc/$ld_path
/usr/lib${libSuffix}
EOF
        fi

        # Now the rest of the path(s)
        cat << EOF >> "${destdir}"/etc/$ld_path
/usr/$(gcc -dumpmachine)/lib
/opt/trinity/lib
/opt/trinity/lib/tqt3/lib
EOF
        chmod 644 "${destdir}"/etc/$ld_path
        touch "${destdir}/etc/.graft-config"
    else
        echo "WARNING: \$ld_path is empty." 1>&2
    fi

    # Add to the post-install script a symlink for compatibility,
    # as some scripts (./configure) still have the Glibc name
    # "ld.so.conf".

    mkdir -p "${destdir}/var/lib/qi"
    cat << EOF > "${destdir}/var/lib/qi/${full_pkgname}.sh"

# Adjust the toolchain only once (as needed)
if test -x /tools/bin/adjust-toolchain
then
    /tools/bin/adjust-toolchain && chmod 644 /tools/bin/adjust-toolchain
fi

# A symlink for compatibility
if test ! -e "\${rootdir}/etc/ld.so.conf"
then
    ( cd -- "\${rootdir}/etc" && ln -sf $ld_path ld.so.conf )
fi

EOF

    unset -v ld_path

    mkdir -p "${destdir}"/usr/sbin
    cat << EOF > "${destdir}"/usr/sbin/ldconfig
#! /bin/true -
EOF
    chmod 755 "${destdir}"/usr/sbin/ldconfig

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"
}

