tools/compat: provide nb_check_cc_flag.m4

Implement m4 macro NB_CHECK_CC_FLAG(FLAG, [VAR=FLAG_DERIVED])
	Determine if the C compiler supports FLAG,
	and sets output variable VAR to FLAG if FLAG is supported.

	If VAR is not provided, default to FLAG_DERIVED, which is
	FLAG converted to upper-case and all special characters
	replaced with "_", and the result prepended with "CC_".
	FLAG_DERIVED is appended to the m4 macro NB_CHECK_CC_FLAG_VARS.
	E.g., if FLAG is "-Wexample=yes", FLAG_DERIVED is "CC_WEXAMPLE_YES".

This is implemented in separate buildaux/nb_check_cc_flag.m4,
based on configure's NB_CC_FLAG(), but supports VAR override.

Adapt configure from internal NB_CC_FLAG() to NB_CHECK_CC_FLAG().

(Note: AX_CHECK_COMPILE_FLAG() from autoconf-archive could be enhanced
to support the clang and gcc workarounds I've implemented here.)
This commit is contained in:
lukem 2023-07-20 15:15:23 +00:00
parent c75b14aa91
commit 27a8da3b8e
2 changed files with 69 additions and 37 deletions

View File

@ -0,0 +1,55 @@
#
# Copyright (c) 2023, Luke Mewburn <lukem@NetBSD.org>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
#
#
# _NB_CHECK_CC_FLAG_PREPARE
# Check for flags to force a compiler (e.g., clang) to fail
# if given an unknown -WWARN, and set $nb_cv_check_cc_flags
# to that flag for NB_CHECK_CC_FLAG() to use.
#
AC_DEFUN([_NB_CHECK_CC_FLAG_PREPARE], [dnl
nb_cv_check_cc_flags=
AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],
[AS_VAR_SET([nb_cv_check_cc_flags], [-Werror=unknown-warning-option])])
]) dnl _NB_CHECK_CC_FLAG_PREPARE
#
# NB_CHECK_CC_FLAG(FLAG, [VAR=FLAG_DERIVED])
# Determine if the C compiler supports FLAG,
# and sets output variable VAR to FLAG if FLAG is supported.
#
# If VAR is not provided, default to FLAG_DERIVED, which is
# FLAG converted to upper-case and all special characters
# replaced with "_", and the result prepended with "CC_".
# FLAG_DERIVED is appended to the m4 macro NB_CHECK_CC_FLAG_VARS.
# E.g., if FLAG is "-Wexample=yes", FLAG_DERIVED is "CC_WEXAMPLE_YES".
#
# Compiler-specific notes:
# clang Uses _NB_CHECK_CC_FLAG_PREPARE() to determine if
# -Werror=unknown-warning-option.
# gcc Check for -WFLAG if FLAG is -Wno-FLAG, to work around
# gcc silently ignoring unknown -Wno-FLAG.
#
AC_DEFUN([NB_CHECK_CC_FLAG], [dnl
AC_REQUIRE([_NB_CHECK_CC_FLAG_PREPARE])dnl
m4_ifblank([$1], [m4_fatal([Usage: $0(FLAG,[VAR=FLAG_DERIVED])])])dnl
m4_pushdef([NB_flag], [$1])dnl
m4_ifblank([$2], [dnl
m4_pushdef([NB_var], [CC]m4_translit(NB_flag, [-=a-z], [__A-Z]))dnl
m4_append([NB_CHECK_CC_FLAG_VARS], NB_var, [ ])dnl
], [dnl
m4_pushdef([NB_var], [$2])dnl
])dnl
m4_pushdef([NB_wflag], m4_bpatsubst(NB_flag, [^-Wno-], [-W]))dnl
AX_CHECK_COMPILE_FLAG(NB_wflag, [AS_VAR_SET(NB_var,NB_flag)], [], [$nb_cv_check_cc_flags])
AC_SUBST(NB_var)
m4_popdef([NB_flag])dnl
m4_popdef([NB_wflag])dnl
m4_popdef([NB_var])dnl
]) dnl NB_CHECK_CC_FLAG

