Repository: mesonbuild/meson

Issue:
gnome.gtkdoc getting wrong linker flags
Issue #981 added the `--extra-library` argument to the introspection scanner's flag, but in doing so it modified the linker flags passed to the gtk-doc helper.

Now, you get:

```sh
Running custom install script '/usr/bin/python3 /usr/bin/meson --internal gtkdoc \
--sourcedir=/home/nacho/checkout/gnome/libgit2-glib \
--builddir=/home/nacho/checkout/gnome/libgit2-glib/build \
--subdir=docs/reference \
--headerdirs=/home/nacho/checkout/gnome/libgit2-glib/libgit2-glib \
--mainfile=libgit2-glib-1.0-docs.sgml \
--modulename=libgit2-glib-1.0 --mode=auto \
--scanargs=--rebuild-types@@--ignore-headers=config.h ggit-convert.h ggit-utils.h \
--gobjects-types-file=libgit2-glib-1.0.types \
--fixxrefargs=--extra-dir=/opt/gnome/share/gtk-doc/html/glib@@--extra-dir=/opt/gnome/share/gtk-doc/html/gobject@@--extra-dir=/opt/gnome/share/gtk-doc/html/gio \
--cflags=-I/home/nacho/checkout/gnome/libgit2-glib/build -I/home/nacho/checkout/gnome/libgit2-glib -I/opt/gnome/include/glib-2.0 -I/opt/gnome/lib/glib-2.0/include -I/home/nacho/checkout/gnome/libgit2-glib/libgit2-glib -I/home/nacho/checkout/gnome/libgit2-glib/build/libgit2-glib -pthread \
--ldflags=-L/home/nacho/checkout/gnome/libgit2-glib/build/libgit2-glib --extra-library=gio-2.0 --extra-library=libgit2-glib-0.0 -Wl,-rpath /home/nacho/checkout/gnome/libgit2-glib/build/libgit2-glib --extra-library=glib-2.0 --extra-library=git2 --extra-library=gobject-2.0 -L/opt/gnome/lib --cc=cc --ld=cc'
Building documentation for libgit2-glib-1.0
 
Error in gtkdoc helper script:
'gtkdoc-scangobj' failed with status 1
gcc: error: unrecognized command line option ‘--extra-library=gio-2.0’
gcc: error: unrecognized command line option ‘--extra-library=libgit2-glib-0.0’
gcc: error: unrecognized command line option ‘--extra-library=glib-2.0’
gcc: error: unrecognized command line option ‘--extra-library=git2’
gcc: error: unrecognized command line option ‘--extra-library=gobject-2.0’
```

Because of the overeager substitution of `-L` flags.


Discussion:
Indeed, this happens because `gnome.gtkdoc()` gathers the cflags and ldflags using `_get_build_args()`, which calls `_get_dependencies_flags()`, which calls `_get_link_args()` — which is also called by the `gnome.generate_gir()` method.

Resolving patch:
diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py
--- a/mesonbuild/modules/gnome.py
+++ b/mesonbuild/modules/gnome.py
@@ -255,8 +255,9 @@ def exists_in_srcdir(f):
 
         return dep_files, depends, subdirs
 
-    def _get_link_args(self, state, lib, depends=None, include_rpath=False):
-        if gir_has_extra_lib_arg():
+    def _get_link_args(self, state, lib, depends=None, include_rpath=False,
+                       use_gir_args=False):
+        if gir_has_extra_lib_arg() and use_gir_args:
             link_command = ['--extra-library=%s' % lib.name]
         else:
             link_command = ['-l%s' % lib.name]
@@ -269,7 +270,8 @@ def _get_link_args(self, state, lib, depends=None, include_rpath=False):
                 depends.append(lib)
         return link_command
 
-    def _get_dependencies_flags(self, deps, state, depends=None, include_rpath=False):
+    def _get_dependencies_flags(self, deps, state, depends=None, include_rpath=False,
+                                use_gir_args=False):
         cflags = set()
         ldflags = set()
         gi_includes = set()
@@ -283,11 +285,13 @@ def _get_dependencies_flags(self, deps, state, depends=None, include_rpath=False
                 cflags.update(get_include_args(state.environment, dep.include_directories))
                 for lib in dep.libraries:
                     ldflags.update(self._get_link_args(state, lib.held_object, depends, include_rpath))
-                    libdepflags = self._get_dependencies_flags(lib.held_object.get_external_deps(), state, depends, include_rpath)
+                    libdepflags = self._get_dependencies_flags(lib.held_object.get_external_deps(), state, depends, include_rpath,
+                                                               use_gir_args)
                     cflags.update(libdepflags[0])
                     ldflags.update(libdepflags[1])
                     gi_includes.update(libdepflags[2])
-                extdepflags = self._get_dependencies_flags(dep.ext_deps, state, depends, include_rpath)
+                extdepflags = self._get_dependencies_flags(dep.ext_deps, state, depends, include_rpath,
+                                                           use_gir_args)
                 cflags.update(extdepflags[0])
                 ldflags.update(extdepflags[1])
                 gi_includes.update(extdepflags[2])
@@ -314,7 +318,7 @@ def _get_dependencies_flags(self, deps, state, depends=None, include_rpath=False
                     # Hack to avoid passing some compiler options in
                     if lib.startswith("-W"):
                         continue
-                    if gir_has_extra_lib_arg():
+                    if gir_has_extra_lib_arg() and use_gir_args:
                         lib = lib.replace('-l', '--extra-library=')
                     ldflags.update([lib])
 
@@ -378,7 +382,8 @@ def generate_gir(self, state, args, kwargs):
             if not isinstance(link_with, list):
                 link_with = [link_with]
             for link in link_with:
-                scan_command += self._get_link_args(state, link.held_object, depends)
+                scan_command += self._get_link_args(state, link.held_object, depends,
+                                                    use_gir_args=True)
 
         if 'includes' in kwargs:
             includes = kwargs.pop('includes')
@@ -478,7 +483,8 @@ def generate_gir(self, state, args, kwargs):
         # ldflags will be misinterpreted by gir scanner (showing
         # spurious dependencies) but building GStreamer fails if they
         # are not used here.
-        cflags, ldflags, gi_includes = self._get_dependencies_flags(deps, state, depends)
+        cflags, ldflags, gi_includes = self._get_dependencies_flags(deps, state, depends,
+                                                                    use_gir_args=True)
         scan_command += list(cflags)
         # need to put our output directory first as we need to use the
         # generated libraries instead of any possibly installed system/prefix


Test patch: