Re: [PATCH 2/2] gdb: Require C++11

Pedro Alves
2016-10-28T09:03:27Z

On 10/28/2016 09:47 AM, Philipp Rudo wrote:

> Without knowing much about autoconf. But you are repeating those
> exact 280 lines a total of 6 times in this patch. Isn't there a nicer
> way? Like moving it to a function? It would make the path significantly
> shorter.

Oh, you're looking at "configure", but that's a generated file that
autoconf spits out. The source for that repeated code is
ax_cxx_compile_stdcxx.m4 (the file added by patch #1), and that one
does contain a single copy of that code. autoconf decides to
expand the same code more than once because the ax_cxx_compile_stdcxx.m4 file
has support for testing c++14 too, and that inlines the C++11 standards
tests too:

dnl Test body for checking C++11 support

m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
_AX_CXX_COMPILE_STDCXX_testbody_new_in_11
)

dnl Test body for checking C++14 support

m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
_AX_CXX_COMPILE_STDCXX_testbody_new_in_11
_AX_CXX_COMPILE_STDCXX_testbody_new_in_14
)

Becasily, those AX_CXX_COMPILE_STDCXX_foo calls mean
"inline body of test code" here. So the duplicated
code is already factored out and maintained in a single place.

(I didn't strip out the support for C++14 from that
script, to minimize changes compared to the upstream
copy.)

Below's the same patch but without the autogenerated bits.

From ffb94ceac7fc12d6147bc3280bd8cd7d30dda4e1 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Thu, 13 Oct 2016 00:27:45 +0100
Subject: [PATCH] gdb: Require C++11

Use AX_CXX_COMPILE_STDCXX to detect if the compiler supports C++11,
and if -std=xxx switches are necessary to enable C++11.

We need to tweak AX_CXX_COMPILE_STDCXX a bit though. Pristine
upstream AX_CXX_COMPILE_STDCXX appends -std=gnu++11 to CXX directly.
That doesn't work for us, because the top level Makefile passes CXX
down to subdirs, and that overrides whatever gdb/Makefile may set CXX
to. The result would be that a make invocation from the build/gdb/
directory would use "g++ -std=gnu++11" as expected, while a make
invocation at the top level would not.

So instead of having AX_CXX_COMPILE_STDCXX set CXX directly, tweak it
to AC_SUBST a separate variable -- CXX_DIALECT -- and use '$(CXX)
(CXX_DIALECT)' to compile/link.

Confirmed that this enables C++11 starting with gcc 4.8, the first gcc
release with full C++11 support.

Also confirmed that configure errors out gracefully with older GCC
releases:

checking whether /opt/gcc-4.7/bin/g++ supports C++11 features by default... no
checking whether /opt/gcc-4.7/bin/g++ supports C++11 features with -std=gnu++11... no
checking whether /opt/gcc-4.7/bin/g++ supports C++11 features with -std=gnu++0x... no
checking whether /opt/gcc-4.7/bin/g++ supports C++11 features with -std=c++11... no
checking whether /opt/gcc-4.7/bin/g++ supports C++11 features with -std=c++0x... no
checking whether /opt/gcc-4.7/bin/g++ supports C++11 features with +std=c++11... no
checking whether /opt/gcc-4.7/bin/g++ supports C++11 features with -h std=c++11... no
configure: error: *** A compiler with support for C++11 language features is required.
Makefile:9451: recipe for target 'configure-gdb' failed
make[1]: *** [configure-gdb] Error 1
make[1]: Leaving directory '/home/pedro/brno/pedro/gdb/mygit/cxx-convertion/build-gcc-4.7'

If we need to revert back to making C++03 optional, all that's
necessary is to change the "mandatory" to "optional" in configure.ac
and regenerate configure (both gdb and gdbserver).

gdb/ChangeLog:
yyyy-mm-dd Pedro Alves <palves@redhat.com>

* Makefile.in (CXX_DIALECT): Get from configure.
(COMPILE.pre, CC_LD): Append $(CXX_DIALECT).
(FLAGS_TO_PASS): Pass CXX_DIALECT.
* acinclude.m4: Include ax_cxx_compile_stdcxx.m4.
* ax_cxx_compile_stdcxx.m4: Add FSF copyright header. Set and
AC_SUBST CXX_DIALECT instead of changing CXX/CXXCPP.
* configure.ac: Call AX_CXX_COMPILE_STDCXX.
* config.in: Regenerate.
* configure: Regenerate.

