[nasm:nasm-2.15.xx] configure.ac: better tests for typeof, snprintf, vsnprintf

nasm-bot for H. Peter Anvin (Intel) hpa at zytor.com
Thu Jul 9 19:09:03 PDT 2020


Commit-ID:  abcdf8356afbeb8d7c977b507c8797a6e1fd77f0
Gitweb:     http://repo.or.cz/w/nasm.git?a=commitdiff;h=abcdf8356afbeb8d7c977b507c8797a6e1fd77f0
Author:     H. Peter Anvin (Intel) <hpa at zytor.com>
AuthorDate: Thu, 9 Jul 2020 19:04:28 -0700
Committer:  H. Peter Anvin (Intel) <hpa at zytor.com>
CommitDate: Thu, 9 Jul 2020 19:04:28 -0700

configure.ac: better tests for typeof, snprintf, vsnprintf

With some combinations of options tests for typeof, snprintf, and
vsnprintf end up with warnings promoted to errors, which then trigger
incorrect results for these tests. Move the typeof test to the end,
and write specific tests for snprintf and vsnprintf.

Signed-off-by: H. Peter Anvin (Intel) <hpa at zytor.com>


---
 autoconf/m4/pa_func_snprintf.m4  | 39 +++++++++++++++++++++++++++++++
 autoconf/m4/pa_func_vsnprintf.m4 | 50 ++++++++++++++++++++++++++++++++++++++++
 configure.ac                     | 46 ++++++++++++++++++++----------------
 3 files changed, 115 insertions(+), 20 deletions(-)

diff --git a/autoconf/m4/pa_func_snprintf.m4 b/autoconf/m4/pa_func_snprintf.m4
new file mode 100644
index 00000000..b06c5580
--- /dev/null
+++ b/autoconf/m4/pa_func_snprintf.m4
@@ -0,0 +1,39 @@
+dnl --------------------------------------------------------------------------
+dnl PA_FUNC_SNPRINTF
+dnl
+dnl See if we have [_]snprintf(), using the proper prototypes in case
+dnl it is a builtin of some kind.
+dnl --------------------------------------------------------------------------
+AC_DEFUN([PA_FUNC_SNPRINTF],
+[AC_CACHE_CHECK([for sprintf], [pa_cv_func_snprintf],
+ [pa_cv_func_snprintf=no
+  for pa_try_func_snprintf in snprintf _snprintf
+  do
+  AS_IF([test $pa_cv_func_snprintf = no],
+        [AC_LINK_IFELSE([AC_LANG_SOURCE([
+AC_INCLUDES_DEFAULT
+const char *snprintf_test(int x);
+const char *snprintf_test(int x)
+{
+    static char buf[[256]];
+    size_t sz;
+    sz = $pa_try_func_snprintf(buf, sizeof buf, "Hello = %d", x);
+    return (sz < sizeof buf) ? buf : NULL;
+}
+
+int main(void) {
+    puts(snprintf_test(33));
+    return 0;
+}
+])],
+ [pa_cv_func_snprintf=$pa_try_func_snprintf])])
+ done
+ ])
+ AS_IF([test $pa_cv_func_snprintf = no],
+       [],
+       [AC_DEFINE([HAVE_SNPRINTF], 1,
+         [Define to 1 if you have some version of the snprintf function.])
+	 AS_IF([test $pa_cv_func_snprintf = snprintf],
+	       [],
+	       [AC_DEFINE_UNQUOTED([snprintf], [$pa_cv_func_snprintf],
+	         [Define if your snprintf function is not named snprintf.])])])])
diff --git a/autoconf/m4/pa_func_vsnprintf.m4 b/autoconf/m4/pa_func_vsnprintf.m4
new file mode 100644
index 00000000..600d4a84
--- /dev/null
+++ b/autoconf/m4/pa_func_vsnprintf.m4
@@ -0,0 +1,50 @@
+dnl --------------------------------------------------------------------------
+dnl PA_FUNC_VSNPRINTF
+dnl
+dnl See if we have [_]vsnprintf(), using the proper prototypes in case
+dnl it is a builtin of some kind.
+dnl --------------------------------------------------------------------------
+AC_DEFUN([PA_FUNC_VSNPRINTF],
+[AC_CACHE_CHECK([for vsnprintf], [pa_cv_func_vsnprintf],
+ [pa_cv_func_vsnprintf=no
+  for pa_try_func_vsnprintf in vsnprintf _vsnprintf
+  do
+  AS_IF([test $pa_cv_func_vsnprintf = no],
+        [AC_LINK_IFELSE([AC_LANG_SOURCE([
+AC_INCLUDES_DEFAULT
+const char *vsnprintf_test(const char *fmt, va_list va);
+const char *vsnprintf_test(const char *fmt, va_list va)
+{
+    static char buf[[256]];
+    size_t sz;
+    sz = $pa_try_func_vsnprintf(buf, sizeof buf, fmt, va);
+    return (sz < sizeof buf) ? buf : NULL;
+}
+
+const char *vsnprintf_caller(const char *fmt, ...);
+const char *vsnprintf_caller(const char *fmt, ...)
+{
+    const char *what;
+    va_list va;
+    va_start(va, fmt);
+    what = vsnprintf_test(fmt, va);
+    va_end(va);
+    return what;
+}
+
+int main(void) {
+    puts(vsnprintf_caller("Hello = %d", 33));
+    return 0;
+}
+])],
+ [pa_cv_func_vsnprintf=$pa_try_func_vsnprintf])])
+ done
+ ])
+ AS_IF([test $pa_cv_func_vsnprintf = no],
+       [],
+       [AC_DEFINE([HAVE_VSNPRINTF], 1,
+         [Define to 1 if you have some version of the vsnprintf function.])
+	 AS_IF([test $pa_cv_func_vsnprintf = vsnprintf],
+	       [],
+	       [AC_DEFINE_UNQUOTED([vsnprintf], [$pa_cv_func_vsnprintf],
+	         [Define if your vsnprintf function is not named vsnprintf.])])])])
diff --git a/configure.ac b/configure.ac
index 8dc47322..d52e87d6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,7 +23,7 @@ export WINELOADER
 dnl Get the canonical target system name
 AC_CANONICAL_HOST
 
