Repository: mesonbuild/meson

Issue:
"warning _FORTIFY_SOURCE requires compiling with optimization" when running g-ir-scanner
**Describe the bug**
When building projects which are using g-ir-scanner with _FORTIFY_SOURCE in CFLAGS, a warning is printed: `warning _FORTIFY_SOURCE requires compiling with optimization (-O)` in steps which use g-ir-scanner.

```
[107/134] ///usr/bin/g-ir-scanner --no-libtool --namespace=Adw --nsversion=1 --warn-all --output src/Adw-1.gir --c-include=adwaita.h --quiet -DADWAITA_COMPILATION -I/home/michal/temp/libadwaita/src -I/home/michal/temp/libadwaita/build/src -I/home/michal/temp/libadwaita/build/. -I/home/michal/temp/libadwaita/. -I/home/michal/temp/libadwaita/build/src -I/home/michal/temp/libadwaita/src --filelist=/home/michal/temp/libadwaita/build/src/libadwaita-1.so.0.p/Adw_1_gir_filelist -L/home/michal/temp/libadwaita/build/src -L/usr/lib64 --extra-library=adwaita-1 --include=Gio-2.0 --include=Gtk-4.0 --symbol-prefix=adw --identifier-prefix=Adw --pkg-export=libadwaita-1 --cflags-begin -DHAVE_CONFIG_H -DADWAITA_COMPILATION -I/home/michal/temp/libadwaita/build -Wcast-align -Wdate-time -Wdeclaration-after-statement -Werror=format-security -Werror=format=2 -Wendif-labels -Werror=incompatible-pointer-types -Werror=missing-declarations -Werror=overflow -Werror=return-type -Werror=shift-count-overflow -Werror=shift-overflow=2 -Werror=implicit-fallthrough=3 -Wformat-nonliteral -Wformat-security -Winit-self -Wmaybe-uninitialized -Wmissing-field-initializers -Wmissing-include-dirs -Wmissing-noreturn -Wnested-externs -Wno-missing-field-initializers -Wno-sign-compare -Wno-strict-aliasing -Wno-unused-parameter -Wold-style-definition -Wpointer-arith -Wredundant-decls -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wtype-limits -Wundef -Wunused-function -fstack-protector-strong -I/home/michal/temp/libadwaita/build/. -I/home/michal/temp/libadwaita/. -I/home/michal/temp/libadwaita/build/src -I/home/michal/temp/libadwaita/src -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/fribidi -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/gtk-4.0 -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/uuid -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/graphene-1.0 -I/usr/lib64/graphene-1.0/include -I/usr/include/gio-unix-2.0 -I/usr/include/gobject-introspection-1.0 -D_FORTIFY_SOURCE=2 --cflags-end --add-include-path=///usr/share/gir-1.0 -L/home/michal/temp/libadwaita/build/src --library adwaita-1 -L/usr/lib64 --extra-library=glib-2.0 --extra-library=gmodule-2.0 -L/usr/lib --extra-library=fribidi --extra-library=gio-2.0 --extra-library=gobject-2.0 --extra-library=gtk-4 --extra-library=pangocairo-1.0 --extra-library=pango-1.0 --extra-library=harfbuzz --extra-library=gdk_pixbuf-2.0 --extra-library=cairo-gobject --extra-library=cairo --extra-library=vulkan --extra-library=graphene-1.0 --extra-library=m --extra-library=rt --extra-library=girepository-1.0 --sources-top-dirs /home/michal/temp/libadwaita/subprojects/ --sources-top-dirs /home/michal/temp/libadwaita/build/subprojects/
In file included from /usr/include/bits/libc-header-start.h:33,
                 from /usr/include/limits.h:26,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/10.2/include-fixed/limits.h:195,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/10.2/include-fixed/syslimits.h:7,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/10.2/include-fixed/limits.h:34,
                 from /usr/lib/glib-2.0/include/glibconfig.h:11,
                 from /usr/include/glib-2.0/glib/gtypes.h:32,
                 from /usr/include/glib-2.0/glib/galloca.h:32,
                 from /usr/include/glib-2.0/glib.h:30,
                 from /usr/include/glib-2.0/gobject/gbinding.h:28,
                 from /usr/include/glib-2.0/glib-object.h:22,
                 from /home/michal/temp/libadwaita/build/src/adw-enums.h:12,
                 from /home/michal/temp/libadwaita/build/g-ir-cpp-kfqhu2nl.c:4:
/usr/include/features.h:397:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
  397 | #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
      |    ^~~~~~~
In file included from /usr/include/bits/libc-header-start.h:33,
                 from /usr/include/limits.h:26,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/10.2/include-fixed/limits.h:195,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/10.2/include-fixed/syslimits.h:7,
                 from /usr/lib/gcc/x86_64-unknown-linux-gnu/10.2/include-fixed/limits.h:34,
                 from /usr/lib/glib-2.0/include/glibconfig.h:11,
                 from /usr/include/glib-2.0/glib/gtypes.h:32,
                 from /usr/include/glib-2.0/glib/galloca.h:32,
                 from /usr/include/glib-2.0/glib.h:30,
                 from /home/michal/temp/libadwaita/build/tmp-introspecthugfmj1e/Adw-1.c:2:
/usr/include/features.h:397:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
  397 | #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
      |    ^~~~~~~
```

**To Reproduce**
Please include your `meson.build` files, preferably as a minimal toy example showing the issue.
You may need to create simple source code files (don't include private/proprietary code).

1. clone for example libadwaita - `git clone https://gitlab.gnome.org/GNOME/libadwaita`
2. write meson.cross:
```
[binaries]
c = 'gcc'
ar = 'ar'
nm = 'nm'
ld = 'ld'
pkgconfig = 'pkg-config'

[built-in options]
c_args = ['-O2','-fstack-protector-strong','-D_FORTIFY_SOURCE=2']
```
3. run `meson --cross-file=meson.cross build`
4. run `ninja -C build`


**Expected behavior**
Project will compile without any warnings. Maybe meson could remove _FORTIFY_SOURCE from CFLAGS when running g-ir-scanner.

**system parameters**
* native build
* Void Linux
* Python 3.9.6
* meson 0.59.1
* ninja 1.10.2



Discussion:
g-ir-scanner filters out cflags and -O is on the list of flags that get dropped.

Resolving patch:
diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py
--- a/mesonbuild/modules/gnome.py
+++ b/mesonbuild/modules/gnome.py
@@ -817,7 +817,9 @@ def _get_external_args_for_langs(self, state, langs):
     def _get_scanner_cflags(cflags):
         'g-ir-scanner only accepts -I/-D/-U; must ignore all other flags'
         for f in cflags:
-            if f.startswith(('-D', '-U', '-I')):
+            # _FORTIFY_SOURCE depends on / works together with -O, on the other hand this
+            # just invokes the preprocessor anyway
+            if f.startswith(('-D', '-U', '-I')) and not f.startswith('-D_FORTIFY_SOURCE'):
                 yield f
 
     @staticmethod


Test patch: