1dnl vim: set et ts=4 sw=4 sts=4:
2dnl
3dnl \brief  Compiler check macros for VICE
4dnl
5dnl A few macros to simplify checking compiler and linker features.
6dnl Incomplete and unused at the moment for 'code review'. The idea is to add
7dnl VICE_CXXFLAGS_[REQUEST|REQUIRE]() as well, and perhaps linker checks,
8dnl should we require those.
9dnl
10dnl \author Bas Wassink <b.wassink@ziggo.nl>
11
12
13dnl VICE_CFLAG_REQUEST(flag, [flags_var_name=VICE_CFLAGS])
14dnl
15dnl Check if the C compiler supports `flag` and add it when suppported
16dnl
17dnl \param[in]  flag            compiler flag to check
18dnl \param[in]  flags_var_name  name of flags variable to update (optional,
19dnl                             defaults to VICE_CFLAGS)
20dnl
21dnl \todo   Use 'local' variables (pushdef/popdef) to avoid name clashes in
22dnl         other m4 or configure.ac code
23dnl
24AC_DEFUN([VICE_CFLAG_REQUEST],
25[
26    dnl Handle optional 'flags_var_name' argument
27    flags_var_name=m4_default($2, [VICE_CFLAGS])
28    AS_VAR_COPY([old_var_CFLAGS], [$flags_var_name])
29
30    AC_LANG_PUSH([C])
31    AC_MSG_CHECKING([if the C compiler supports $1])
32
33    dnl We need -Werror to make AC_TRY_COMPILE error out with Clang, and we
34    dnl need it to be before any -Werror=foo flags to make gcc 9.2+ error out,
35    dnl otherwise gcc 9.2 will complain but still return 0.
36    dnl We need -Wno-strict-prototypes to avoid barfing on 'main()'
37    old_CFLAGS="$CFLAGS"
38    CFLAGS="-Werror $old_var_CFLAGS $1 -Wno-strict-prototypes"
39
40    dnl Try compiling a minimal piece of code to test the requested flag
41    AC_TRY_COMPILE(
42        [],
43        [int the_answer = 42; return the_answer;],
44        [AC_MSG_RESULT([yes])
45         AS_VAR_SET([$flags_var_name], ["$old_var_CFLAGS $1"])],
46        [AC_MSG_RESULT([no])]
47    )
48    CFLAGS="$old_CFLAGS"
49    AC_LANG_POP([C])
50])
51
52
53dnl VICE_CFLAG_REQUIRE(flag)
54dnl
55dnl Check if the compiler supports `flag` and error out when not suppported
56dnl
57AC_DEFUN([VICE_CFLAG_REQUIRE],
58[
59    AC_LANG_PUSH([C])
60    AC_MSG_CHECKING([if the C compiler supports $1])
61    old_CFLAGS="$CFLAGS"
62    dnl We need -Werror to make AC_TRY_COMPILE error out with Clang
63    CFLAGS="-Werror $VICE_CFLAGS $1 -Wno-strict-prototypes"
64    AC_TRY_COMPILE(
65        [],
66        [int poop = 42; return poop;],
67        [AC_MSG_RESULT([yes])
68         VICE_CFLAGS="$VICE_CFLAGS $1"],
69        [AC_MSG_ERROR([no, $1 is required])]
70    )
71    CFLAGS="$old_CFLAGS"
72    AC_LANG_POP([C])
73])
74
75
76dnl VICE_CXXFLAG_REQUEST(flag, [flags_var_name=VICE_CXXFLAGS])
77dnl
78dnl Check if the C++ compiler supports `flag` and add it when suppported
79dnl
80dnl \param[in]  flag            compiler flag to check
81dnl \param[in]  flags_var_name  name of flags variable to update (optional,
82dnl                             defaults to VICE_CXXFLAGS)
83dnl
84dnl \todo   Use 'local' variables (pushdef/popdef) to avoid name clashes in
85dnl         other m4 or configure.ac code
86dnl
87AC_DEFUN([VICE_CXXFLAG_REQUEST],
88[
89    dnl Handle optional 'flags_var_name' argument
90    flags_var_name=m4_default($2, [VICE_CXXFLAGS])
91    AS_VAR_COPY([old_var_CXXFLAGS], [$flags_var_name])
92
93    AC_LANG_PUSH([C++])
94    AC_MSG_CHECKING([if the C++ compiler supports $1])
95
96    dnl We need -Werror to make AC_TRY_COMPILE error out with Clang, and we
97    dnl need it to be before any -Werror=foo flags to make gcc 9.2+ error out,
98    dnl otherwise gcc 9.2 will complain but still return 0.
99    dnl We need -Wno-strict-prototypes to avoid barfing on 'main()'
100    old_CXXFLAGS="$CXXFLAGS"
101    CXXFLAGS="-Werror $old_var_CXXFLAGS $1"
102
103    dnl Try compiling a minimal piece of code to test the requested flag
104    AC_TRY_COMPILE(
105        [],
106        [int the_answer = 42; return the_answer;],
107        [AC_MSG_RESULT([yes])
108         AS_VAR_SET([$flags_var_name], ["$old_var_CXXFLAGS $1"])],
109        [AC_MSG_RESULT([no])]
110    )
111    CXXFLAGS="$old_CXXFLAGS"
112    AC_LANG_POP([C++])
113])
114
115
116