View File

@ -1,4 +1,4 @@
# $NetBSD: configure.ac,v 1.105 2023/06/03 09:10:13 lukem Exp $
# $NetBSD: configure.ac,v 1.106 2023/07/20 15:15:23 lukem Exp $
#
# Autoconf definition file for libnbcompat.
#
@ -327,50 +327,27 @@ dnl
dnl Set per-warning CC_* variables if supported by HOST_CC.
dnl
dnl clang needs to fail if given an unknown -WWARN.
cc_fail_unknown=
AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],
[AS_VAR_APPEND([cc_fail_unknown], [-Werror=unknown-warning-option])])
dnl NB_CC_FLAG(FLAG)
dnl Determine if HOST_CC supports FLAG, and
dnl sets output variable VAR (derived from FLAG)
dnl to FLAG if it is supported.
dnl
AC_DEFUN([NB_CC_FLAG], [dnl
m4_pushdef([NB_FLAG], [$1])
dnl gcc ignores unknown -Wno-WARN but will fail unknown equivalent -WWARN.
m4_pushdef([NB_WFLAG], m4_bpatsubst(NB_FLAG, [^-Wno-], [-W]))
m4_pushdef([NB_VAR], [CC]m4_translit(NB_FLAG, [-=a-z], [__A-Z]))
AX_CHECK_COMPILE_FLAG(NB_WFLAG, [AS_VAR_SET(NB_VAR,NB_FLAG)], [], [$cc_fail_unknown])
AC_SUBST(NB_VAR)
m4_append([NB_CC_FLAG_VARS], NB_VAR, [ ])
m4_popdef([NB_FLAG])
m4_popdef([NB_WFLAG])
m4_popdef([NB_VAR])
]) dnl NB_CC_FLAG
dnl Disable use of pre-compiled headers on Darwin.
NB_CC_FLAG([-no-cpp-precomp])
NB_CHECK_CC_FLAG([-no-cpp-precomp])
dnl Detect HOST_CC support for <bsd.own.mk> CC_* warnings
NB_CC_FLAG([-Wno-address-of-packed-member])
NB_CC_FLAG([-Wno-cast-function-type])
NB_CC_FLAG([-Wno-error=address-of-packed-member])
NB_CC_FLAG([-Wno-format-overflow])
NB_CC_FLAG([-Wno-format-truncation])
NB_CC_FLAG([-Wno-implicit-fallthrough])
NB_CC_FLAG([-Wno-maybe-uninitialized])
NB_CC_FLAG([-Wno-return-local-addr])
NB_CC_FLAG([-Wno-stringop-overflow])
NB_CC_FLAG([-Wno-stringop-truncation])
NB_CHECK_CC_FLAG([-Wno-address-of-packed-member])
NB_CHECK_CC_FLAG([-Wno-cast-function-type])
NB_CHECK_CC_FLAG([-Wno-error=address-of-packed-member])
NB_CHECK_CC_FLAG([-Wno-format-overflow])
NB_CHECK_CC_FLAG([-Wno-format-truncation])
NB_CHECK_CC_FLAG([-Wno-implicit-fallthrough])
NB_CHECK_CC_FLAG([-Wno-maybe-uninitialized])
NB_CHECK_CC_FLAG([-Wno-return-local-addr])
NB_CHECK_CC_FLAG([-Wno-stringop-overflow])
NB_CHECK_CC_FLAG([-Wno-stringop-truncation])
AC_OUTPUT()
dnl Display results
dnl
AC_MSG_NOTICE([============= Configuration results =============])
m4_foreach_w([NB_VAR], [CC LIBS ]NB_CC_FLAG_VARS, [dnl
AC_MSG_NOTICE([========= configure results for compat ==========])
m4_foreach_w([NB_VAR], [CC LIBS ]NB_CHECK_CC_FLAG_VARS, [dnl
AC_MSG_NOTICE(m4_format([%-40s],NB_VAR)[$NB_VAR])
])
AC_MSG_NOTICE([================================================])