Skip to contentSkip to conversation

tools: fix compile issues when GNU extensions are not present

PATCH Superseded Benjamin Li 3 participants 8 messages Feb 25, 2023 – Feb 27, 2023 GPIO

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover tools: fix compile issues when GNU extensions are not present

v2:
- Add missing Signed-off-by: tags.

v1:
Fix issue when building against a C library without GNU extensions, such as Android's Bionic libc.

A commit with a sample Android.bp file that's working against Android 11 is included for informational purposes, which I don't expect Bartosz to pull.

Benjamin Li (2):
tools: use getprogname() when available to remove dependency on glibc
build: add Android.bp to build within Android tree

 Android.bp           | 129 +++++++++++++++++++++++++++++++++++++++++++
 configure.ac         |  11 +++-
 tools/gpiodetect.c   |   4 +-
 tools/gpioget.c      |   4 +-
 tools/gpioinfo.c     |   4 +-
 tools/gpiomon.c      |   4 +-
 tools/gpionotify.c   |   4 +-
 tools/gpioset.c      |   4 +-
 tools/tools-common.c |  16 +++---
 tools/tools-common.h |   4 +-
 10 files changed, 161 insertions(+), 23 deletions(-)
 create mode 100644 Android.bp
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 tools: use getprogname() when available to remove dependency on glibc

Platforms like Bionic libc don't have program_invocation_[short_]name, which is a GNU extension. Use getprogname() from stdlib.h, another widely agreed extension, when it's available.

It seemed a little heavyweight to add gnulib to this project's autotools set-up just for making one function portable, so I've just added the portabilty shim to tools-common.c for the moment.

---
 configure.ac         | 11 ++++++++---
 tools/gpiodetect.c   |  4 ++--
 tools/gpioget.c      |  4 ++--
 tools/gpioinfo.c     |  4 ++--
 tools/gpiomon.c      |  4 ++--
 tools/gpionotify.c   |  4 ++--
 tools/gpioset.c      |  4 ++--
 tools/tools-common.c | 16 +++++++++-------
 tools/tools-common.h |  4 +++-
 9 files changed, 32 insertions(+), 23 deletions(-)
