Commit bc65806ce22a6f838c8788435aea298e1a499c75

Guillem Jover 2023-04-06T23:05:27

build: Select whether to include funopen() in the build system This makes sure we include it when expected, alongside the man pages, and the test cases, and do not accidentally break the ABI if the system starts providing such interface.

diff --git a/configure.ac b/configure.ac
index 44717c2..c2bd521 100644
--- a/configure.ac
+++ b/configure.ac
@@ -297,6 +297,7 @@ AC_CHECK_FUNCS([\
   flock \
   fopencookie \
   __fpurge \
+  funopen \
   getauxval \
   getentropy \
   getexecname \
@@ -305,8 +306,31 @@ AC_CHECK_FUNCS([\
   pstat_getproc \
   sysconf \
 ])
+
+need_funopen=yes
+AS_CASE([$host_os],
+  [*-musl*], [
+    # On musl >= 1.1.19, fopencookie() got implemented, and because we were
+    # checking for its presence to decide whether to build funopen(), it got
+    # included in builds even when previously it had not been included, which
+    # is partially an ABI issue, but given that disabling it now would be
+    # worse, we'll ignore this as this is only a problem with downgrades. And
+    # enable it explicitly
+    need_funopen=yes
+  ],
+  [darwin*], [
+    # On macOS we do not have fopencookie(), and cannot implement it.
+    need_funopen=no
+  ],
+)
+
 AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xyes"])
-AM_CONDITIONAL([HAVE_FOPENCOOKIE], [test "x$ac_cv_func_fopencookie" = "xyes"])
+AM_CONDITIONAL([NEED_FUNOPEN], [test "x$need_funopen" = "xyes"])
+AS_IF([test "x$need_funopen" = "xno" && \
+       test "x$ac_cv_func_funopen" != "xyes" && \
+       test "x$ac_cv_func_fopencookie" = "xyes"], [
+  AC_MSG_WARN([[can implement funopen() now based on newly added fopencooke(), report upstream]])
+])
 
 AC_SUBST([MD5_LIBS])
 AC_SUBST([LIBBSD_LIBS])
diff --git a/man/Makefile.am b/man/Makefile.am
index 961d2f5..6d1c86a 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -178,7 +178,6 @@ dist_man_MANS = \
 	fmtcheck.3bsd \
 	fparseln.3bsd \
 	fpurge.3bsd \
-	funopen.3bsd \
 	getbsize.3bsd \
 	getmode.3bsd \
 	getpeereid.3bsd \
@@ -252,3 +251,9 @@ dist_man_MANS = \
 	wcslcat.3bsd \
 	wcslcpy.3bsd \
 	# EOL
+
+if NEED_FUNOPEN
+dist_man_MANS += \
+	funopen.3bsd \
+	# EOL
+endif
diff --git a/src/Makefile.am b/src/Makefile.am
index bc1add6..4781bdb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -95,7 +95,6 @@ libbsd_la_SOURCES = \
 	fmtcheck.c \
 	fparseln.c \
 	fpurge.c \
-	funopen.c \
 	getbsize.c \
 	getpeereid.c \
 	heapsort.c \
@@ -137,6 +136,12 @@ libbsd_la_SOURCES += \
 	# EOL
 endif
 
+if NEED_FUNOPEN
+libbsd_la_SOURCES += \
+	funopen.c \
+	# EOL
+endif
+
 if NEED_TRANSPARENT_LIBMD
 CLEANFILES += \
 	format.ld \
diff --git a/src/funopen.c b/src/funopen.c
index 0513e38..fdcdcba 100644
--- a/src/funopen.c
+++ b/src/funopen.c
@@ -138,5 +138,5 @@ funopen(const void *cookie,
 	return fopencookie(cookiewrap, mode, funcswrap);
 }
 #else
-#warning "Function funopen() is not provided on this platform."
+#error "Function funopen() needs to be ported."
 #endif
diff --git a/test/Makefile.am b/test/Makefile.am
index 458a4e9..985e3e1 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -55,7 +55,7 @@ check_PROGRAMS = \
 	vis-openbsd \
 	# EOL
 
-if HAVE_FOPENCOOKIE
+if NEED_FUNOPEN
 check_PROGRAMS += funopen
 endif