configure: allow adding compiler-specific warning flags
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 25 Oct 2019 13:38:13 +0000 (09:38 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 28 Oct 2019 20:25:02 +0000 (16:25 -0400)
commit02bb4fcc399072b657877925489f4b4e43754a57
tree1245ff9581995ae99015a46e9abda88ee7812647
parentc7fa66191176d600d53a69266b8567453c9f977d
configure: allow adding compiler-specific warning flags

Goal
----

The goal of this patch is to add a system where we can provide a bunch
of warning flags, and it will check if the current compiler supports
each of them individually.

This is inspired by how GDB's configure does:

  https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=gdb/warning.m4;h=c9e64a1836a8e01339ca4e3c3d689657ff9ef92c;hb=5c49f2cd78c69d50bc7c7119596a226f05939d06#l19

GDB's macro is homegrown, but unfortunately we can't just copy it
because of the license (it's GPLv3).  We could write our own macro that
does the same thing, it's not terribly complicated.  But rather than
writing our own macro...

Implementation
--------------

There is a macro in the Autoconf Archive, AX_COMPILER_FLAGS, which does
pretty much that.  You can pass it a list of flags and it will only keep
the ones supported by the compiler.

However, it also provides a bunch of arbitrary warning flags of its own.
Some of them we want, some of them we probably don't want, and some of
them we want but the codebase is not ready for them yet.

To keep this patch reasonable, I chose to pass all the -Wno-* flags
needed to disable the warnings we have currently.  Over time, we can
gradually fix the warnings and remove them from that list.

The documentation for AX_COMPILER_FLAGS specifically says:

    The set of base and enabled flags can be augmented using the
    EXTRA-*-CFLAGS and EXTRA-*-LDFLAGS variables, which are tested and
    appended to the output variable if –enable-compile-warnings is not
    "no".  Flags should not be disabled using these arguments, as the
    entire point of AX_COMPILER_FLAGS is to enforce a consistent set of
    useful compiler warnings on code, using warnings which have been
    chosen for low false positive rates. If a compiler emits false
    positives for a warning, a #pragma should be used in the code to
    disable the warning locally.

However, there's no way we're going to add so many pragmas, some of them
for warnings we might not even want.  So I took the liberty to use the
EXTRA-YES-CFLAGS to pass the arguments to disable those warnings.

Notes
-----

(1)

I have fixed the few occurences where we pass a const pointer as a
non-const pointer (e.g.: error: passing argument 2 of ‘g_spawn_sync’
from incompatible pointer type), because with gcc 4.8 there is no way of
silencing that warning.

(2)

As explained in the comment in configure.ac, I have moved the glib
sizeof(size_t) check before the call to AX_COMPILER_FLAGS.  This is so
the sizeof(size_t) check is not affected by the strict warning flags
enabled by AX_COMPILER_FLAGS.

In practice, the problem is that the program generated by
AC_LANG_PROGRAM defines main as "int main()" instead of
"int main(void)", causing a -Wold-style-declaration warning.

(3)

As explained in the comment in configure.ac, I have explicitly enabled
-Wold-style-declaration.  I have fixed the offender.

(4)

This macro adds the following arguments to configure:

  --enable-compile-warnings=[no/yes/error]
                          Enable compiler warnings and errors
  --disable-Werror        Unconditionally make all compiler warnings non-fatal

(5)

The AX_COMPILER_FLAGS macro also provides some useful LDFLAGS.  However,
it provides -Wl,--no-as-needed, and we specifically use -Wl,--as-needed
when building babeltrace2.exe.  I chose not to use the LDFLAGS from
AX_COMPILER_FLAGS for now, by fear that it will break things in a subtle
way.

Alternatives
------------

If this is deemed too invasive, alternatives would be to:

- write our own macro, like the GDB one
- copy the AX_COMPILER_FLAGS and modify it to only keep what we want

Change-Id: Ic3e292743a3eb96d786372cd72bedbfbc5986cd0
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2257
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Reviewed-by: Michael Jeanson <mjeanson@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
13 files changed:
configure.ac
m4/ax_append_compile_flags.m4 [new file with mode: 0644]
m4/ax_check_compile_flag.m4 [new file with mode: 0644]
m4/ax_compiler_flags.m4 [new file with mode: 0644]
m4/ax_compiler_flags_cflags.m4 [new file with mode: 0644]
m4/ax_compiler_flags_gir.m4 [new file with mode: 0644]
m4/ax_compiler_flags_ldflags.m4 [new file with mode: 0644]
src/cli/babeltrace2-cfg-cli-args.c
src/ctf-writer/stream.c
src/param-parse/param-parse.c
tests/ctf-writer/ctf_writer.c
tests/plugins/flt.lttng-utils.debug-info/test_bin_info.c
tests/utils/tap/tap.c
This page took 0.027538 seconds and 4 git commands to generate.