diff mbox series

[meta-oe,3/3] stdbuf: add BSD-licensed stdbuf from FreeBSD

Message ID 20261206132627.3617509-6-alex.kiernan@gmail.com
State New
Headers show
Series [meta-oe,1/3] fatrace: add recipe | expand

Commit Message

Alex Kiernan Dec. 6, 2026, 1:26 p.m. UTC
A standalone stdbuf for images which exclude GPL-3.0, so cannot use
coreutils', and which do not want to replace coreutils wholesale with
uutils-coreutils.

The two source files are fetched from the GitHub mirror rather than
cloning the whole FreeBSD tree. Installed via update-alternatives at a
lower priority than coreutils and uutils-coreutils.

AI-Generated: Claude Opus 5 (Claude Code)
Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
---
 .../packagegroups/packagegroup-meta-oe.bb     |  1 +
 ...f-stop-option-parsing-at-the-command.patch | 29 +++++++++++
 .../recipes-extended/stdbuf/stdbuf_14.0.bb    | 52 +++++++++++++++++++
 3 files changed, 82 insertions(+)
 create mode 100644 meta-oe/recipes-extended/stdbuf/stdbuf/0001-stdbuf-stop-option-parsing-at-the-command.patch
 create mode 100644 meta-oe/recipes-extended/stdbuf/stdbuf_14.0.bb
diff mbox series

Patch

diff --git a/meta-oe/recipes-core/packagegroups/packagegroup-meta-oe.bb b/meta-oe/recipes-core/packagegroups/packagegroup-meta-oe.bb
index 52365b440580..cfc135027b95 100644
--- a/meta-oe/recipes-core/packagegroups/packagegroup-meta-oe.bb
+++ b/meta-oe/recipes-core/packagegroups/packagegroup-meta-oe.bb
@@ -415,6 +415,7 @@  RDEPENDS:packagegroup-meta-oe-extended = "\
     smartmontools \
     s-nail \
     snappy \
+    stdbuf \
     tipcutils \
     tiptop \
     tmate \
diff --git a/meta-oe/recipes-extended/stdbuf/stdbuf/0001-stdbuf-stop-option-parsing-at-the-command.patch b/meta-oe/recipes-extended/stdbuf/stdbuf/0001-stdbuf-stop-option-parsing-at-the-command.patch
new file mode 100644
index 000000000000..b8b33d09bfc7
--- /dev/null
+++ b/meta-oe/recipes-extended/stdbuf/stdbuf/0001-stdbuf-stop-option-parsing-at-the-command.patch
@@ -0,0 +1,29 @@ 
+From a49fd0b8e092d84f262c3d343fe24a7c7cbbffb2 Mon Sep 17 00:00:00 2001
+From: Alex Kiernan <alex.kiernan@gmail.com>
+Date: Tue, 6 Oct 2026 17:38:29 +0100
+Subject: [PATCH] stdbuf: stop option parsing at the command
+
+glibc's getopt permutes argv, so the wrapped command's own options are
+taken as stdbuf's: "stdbuf -oL grep -e foo" sets stderr's buffering to
+"foo". A leading '+' stops at the first operand, as FreeBSD's getopt
+always does.
+
+Upstream-Status: Inappropriate [glibc-specific]
+Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
+---
+ usr-stdbuf.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/usr-stdbuf.c b/usr-stdbuf.c
+index 6b09bb6149f5..22cf67672280 100644
+--- a/usr-stdbuf.c
++++ b/usr-stdbuf.c
+@@ -69,7 +69,7 @@ main(int argc, char *argv[])
+ 	int i;
+ 
+ 	ibuf = obuf = ebuf = NULL;
+-	while ((i = getopt(argc, argv, "e:i:o:")) != -1) {
++	while ((i = getopt(argc, argv, "+e:i:o:")) != -1) {
+ 		switch (i) {
+ 		case 'e':
+ 			ebuf = optarg;
diff --git a/meta-oe/recipes-extended/stdbuf/stdbuf_14.0.bb b/meta-oe/recipes-extended/stdbuf/stdbuf_14.0.bb
new file mode 100644
index 000000000000..17d8f48519c1
--- /dev/null
+++ b/meta-oe/recipes-extended/stdbuf/stdbuf_14.0.bb
@@ -0,0 +1,52 @@ 
+SUMMARY = "Run a command with altered stdio buffering"
+DESCRIPTION = "BSD-licensed stdbuf from FreeBSD, for images which exclude \
+GPL-3.0 and so cannot use the coreutils version."
+HOMEPAGE = "https://git.freebsd.org/src.git"
+SECTION = "console/utils"
+
+LICENSE = "BSD-2-Clause"
+LIC_FILES_CHKSUM = "file://usr-stdbuf.c;beginline=1;endline=26;md5=4afefc1eaad5cbfec47a4a2f0a8cfda1"
+
+# Fetch just the two files from the GitHub mirror rather than cloning the
+# whole FreeBSD tree. FBSD_SRCREV is the last commit to touch either file.
+FBSD_SRCREV = "b3e7694832e81d7a904a10f525f8797b753bf0d3"
+FBSD_RAW = "https://raw.githubusercontent.com/freebsd/freebsd-src/${FBSD_SRCREV}"
+
+SRC_URI = "${FBSD_RAW}/usr.bin/stdbuf/stdbuf.c;name=stdbuf;downloadfilename=usr-stdbuf.c \
+           ${FBSD_RAW}/lib/libstdbuf/stdbuf.c;name=libstdbuf;downloadfilename=lib-stdbuf.c \
+           file://0001-stdbuf-stop-option-parsing-at-the-command.patch"
+SRC_URI[stdbuf.sha256sum] = "d217bb2c3ea074ea49b844b5f7a9494da5834bd49d91f02dc0c648bb15f8decc"
+SRC_URI[libstdbuf.sha256sum] = "e8245611a10e8db646c7d0f73fdcbbf63e6cb1f9b9cb48376f24ef7f0cc9ec83"
+
+S = "${UNPACKDIR}"
+
+inherit update-alternatives
+
+# stdbuf hardcodes /usr/lib/libstdbuf.so as its preload path
+do_configure() {
+	sed -i 's|"/usr/lib/libstdbuf.so"|"${libdir}/libstdbuf.so"|' ${S}/usr-stdbuf.c
+	grep -q '"${libdir}/libstdbuf.so"' ${S}/usr-stdbuf.c
+}
+
+# _GNU_SOURCE for asprintf(3)
+do_compile() {
+	${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS} -D_GNU_SOURCE \
+		-fPIC -shared -o libstdbuf.so lib-stdbuf.c
+	${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS} -D_GNU_SOURCE \
+		-o stdbuf usr-stdbuf.c
+}
+
+do_install() {
+	install -d ${D}${bindir} ${D}${libdir}
+	install -m 0755 ${B}/stdbuf ${D}${bindir}/stdbuf
+	install -m 0755 ${B}/libstdbuf.so ${D}${libdir}/libstdbuf.so
+}
+
+# libstdbuf.so is a preload object and belongs in ${PN}, not ${PN}-dev
+FILES_SOLIBSDEV = ""
+FILES:${PN} += "${libdir}/libstdbuf.so"
+
+# Lower than coreutils and uutils-coreutils, which also provide stdbuf
+ALTERNATIVE:${PN} = "stdbuf"
+ALTERNATIVE_LINK_NAME[stdbuf] = "${bindir}/stdbuf"
+ALTERNATIVE_PRIORITY = "90"