Skip to contentSkip to conversation

opkg: Use own version of portable basename function

PATCH Superseded Khem Raj 1 participant 6 messages Dec 10, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Khem Raj

author
#0 lore raw

PATCH 1/6 opkg: Use own version of portable basename function

Fixes build with upcoming musl release.

---
 ...Provide-basename-function-as-utility.patch | 131 ++++++++++++++++++
 meta/recipes-devtools/opkg/opkg_0.6.2.bb      |   1 +
 2 files changed, 132 insertions(+)
 create mode 100644 meta/recipes-devtools/opkg/opkg/0001-libopkg-Provide-basename-function-as-utility.patch
meta/recipes-devtools/opkg/opkg/0001-libopkg-Provide-basename-function-as-utility.patch new file +131 -0
new file mode 100644
index 00000000000..91b00bb4085
@@ -0,0 +1,131 @@
+From c3770c1254a288f1312a66ebf4a5da60a1ae215b Mon Sep 17 00:00:00 2001
+From: Khem Raj <[email protected]>
+Date: Sat, 9 Dec 2023 17:07:22 -0800
+Subject: [PATCH] libopkg: Provide basename function as utility
+
+glibc provides two implementations one when string.h is used and Posix
+compliant one from libgen.h when string.h is not included. Lets
+implement gnu compliant version using strchr and use it Everywhere for
+portability reasons. This will help compiling with next release of musl
+where signature is removed from strin.h [1]
+
+[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7
+
+Upstream-Status: Submitted [https://lists.yoctoproject.org/g/opkg/message/11]
+Signed-off-by: Khem Raj <[email protected]>
+---
+ libopkg/opkg_archive.c  | 2 +-
+ libopkg/opkg_download.c | 6 ++----
+ libopkg/opkg_remove.c   | 2 +-
+ libopkg/pkg.c           | 4 +---
+ libopkg/xfuncs.c        | 6 ++++++
+ libopkg/xfuncs.h        | 1 +
+ 6 files changed, 12 insertions(+), 9 deletions(-)
+
+diff --git a/libopkg/opkg_archive.c b/libopkg/opkg_archive.c
+index 03a4afb..da37d8a 100644
+--- a/libopkg/opkg_archive.c
++++ b/libopkg/opkg_archive.c
+@@ -797,7 +797,7 @@ int gz_write_archive(const char *filename, const char *gz_filename)
+     }
+ 
+     /* Remove path hierarchy, as we are only compressing a single file */
+-    archive_entry_set_pathname(entry, basename(filename));
++    archive_entry_set_pathname(entry, xbasename(filename));
+ 
+     r = archive_write_header(a, entry);
+     if (r != ARCHIVE_OK) {
+diff --git a/libopkg/opkg_download.c b/libopkg/opkg_download.c
+index 4086f83..130a8c1 100644
+--- a/libopkg/opkg_download.c
++++ b/libopkg/opkg_download.c
+@@ -158,7 +158,6 @@ static char *get_cache_location(const char *src)
+     char *md5sum_hex;
+     char *cache_location;
+     char *short_file_name;
+-    char *tmp = xstrdup(src);
+ 
+     md5_buffer(src, strlen(src), md5sum_bin);
+     md5sum_hex = md5_to_string(md5sum_bin);
+@@ -168,14 +167,13 @@ static char *get_cache_location(const char *src)
+      * MAX_SHORT_FILE_NAME_LENGTH to ensure that the total cache file name
+      * length is reasonable.
+      */
+-    short_file_name = basename(tmp);
++    short_file_name = xbasename(src);
+     if (strlen(short_file_name) > MAX_SHORT_FILE_NAME_LENGTH)
+         short_file_name[MAX_SHORT_FILE_NAME_LENGTH] = '\0';
+ 
+     sprintf_alloc(&cache_location, "%s/%s_%s", opkg_config->cache_dir,
+                   md5sum_hex, short_file_name);
+     free(md5sum_hex);
+-    free(tmp);
+     return cache_location;
+ }
+ 
+@@ -361,7 +359,7 @@ int opkg_download_pkg_to_dir(pkg_t * pkg, const char *dir)
+     char *url = NULL;
+     int err = 0;
+ 
+-    sprintf_alloc(&dest_file_name, "%s/%s", dir, basename(pkg->filename));
++    sprintf_alloc(&dest_file_name, "%s/%s", dir, xbasename(pkg->filename));
+ 
+     if (opkg_config->volatile_cache) {
+         url = get_pkg_url(pkg);
+diff --git a/libopkg/opkg_remove.c b/libopkg/opkg_remove.c
+index 889c672..e9d5714 100644
+--- a/libopkg/opkg_remove.c
++++ b/libopkg/opkg_remove.c
+@@ -215,7 +215,7 @@ void remove_maintainer_scripts(pkg_t * pkg)
+         return;
+ 
+     for (i = 0; i < globbuf.gl_pathc; i++) {
+-        filename = xstrdup(basename(globbuf.gl_pathv[i]));
++        filename = xstrdup(xbasename(globbuf.gl_pathv[i]));
+         lastdot = strrchr(filename, '.');
+         *lastdot = '\0';
+         // Only delete files that match the package name (the glob may match files
+diff --git a/libopkg/pkg.c b/libopkg/pkg.c
+index 6b1bd8f..3d13fed 100644
+--- a/libopkg/pkg.c
++++ b/libopkg/pkg.c
+@@ -271,10 +271,8 @@ int pkg_init_from_file(pkg_t * pkg, const char *filename)
+ 
+     pkg->local_filename = xstrdup(filename);
+ 
+-    tmp = xstrdup(filename);
+     sprintf_alloc(&control_path, "%s/%s.control.XXXXXX", opkg_config->tmp_dir,
+-                  basename(tmp));
+-    free(tmp);
++                  xbasename(filename));
+     fd = mkstemp(control_path);
+     if (fd == -1) {
+         opkg_perror(ERROR, "Failed to make temp file %s", control_path);
+diff --git a/libopkg/xfuncs.c b/libopkg/xfuncs.c
+index 9ee1a7d..11e3d92 100644
+--- a/libopkg/xfuncs.c
++++ b/libopkg/xfuncs.c
+@@ -110,3 +110,9 @@ extern char *xdirname(const char *path)
+     free(pathcopy);
+     return parent;
+ }
++/* implement Glibc basename behavior */
++const char *xbasename(const char *path)
++{
++    const char *tmp = strrchr(path, '/');
++    return tmp ? tmp+1 : path;
++}
+diff --git a/libopkg/xfuncs.h b/libopkg/xfuncs.h
+index 5d278b4..0d4ef32 100644
+--- a/libopkg/xfuncs.h
++++ b/libopkg/xfuncs.h
+@@ -30,5 +30,6 @@ extern void *xcalloc(size_t nmemb, size_t size);
+ extern char *xstrdup(const char *s);
+ extern char *xstrndup(const char *s, int n);
+ extern char *xdirname(const char *path);
++extern const char *xbasename(const char *path);
+ 
+ #endif                          /* XFUNCS_H */
+-- 
+2.43.0
+
meta/recipes-devtools/opkg/opkg_0.6.2.bb +1 -0
index 46be137354c..02f4a46561e 100644
@@ -15,6 +15,7 @@ PE = "1"
 SRC_URI = "http://downloads.yoctoproject.org/releases/${BPN}/${BPN}-${PV}.tar.gz \
            file://opkg.conf \
            file://0001-opkg_conf-create-opkg.lock-in-run-instead-of-var-run.patch \
+           file://0001-libopkg-Provide-basename-function-as-utility.patch \
            file://run-ptest \
            "
 
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 2/6 kmod: Fix build with latest musl

implement glibc compatible basename() funciton for portability

---
 ...able-implementation-for-basename-API.patch | 136 ++++++++++++++++++
 meta/recipes-kernel/kmod/kmod_31.bb           |   1 +
 2 files changed, 137 insertions(+)
 create mode 100644 meta/recipes-kernel/kmod/kmod/0001-Use-portable-implementation-for-basename-API.patch
meta/recipes-kernel/kmod/kmod/0001-Use-portable-implementation-for-basename-API.patch new file +136 -0
new file mode 100644
index 00000000000..6a7f9ded4fa
@@ -0,0 +1,136 @@
+From 721ed6040c7aa47070faf6378c433089e178bd43 Mon Sep 17 00:00:00 2001
+From: Khem Raj <[email protected]>
+Date: Sat, 9 Dec 2023 17:35:59 -0800
+Subject: [PATCH] Use portable implementation for basename API
+
+musl has removed the non-prototype declaration of basename from
+string.h [1] which now results in build errors with clang-17+ compiler
+
+Implement GNU basename behavior using strchr which is portable across libcs
+
+Fixes
+../git/tools/kmod.c:71:19: error: call to undeclared function 'basename'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
+71 | "Commands:\n", basename(argv[0]));
+| ^
+
+[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7
+
+Upstream-Status: Submitted [https://github.com/kmod-project/kmod/pull/32]
+
+Suggested-by: Rich Felker
+
+Signed-off-by: Khem Raj <[email protected]>
+---
+ libkmod/libkmod-config.c | 2 +-
+ shared/util.c            | 4 ++--
+ shared/util.h            | 7 +++++++
+ testsuite/testsuite.c    | 2 +-
+ tools/depmod.c           | 2 +-
+ tools/kmod.c             | 4 ++--
+ 6 files changed, 14 insertions(+), 7 deletions(-)
+
+diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c
+index e83621b..8aa555a 100644
+--- a/libkmod/libkmod-config.c
++++ b/libkmod/libkmod-config.c
+@@ -794,7 +794,7 @@ static int conf_files_insert_sorted(struct kmod_ctx *ctx,
+ 	bool is_single = false;
+ 
+ 	if (name == NULL) {
+-		name = basename(path);
++		name = gnu_basename(path);
+ 		is_single = true;
+ 	}
+ 
+diff --git a/shared/util.c b/shared/util.c
+index e2bab83..0e16670 100644
+--- a/shared/util.c
++++ b/shared/util.c
+@@ -172,9 +172,9 @@ char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *
+ 
+ char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len)
+ {
+-	char *modname;
++	const char *modname;
+ 
+-	modname = basename(path);
++	modname = gnu_basename(path);
+ 	if (modname == NULL || modname[0] == '\0')
+ 		return NULL;
+ 
+diff --git a/shared/util.h b/shared/util.h
+index c4a3916..073dc5a 100644
+--- a/shared/util.h
++++ b/shared/util.h
+@@ -5,6 +5,7 @@
+ #include <stdbool.h>
+ #include <stdlib.h>
+ #include <stdio.h>
++#include <string.h>
+ #include <sys/types.h>
+ #include <sys/stat.h>
+ #include <time.h>
+@@ -76,6 +77,12 @@ do {						\
+ 	__p->__v = (val);			\
+ } while(0)
+ 
++static _always_inline_ const char *gnu_basename(const char *s)
++{
++  const char *p = strrchr(s, '/');
++  return p ? p+1 : s;
++}
++
+ static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u)
+ {
+ 	return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1));
+diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c
+index 318343a..aafc987 100644
+--- a/testsuite/testsuite.c
++++ b/testsuite/testsuite.c
+@@ -70,7 +70,7 @@ static void help(void)
+ 
+ 	printf("Usage:\n"
+ 	       "\t%s [options] <test>\n"
+-	       "Options:\n", basename(progname));
++	       "Options:\n", gnu_basename(progname));
+ 
+ 	for (itr = options, itr_short = options_short;
+ 				itr->name != NULL; itr++, itr_short++)
+diff --git a/tools/depmod.c b/tools/depmod.c
+index 43fc354..cfb15b1 100644
+--- a/tools/depmod.c
++++ b/tools/depmod.c
+@@ -762,7 +762,7 @@ static int cfg_files_insert_sorted(struct cfg_file ***p_files, size_t *p_n_files
+ 	if (name != NULL)
+ 		namelen = strlen(name);
+ 	else {
+-		name = basename(dir);
++		name = gnu_basename(dir);
+ 		namelen = strlen(name);
+ 		dirlen -= namelen + 1;
+ 	}
+diff --git a/tools/kmod.c b/tools/kmod.c
+index 55689c0..df91e5c 100644
+--- a/tools/kmod.c
++++ b/tools/kmod.c
+@@ -68,7 +68,7 @@ static int kmod_help(int argc, char *argv[])
+ 			"Options:\n"
+ 			"\t-V, --version     show version\n"
+ 			"\t-h, --help        show this help\n\n"
+-			"Commands:\n", basename(argv[0]));
++			"Commands:\n", gnu_basename(argv[0]));
+ 
+ 	for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) {
+ 		if (kmod_cmds[i]->help != NULL) {
+@@ -156,7 +156,7 @@ static int handle_kmod_compat_commands(int argc, char *argv[])
+ 	const char *cmd;
+ 	size_t i;
+ 
+-	cmd = basename(argv[0]);
++	cmd = gnu_basename(argv[0]);
+ 
+ 	for (i = 0; i < ARRAY_SIZE(kmod_compat_cmds); i++) {
+ 		if (streq(kmod_compat_cmds[i]->name, cmd))
+-- 
+2.43.0
+
meta/recipes-kernel/kmod/kmod_31.bb +1 -0
index 934a678a062..c11ce456f21 100644
@@ -20,6 +20,7 @@ SRCREV = "aff617ea871d0568cc491bd116c0be1e857463bb"
 SRC_URI = "git://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git;branch=master;protocol=https \
            file://depmod-search.conf \
            file://avoid_parallel_tests.patch \
+           file://0001-Use-portable-implementation-for-basename-API.patch \
            "
 
 S = "${WORKDIR}/git"
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 3/6 elfutils: Use own basename API implementation

In reply to Khem Raj

This helps in building it for musl libc after this change [1]

[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

---
 .../elfutils/elfutils_0.189.bb                |   1 +
 ...001-Add-helper-function-for-basename.patch | 404 ++++++++++++++++++
 2 files changed, 405 insertions(+)
 create mode 100644 meta/recipes-devtools/elfutils/files/0001-Add-helper-function-for-basename.patch
meta/recipes-devtools/elfutils/elfutils_0.189.bb +1 -0
index d69828131e4..220f747cb95 100644
@@ -21,6 +21,7 @@ SRC_URI = "https://sourceware.org/elfutils/ftp/${PV}/${BP}.tar.bz2 \
            file://0001-skip-the-test-when-gcc-not-deployed.patch \
            file://ptest.patch \
            file://0001-tests-Makefile.am-compile-test_nlist-with-standard-C.patch \
+           file://0001-Add-helper-function-for-basename.patch \
            "
 SRC_URI:append:libc-musl = " \
            file://0003-musl-utils.patch \
meta/recipes-devtools/elfutils/files/0001-Add-helper-function-for-basename.patch new file +404 -0

404 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#3 lore raw

PATCH 4/6 util-linux: Fix build with latest musl

In reply to Khem Raj

Musl has removed basename declaration in string.h which exposes this error.

---
 meta/recipes-core/util-linux/util-linux.inc   |  1 +
 ...ls-include-libgen.h-for-basename-API.patch | 57 +++++++++++++++++++
 2 files changed, 58 insertions(+)
 create mode 100644 meta/recipes-core/util-linux/util-linux/0001-login-utils-include-libgen.h-for-basename-API.patch
meta/recipes-core/util-linux/util-linux.inc +1 -0
index 952a680a849..e3bef5acfc5 100644
@@ -36,6 +36,7 @@ SRC_URI = "${KERNELORG_MIRROR}/linux/utils/util-linux/v${MAJOR_VERSION}/util-lin
            file://display_testname_for_subtest.patch \
            file://avoid_parallel_tests.patch \
            file://0001-lscpu-Use-4K-buffer-size-instead-of-BUFSIZ.patch \
+           file://0001-login-utils-include-libgen.h-for-basename-API.patch \
            "
 
 SRC_URI[sha256sum] = "87abdfaa8e490f8be6dde976f7c80b9b5ff9f301e1b67e3899e1f05a59a1531f"
meta/recipes-core/util-linux/util-linux/0001-login-utils-include-libgen.h-for-basename-API.patch new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 5/6 sysvinit: Include libgen.h for basename API

In reply to Khem Raj

This fixes build with latest musl which drops basename declaration API from string.h

---
 ...wn-include-libgen.h-for-basename-API.patch | 38 +++++++++++++++++++
 meta/recipes-core/sysvinit/sysvinit_3.04.bb   |  1 +
 2 files changed, 39 insertions(+)
 create mode 100644 meta/recipes-core/sysvinit/sysvinit/0001-hddown-include-libgen.h-for-basename-API.patch
meta/recipes-core/sysvinit/sysvinit/0001-hddown-include-libgen.h-for-basename-API.patch new file +38 -0
new file mode 100644
index 00000000000..5e4053bad1d
@@ -0,0 +1,38 @@
+From a07c1d94e79840c59563741b45e690e77d4f3dfa Mon Sep 17 00:00:00 2001
+From: Khem Raj <[email protected]>
+Date: Sun, 3 Dec 2023 20:09:30 -0800
+Subject: [PATCH] hddown: include libgen.h for basename API
+
+musl has removed the non-prototype declaration of basename from string.h [1] which now results in build errors with clang-17+ compiler
+
+include libgen.h for using the posix declaration of the funciton.
+
+Fixes
+
+hddown.c:135:8: error: incompatible integer to pointer conversion assigning to 'char *' from 'int' [-Wint-conversion]
+  135 |                         ptr = basename(lnk);
+      |                             ^ ~~~~~~~~~~~~~
+
+[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7
+
+Upstream-Status: Submitted [https://github.com/slicer69/sysvinit/pull/21]
+Signed-off-by: Khem Raj <[email protected]>
+---
+ src/hddown.c | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/src/hddown.c b/src/hddown.c
+index 7a2cf28..3b31bc0 100644
+--- a/src/hddown.c
++++ b/src/hddown.c
+@@ -24,6 +24,7 @@ char *v_hddown = "@(#)hddown.c  1.02  22-Apr-2003  [email protected]";
+ #ifndef _GNU_SOURCE
+ #define _GNU_SOURCE
+ #endif
++#include <libgen.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <unistd.h>
+-- 
+2.43.0
+
meta/recipes-core/sysvinit/sysvinit_3.04.bb +1 -0
index 76b187c1961..6a612468f35 100644
@@ -21,6 +21,7 @@ SRC_URI = "${SAVANNAH_GNU_MIRROR}/sysvinit/sysvinit-${PV}.tar.xz \
            file://rcS \
            file://bootlogd.init \
            file://01_bootlogd \
+           file://0001-hddown-include-libgen.h-for-basename-API.patch \
            "
 SRC_URI[sha256sum] = "2a621fe6e4528bc91308b74867ddaaebbdf7753f02395c0c5bae817bd2b7e3a5"
 
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH 6/6 attr: Fix build with latest musl

In reply to Khem Raj

Include libgen.h to get basename() signature

---
 meta/recipes-support/attr/attr.inc            |  1 +
 ...ibgen.h-for-posix-version-of-basenam.patch | 35 +++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 meta/recipes-support/attr/attr/0001-attr.c-Include-libgen.h-for-posix-version-of-basenam.patch
meta/recipes-support/attr/attr.inc +1 -0
index e8835398afa..75d616893a5 100644
@@ -16,6 +16,7 @@ LIC_FILES_CHKSUM = "file://doc/COPYING;md5=2d0aa14b3fce4694e4f615e30186335f \
 
 SRC_URI = "${SAVANNAH_GNU_MIRROR}/attr/${BP}.tar.gz \
            file://run-ptest \
+           file://0001-attr.c-Include-libgen.h-for-posix-version-of-basenam.patch \
 "
 
 inherit ptest update-alternatives autotools gettext
meta/recipes-support/attr/attr/0001-attr.c-Include-libgen.h-for-posix-version-of-basenam.patch new file +35 -0
new file mode 100644
index 00000000000..1e2bea5067e
@@ -0,0 +1,35 @@
+From 6d9e827bcacf387bb3cfae64bd4fe520168ccad4 Mon Sep 17 00:00:00 2001
+From: Khem Raj <[email protected]>
+Date: Sun, 3 Dec 2023 19:29:27 -0800
+Subject: [PATCH] attr.c: Include libgen.h for posix version of basename API
+
+Musl has removed the definition from string.h [1] which results in
+compile failures with clang
+
+| ../attr-2.5.1/tools/attr.c:69:13: error: call to undeclared function 'basename'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
+|    69 |         progname = basename(argv[0]);
+|       |                    ^
+
+[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7
+
+Upstream-Status: Submitted [https://savannah.nongnu.org/bugs/index.php?64972]
+Signed-off-by: Khem Raj <[email protected]>
+---
+ tools/attr.c | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/tools/attr.c b/tools/attr.c
+index 312aef1..90dab83 100644
+--- a/tools/attr.c
++++ b/tools/attr.c
+@@ -26,6 +26,7 @@
+ #include <stdlib.h>
+ #include <unistd.h>
+ #include <errno.h>
++#include <libgen.h>
+ #include <string.h>
+ #include <locale.h>
+ 
+-- 
+2.43.0
+
Signature · 1 line
2.43.0