[gnome-builder] autotools: add template support for C++



commit 6737edc1fe5b8d9cf5ece95b99b1b6abdc6c9455
Author: Christian Hergert <christian hergert me>
Date:   Fri Apr 22 18:07:26 2016 -0700

    autotools: add template support for C++
    
    This also ensures that we compile correctly for the empty project. There
    is some trickery that required some moving around of when we check for
    compilers vs debug compiler flags that was non-obvious due to the
    implementation of CXX flags checking in m4.

 .../autotools_templates/__init__.py                |   20 ++-
 .../autotools_templates/resources/Makefile.am      |    2 +
 .../autotools_templates/resources/configure.ac     |   41 ++++++-
 .../autotools_templates/resources/data/Makefile.am |    2 +
 .../resources/m4/ax_compiler_flags_cxxflags.m4     |  129 ++++++++++++++++++++
 .../resources/src/Makefile.shared-library-c        |    7 +
 6 files changed, 191 insertions(+), 10 deletions(-)
---
diff --git a/plugins/autotools-templates/autotools_templates/__init__.py 
b/plugins/autotools-templates/autotools_templates/__init__.py
index 0c679d4..001cc9e 100644
--- a/plugins/autotools-templates/autotools_templates/__init__.py
+++ b/plugins/autotools-templates/autotools_templates/__init__.py
@@ -82,7 +82,7 @@ class AutotoolsTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
         if 'language' in params:
             self.language = params['language'].get_string().lower()
 
-        if self.language not in ('c', 'vala'):
+        if self.language not in ('c', 'c++', 'vala'):
             task.return_error(GLib.Error("Language %s not supported" % self.language))
             return
 
@@ -90,6 +90,8 @@ class AutotoolsTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
 
         scope = Template.Scope.new()
 
+        scope.get('template').assign_string(self.id)
+
         prefix = name if not name.endswith('-glib') else name[:-5]
         PREFIX = prefix.upper().replace('-','_')
         prefix_ = prefix.lower().replace('-','_')
@@ -107,6 +109,8 @@ class AutotoolsTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
         scope.get('major_version').assign_string('0')
         scope.get('minor_version').assign_string('1')
         scope.get('micro_version').assign_string('0')
+        scope.get('enable_c').assign_boolean(True)
+        scope.get('enable_cplusplus').assign_boolean(self.language == 'c++')
         scope.get('enable_i18n').assign_boolean(True)
         scope.get('enable_gtk_doc').assign_boolean(False)
         scope.get('enable_gobject_introspection').assign_boolean(True)
@@ -139,6 +143,7 @@ class AutotoolsTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
             'resources/m4/ax_check_compile_flag.m4':    'm4/ax_check_compile_flag.m4',
             'resources/m4/ax_check_link_flag.m4':       'm4/ax_check_link_flag.m4',
             'resources/m4/ax_compiler_vendor.m4':       'm4/ax_compiler_vendor.m4',
+            'resources/m4/ax_compiler_flags_cxxflags.m4':'m4/ax_compiler_flags_cxxflags.m4',
             'resources/m4/ax_cxx_compile_stdcxx_11.m4': 'm4/ax_cxx_compile_stdcxx_11.m4',
             'resources/m4/ax_require_defined.m4':       'm4/ax_require_defined.m4',
             'resources/m4/glib-gettext.m4':             'm4/glib-gettext.m4',
@@ -150,7 +155,6 @@ class AutotoolsTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
             'resources/m4/vala.m4':                     'm4/vala.m4',
             'resources/m4/vapigen.m4':                  'm4/vapigen.m4',
 
-            'resources/data/package.pc.in':             'data/%(name)s.pc.in',
             'resources/data/Makefile.am':               'data/Makefile.am',
             'resources/po/Makevars':                    'po/Makevars',
             'resources/po/POTFILES.in':                 'po/POTFILES.in',
@@ -176,7 +180,7 @@ class AutotoolsTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
             task.return_boolean(True)
         except Exception as exc:
             print(exc)
-            task.return_error(GLib.Error(exc))
+            task.return_error(GLib.Error(repr(exc)))
 
     def prepare_scope(self, scope):
         pass
@@ -194,16 +198,18 @@ class LibraryProjectTemplate(AutotoolsTemplate):
             _("Shared Library"),
             'application-x-executable-symbolic',
             _("Create a new autotools project with a shared library"),
-            ['C', 'Vala']
+            ['C', 'C++', 'Vala']
          )
 
     def prepare_files(self, files):
