diff options
author | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2020-02-18 21:18:55 +0100 |
---|---|---|
committer | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2020-02-19 05:10:00 +0100 |
commit | 6f65d24796a0aaca0c20f4c074aa71e39f38e68d (patch) | |
tree | 8b083dacc9d8e3870c0750d17b7e53ceec0a79a4 /gphoto-m4 | |
parent | b56a50827ffdc43dd3b6a32898f8f184eeea7234 (diff) | |
download | libgphoto2-6f65d24796a0aaca0c20f4c074aa71e39f38e68d.tar.gz |
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++.
Diffstat (limited to 'gphoto-m4')
-rw-r--r-- | gphoto-m4/gp-pedantic-compiler-flags.m4 | 123 |
1 files changed, 123 insertions, 0 deletions
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 <stdio.h> +int main(int argc, char *argv[]) +{ + int i; + /* Use argc and argv to prevent warning about unused function params */ + for (i=0; i<argc; ++i) { + printf(" %d %s\n", i, argv[i]); + } + return 0; +} +]])dnl +], [CXXFLAGS], [dnl +AC_LANG_SOURCE([[ +#include <iostream> +int main(int argc, char *argv[]) +{ + int i; + /* Use argc and argv to prevent warning about unused function params */ + for (i=0; i<argc; ++i) { + std::cout << " " << i << " " << argv[i] << std::endl; + } + return 0; +} +]])dnl +], [m4_fatal([wrong compiler flag])])], [dnl + : "Added flag $2 to $1 and it works" + AC_MSG_RESULT([yes]) +], [dnl + : "Added flag $2 to $1 and it does not work" + AC_MSG_RESULT([no]) + gp_have_pedantic_compiler=no +]) +# END $0($@) +])dnl +dnl +dnl +dnl #################################################################### +dnl Params: +dnl $1 Depending on the language, CFLAGS or CXXFLAGS +dnl $2 The flag variable the caller wants us to set +dnl $3 The AM_CONDITIONAL the caller wants us to set +dnl $4 The -std= argument (such as -std=c++98 or -std=c99) +dnl #################################################################### +AC_DEFUN([__GP_PEDANTIC_COMPILER_FLAGS], [dnl +# BEGIN $0($@) +gp_have_pedantic_compiler=yes +_GP_CONDITIONAL_COMPILE_FLAGS([$1], [$4])dnl +AS_VAR_IF([gp_have_pedantic_compiler], [yes], [dnl + _GP_CONDITIONAL_COMPILE_FLAGS([$1], [-pedantic -Wall -Wextra -Werror])dnl +]) +AM_CONDITIONAL([$3], [test "x$gp_have_pedantic_compiler" = "xyes"]) +AC_SUBST([$2], ["[$]$1"]) +AC_MSG_CHECKING([whether to test pedantic ][$4]) +AM_COND_IF([$3], [dnl + AC_MSG_RESULT([yes]) +], [dnl + AC_MSG_RESULT([no]) +]) +# END $0($@) +])dnl +dnl +dnl +dnl #################################################################### +dnl Params: +dnl $1 Depending on the language, CFLAGS or CXXFLAGS +dnl $2 The variable/conditional component the caller wants us to use +dnl $3 The -std= argument (such as -std=c++98 or -std=c99) +dnl #################################################################### +m4_pattern_allow([GP_PEDANTIC_CFLAGS_])dnl +m4_pattern_allow([GP_PEDANTIC_CXXFLAGS_])dnl +m4_pattern_allow([GP_HAVE_PEDANTIC_FLAGS_])dnl +AC_DEFUN([_GP_PEDANTIC_COMPILER_FLAGS], [dnl +# BEGIN $0($@) +gp_compiler_flags_saved_$1="[$]$1" +$1="" +__GP_PEDANTIC_COMPILER_FLAGS([$1], + [GP_PEDANTIC_][$1][_][$2], + [GP_HAVE_PEDANTIC_FLAGS_][$2], + [$3]) +GP_PEDANTIC_$1_$2="[$]$1" +$1="[$]gp_compiler_flags_saved_$1" +# END $0($@) +])dnl +dnl +dnl +dnl #################################################################### +dnl GP_PEDANTIC_COMPILER_FLAGS(VAR_COMPONENT, LANG, std-arg) +dnl Start a new series of compiler flags +dnl #################################################################### +AC_DEFUN([GP_PEDANTIC_COMPILER_FLAGS], [dnl +# BEGIN $0($@) +AC_LANG_PUSH([$2]) +_GP_PEDANTIC_COMPILER_FLAGS(m4_case($2,[C],[CFLAGS],[C++],[CXXFLAGS],[m4_fatal([Unknown language given: ][$2])]), [$1], [$3]) +AC_LANG_POP([$2]) +# END $0($@) +])dnl +dnl +dnl +dnl #################################################################### +dnl +dnl Local Variables: +dnl mode: autoconf +dnl End: |