[PATCH 3/3] libcody, c++tools: Only use GCC-specific flags the compiler accepts
Shiva S
shivasubramanian@outlook.com
Fri Nov 6 21:05:07 GMT 2026
-fno-exceptions -fno-rtti and -MMD -MP -MF were passed unconditionally,
and libcody used -include config.h, so host compilers without these
options could not build these directories. Test for the flags at
configure time, and include config.h from internal.hh.
config/ChangeLog:
* acx.m4 (ACX_PROG_CXX_FLAGS): New macro.
libcody/ChangeLog:
* configure.ac: Include ../config/acx.m4. Check for
-fno-exceptions -fno-rtti and -MMD -MP -MF.
* configure: Regenerate.
* Makefile.in (CXXOPTS): Only add -fno-exceptions -fno-rtti if
accepted. Do not use -include config.h.
(DEPFLAGS): New, set if dependency flags are accepted.
(%.o): Use it.
(CXXFLAGS/): Add -I.
* internal.hh: Include config.h.
c++tools/ChangeLog:
* configure.ac: Check for -fno-exceptions -fno-rtti and
-MMD -MP -MF.
* configure: Regenerate.
* Makefile.in (CXXOPTS): Only add -fno-exceptions -fno-rtti if
accepted.
(DEPFLAGS): New, set if dependency flags are accepted.
(%.o): Use it.
Signed-off-by: Shiva <shivasubramanian@outlook.com>
---
c++tools/Makefile.in | 11 ++++--
c++tools/configure | 84 ++++++++++++++++++++++++++++++++++++++++++-
c++tools/configure.ac | 2 ++
config/acx.m4 | 17 +++++++++
libcody/Makefile.in | 12 +++++--
libcody/configure | 82 ++++++++++++++++++++++++++++++++++++++++++
libcody/configure.ac | 4 +++
libcody/internal.hh | 1 +
8 files changed, 207 insertions(+), 6 deletions(-)
diff --git a/c++tools/Makefile.in b/c++tools/Makefile.in
index 92f0caff339..06b87f2b80e 100644
--- a/c++tools/Makefile.in
+++ b/c++tools/Makefile.in
@@ -31,7 +31,10 @@ CXX := @CXX@
CXXFLAGS := @CXXFLAGS@
PICFLAG := @PICFLAG@
LD_PICFLAG := @LD_PICFLAG@
-CXXOPTS := $(CXXFLAGS) $(PICFLAG) -fno-exceptions -fno-rtti
+CXXOPTS := $(CXXFLAGS) $(PICFLAG)
+ifeq (@CXX_NOEXCEPTIONS@,yes)
+CXXOPTS += -fno-exceptions -fno-rtti
+endif
LDFLAGS := @LDFLAGS@
exeext := @EXEEXT@
LIBIBERTY := ../libiberty/libiberty.a
@@ -82,9 +85,13 @@ SRC_CXXFLAGS = $(CXXFLAGS$(patsubst $(srcdir)%,%,$1)) \
$(if $(filter-out $(srcdir)/,$1),\
$(call $0,$(dir $(patsubst %/,%,$1))))
+ifeq (@CXX_DEPFLAGS@,yes)
+DEPFLAGS = -MMD -MP -MF ${@:.o=.d}
+endif
+
%.o: %.cc
$(CXX) $(strip $(CXXOPTS) $(call SRC_CXXFLAGS,$<) $(CXXINC)) \
- -MMD -MP -MF ${@:.o=.d} -c -o $@ $<
+ $(DEPFLAGS) -c -o $@ $<
ifeq (@CXX_AUX_TOOLS@,yes)
diff --git a/c++tools/configure b/c++tools/configure
index b7f71658463..eb0c007b0ff 100755
--- a/c++tools/configure
+++ b/c++tools/configure
@@ -631,6 +631,8 @@ enable_host_bind_now
PICFLAG
MAINTAINER
CXX_AUX_TOOLS
+CXX_DEPFLAGS
+CXX_NOEXCEPTIONS
AUTOHEADER
AUTOCONF
OBJEXT
@@ -2878,6 +2880,87 @@ ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS
$CPPFLAGS $LDFLAGS conftest.$ac_ex
ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CXX accepts
-fno-exceptions -fno-rtti" >&5
+$as_echo_n "checking whether $CXX accepts -fno-exceptions -fno-rtti...
" >&6; }
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+acx_save_CXXFLAGS="$CXXFLAGS"
+CXXFLAGS="$CXXFLAGS -fno-exceptions -fno-rtti"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+int
+main ()
+{
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+ CXX_NOEXCEPTIONS=yes
+else
+ CXX_NOEXCEPTIONS=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+CXXFLAGS="$acx_save_CXXFLAGS"
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+rm -f conftest.d
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $CXX_NOEXCEPTIONS" >&5
+$as_echo "$CXX_NOEXCEPTIONS" >&6; }
+
+
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CXX accepts
-MMD -MP -MF conftest.d" >&5
+$as_echo_n "checking whether $CXX accepts -MMD -MP -MF conftest.d... "
>&6; }
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+acx_save_CXXFLAGS="$CXXFLAGS"
+CXXFLAGS="$CXXFLAGS -MMD -MP -MF conftest.d"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+int
+main ()
+{
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+ CXX_DEPFLAGS=yes
+else
+ CXX_DEPFLAGS=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+CXXFLAGS="$acx_save_CXXFLAGS"
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+rm -f conftest.d
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $CXX_DEPFLAGS" >&5
+$as_echo "$CXX_DEPFLAGS" >&6; }
+
+
+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether to build C++
tools" >&5
$as_echo_n "checking whether to build C++ tools... " >&6; }
# Check whether --enable-c++-tools was given.
@@ -2961,7 +3044,6 @@ fi
# Check if O_CLOEXEC is defined by fcntl
-
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for O_CLOEXEC" >&5
$as_echo_n "checking for O_CLOEXEC... " >&6; }
if ${ac_cv_o_cloexec+:} false; then :
diff --git a/c++tools/configure.ac b/c++tools/configure.ac
index 84060f97695..fba7c39080f 100644
--- a/c++tools/configure.ac
+++ b/c++tools/configure.ac
@@ -41,6 +41,8 @@ AC_CHECK_PROGS([AUTOCONF], [autoconf], [$MISSING
autoconf])
AC_CHECK_PROGS([AUTOHEADER], [autoheader], [$MISSING autoheader])
AC_LANG(C++)
+ACX_PROG_CXX_FLAGS([-fno-exceptions -fno-rtti], [CXX_NOEXCEPTIONS])
+ACX_PROG_CXX_FLAGS([-MMD -MP -MF conftest.d], [CXX_DEPFLAGS])
dnl Enabled by default
AC_MSG_CHECKING([whether to build C++ tools])
diff --git a/config/acx.m4 b/config/acx.m4
index 3a9a36a2cab..8c94f1988e8 100644
--- a/config/acx.m4
+++ b/config/acx.m4
@@ -705,3 +705,20 @@ export SHELLOPTS
])
fi
])
+
+dnl ####
+dnl # ACX_PROG_CXX_FLAGS(FLAGS, VARIABLE)
+dnl # Set VARIABLE to yes if the C++ compiler accepts FLAGS, else to no,
+dnl # and substitute it. Call it after the C++ compiler has been found.
+AC_DEFUN([ACX_PROG_CXX_FLAGS],[
+AC_MSG_CHECKING([whether $CXX accepts $1])
+AC_LANG_PUSH(C++)
+acx_save_CXXFLAGS="$CXXFLAGS"
+CXXFLAGS="$CXXFLAGS $1"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])], [$2=yes], [$2=no])
+CXXFLAGS="$acx_save_CXXFLAGS"
+AC_LANG_POP(C++)
+rm -f conftest.d
+AC_MSG_RESULT([$$2])
+AC_SUBST([$2])
+])
diff --git a/libcody/Makefile.in b/libcody/Makefile.in
index cb01b0092d8..632f56d5fb0 100644
--- a/libcody/Makefile.in
+++ b/libcody/Makefile.in
@@ -24,11 +24,13 @@ CXXINC := $(filter -I%,@CXX@)
CXXOPTS := $(CXXFLAGS) @PICFLAG@
ifneq (@EXCEPTIONS@,yes)
+ifeq (@CXX_NOEXCEPTIONS@,yes)
CXXOPTS += -fno-exceptions -fno-rtti
endif
+endif
# Config
-CXXOPTS += $(filter-out -DHAVE_CONFIG_H,@DEFS@) -include config.h
+CXXOPTS += $(filter-out -DHAVE_CONFIG_H,@DEFS@)
# Linker options
LDFLAGS := @LDFLAGS@ @LD_PICFLAG@
@@ -53,10 +55,14 @@ vpath %.cc $(srcdir)
.SUFFIXES: .o .cc
+ifeq (@CXX_DEPFLAGS@,yes)
+DEPFLAGS = -MMD -MP -MF ${@:.o=.d}
+endif
+
%.o: %.cc
@mkdir -p $(dir $@)
$(CXX) $(strip $(CXXOPTS) $(call SRC_CXXFLAGS,$<) $(CXXINC)) \
- -MMD -MP -MF ${@:.o=.d} -c -o $@ $<
+ $(DEPFLAGS) -c -o $@ $<
all:: Makefile
@@ -119,7 +125,7 @@ maintainer-clean:: distclean
.PHONY: all check clean distclean maintainer-clean mostlyclean
-CXXFLAGS/ := -I$(srcdir)
+CXXFLAGS/ := -I. -I$(srcdir)
LIBCODY.O := buffer.o client.o fatal.o netclient.o netserver.o \
resolver.o packet.o server.o
diff --git a/libcody/configure b/libcody/configure
index beb83087824..5d00b4e6a10 100755
--- a/libcody/configure
+++ b/libcody/configure
@@ -590,6 +590,8 @@ LIBOBJS
configure_args
AR
RANLIB
+CXX_DEPFLAGS
+CXX_NOEXCEPTIONS
EXCEPTIONS
LD_PICFLAG
PICFLAG
@@ -2694,6 +2696,86 @@ if test "$nms_exceptions" != no ; then
fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CXX accepts
-fno-exceptions -fno-rtti" >&5
+$as_echo_n "checking whether $CXX accepts -fno-exceptions -fno-rtti...
" >&6; }
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+acx_save_CXXFLAGS="$CXXFLAGS"
+CXXFLAGS="$CXXFLAGS -fno-exceptions -fno-rtti"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+int
+main ()
+{
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+ CXX_NOEXCEPTIONS=yes
+else
+ CXX_NOEXCEPTIONS=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+CXXFLAGS="$acx_save_CXXFLAGS"
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+rm -f conftest.d
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $CXX_NOEXCEPTIONS" >&5
+$as_echo "$CXX_NOEXCEPTIONS" >&6; }
+
+
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CXX accepts
-MMD -MP -MF conftest.d" >&5
+$as_echo_n "checking whether $CXX accepts -MMD -MP -MF conftest.d... "
>&6; }
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+acx_save_CXXFLAGS="$CXXFLAGS"
+CXXFLAGS="$CXXFLAGS -MMD -MP -MF conftest.d"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+int
+main ()
+{
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+ CXX_DEPFLAGS=yes
+else
+ CXX_DEPFLAGS=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+CXXFLAGS="$acx_save_CXXFLAGS"
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+rm -f conftest.d
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $CXX_DEPFLAGS" >&5
+$as_echo "$CXX_DEPFLAGS" >&6; }
+
+
+
if test -n "$ac_tool_prefix"; then
# Extract the first word of "${ac_tool_prefix}ranlib", so it can be
a program name with args.
set dummy ${ac_tool_prefix}ranlib; ac_word=$2
diff --git a/libcody/configure.ac b/libcody/configure.ac
index 14e8dd4a226..810e6d6159e 100644
--- a/libcody/configure.ac
+++ b/libcody/configure.ac
@@ -2,6 +2,8 @@
# Copyright (C) 2020 Nathan Sidwell, nathan@acm.org
# License: Apache v2.0
+sinclude(../config/acx.m4)
+
AC_INIT([codylib],[0.0],[github.com/urnathan/libcody])
AC_CONFIG_SRCDIR(cody.hh)
m4_include(config.m4)
@@ -90,6 +92,8 @@ AC_SUBST(PICFLAG)
AC_SUBST(LD_PICFLAG)
NMS_ENABLE_EXCEPTIONS
+ACX_PROG_CXX_FLAGS([-fno-exceptions -fno-rtti], [CXX_NOEXCEPTIONS])
+ACX_PROG_CXX_FLAGS([-MMD -MP -MF conftest.d], [CXX_DEPFLAGS])
AC_PROG_RANLIB
AC_CHECK_TOOL([AR],[ar])
diff --git a/libcody/internal.hh b/libcody/internal.hh
index 405b5e8a5e0..568a99fcd7c 100644
--- a/libcody/internal.hh
+++ b/libcody/internal.hh
@@ -2,6 +2,7 @@
// Copyright (C) 2020 Nathan Sidwell, nathan@acm.org
// License: Apache v2.0
+#include "config.h"
#include "cody.hh"
#ifndef __has_builtin
More information about the Binutils
mailing list