compiler.m4 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright © 2004 Scott James Remnant <scott@netsplit.com>.
  2. # DPKG_COMPILER_WARNINGS
  3. # ---------------------
  4. # Add configure option to enable additional compiler warnings and treat
  5. # them as errors.
  6. AC_DEFUN([DPKG_COMPILER_WARNINGS],
  7. [AC_ARG_ENABLE(compiler-warnings,
  8. AS_HELP_STRING([--enable-compiler-warnings],
  9. [Enable additional compiler warnings]),
  10. [WFLAGS="-Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers"
  11. if test "x$enable_compiler_warnings" = "xyes"; then
  12. if test "x$GCC" = "xyes"; then
  13. CFLAGS="$WFLAGS $CFLAGS"
  14. fi
  15. if test "x$GXX" = "xyes"; then
  16. CXXFLAGS="$WFLAGS $CXXFLAGS"
  17. fi
  18. fi])dnl
  19. ])
  20. # DPKG_COMPILER_OPTIMISATIONS
  21. # --------------------------
  22. # Add configure option to disable optimisations.
  23. AC_DEFUN([DPKG_COMPILER_OPTIMISATIONS],
  24. [AC_ARG_ENABLE(compiler-optimisations,
  25. AS_HELP_STRING([--disable-compiler-optimisations],
  26. [Disable compiler optimisations]),
  27. [if test "x$enable_compiler_optimisations" = "xno"; then
  28. [CFLAGS=$(echo "$CFLAGS" | sed -e "s/ -O[1-9]*\b/ -O0/g")]
  29. fi])dnl
  30. ])
  31. # DPKG_C_ATTRIBUTE
  32. # ----------------
  33. # Check whether the C compiler supports __attribute__, defines HAVE_C_ATTRIBUTE
  34. AC_DEFUN([DPKG_C_ATTRIBUTE],
  35. [AC_CACHE_CHECK([whether compiler supports __attribute__], [dpkg_cv_attribute],
  36. [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
  37. [[]],
  38. [[extern int testfunction(int x) __attribute__((unused))]]
  39. )],
  40. [dpkg_cv_attribute=yes],
  41. [dpkg_cv_attribute=no])])
  42. AS_IF([test "x$dpkg_cv_attribute" = "xyes"],
  43. [AC_DEFINE([HAVE_C_ATTRIBUTE], 1,
  44. [Define to 1 if compiler supports '__attribute__', 0 otherwise.])],
  45. [AC_DEFINE([HAVE_C_ATTRIBUTE], 0)])dnl
  46. ])# DPKG_C_ATTRIBUTE
  47. # DPKG_TRY_C99([ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
  48. # ------------------------------------------------------
  49. # Try compiling some C99 code to see whether it works
  50. AC_DEFUN([DPKG_TRY_C99],
  51. [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
  52. #include <stdio.h>
  53. #include <stdbool.h>
  54. #include <inttypes.h>
  55. /* Variadic macro arguments */
  56. #define variadic_macro(foo, ...) printf(foo, __VA_ARGS__)
  57. ]],
  58. [[
  59. /* Compound initialisers */
  60. struct { int a, b; } foo = { .a = 1, .b = 2 };
  61. /* Boolean type */
  62. bool bar = false;
  63. /* Specific size type */
  64. uint32_t baz = 0;
  65. /* C99-style for-loop declarations */
  66. for (int i = 0; i < 10; i++)
  67. continue;
  68. /* Magic __func__ variable */
  69. printf("%s", __func__);
  70. ]])], [$1], [$2])dnl
  71. ])# DPKG_TRY_C99
  72. # DPKG_C_C99
  73. # ----------
  74. # Check whether the compiler can do C99
  75. AC_DEFUN([DPKG_C_C99],
  76. [AC_CACHE_CHECK([whether compiler supports C99 features], [dpkg_cv_c99],
  77. [DPKG_TRY_C99([dpkg_cv_c99=yes], [dpkg_cv_c99=no])])
  78. AS_IF([test "x$dpkg_cv_c99" = "xyes"],
  79. [AC_DEFINE([HAVE_C99], 1, [Define to 1 if the compiler supports C99.])],
  80. [AC_CACHE_CHECK([what argument makes compiler support C99 features],
  81. [dpkg_cv_c99_arg],
  82. [dpkg_cv_c99_arg=none
  83. dpkg_save_CC="$CC"
  84. for arg in "-std=gnu99" "-std=c99" "-c99"; do
  85. CC="$dpkg_save_CC $arg"
  86. DPKG_TRY_C99([dpkg_arg_worked=yes], [dpkg_arg_worked=no])
  87. CC="$dpkg_save_CC"
  88. AS_IF([test "x$dpkg_arg_worked" = "xyes"],
  89. [dpkg_cv_c99_arg="$arg"; break])
  90. done])
  91. AS_IF([test "x$dpkg_cv_c99_arg" != "xnone"],
  92. [CC="$CC $dpkg_cv_c99_arg"
  93. AC_DEFINE([HAVE_C99], 1)])])[]dnl
  94. ])# DPKG_C_C99