configure.ac +8 -3
index 0a56074..7a77151 100644
@@ -117,9 +117,14 @@ AM_CONDITIONAL([WITH_GPIOSET_INTERACTIVE],
 AS_IF([test "x$with_tools" = xtrue],
 	[# These are only needed to build tools
 	AC_CHECK_FUNC([daemon], [], [FUNC_NOT_FOUND_TOOLS([daemon])])
-	AC_CHECK_FUNC([asprintf], [], [FUNC_NOT_FOUND_LIB([asprintf])])
-	AC_CHECK_FUNC([scandir], [], [FUNC_NOT_FOUND_LIB([scandir])])
-	AC_CHECK_FUNC([alphasort], [], [FUNC_NOT_FOUND_LIB([alphasort])])
+	AC_CHECK_FUNC([asprintf], [], [FUNC_NOT_FOUND_TOOLS([asprintf])])
+	AC_CHECK_FUNC([scandir], [], [FUNC_NOT_FOUND_TOOLS([scandir])])
+	AC_CHECK_FUNC([alphasort], [], [FUNC_NOT_FOUND_TOOLS([alphasort])])
+	AC_CHECK_FUNC([getprogname],
+		[AC_DEFINE([HAVE_GETPROGNAME], 1, [Define to 1 if getprogname() is available.])],
+		[AC_CHECK_DECL([program_invocation_short_name], [],
+			[FUNC_NOT_FOUND_TOOLS([getprogname or program_invocation_short_name])],
+			[#include <errno.h>])])
 	AS_IF([test "x$with_gpioset_interactive" = xtrue],
 		[PKG_CHECK_MODULES([LIBEDIT], [libedit >= 3.1])])
 	])
tools/gpiodetect.c +2 -2
index f0211da..f5bdc01 100644
@@ -12,7 +12,7 @@
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] [chip]...\n", get_progname());
+	printf("Usage: %s [OPTIONS] [chip]...\n", getprogname());
 	printf("\n");
 	printf("List GPIO chips, print their labels and number of GPIO lines.\n");
 	printf("\n");
@@ -51,7 +51,7 @@ static int parse_config(int argc, char **argv)
 			print_version();
 			exit(EXIT_SUCCESS);
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
tools/gpioget.c +2 -2
index 08c17e6..43abdf0 100644
@@ -26,7 +26,7 @@ struct config {
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+	printf("Usage: %s [OPTIONS] <line>...\n", getprogname());
 	printf("\n");
 	printf("Read values of GPIO lines.\n");
 	printf("\n");
@@ -120,7 +120,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
 			print_version();
 			exit(EXIT_SUCCESS);
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		case 0:
 			break;
 		default:
tools/gpioinfo.c +2 -2
index 1ec7f63..960f661 100644
@@ -20,7 +20,7 @@ struct config {
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] [line]...\n", get_progname());
+	printf("Usage: %s [OPTIONS] [line]...\n", getprogname());
 	printf("\n");
 	printf("Print information about GPIO lines.\n");
 	printf("\n");
@@ -83,7 +83,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
 			print_version();
 			exit(EXIT_SUCCESS);
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		case 0:
 			break;
 		default:
tools/gpiomon.c +2 -2
index 93ff463..69ec2c1 100644
@@ -34,7 +34,7 @@ struct config {
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+	printf("Usage: %s [OPTIONS] <line>...\n", getprogname());
 	printf("\n");
 	printf("Wait for events on GPIO lines and print them to standard output.\n");
 	printf("\n");
@@ -195,7 +195,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
 			print_version();
 			exit(EXIT_SUCCESS);
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		case 0:
 			break;
 		default:
tools/gpionotify.c +2 -2
index a0976f7..9242ae8 100644
@@ -27,7 +27,7 @@ struct config {
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+	printf("Usage: %s [OPTIONS] <line>...\n", getprogname());
 	printf("\n");
 	printf("Wait for changes to info on GPIO lines and print them to standard output.\n");
 	printf("\n");
@@ -144,7 +144,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
 			print_version();
 			exit(EXIT_SUCCESS);
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		case 0:
 			break;
 		default:
tools/gpioset.c +2 -2
index a7084a3..291f727 100644
@@ -36,7 +36,7 @@ struct config {
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] <line=value>...\n", get_progname());
+	printf("Usage: %s [OPTIONS] <line=value>...\n", getprogname());
 	printf("\n");
 	printf("Set values of GPIO lines.\n");
 	printf("\n");
@@ -212,7 +212,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
 			print_version();
 			exit(EXIT_SUCCESS);
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		case 0:
 			break;
 		default:
tools/tools-common.c +9 -7
index 0a54571..67530a9 100644
@@ -20,17 +20,19 @@
 
 #include "tools-common.h"
 
-const char *get_progname(void)
+#ifndef HAVE_GETPROGNAME
+const char *getprogname(void)
 {
-	return program_invocation_name;
+	return program_invocation_short_name;
 }
+#endif
 
 void print_error(const char *fmt, ...)
 {
 	va_list va;
 
 	va_start(va, fmt);
-	fprintf(stderr, "%s: ", program_invocation_name);
+	fprintf(stderr, "%s: ", getprogname());
 	vfprintf(stderr, fmt, va);
 	fprintf(stderr, "\n");
 	va_end(va);
@@ -41,7 +43,7 @@ void print_perror(const char *fmt, ...)
 	va_list va;
 
 	va_start(va, fmt);
-	fprintf(stderr, "%s: ", program_invocation_name);
+	fprintf(stderr, "%s: ", getprogname());
 	vfprintf(stderr, fmt, va);
 	fprintf(stderr, ": %s\n", strerror(errno));
 	va_end(va);
@@ -52,7 +54,7 @@ void die(const char *fmt, ...)
 	va_list va;
 
 	va_start(va, fmt);
-	fprintf(stderr, "%s: ", program_invocation_name);
+	fprintf(stderr, "%s: ", getprogname());
 	vfprintf(stderr, fmt, va);
 	fprintf(stderr, "\n");
 	va_end(va);
@@ -65,7 +67,7 @@ void die_perror(const char *fmt, ...)
 	va_list va;
 
 	va_start(va, fmt);
-	fprintf(stderr, "%s: ", program_invocation_name);
+	fprintf(stderr, "%s: ", getprogname());
 	vfprintf(stderr, fmt, va);
 	fprintf(stderr, ": %s\n", strerror(errno));
 	va_end(va);
@@ -76,7 +78,7 @@ void die_perror(const char *fmt, ...)
 void print_version(void)
 {
 	printf("%s (libgpiod) v%s\n",
-	       program_invocation_short_name, gpiod_api_version());
+	       getprogname(), gpiod_api_version());
 	printf("Copyright (C) 2017-2023 Bartosz Golaszewski\n");
 	printf("License: GPL-2.0-or-later\n");
 	printf("This is free software: you are free to change and redistribute it.\n");
tools/tools-common.h +3 -1
index 434e5ba..3aebef0 100644
@@ -78,7 +78,9 @@ struct line_resolver {
 	struct resolved_line lines[];
 };
 
-const char *get_progname(void);
+#ifndef HAVE_GETPROGNAME
+const char *getprogname(void);
+#endif
 void print_error(const char *fmt, ...) PRINTF(1, 2);
 void print_perror(const char *fmt, ...) PRINTF(1, 2);
 void die(const char *fmt, ...) NORETURN PRINTF(1, 2);
Signature · 1 line
2.25.1
#2 lore raw

Re: [libgpiod,v2 1/2] tools: use getprogname() when available to remove dependency on glibc

Fri, Feb 24, 2023 at 07:12:34PM -0800, Benjamin Li kirjoitti:

Quoted 7 lines

Platforms like Bionic libc don't have program_invocation_[short_]name, which is a GNU extension. Use getprogname() from stdlib.h, another widely agreed extension, when it's available.

It seemed a little heavyweight to add gnulib to this project's autotools set-up just for making one function portable, so I've just added the portabilty shim to tools-common.c for the moment.

...

tools/tools-common.c void die_perror(const char *fmt, ...) · line 81

Quoted 3 lines
tools/tools-common.c +1 -1
 	printf("%s (libgpiod) v%s\n",
-	       program_invocation_short_name, gpiod_api_version());
+	       getprogname(), gpiod_api_version());

Now this can be a single line.

  	printf("%s (libgpiod) v%s\n", getprogname(), gpiod_api_version());
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [libgpiod,v2 1/2] tools: use getprogname() when available to remove dependency on glibc

In reply to Benjamin Li
Quoted 11 lines · Benjamin Li

Platforms like Bionic libc don't have program_invocation_[short_]name, which is a GNU extension. Use getprogname() from stdlib.h, another widely agreed extension, when it's available.

It seemed a little heavyweight to add gnulib to this project's autotools set-up just for making one function portable, so I've just added the portabilty shim to tools-common.c for the moment.

---

This is only relevant for gpio-tools so maybe instead of relying on either a GNU or BSD extension, let's just ditch both (and the checking infrastructure) and introduce our own local helpers:

void set_prog_name(const char *name);
const char *get_prog_name(void);
const char *get_prog_shortname(void);

And call set_prog_name(argv[0]) at the start of each program? Alternatively we can just use prctl() like libgpiosim does for tests but then we'd lose the potential info about the executable path.

I'm thinking this is better than relying on extensions as I've already had to stop using twalk_r() due to it being a GNU extension not provided by musl (not by bionic as I can see).

Bart

#4 lore raw

Re: [libgpiod,v2 1/2] tools: use getprogname() when available to remove dependency on glibc

Quoted 8 lines · On 2/25/23 4:12 AM, [email protected] wrote:

tools/tools-common.c void die_perror(const char *fmt, ...) · line 81

Quoted 3 lines
tools/tools-common.c +1 -1
 	printf("%s (libgpiod) v%s\n",
-	       program_invocation_short_name, gpiod_api_version());
+	       getprogname(), gpiod_api_version());

Now this can be a single line.

  	printf("%s (libgpiod) v%s\n", getprogname(), gpiod_api_version());

Thanks, will collapse.

Quoted 12 lines · Bartosz Golaszewski

This is only relevant for gpio-tools so maybe instead of relying on either a GNU or BSD extension, let's just ditch both (and the checking infrastructure) and introduce our own local helpers:

void set_prog_name(const char *name);
const char *get_prog_name(void);
const char *get_prog_shortname(void);

And call set_prog_name(argv[0]) at the start of each program? Alternatively we can just use prctl() like libgpiosim does for tests but then we'd lose the potential info about the executable path.

Quoted 5 lines

I'm thinking this is better than relying on extensions as I've already had to stop using twalk_r() due to it being a GNU extension not provided by musl (not by bionic as I can see).

Bart

Sure, that sounds like a better solution. Will make the change.

Ben

#5 lore raw

PATCH v2 2/2 build: add Android.bp to build within Android tree

In reply to Benjamin Li

Add an Android.bp file for Soong, the Android build system, to build the library including C++ bindings along with all the CLI tools.

---
 Android.bp | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
 create mode 100644 Android.bp
Android.bp new file +129 -0
new file mode 100644
index 0000000..55ddcc8
@@ -0,0 +1,129 @@
+//
+// libgpiod main library
+//
+
+cc_library {
+    name: "libgpiod",
+    defaults: [
+        "libgpiod_defaults",
+    ],
+    srcs: [
+        "lib/*.c",
+        "bindings/cxx/*.cpp",
+    ],
+    export_include_dirs: [
+        "include",
+        // Not great to mix sources and includes in this dir.
+        "bindings/cxx",
+    ],
+}
+
+cc_defaults {
+    name: "libgpiod_defaults",
+    device_specific: true,
+    cpp_std: "gnu++17",
+    cflags: [
+        "-DGPIOD_VERSION_STR=\"2.0-rc3\"",
+        "-DHAVE_GETPROGNAME=1",
+    ],
+    cppflags: [
+        // Google C++ style is to not use exceptions, but this library does
+        // use them.
+        "-fexceptions",
+    ],
+    // Google C++ style is to not use runtime type information, but this
+    // library does use it.
+    rtti: true,
+}
+
+//
+// libgpiod tools
+//
+
+phony {
+    name: "libgpiod_tools",
+    required: [
+        "gpiodetect",
+        "gpioget",
+        "gpioinfo",
+        "gpiomon",
+        "gpionotify",
+        "gpioset",
+    ],
+}
+
+cc_binary {
+    name: "gpiodetect",
+    defaults: [
+        "libgpiod_defaults",
+        "libgpiod_tools_defaults",
+    ],
+    srcs: [
+        "tools/gpiodetect.c",
+    ],
+}
+
+cc_binary {
+    name: "gpioget",
+    defaults: [
+        "libgpiod_defaults",
+        "libgpiod_tools_defaults",
+    ],
+    srcs: [
+        "tools/gpioget.c",
+    ],
+}
+
+cc_binary {
+    name: "gpioinfo",
+    defaults: [
+        "libgpiod_defaults",
+        "libgpiod_tools_defaults",
+    ],
+    srcs: [
+        "tools/gpioinfo.c",
+    ],
+}
+
+cc_binary {
+    name: "gpiomon",
+    defaults: [
+        "libgpiod_defaults",
+        "libgpiod_tools_defaults",
+    ],
+    srcs: [
+        "tools/gpiomon.c",
+    ],
+}
+
+cc_binary {
+    name: "gpionotify",
+    defaults: [
+        "libgpiod_defaults",
+        "libgpiod_tools_defaults",
+    ],
+    srcs: [
+        "tools/gpionotify.c",
+    ],
+}
+
+cc_binary {
+    name: "gpioset",
+    defaults: [
+        "libgpiod_defaults",
+        "libgpiod_tools_defaults",
+    ],
+    srcs: [
+        "tools/gpioset.c",
+    ],
+}
+
+cc_defaults {
+    name: "libgpiod_tools_defaults",
+    srcs: [
+        "tools/tools-common.c",
+    ],
+    shared_libs: [
+        "libgpiod",
+    ],
+}
Signature · 1 line
2.25.1
#6 lore raw

Re: [libgpiod,v2 2/2] build: add Android.bp to build within Android tree

Quoted 6 lines · Benjamin Li

Add an Android.bp file for Soong, the Android build system, to build the library including C++ bindings along with all the CLI tools.

---

If I take it into the main tree, I would like to be at least able to check that it builds correctly. Could you post some instructions on how to build the android package in the least number of steps? Will I need to jump through all the regular hoops involved in building android images?

Bart

#7 lore raw

Re: [libgpiod,v2 2/2] build: add Android.bp to build within Android tree

Quoted 7 lines · Bartosz Golaszewski

If I take it into the main tree, I would like to be at least able to check that it builds correctly. Could you post some instructions on how to build the android package in the least number of steps? Will I need to jump through all the regular hoops involved in building android images?

Bart

Ah, I mentioned in the cover letter that this was informational and I didn't expect you to take it. Unfortunately I think you would need to jump through all the regular hoops.

Roughly, the steps would be:
- Checkout AOSP: https://source.android.com/docs/setup/download/downloading
- Checkout libgpiod under external/
- Build AOSP: https://source.android.com/docs/setup/build/building

Of course I would appreciate if you went out of your way to take this, but otherwise hopefully this public mailing list patch helps other Android device manufacturers/tinkerers for now.

Ben