-        if self.language == 'c':
+        files['resources/data/package.pc.in'] = 'data/%(name)s.pc.in'
+
+        if self.language in ('c', 'c++'):
             files['resources/src/Makefile.shared-library-c'] = 'src/Makefile.am'
             files['resources/src/package.h'] = 'src/%(name)s.h'
             files['resources/src/package-version.h.in'] = 'src/%(prefix)s-version.h.in'
 
-        if self.language == 'vala':
+        elif self.language == 'vala':
             files['resources/src/Makefile.shared-library-vala'] = 'src/Makefile.am'
             files['resources/src/package.vala'] = 'src/%(prefix)s.vala'
 
@@ -215,6 +221,6 @@ class EmptyProjectTemplate(AutotoolsTemplate):
             _("Empty Project"),
             'application-x-executable-symbolic',
             _("Create a new empty autotools project"),
-            ['C', 'Vala']
+            ['C', 'C++', 'Vala']
          )
 
diff --git a/plugins/autotools-templates/autotools_templates/resources/Makefile.am 
b/plugins/autotools-templates/autotools_templates/resources/Makefile.am
index 05caf8c..5bffab3 100644
--- a/plugins/autotools-templates/autotools_templates/resources/Makefile.am
+++ b/plugins/autotools-templates/autotools_templates/resources/Makefile.am
@@ -3,7 +3,9 @@ SUBDIRS = \
 {{if enable_i18n}}
        po \
 {{end}}
+{{if template != "empty"}}
        src \
+{{end}}
        $(NULL)
 
 EXTRA_DIST = AUTHORS
diff --git a/plugins/autotools-templates/autotools_templates/resources/configure.ac 
b/plugins/autotools-templates/autotools_templates/resources/configure.ac
index bfdeb36..6d019b1 100644
--- a/plugins/autotools-templates/autotools_templates/resources/configure.ac
+++ b/plugins/autotools-templates/autotools_templates/resources/configure.ac
@@ -18,7 +18,11 @@ dnl Initialize autoconf
 dnl ***********************************************************************
 AC_INIT([{{name}}],[package_version],[bug_report_url])
 AC_CONFIG_HEADERS([config.h])
+{{if template != "empty"}}
 AC_CONFIG_SRCDIR([data/{{name}}.pc.in])
+{{else}}
+AC_CONFIG_SRCDIR([NEWS])
+{{end}}
 AC_CONFIG_MACRO_DIR([m4])
 AC_CONFIG_AUX_DIR([build-aux])
 AC_SUBST([ACLOCAL_AMFLAGS], "-I m4")
@@ -42,12 +46,13 @@ AM_INIT_AUTOMAKE([1.11 foreign subdir-objects tar-ustar no-dist-gzip dist-xz -Wn
 AM_MAINTAINER_MODE([enable])
 AX_GENERATE_CHANGELOG
 
+
 dnl ***********************************************************************
 dnl Add extra debugging with --enable-debug and --enable-compile-warnings
 dnl ***********************************************************************
 AX_CHECK_ENABLE_DEBUG([no],[]
                       [G_DISABLE_ASSERT G_DISABLE_CHECKS G_DISABLE_CAST_CHECKS])
-AX_COMPILER_FLAGS
+
 
 {{if enable_i18n}}
 dnl ***********************************************************************
@@ -59,13 +64,16 @@ AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["$GETTEXT_PACKAGE"], [GETTEXT package nam
 
 AM_GNU_GETTEXT_VERSION([0.19.6])
 AM_GNU_GETTEXT([external])
-{{end}}
 
 
+{{end}}
 dnl ***********************************************************************
 dnl Check for required programs
 dnl ***********************************************************************
 AC_PROG_CC
+{{if enable_cplusplus}}
+AC_PROG_CXX
+{{end}}
 AC_PROG_INSTALL
 AC_PATH_PROG([GLIB_GENMARSHAL],[glib-genmarshal])
 AC_PATH_PROG([GLIB_MKENUMS],[glib-mkenums])
@@ -83,6 +91,27 @@ VAPIGEN_CHECK
 {{end}}
 
 
+AX_COMPILER_FLAGS
+
+
+{{if enable_c}}
+dnl ***********************************************************************
+dnl Ensure C11 is Supported
+dnl ***********************************************************************
+AX_CHECK_COMPILE_FLAG([-std=gnu11],
+                      [CFLAGS="$CFLAGS -std=gnu11"],
+                      [AC_MSG_ERROR([C compiler cannot compile GNU C11 code])])
+
+
+{{end}}
+{{if enable_cplusplus}}
+dnl ***********************************************************************
+dnl Ensure C++11 is Supported
+dnl ***********************************************************************
+AX_CXX_COMPILE_STDCXX_11([noext], [mandatory])
+
+
+{{end}}
 dnl ***********************************************************************
 dnl Check for required packages
 dnl ***********************************************************************
@@ -102,13 +131,19 @@ dnl ***********************************************************************
 AC_CONFIG_FILES([
        Makefile
 
+{{if template != "empty"}}
        src/Makefile
-{{if language == "c"}}
+{{end}}
+{{if template == "shared-library"}}
+{{if language != "vala"}}
        src/{{prefix}}-version.h
 {{end}}
+{{end}}
 
        data/Makefile
+{{if template == "shared-library"}}
        data/{{name}}-$API_VERSION.pc:data/{{name}}.pc.in
+{{end}}
 
 {{if enable_i18n}}
        po/Makefile.in
diff --git a/plugins/autotools-templates/autotools_templates/resources/data/Makefile.am 
b/plugins/autotools-templates/autotools_templates/resources/data/Makefile.am
index 810691d..c9695f0 100644
--- a/plugins/autotools-templates/autotools_templates/resources/data/Makefile.am
+++ b/plugins/autotools-templates/autotools_templates/resources/data/Makefile.am
@@ -1,3 +1,4 @@
+{{if template == "shared-library"}}
 # pkg-config data
 # Note that the template file is called {{name}}.pc.in, but generates a
 # versioned .pc file using some magic in AC_CONFIG_FILES.
@@ -6,5 +7,6 @@ pkgconfig_DATA = {{name}}-$(API_VERSION).pc
 
 DISTCLEANFILES = $(pkgconfig_DATA)
 EXTRA_DIST = {{name}}.pc.in
+{{end}}
 
 -include $(top_srcdir)/git.mk
diff --git a/plugins/autotools-templates/autotools_templates/resources/m4/ax_compiler_flags_cxxflags.m4 
b/plugins/autotools-templates/autotools_templates/resources/m4/ax_compiler_flags_cxxflags.m4
new file mode 100644
index 0000000..2d875e8
--- /dev/null
+++ b/plugins/autotools-templates/autotools_templates/resources/m4/ax_compiler_flags_cxxflags.m4
@@ -0,0 +1,129 @@
+# ==============================================================================
+#  http://www.gnu.org/software/autoconf-archive/ax_compiler_flags_cxxflags.html
+# ==============================================================================
+#
+# SYNOPSIS
+#
+#   AX_COMPILER_FLAGS_CXXFLAGS([VARIABLE], [IS-RELEASE], [EXTRA-BASE-FLAGS], [EXTRA-YES-FLAGS])
+#
+# DESCRIPTION
+#
+#   Add warning flags for the C++ compiler to VARIABLE, which defaults to
+#   WARN_CXXFLAGS.  VARIABLE is AC_SUBST-ed by this macro, but must be
+#   manually added to the CXXFLAGS variable for each target in the code
+#   base.
+#
+#   This macro depends on the environment set up by AX_COMPILER_FLAGS.
+#   Specifically, it uses the value of $ax_enable_compile_warnings to decide
+#   which flags to enable.
+#
+# LICENSE
+#
+#   Copyright (c) 2015 David King <amigadave amigadave com>
+#
+#   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.
+
+#serial 7
+
+AC_DEFUN([AX_COMPILER_FLAGS_CXXFLAGS],[
+    AC_REQUIRE([AC_PROG_SED])
+    AX_REQUIRE_DEFINED([AX_APPEND_COMPILE_FLAGS])
+    AX_REQUIRE_DEFINED([AX_APPEND_FLAG])
+    AX_REQUIRE_DEFINED([AX_CHECK_COMPILE_FLAG])
+
+    # Variable names
+    m4_define(ax_warn_cxxflags_variable,
+              [m4_normalize(ifelse([$1],,[WARN_CXXFLAGS],[$1]))])
+
+    AC_LANG_PUSH([C++])
+
+    # Always pass -Werror=unknown-warning-option to get Clang to fail on bad
+    # flags, otherwise they are always appended to the warn_cxxflags variable,
+    # and Clang warns on them for every compilation unit.
+    # If this is passed to GCC, it will explode, so the flag must be enabled
+    # conditionally.
+    AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],[
+        ax_compiler_flags_test="-Werror=unknown-warning-option"
+    ],[
+        ax_compiler_flags_test=""
+    ])
+
+    # Base flags
+    AX_APPEND_COMPILE_FLAGS([ dnl
+        -fno-strict-aliasing dnl
+        $3 dnl
+    ],ax_warn_cxxflags_variable,[$ax_compiler_flags_test])
+
+    AS_IF([test "$ax_enable_compile_warnings" != "no"],[
+        # "yes" flags
+        AX_APPEND_COMPILE_FLAGS([ dnl
+            -Wall dnl
+            -Wextra dnl
+            -Wundef dnl
+            -Wwrite-strings dnl
+            -Wpointer-arith dnl
+            -Wmissing-declarations dnl
+            -Wredundant-decls dnl
+            -Wno-unused-parameter dnl
+            -Wno-missing-field-initializers dnl
+            -Wformat=2 dnl
+            -Wcast-align dnl
+            -Wformat-nonliteral dnl
+            -Wformat-security dnl
+            -Wsign-compare dnl
+            -Wstrict-aliasing dnl
+            -Wshadow dnl
+            -Winline dnl
+            -Wpacked dnl
+            -Wmissing-format-attribute dnl
+            -Wmissing-noreturn dnl
+            -Winit-self dnl
+            -Wredundant-decls dnl
+            -Wmissing-include-dirs dnl
+            -Wunused-but-set-variable dnl
+            -Warray-bounds dnl
+            -Wreturn-type dnl
+            -Wno-overloaded-virtual dnl
+            -Wswitch-enum dnl
+            -Wswitch-default dnl
+            $4 dnl
+            $5 dnl
+            $6 dnl
+            $7 dnl
+        ],ax_warn_cxxflags_variable,[$ax_compiler_flags_test])
+    ])
+    AS_IF([test "$ax_enable_compile_warnings" = "error"],[
+        # "error" flags; -Werror has to be appended unconditionally because
+        # it's not possible to test for
+        #
+        # suggest-attribute=format is disabled because it gives too many false
+        # positives
+        AX_APPEND_FLAG([-Werror],ax_warn_cxxflags_variable)
+
+        AX_APPEND_COMPILE_FLAGS([ dnl
+            -Wno-suggest-attribute=format dnl
+        ],ax_warn_cxxflags_variable,[$ax_compiler_flags_test])
+    ])
+
+    # In the flags below, when disabling specific flags, always add *both*
+    # -Wno-foo and -Wno-error=foo. This fixes the situation where (for example)
+    # we enable -Werror, disable a flag, and a build bot passes CXXFLAGS=-Wall,
+    # which effectively turns that flag back on again as an error.
+    for flag in $ax_warn_cxxflags_variable; do
+        AS_CASE([$flag],
+                [-Wno-*=*],[],
+                [-Wno-*],[
+                    AX_APPEND_COMPILE_FLAGS([-Wno-error=$(AS_ECHO([$flag]) | $SED 's/^-Wno-//')],
+                                            ax_warn_cxxflags_variable,
+                                            [$ax_compiler_flags_test])
+                ])
+    done
+
+    AC_LANG_POP([C++])
+
+    # Substitute the variables
+    AC_SUBST(ax_warn_cxxflags_variable)
+])dnl AX_COMPILER_FLAGS_CXXFLAGS
diff --git a/plugins/autotools-templates/autotools_templates/resources/src/Makefile.shared-library-c 
b/plugins/autotools-templates/autotools_templates/resources/src/Makefile.shared-library-c
index f30854e..7c0d77d 100644
--- a/plugins/autotools-templates/autotools_templates/resources/src/Makefile.shared-library-c
+++ b/plugins/autotools-templates/autotools_templates/resources/src/Makefile.shared-library-c
@@ -5,10 +5,17 @@ lib{{name_}}_ API_VERSION@_la_SOURCES = \
        {{name}}.h \
        $(NULL)
 
+{{if enable_cplusplus}}
+lib{{name_}}_ API_VERSION@_la_CXXFLAGS = \
+       $({{NAME}}_CFLAGS) \
+       $(WARN_CFLAGS) \
+       $(NULL)
+{{else}}
 lib{{name_}}_ API_VERSION@_la_CFLAGS = \
        $({{NAME}}_CFLAGS) \
        $(WARN_CFLAGS) \
        $(NULL)
+{{end}}
 
 lib{{name_}}_ API_VERSION@_la_LIBADD = \
        $({{NAME}}_LIBS) \


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]