compiler.m4 3.1 KB

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