Skip to contentSkip to conversation

build: fix compile issues against non-glibc

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

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover build: fix compile issues against non-glibc

Fix issues when building against a C library other than glibc, such as Android's Bionic libc. The basename() patch isn't needed on master.

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 (3):
tools: use getprogname() when available to remove dependency on glibc
core: add missing #include for basename()
build: add Android.bp to build within Android tree

 Android.bp           | 126 +++++++++++++++++++++++++++++++++++++++++++
 configure.ac         |   5 ++
 lib/core.c           |   1 +
 tools/gpiodetect.c   |   4 +-
 tools/gpiofind.c     |   4 +-
 tools/gpioget.c      |   4 +-
 tools/gpioinfo.c     |   4 +-
 tools/gpiomon.c      |   4 +-
 tools/gpioset.c      |   4 +-
 tools/tools-common.c |  12 +++--
 tools/tools-common.h |   4 +-
 11 files changed, 154 insertions(+), 18 deletions(-)
 create mode 100644 Android.bp
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/3 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         |  5 +++++
 tools/gpiodetect.c   |  4 ++--
 tools/gpiofind.c     |  4 ++--
 tools/gpioget.c      |  4 ++--
 tools/gpioinfo.c     |  4 ++--
 tools/gpiomon.c      |  4 ++--
 tools/gpioset.c      |  4 ++--
 tools/tools-common.c | 12 +++++++-----
 tools/tools-common.h |  4 +++-
 9 files changed, 27 insertions(+), 18 deletions(-)
configure.ac +5 -0
index 31681c2..53c26a0 100644
@@ -132,6 +132,11 @@ then
 	AC_CHECK_FUNC([signalfd], [], [FUNC_NOT_FOUND_TOOLS([signalfd])])
 	AC_CHECK_FUNC([setlinebuf], [], [FUNC_NOT_FOUND_TOOLS([setlinebuf])])
 	AC_CHECK_HEADERS([sys/signalfd.h], [], [HEADER_NOT_FOUND_TOOLS([sys/signalfd.h])])
+	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>])])
 fi
 
 AC_ARG_ENABLE([tests],
tools/gpiodetect.c +2 -2
index 1c992a4..9e06b51 100644
@@ -22,7 +22,7 @@ static const char *const shortopts = "+hv";
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS]\n", get_progname());
+	printf("Usage: %s [OPTIONS]\n", getprogname());
 	printf("\n");
 	printf("List all GPIO chips, print their labels and number of GPIO lines.\n");
 	printf("\n");
@@ -50,7 +50,7 @@ int main(int argc, char **argv)
 			print_version();
 			return EXIT_SUCCESS;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
tools/gpiofind.c +2 -2
index 931ac40..6a81b63 100644
@@ -22,7 +22,7 @@ static const char *const shortopts = "+hv";
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] <name>\n", get_progname());
+	printf("Usage: %s [OPTIONS] <name>\n", getprogname());
 	printf("\n");
 	printf("Find a GPIO line by name. The output of this command can be used as input for gpioget/set.\n");
 	printf("\n");
@@ -50,7 +50,7 @@ int main(int argc, char **argv)
 			print_version();
 			return EXIT_SUCCESS;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
tools/gpioget.c +2 -2
index 815507d..066a8a8 100644
@@ -26,7 +26,7 @@ static const char *const shortopts = "+hvlB:";
 static void print_help(void)
 {
 	printf("Usage: %s [OPTIONS] <chip name/number> <offset 1> <offset 2> ...\n",
-	       get_progname());
+	       getprogname());
 	printf("\n");
 	printf("Read line value(s) from a GPIO chip\n");
 	printf("\n");
@@ -67,7 +67,7 @@ int main(int argc, char **argv)
 			flags = bias_flags(optarg);
 			break;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
tools/gpioinfo.c +2 -2
index 67be379..779475b 100644
@@ -73,7 +73,7 @@ static const char *const shortopts = "+hv";
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] <gpiochip1> ...\n", get_progname());
+	printf("Usage: %s [OPTIONS] <gpiochip1> ...\n", getprogname());
 	printf("\n");
 	printf("Print information about all lines of the specified GPIO chip(s) (or all gpiochips if none are specified).\n");
 	printf("\n");
@@ -201,7 +201,7 @@ int main(int argc, char **argv)
 			print_version();
 			return EXIT_SUCCESS;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