-dnl Checks for programs and enable necessary CC extensions
+dnl Enable necessary CC extensions
 AC_USE_SYSTEM_EXTENSIONS
 AC_SYS_LARGEFILE
 AC_PROG_CC
@@ -36,11 +36,6 @@ PA_ARG_DISABLED([optimization],
  [pa_no_optimize=true])
 
 dnl Other programs
-AC_PROG_LN_S
-AC_PROG_MAKE_SET
-AC_PROG_INSTALL
-AC_PROG_MKDIR_P
-
 pa_no_optimize=false
 
 dnl Compile and link with dwarf debug
@@ -65,13 +60,7 @@ PA_ARG_ENABLED([panic-abort],
 AH_TEMPLATE(ABORT_ON_PANIC,
 [Define to 1 to call abort() on panics (internal errors), for debugging.])
 
-dnl Check for library extension
-PA_LIBEXT
-
 dnl Checks for typedefs, structures, and compiler characteristics.
-AC_C_CONST
-AC_C_INLINE
-AC_C_RESTRICT
 AC_TYPE_SIZE_T
 AC_C_BIGENDIAN(AC_DEFINE(WORDS_BIGENDIAN),AC_DEFINE(WORDS_LITTLEENDIAN),,)
 AH_TEMPLATE(WORDS_BIGENDIAN,
@@ -103,10 +92,15 @@ dnl assume all compilers support common, and this will help find those
 dnl problems.  This also works around an OSX linker problem.
 PA_ADD_CFLAGS([-fno-common])
 
-dnl Other C features
-PA_C_TYPEOF
+dnl Check for library extension
+PA_LIBEXT
 
 dnl Look for programs...
+AC_PROG_LN_S
+AC_PROG_MAKE_SET
+AC_PROG_INSTALL
+AC_PROG_MKDIR_P
+
 AC_CHECK_PROGS(NROFF,    nroff,    false)
 AC_CHECK_PROGS(ASCIIDOC, asciidoc, false)
 AC_CHECK_PROGS(XMLTO,    xmlto,    false)
@@ -166,8 +160,21 @@ AC_CHECK_TOOL(AR, ar)
 AC_CHECK_TOOL(RANLIB, ranlib, :)
 AC_CHECK_TOOL(STRIP, strip)
 
+dnl
+dnl NOTE: the tests for header files and library functions use constructs
+dnl that create warnings on modern compilers, due to lack of prototypes,
+dnl etc. Therefore, do not add the -Werror options before this.
+dnl
+
+dnl Tests which may trigger warnings on some compilers
+AC_C_CONST
+AC_C_INLINE
+AC_C_RESTRICT
+
 dnl Checks for header files.
 AC_HEADER_STDC
+PA_ADD_HEADERS(string.h)
+PA_ADD_HEADERS(stdarg.h)
 AC_CHECK_HEADERS(inttypes.h)
 AC_CHECK_HEADERS(strings.h)
 AC_HEADER_STDBOOL
@@ -220,9 +227,8 @@ PA_HAVE_FUNC(__builtin_clzll, (0ULL))
 PA_HAVE_FUNC(_BitScanReverse, (0))
 PA_HAVE_FUNC(_BitScanReverse64, (0))
 
-dnl Functions for which we have replacements available in stdlib/
-AC_CHECK_FUNCS([vsnprintf _vsnprintf])
-AC_CHECK_FUNCS([snprintf _snprintf])
+PA_FUNC_SNPRINTF
+PA_FUNC_VSNPRINTF
 AC_CHECK_FUNCS([strlcpy])
 AC_CHECK_FUNCS([strrchrnul])
 
@@ -388,11 +394,11 @@ PA_ARG_ENABLED([werror],
 )
 
 dnl
-dnl On some versions of gcc, -Werror=missing-prototypes causes problems
-dnl with C99-style external inlines.  Test this *after* adding the -Werror
-dnl options.
+dnl Test compiler features. On some compilers, this can be affected
+dnl by -Werror options, so run this *after* those options are added.
 dnl
 PA_CHECK_BAD_STDC_INLINE
+PA_C_TYPEOF
 
 dnl
 dnl support ccache


More information about the Nasm-commits mailing list