# Build script for musl.
#
# Copyright (c) 2014-2023 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.

version=1.2.3

cd -- "$TMPDIR"
rm -rf musl-${version}
unpack "${worktree}/sources/musl-${version}.tar.gz"

# Build instructions
cd musl-${version}

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

# Fix debugger tracking of shared libraries on mips with PIE main program
patch -Np1 -i "${worktree}/patches/musl/f897461d4fe72bb71854a6d0662de83008caccb7"

# Fix wrong sigaction syscall ABI on mips*, or1k, microblaze, riscv64
patch -Np1 -i "${worktree}/patches/musl/269d193820342dc109f39909d78fb30f4c978f76"

# Support DT_RELR relative relocation format
patch -Np1 -i "${worktree}/patches/musl/d32dadd60efb9d3b255351a3b532f8e4c3dd0db1"

# Process DT_RELR relocations in ldso-startup/static-pie
patch -Np1 -i "${worktree}/patches/musl/6f3ead0ae16deb9f0004b275e29a276c9712ee3c"

# Fix double-processing of DT_RELR relocations in ldso relocating itself
patch -Np1 -i "${worktree}/patches/musl/29e4319178cbc2a4e9f058a99ae8098d4b6ac055"

./configure CROSS_COMPILE="${target}-" CFLAGS="$BTCFLAGS" \
 --prefix=/tools \
 --libdir=/tools/lib${libSuffix} \
 --syslibdir=/tools/lib \
 --build=$host \
 --host=$target \
 --enable-optimize

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

cleanup()
{
    cd -- "$TMPDIR" && rm -rf musl-${version}
}