tools/gpiomon.c +2 -2
index 213efaa..a2ef4ea 100644
@@ -35,7 +35,7 @@ static const char *const shortopts = "+hvlB:n:srfbF:";
 static void print_help(void)
 {
 	printf("Usage: %s [OPTIONS] <chip name/number> <offset 1> <offset 2> ...\n",
-	       get_progname());
+	       getprogname());
 	printf("\n");
 	printf("Wait for events on GPIO lines and print them to standard output\n");
 	printf("\n");
@@ -275,7 +275,7 @@ int main(int argc, char **argv)
 			ctx.fmt = optarg;
 			break;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
tools/gpioset.c +2 -2
index fb6d51e..0152179 100644
@@ -35,7 +35,7 @@ static const char *const shortopts = "+hvlB:D:m:s:u:b";
 static void print_help(void)
 {
 	printf("Usage: %s [OPTIONS] <chip name/number> <offset1>=<value1> <offset2>=<value2> ...\n",
-	       get_progname());
+	       getprogname());
 	printf("\n");
 	printf("Set GPIO line values of a GPIO chip and maintain the state until the process exits\n");
 	printf("\n");
@@ -242,7 +242,7 @@ int main(int argc, char **argv)
 			cbdata.daemonize = true;
 			break;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
tools/tools-common.c +7 -5
index 12bde20..cf829c3 100644
@@ -19,17 +19,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 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);
@@ -42,7 +44,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);
@@ -53,7 +55,7 @@ void die_perror(const char *fmt, ...)
 void print_version(void)
 {
 	printf("%s (libgpiod) v%s\n",
-	       program_invocation_short_name, gpiod_version_string());
+	       getprogname(), gpiod_version_string());
 	printf("Copyright (C) 2017-2018 Bartosz Golaszewski\n");
 	printf("License: LGPLv2.1\n");
 	printf("This is free software: you are free to change and redistribute it.\n");
tools/tools-common.h +3 -1
index 9f1b801..7582c8b 100644
@@ -21,7 +21,9 @@
 
 #define GETOPT_NULL_LONGOPT	NULL, 0, NULL, 0
 
-const char *get_progname(void);
+#ifndef HAVE_GETPROGNAME
+const char *getprogname(void);
+#endif
 void die(const char *fmt, ...) NORETURN PRINTF(1, 2);
 void die_perror(const char *fmt, ...) NORETURN PRINTF(1, 2);
 void print_version(void);
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/3 core: add missing #include for basename()

In reply to Benjamin Li

This is a standard include, it looks like an error that it's missing. Probably pulled in transitively by one of the other includes on some/ most other platforms.

---
 lib/core.c | 1 +
 1 file changed, 1 insertion(+)
lib/core.c +1 -0
index 35dcd8e..cf1d677 100644
@@ -10,6 +10,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <gpiod.h>
+#include <libgen.h>
 #include <limits.h>
 #include <linux/gpio.h>
 #include <poll.h>
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/3 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 | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 126 insertions(+)
 create mode 100644 Android.bp