gdb/gdbserver/ChangeLog:
yyyy-mm-dd Pedro Alves <palves@redhat.com>

* Makefile.in (CXX_DIALECT): Get from configure.
(COMPILE.pre, CC_LD): Append $(CXX_DIALECT).
* acinclude.m4: Include ../ax_cxx_compile_stdcxx.m4.
* configure.ac: Call AX_CXX_COMPILE_STDCXX.
* config.in: Regenerate.
* configure: Regenerate.
---
gdb/Makefile.in | 6 +-
gdb/acinclude.m4 | 2 +
gdb/ax_cxx_compile_stdcxx.m4 | 21 +-
gdb/config.in | 3 +
gdb/configure | 981 ++++++++++++++++++++++++++++++++++++++++++-
gdb/configure.ac | 4 +
gdb/gdbserver/Makefile.in | 5 +-
gdb/gdbserver/acinclude.m4 | 2 +
gdb/gdbserver/config.in | 3 +
gdb/gdbserver/configure | 981 ++++++++++++++++++++++++++++++++++++++++++-
gdb/gdbserver/configure.ac | 4 +
11 files changed, 1998 insertions(+), 14 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 2c88434..d035d8e 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -86,6 +86,7 @@ CATALOGS = @CATALOGS@
# distribution will fix your include files up.
CC=@CC@
CXX=@CXX@
+CXX_DIALECT= @CXX_DIALECT@

# Dependency tracking information.
DEPMODE = @CCDEPMODE@
@@ -94,7 +95,7 @@ depcomp = $(SHELL) $(srcdir)/../depcomp

# Note that these are overridden by GNU make-specific code below if
# GNU make is used. The overrides implement dependency tracking.
-COMPILE.pre = $(CXX)
+COMPILE.pre = $(CXX) $(CXX_DIALECT)
COMPILE.post = -c -o $@
COMPILE = $(COMPILE.pre) $(INTERNAL_CFLAGS) $(COMPILE.post)
POSTCOMPILE = @true
@@ -125,7 +126,7 @@ MAKEHTMLFLAGS =
# Set this up with gcc if you have gnu ld and the loader will print out
# line numbers for undefined references.
#CC_LD=g++ -static
-CC_LD=$(CXX)
+CC_LD=$(CXX) $(CXX_DIALECT)

# Where is our "include" directory? Typically $(srcdir)/../include.
# This is essentially the header file directory for the library
@@ -742,6 +743,7 @@ FLAGS_TO_PASS = \
"CC=$(CC)" \
"CFLAGS=$(CFLAGS)" \
"CXX=$(CXX)" \
+ "CXX_DIALECT=$(CXX_DIALECT)" \
"CXXFLAGS=$(CXXFLAGS)" \
"DLLTOOL=$(DLLTOOL)" \
"LDFLAGS=$(LDFLAGS)" \
diff --git a/gdb/acinclude.m4 b/gdb/acinclude.m4
index 4b3f7fc..daf4a91 100644
--- a/gdb/acinclude.m4
+++ b/gdb/acinclude.m4
@@ -68,6 +68,8 @@ m4_include(libiberty.m4)
dnl For GDB_AC_PTRACE.
m4_include(ptrace.m4)

