# Contributor: Ariadne Conill <ariadne@dereferenced.org>
# Contributor: Timo Teräs <timo.teras@iki.fi>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=musl
pkgver=1.2.5
pkgrel=10
pkgdesc="the musl c library (libc) implementation"
url="https://musl.libc.org/"
arch="all"
license="MIT"
options="lib64 !check"
subpackages="
	$pkgname-dbg
	$pkgname-libintl:libintl:noarch
	$pkgname-dev
	"
case "$BOOTSTRAP" in
nocc)	pkgname="musl-dev"; subpackages="";;
nolibc) ;;
*)	subpackages="$subpackages $pkgname-utils";;
esac
source="https://musl.libc.org/releases/musl-$pkgver.tar.gz
	handle-aux-at_base.patch
	isatty-glibc-align.patch
	ldso-fix-non-functional-fix-to-early-dynamic-PAGE_SI.patch
	loongarch64-Add-lsx-and-lasx-vector-structure-defini.patch
	loongarch64-add-TLSDESC-support.patch
	loongarch64-add-fpu-and-simd-context-to-signal.h.patch
	loongarch64-add-bits-hwcap.h.patch
	LoongArch64-add-new-reloc-types-and-NT_LOONGARCH_HW_.patch
	CVE-2025-26519.patch

	ldconfig
	__stack_chk_fail_local.c
	getconf.c
	getent.c
	iconv.c
	"
# this is needed for BOOTSTRAP=nocc (see above)
builddir="$srcdir/musl-$pkgver"

# secfixes:
#   1.2.5-r10:
#     - CVE-2025-26519
#   1.2.2_pre2-r0:
#     - CVE-2020-28928
#   1.1.23-r2:
#     - CVE-2019-14697
#   1.1.15-r4:
#     - CVE-2016-8859

prepare() {
	default_prepare

	echo "$pkgver" > VERSION
}

build() {
	[ "$BOOTSTRAP" = "nocc" ] && return 0

	# provide minimal libssp_nonshared.a so we don't need libssp from gcc
	${CROSS_COMPILE}cc $CFLAGS -c "$srcdir"/__stack_chk_fail_local.c -o __stack_chk_fail_local.o
	${CROSS_COMPILE}ar r libssp_nonshared.a __stack_chk_fail_local.o

	if [ "$BOOTSTRAP" != "nolibc" ]; then
		# getconf/getent/iconv
		local i
		for i in getconf getent iconv ; do
			${CROSS_COMPILE}cc $CPPFLAGS $CFLAGS "$srcdir"/$i.c -o $i
		done
	fi

	# musl sets a sane default for flags that they tune based on -O2 with alignment things turned off.
	# see https://git.musl-libc.org/cgit/musl/commit/?id=b90841e2583237a4132bbbd74752e0e9563660cd ,
	# and the discussion in https://www.openwall.com/lists/musl/2023/05/22/2
	# this makes libc.so about 5% bigger in itself, but should yield an overall improved libc.so for general use.
	# NB: if musl detects an -O it skips its own, so remove just the O value.
	# Also environment CFLAGS are placed after automatic flags so --enable-optimize doesn't work.
	export CFLAGS="${CFLAGS/-O* /}"

	# note: not autotools
	# shellcheck disable=2153
	LDFLAGS="$LDFLAGS -Wl,-soname,libc.musl-$CARCH.so.1" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--enable-debug
	make
}

package() {
	case "$CARCH" in
	aarch64*)	ARCH="aarch64" ;;
	arm*)		ARCH="arm" ;;
	x86)		ARCH="i386" ;;
	x86_64)		ARCH="x86_64" ;;
	ppc)		ARCH="powerpc" ;;
	ppc64*)		ARCH="powerpc64" ;;
	s390*)		ARCH="s390x" ;;
	mips64*)	ARCH="mips64" ;;
	mips*)		ARCH="mips" ;;
	riscv64)	ARCH="riscv64" ;;
	loongarch64)	ARCH="loongarch64" ;;
	esac

	if [ "$BOOTSTRAP" = "nocc" ]; then
		make ARCH="$ARCH" prefix=/usr DESTDIR="$pkgdir" install-headers
	else
		make DESTDIR="$pkgdir" install

		cp libssp_nonshared.a "$pkgdir"/usr/lib

		# make LDSO the be the real file, and libc the symlink
		local rule="$(printf "%s\n\t%s\n" 'print-ldso:' '@echo $$(basename $(LDSO_PATHNAME))')"
		local LDSO=$(make -f Makefile --eval "$rule" print-ldso)
		mv -f "$pkgdir"/usr/lib/libc.so "$pkgdir"/lib/"$LDSO"
		ln -sf "$LDSO" "$pkgdir"/lib/libc.musl-$CARCH.so.1
		ln -sf ../../lib/"$LDSO" "$pkgdir"/usr/lib/libc.so
		mkdir -p "$pkgdir"/usr/bin

		cat >>"$pkgdir"/usr/bin/ldd <<-EOF
		#!/bin/sh
		exec /lib/$LDSO --list "\$@"
		EOF
		chmod 755 "$pkgdir"/usr/bin/ldd
	fi

}

