summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--meson-cc-tests/check-unused-result.c9
-rw-r--r--meson.build55
2 files changed, 62 insertions, 2 deletions
diff --git a/meson-cc-tests/check-unused-result.c b/meson-cc-tests/check-unused-result.c
new file mode 100644
index 000000000..ce7279ec8
--- /dev/null
+++ b/meson-cc-tests/check-unused-result.c
@@ -0,0 +1,9 @@
+__attribute__((__warn_unused_result__)) void f (void) {}
+__attribute__((__warn_unused_result__)) int g;
+
+int main(int c, char **v)
+{
+ (void)c;
+ (void)v;
+ return 0;
+}
diff --git a/meson.build b/meson.build
index ca9d72825..ce7449ffb 100644
--- a/meson.build
+++ b/meson.build
@@ -33,8 +33,61 @@ else
cairo_libversion = '@0@.@1@.@2@'.format(cairo_version_sonum, cairo_version, cairo_version_micro)
endif
+conf = configuration_data()
+
cc = meson.get_compiler('c')
+# Compiler flags
+cflags = []
+if cc.get_id() != 'msvc'
+ cflags += [
+ '-Wall',
+ '-Wextra',
+ '-Wmissing-declarations',
+ '-Werror-implicit-function-declaration',
+ '-Wpointer-arith',
+ '-Wwrite-strings',
+ '-Wsign-compare',
+ '-Wpacked',
+ '-Wswitch-enum',
+ '-Wmissing-format-attribute',
+ '-Wvolatile-register-var',
+ '-Wstrict-aliasing=2',
+ '-Winit-self',
+ '-Wunsafe-loop-optimizations',
+ '-Wno-missing-field-initializers',
+ '-Wno-unused-parameter',
+ '-Wno-attributes',
+ '-Wno-long-long',
+ '-Winline'
+ ]
+
+ cflags += ['-Wno-unused-but-set-variable',
+ '-Wno-enum-conversion'
+ ]
+
+ cflags += [
+ '-fno-strict-aliasing',
+ '-fno-common'
+ ]
+
+ cflags += '-Wp,-D_FORTIFY_SOURCE=2'
+
+ supported_cflags = cc.get_supported_arguments(cflags)
+ add_project_arguments(supported_cflags, language: 'c')
+
+ # We only wish to enable attribute(warn_unused_result) if we can prevent
+ # gcc from generating thousands of warnings about the misapplication of the
+ # attribute to void functions and variables.
+ warn_unused_result = ''
+ if supported_cflags.contains('-Wno-attributes')
+ if cc.compiles(files('meson-cc-tests/check-unused-result.c'), args : ['-Wno-attributes', '-Werror'])
+ warn_unused_result = '__attribute__((__warn_unused_result__))'
+ endif
+ endif
+ conf.set('WARN_UNUSED_RESULT', warn_unused_result)
+endif
+
if cc.get_id() == 'msvc'
# Basic usage in the cairo type system that causes spammy and useless warnings
add_project_arguments('/wd4244', '/wd4146',
@@ -137,8 +190,6 @@ test_deps = []
internal_deps = []
extra_link_args = []
-conf = configuration_data()
-
if host_machine.endian() == 'big'
conf.set('WORDS_BIGENDIAN', 1)
endif