+m4_include(ax_cxx_compile_stdcxx.m4)
+
## ----------------------------------------- ##
## ANSIfy the C compiler whenever possible. ##
## From Franc,ois Pinard ##
diff --git a/gdb/ax_cxx_compile_stdcxx.m4 b/gdb/ax_cxx_compile_stdcxx.m4
index 2c18e49..ffaeb6e 100644
--- a/gdb/ax_cxx_compile_stdcxx.m4
+++ b/gdb/ax_cxx_compile_stdcxx.m4
@@ -1,3 +1,12 @@
+# Copyright (c) 2016 Free Software Foundation, Inc.
+#
+# Originally based on the AX_CXX_COMPILE_STDCXX macro found at the url
+# below.
+#
+# Local GDB customizations:
+#
+# - AC_SUBST CXX_DIALECT instead of changing CXX/CXXCPP.
+#
# ===========================================================================
# http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
# ===========================================================================
@@ -58,6 +67,7 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
[$3], [optional], [ax_cxx_compile_cxx$1_required=false],
[m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
AC_LANG_PUSH([C++])dnl
+ CXX_DIALECT=""
ac_success=no
AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
ax_cv_cxx_compile_cxx$1,
@@ -81,10 +91,7 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
[eval $cachevar=no])
CXX="$ac_save_CXX"])
if eval test x\$$cachevar = xyes; then
- CXX="$CXX $switch"
- if test -n "$CXXCPP" ; then
- CXXCPP="$CXXCPP $switch"
- fi
+ CXX_DIALECT="$switch"
ac_success=yes
break
fi
@@ -107,10 +114,7 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
[eval $cachevar=no])
CXX="$ac_save_CXX"])
if eval test x\$$cachevar = xyes; then
- CXX="$CXX $switch"
- if test -n "$CXXCPP" ; then
- CXXCPP="$CXXCPP $switch"
- fi
+ CXX_DIALECT="$switch"
ac_success=yes
break
fi
@@ -131,6 +135,7 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
[define if the compiler supports basic C++$1 syntax])
fi
AC_SUBST(HAVE_CXX$1)
+ AC_SUBST(CXX_DIALECT)
])

diff --git a/gdb/configure.ac b/gdb/configure.ac
index e451e60..83c2707 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -38,6 +38,10 @@ AC_CONFIG_AUX_DIR(..)
AC_CANONICAL_SYSTEM
AC_ARG_PROGRAM

+# We require a C++11 compiler. Check if one is available, and if
+# necessary, set CXX_DIALECT to some -std=xxx switch.
+AX_CXX_COMPILE_STDCXX(11, , mandatory)
+
# Dependency checking.
ZW_CREATE_DEPDIR
ZW_PROG_COMPILER_DEPENDENCIES([CC])
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index 0db5287..5ba559c 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -51,6 +51,7 @@ RANLIB = @RANLIB@

CC = @CC@
CXX = @CXX@
+CXX_DIALECT= @CXX_DIALECT@
AR = @AR@
AR_FLAGS = rc

@@ -61,7 +62,7 @@ depcomp = $(SHELL) $(srcdir)/../depcomp

# Note that these are overridden by GNU make-specific code below if
# GNU make is used. The overrides implement dependency tracking.
-COMPILE.pre = $(CXX)
+COMPILE.pre = $(CXX) $(CXX_DIALECT)
COMPILE.post = -c -o $@
COMPILE = $(COMPILE.pre) $(INTERNAL_CFLAGS) $(COMPILE.post)
POSTCOMPILE = @true
@@ -80,7 +81,7 @@ VPATH = @srcdir@
# Set this up with gcc if you have gnu ld and the loader will print out
# line numbers for undefinded refs.
#CC_LD=g++ -static
-CC_LD=$(CXX)
+CC_LD=$(CXX) $(CXX_DIALECT)

# Where is the "include" directory? Traditionally ../include or ./include
INCLUDE_DIR = ${srcdir}/../../include
diff --git a/gdb/gdbserver/acinclude.m4 b/gdb/gdbserver/acinclude.m4
index 8ec9188..c75d783 100644
--- a/gdb/gdbserver/acinclude.m4
+++ b/gdb/gdbserver/acinclude.m4
@@ -29,6 +29,8 @@ m4_include(../libiberty.m4)
dnl For GDB_AC_PTRACE.
m4_include(../ptrace.m4)

+m4_include(../ax_cxx_compile_stdcxx.m4)
+
dnl Check for existence of a type $1 in libthread_db.h
dnl Based on BFD_HAVE_SYS_PROCFS_TYPE in bfd/bfd.m4.

diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index 6d5907b..11d8c79 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -38,6 +38,10 @@ AC_PROG_RANLIB

AC_ARG_PROGRAM

+# We require a C++11 compiler. Check if one is available, and if
+# necessary, set CXX_DIALECT to some -std=xxx switch.
+AX_CXX_COMPILE_STDCXX(11, , mandatory)
+
AC_HEADER_STDC

# Set the 'development' global.
--
2.5.5