From 6f65d24796a0aaca0c20f4c074aa71e39f38e68d Mon Sep 17 00:00:00 2001 From: Hans Ulrich Niedermann Date: Tue, 18 Feb 2020 21:18:55 +0100 Subject: Make sure both C and C++ programs including gphoto2 headers compile Check that programs including libgphoto2 headers compile when compiled for a number of different language standards: C: ansi c99 c11 C++: ansi c++98 c++11 c++14 c++17 For each of these language standards, if the compiler does not compile an empty example program, we do not test whether compiling with the gphoto2/*.h headers included works. This will work with GCC as the compiler, and also should work with CLANG as the compiler as CLANG is mostly compatible in these matters. On other compilers, the test compile of the empty program should fail, and thus no checks with the gphoto2/*.h headers included will be performed. C90/ANSI C apparently does not define __STDC_VERSION__, so the C source code needed to be changed to become ANSI C compatible. In C++ pedantic compilation source, we now use actual C++ code instead of relying on C code working when compiled as C++ code. In Travis CI, we now actually require both C and C++. --- configure.ac | 31 ++++++-- gphoto-m4/gp-pedantic-compiler-flags.m4 | 123 ++++++++++++++++++++++++++++++++ tests/Makefile.am | 95 ++++++++++++++++++++++++ tests/test-pedantic-compilation.c | 6 ++ tests/test-pedantic-compilation.cxx | 4 +- 5 files changed, 253 insertions(+), 6 deletions(-) create mode 100644 gphoto-m4/gp-pedantic-compiler-flags.m4 diff --git a/configure.ac b/configure.ac index e2839ca86..1e4c3ef47 100644 --- a/configure.ac +++ b/configure.ac @@ -83,6 +83,33 @@ dnl SED is supposed to be set in AC_LIBTOOL_SETUP, but the dnl sequence seems to get mixed up. SED=${SED-sed} + +dnl If CXX is neither unset nor empty nor "no", presume we can compile +dnl C++ sources. A C++ compiler is not required for libgphoto2 itself, +dnl but is useful to make sure that the libgphoto2 header files +dnl compile when included into C++ source files. +AM_CONDITIONAL([HAVE_CXX], [test "x$CXX" != "x" && test "x$CXX" != "xno"]) + + +dnl -------------------------------------------------------------------- +dnl Determine the most pedantic compiler flags we can can find for both +dnl C and C++, so that we can test that the libgphoto2 headers compile +dnl with those flags. +dnl -------------------------------------------------------------------- +GP_PEDANTIC_COMPILER_FLAGS([C90], [C], [-std=c90]) +GP_PEDANTIC_COMPILER_FLAGS([C99], [C], [-std=c99]) +GP_PEDANTIC_COMPILER_FLAGS([C11], [C], [-std=c11]) +dnl +GP_PEDANTIC_COMPILER_FLAGS([CXX98], [C++], [-std=c++98]) +GP_PEDANTIC_COMPILER_FLAGS([CXX11], [C++], [-std=c++11]) +GP_PEDANTIC_COMPILER_FLAGS([CXX14], [C++], [-std=c++14]) +GP_PEDANTIC_COMPILER_FLAGS([CXX17], [C++], [-std=c++17]) + + +dnl Every compile example after here will be using the C language +AC_LANG([C]) + + dnl --------------------------------------------------------------------------- dnl Turn on (almost) all warnings when using gcc dnl --------------------------------------------------------------------------- @@ -631,10 +658,6 @@ CPPFLAGS="$CPPFLAGS_save" ],[],[default-on],[http://www.sourceforge.net/projects/libexif])dnl -dnl FIXME: Is this accurate? -AM_CONDITIONAL([HAVE_CXX], [test -n "$CXX" && test "X$CXX" != "Xno"]) - - dnl --------------------------------------------------------------------------- dnl Configure subprojects dnl --------------------------------------------------------------------------- diff --git a/gphoto-m4/gp-pedantic-compiler-flags.m4 b/gphoto-m4/gp-pedantic-compiler-flags.m4 new file mode 100644 index 000000000..bb5adfc71 --- /dev/null +++ b/gphoto-m4/gp-pedantic-compiler-flags.m4 @@ -0,0 +1,123 @@ +dnl #################################################################### +dnl GP_PEDANTIC_COMPILER_FLAGS & Co. +dnl #################################################################### +dnl +m4_pattern_forbid([_GP_])dnl +dnl +dnl +dnl #################################################################### +dnl _GP_CONDITIONAL_COMPILE_FLAGS(FLAG_VAR, FLAGS) +dnl #################################################################### +AC_DEFUN([_GP_CONDITIONAL_COMPILE_FLAGS], [dnl +# BEGIN $0($@) +AS_VAR_IF([$1], [], [dnl + $1="$2" +], [dnl + $1="[$]{$1} $2" +]) +AC_MSG_CHECKING([whether $1="[$]$1" compiles]) +AC_COMPILE_IFELSE([m4_case([$1], [CFLAGS], [dnl +AC_LANG_SOURCE([[ +#include +int main(int argc, char *argv[]) +{ + int i; + /* Use argc and argv to prevent warning about unused function params */ + for (i=0; i +int main(int argc, char *argv[]) +{ + int i; + /* Use argc and argv to prevent warning about unused function params */ + for (i=0; i +#ifdef __STDC_VERSION__ unsigned long stdc_version = __STDC_VERSION__; +#endif int main() { +#ifdef __STDC_VERSION__ printf("stdc_version = %lu\n", stdc_version); +#else + printf("stdc_version = undefined\n"); +#endif return 0; } diff --git a/tests/test-pedantic-compilation.cxx b/tests/test-pedantic-compilation.cxx index 70bac2f2b..5dbe12083 100644 --- a/tests/test-pedantic-compilation.cxx +++ b/tests/test-pedantic-compilation.cxx @@ -15,12 +15,12 @@ #include #include -#include +#include unsigned long cxx_version = __cplusplus; int main() { - printf("cxx_version = %lu\n", cxx_version); + std::cout << "cxx_version = " << cxx_version << std::endl; return 0; } -- cgit v1.2.1