utils() {
	depends="scanelf"
	replaces="libiconv"
	license="MIT AND BSD-2-Clause AND GPL-2.0-or-later"

	# for backwards compatibility
	provides="libc-utils=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr "$subpkgdir"/sbin
	mv "$pkgdir"/usr/bin "$subpkgdir"/usr/

	install -D \
		"$builddir"/getent \
		"$builddir"/getconf \
		"$builddir"/iconv \
		"$subpkgdir"/usr/bin

	install -D -m755 "$srcdir"/ldconfig "$subpkgdir"/sbin
}

# currently we don't want by default any NLS
# and use GNU gettext where needed. the plan is to migrate to
# musl gettext() later on as fully as possible.
libintl() {
	pkgdesc="musl libintl.h header"
	mkdir -p "$subpkgdir"/usr/include
	mv "$pkgdir"/usr/include/libintl.h \
		"$subpkgdir"/usr/include/
}

dev() {
	default_dev

	# for backwards compatibility
	provides="libc-dev=$pkgver-r$pkgrel"
}

sha512sums="
7bb7f7833923cd69c7a1a9b8a5f1784bfd5289663eb6061dcd43d583e45987df8a68a1be05d75cc1c88a3f5b610653d1a70f4a9cff4d8f7fd41ae73ee058c17c  musl-1.2.5.tar.gz
a76f79b801497ad994746cf82bb6eaf86f9e1ae646e6819fbae8532a7f4eee53a96ac1d4e789ec8f66aea2a68027b0597f7a579b3369e01258da8accfce41370  handle-aux-at_base.patch
4fe4309d9e5eb12f3aaf9dac300c7bf1a9e104d9b0e7754353314f4b6507de68d8d5bf0ac45164d7e2ab4d71728b5ef8b302f8ee7a6b548f54e022218031e2cf  isatty-glibc-align.patch
1b85079c706b017106ecbf4afbc65b4a2171297a673035645e4c3ddb7e2aae0549b04932df83778323b7329ae8d32160a89e0c1b2534c278708c61b1f40a1ed8  ldso-fix-non-functional-fix-to-early-dynamic-PAGE_SI.patch
b4d95231944fd236827a62343bbce74b20a56739e2b8937bda69bfc7b53f2ce5bd709459ee61615bd10d2839f43311eced9344ee8185701fd38ec0de0d43c6ae  loongarch64-Add-lsx-and-lasx-vector-structure-defini.patch
363d0cbfe11f024b120b6d54e909c038cd5b37f1f195048ddb29591191a1288ff50842ccd14e0cab80359e5b46c0e02d2b5af3016bd1507c1d269e590690a3fe  loongarch64-add-TLSDESC-support.patch
b0f93f8868caf15d436ebe81088f4914cac3e21cc5b86cb0d5f40092edc6e29909188a14cde0c69ab86b51631aa34a4b8136a515f2a1b53c424c8b251c5ea689  loongarch64-add-fpu-and-simd-context-to-signal.h.patch
70003d89e31b1c83274d1e850f86d8a54f30c1d5e22f769ae229d30d2bbdc095dbf4c262fb55079aaf8bfdf9a213f9468150b50c000b6596b051729a0d25f913  loongarch64-add-bits-hwcap.h.patch
546fa317b532c3a63a592b6613e9a2136ee7750113ab048b261acf94e33a34732f3f26d6f734879c025d944f0b8e5307656c485bd32ce8f2364ee537ab236c95  LoongArch64-add-new-reloc-types-and-NT_LOONGARCH_HW_.patch
7a6a9836d2de91afc1115868e68f347bd2365fa48188e65938cfa654ae9bafdbb3a56bf12d3185a96800a85198378c8dbf9c25d977ca0e318220529fa4458123  CVE-2025-26519.patch
8d3a2d5315fc56fee7da9abb8b89bb38c6046c33d154c10d168fb35bfde6b0cf9f13042a3bceee34daf091bc409d699223735dcf19f382eeee1f6be34154f26f  ldconfig
062bb49fa54839010acd4af113e20f7263dde1c8a2ca359b5fb2661ef9ed9d84a0f7c3bc10c25dcfa10bb3c5a4874588dff636ac43d5dbb3d748d75400756d0b  __stack_chk_fail_local.c
0d80f37b34a35e3d14b012257c50862dfeb9d2c81139ea2dfa101d981d093b009b9fa450ba27a708ac59377a48626971dfc58e20a3799084a65777a0c32cbc7d  getconf.c
27fa820dc847b1e4a89a45ab7c7ffb8b6436b6b7f1089004b12561d939b227617797d5fc81696658da3f07daab6cc71ea171eb6afb6f5082336b57e2d0ac8463  getent.c
9d42d66fb1facce2b85dad919be5be819ee290bd26ca2db00982b2f8e055a0196290a008711cbe2b18ec9eee8d2270e3b3a4692c5a1b807013baa5c2b70a2bbf  iconv.c
"
