From: Bartosz Golaszewski Date: Tue, 24 Jan 2017 10:45:36 +0000 (+0100) Subject: build: use AC_FUNC_STRERROR_R to check the version of strerror_r() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=8e1fdcdc2028a3d5184bd99e320b62d0fe59e3c9;p=qemu-gpiodev%2Flibgpiod.git build: use AC_FUNC_STRERROR_R to check the version of strerror_r() Define _GNU_SOURCE globally in configure.ac and use the AC_FUNC_STRERROR_R macro to figure out what version of strerror_r() is provided. Some libraries (e.g. musl) only provide the POSIX version even if we explicitly request GNU extensions. Signed-off-by: Bartosz Golaszewski --- diff --git a/configure.ac b/configure.ac index c4445e9..bb9820e 100644 --- a/configure.ac +++ b/configure.ac @@ -24,6 +24,8 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) AC_CONFIG_SRCDIR([src]) AC_CONFIG_HEADER([config.h]) +AC_DEFINE([_GNU_SOURCE], [], [We want GNU extensions]) + AM_PROG_AR AC_PROG_CC AC_PROG_LIBTOOL @@ -41,6 +43,7 @@ AC_DEFUN([HEADER_NOT_FOUND_LIB], # This is always checked (library needs this) AC_HEADER_STDC AC_FUNC_MALLOC +AC_FUNC_STRERROR_R AC_CHECK_FUNC([ioctl], [], [FUNC_NOT_FOUND_LIB([ioctl])]) AC_CHECK_FUNC([asprintf], [], [FUNC_NOT_FOUND_LIB([asprintf])]) AC_CHECK_FUNC([readdir], [], [FUNC_NOT_FOUND_LIB([readdir])]) diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 35b8e70..92dea86 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -8,7 +8,7 @@ lib_LTLIBRARIES = libgpiod.la libgpiod_la_SOURCES = core.c -libgpiod_la_CFLAGS = -Wall -Wextra -g -D_GNU_SOURCE +libgpiod_la_CFLAGS = -Wall -Wextra -g libgpiod_la_CFLAGS += -fvisibility=hidden -I$(top_srcdir)/include/ libgpiod_la_CFLAGS += -include $(top_srcdir)/config.h libgpiod_la_LDFLAGS = -version-number $(subst .,:,$(PACKAGE_VERSION)) diff --git a/src/lib/core.c b/src/lib/core.c index 2d45e35..494a3b4 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -143,10 +143,25 @@ int gpiod_errno(void) return last_error; } +static const char * strerror_r_wrapper(int errnum, char *buf, size_t buflen) +{ +#ifdef STRERROR_R_CHAR_P + return strerror_r(errnum, buf, buflen); +#else + int status; + + status = strerror_r(errnum, buf, buflen); + if (status != 0) + snprintf(buf, buflen, "error in strerror_r(): %d", status); + + return buf; +#endif +} + const char * gpiod_strerror(int errnum) { if (errnum < __GPIOD_ERRNO_OFFSET) - return strerror_r(errnum, errmsg, sizeof(errmsg)); + return strerror_r_wrapper(errnum, errmsg, sizeof(errmsg)); else if (errnum > __GPIOD_MAX_ERR) return "invalid error number"; else diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am index 35e5f22..016006e 100644 --- a/src/tools/Makefile.am +++ b/src/tools/Makefile.am @@ -7,7 +7,7 @@ # AM_CFLAGS = -I$(top_srcdir)/include/ -include $(top_srcdir)/config.h -AM_CFLAGS += -Wall -Wextra -g -D_GNU_SOURCE -L$(top_srcdir)/src/lib +AM_CFLAGS += -Wall -Wextra -g -L$(top_srcdir)/src/lib AM_LDFLAGS = -lgpiod DEPENDENCIES = libgpiod.la