Android.bp new file +126 -0
new file mode 100644
index 0000000..82a2f98
@@ -0,0 +1,126 @@
+//
+// 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++11",
+    cflags: [
+        "-DGPIOD_VERSION_STR=\"1.6.x\"",
+        "-DHAVE_GETPROGNAME=1",
+    ],
+    cppflags: [
+        // Google C++ style is to not use exceptions, but this library does
+        // use them.
+        "-fexceptions",
+    ],
+}
+
+//
+// libgpiod tools
+//
+
+phony {
+    name: "libgpiod_tools",
+    required: [
+        "gpiodetect",
+        "gpiofind",
+        "gpioget",
+        "gpioinfo",
+        "gpiomon",
+        "gpioset",
+    ],
+}
+
+cc_binary {
+    name: "gpiodetect",
+    defaults: [
+        "libgpiod_defaults",
+        "libgpiod_tools_defaults",
+    ],
+    srcs: [
+        "tools/gpiodetect.c",
+    ],
+}
+
+cc_binary {
+    name: "gpiofind",
+    defaults: [
+        "libgpiod_defaults",
+        "libgpiod_tools_defaults",
+    ],
+    srcs: [
+        "tools/gpiofind.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: "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
#4 lore raw
In reply to Benjamin Li
Quoted 26 lines · Benjamin Li

Fix issues when building against a C library other than glibc, such as Android's Bionic libc. The basename() patch isn't needed on master.

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 (3):
tools: use getprogname() when available to remove dependency on glibc
core: add missing #include for basename()
build: add Android.bp to build within Android tree

 Android.bp           | 126 +++++++++++++++++++++++++++++++++++++++++++
 configure.ac         |   5 ++
 lib/core.c           |   1 +
 tools/gpiodetect.c   |   4 +-
 tools/gpiofind.c     |   4 +-
 tools/gpioget.c      |   4 +-
 tools/gpioinfo.c     |   4 +-
 tools/gpiomon.c      |   4 +-
 tools/gpioset.c      |   4 +-
 tools/tools-common.c |  12 +++--
 tools/tools-common.h |   4 +-
 11 files changed, 154 insertions(+), 18 deletions(-)
 create mode 100644 Android.bp
Quoted 3 lines

--
2.25.1

I applied patch 2/3 as it's a fix. For the rest of them - my goal for the v1.6.x series is to not support it anymore, than is absolutely necessary - that means no new features and android build looks like one to me. Any chance you can carry this locally?

For v2.x I'm open to adding it but have a couple comments, see the relevant email threads.

Bart

#5 lore raw
Quoted 4 lines · Bartosz Golaszewski

I applied patch 2/3 as it's a fix. For the rest of them - my goal for the v1.6.x series is to not support it anymore, than is absolutely necessary - that means no new features and android build looks like one to me. Any chance you can carry this locally?

Sure, we don't mind. But I'd like to gently remind that a lot of device manufacturers like us have platforms that are unfortunately stuck on older kernel versions that don't have the v2 IOCTLs.

Are there plans to introduce backwards compatibility to libgpiod v2 to support pre-5.10 kernels without v2 IOCTLs? I assume no based on your talk from July since there's a major data model re-architecture.

Anyways, as an aside it would be nice to note in the README about the 5.10-or-later requirement for libgpiod v2 (apologies if it's mentioned and I missed it).

I didn't learn about the compatibility gap until I tested libgpiod v2 tools on-device and found that they failed without a sufficiently new kernel. Bummer as I was looking forward to being able to reference GPIOs by just the labels in the CLI!

Quoted 3 lines

For v2.x I'm open to adding it but have a couple comments, see the relevant email threads.

Thanks, will respond there.

Ben

Quoted 2 lines

Bart

#6 lore raw
Quoted 11 lines · Benjamin Li
Quoted 4 lines · Bartosz Golaszewski

I applied patch 2/3 as it's a fix. For the rest of them - my goal for the v1.6.x series is to not support it anymore, than is absolutely necessary - that means no new features and android build looks like one to me. Any chance you can carry this locally?

Sure, we don't mind. But I'd like to gently remind that a lot of device manufacturers like us have platforms that are unfortunately stuck on older kernel versions that don't have the v2 IOCTLs.

Ah, right. Android again. :)

Does the android build file need to live in the top source directory? Can it go into a separate contrib/ directory where this kind of less maintained stuff could go?

Quoted 4 lines

Are there plans to introduce backwards compatibility to libgpiod v2 to support pre-5.10 kernels without v2 IOCTLs? I assume no based on your talk from July since there's a major data model re-architecture.

Not really, as the previous uAPI is quite limited and a lot of features of libgpiod v2 would no longer work.

Quoted 3 lines

Anyways, as an aside it would be nice to note in the README about the 5.10-or-later requirement for libgpiod v2 (apologies if it's mentioned and I missed it).

Sounds good I will add it. Unfortunately it's impossible to know the kernel version at build-time so configure checks are useless. We also carry our own copy of the kernel uAPI headers for other reasons so checking the target's headers won't fly either.

Bart

Quoted 16 lines

I didn't learn about the compatibility gap until I tested libgpiod v2 tools on-device and found that they failed without a sufficiently new kernel. Bummer as I was looking forward to being able to reference GPIOs by just the labels in the CLI!

Quoted 3 lines

For v2.x I'm open to adding it but have a couple comments, see the relevant email threads.

Thanks, will respond there.

Ben

Quoted 2 lines

Bart

#7 lore raw
Quoted 5 lines · Bartosz Golaszewski

Ah, right. Android again. :)

Does the android build file need to live in the top source directory? Can it go into a separate contrib/ directory where this kind of less maintained stuff could go?

Yeah, that should work as far as I understand. I'll test it out and re-send the Android build file patch if this works out.

Quoted 4 lines

Sounds good I will add it. Unfortunately it's impossible to know the kernel version at build-time so configure checks are useless. We also carry our own copy of the kernel uAPI headers for other reasons so checking the target's headers won't fly either.

Thanks!

Ben

Quoted 2 lines

Bart