Repository: mesonbuild/meson

Issue:
gnome.generate_gir() includes extraneous libraries in the gir file
Currently when you pass a library to `gnome.generate_gir()` we extract every dependency from that and pass it along to `g-ir-scanner` as `LDFLAGS`. What happens after that is to be investigated but I can say the end result is incorrect:

Meson:
```xml
<namespace name="Ide"
           version="1.0"
           shared-library="libegg-private.so.0,libtemplate-glib-1.0.so.0,libcairo.so.2,libjavascriptcoregtk-4.0.so.18,libgdk_pixbuf-2.0.so.0,libatk-1.0.so.0,libwebkit2gtk-4.0.so.37,libcairo-gobject.so.2,libxml2.so.2,libgirepository-1.0.so.1,libgdk-3.so.0,libgtksourceview-3.0.so.1,libsoup-2.4.so.1,libgio-2.0.so.0,libgmodule-2.0.so.0,libgobject-2.0.so.0,libpangocairo-1.0.so.0,libglib-2.0.so.0,libfreetype.so.6,libgtk-3.so.0,libpango-1.0.so.0,libfontconfig.so.1,libpangoft2-1.0.so.0,libpeas-1.0.so.0,libide-1.0.so"
           c:identifier-prefixes="Ide"
           c:symbol-prefixes="ide">
```

Autotools:
```xml
<namespace name="Ide"
           version="1.0"
            shared-library="libide-1.0.so,libegg-private.so.0,libtemplate-glib-1.0.so.0"
           c:identifier-prefixes="Ide"
           c:symbol-prefixes="ide">
```

The Autotools version is correct and are explicitly passed dependencies by the user (effectively equal to `link_with` in Meson). The problem is the automatic dependencies only needed for building also get shoved there.

As a naive approach I just stopped passing the flags as they didn't seem to matter for my project but it is correct to be passed as #947 indicates. We just need to figure out why us passing them has the incorrect result and in autotools it has the correct one.
gnome.generate_gir() includes extraneous libraries in the gir file
Currently when you pass a library to `gnome.generate_gir()` we extract every dependency from that and pass it along to `g-ir-scanner` as `LDFLAGS`. What happens after that is to be investigated but I can say the end result is incorrect:

Meson:
```xml
<namespace name="Ide"
           version="1.0"
           shared-library="libegg-private.so.0,libtemplate-glib-1.0.so.0,libcairo.so.2,libjavascriptcoregtk-4.0.so.18,libgdk_pixbuf-2.0.so.0,libatk-1.0.so.0,libwebkit2gtk-4.0.so.37,libcairo-gobject.so.2,libxml2.so.2,libgirepository-1.0.so.1,libgdk-3.so.0,libgtksourceview-3.0.so.1,libsoup-2.4.so.1,libgio-2.0.so.0,libgmodule-2.0.so.0,libgobject-2.0.so.0,libpangocairo-1.0.so.0,libglib-2.0.so.0,libfreetype.so.6,libgtk-3.so.0,libpango-1.0.so.0,libfontconfig.so.1,libpangoft2-1.0.so.0,libpeas-1.0.so.0,libide-1.0.so"
           c:identifier-prefixes="Ide"
           c:symbol-prefixes="ide">
```

Autotools:
```xml
<namespace name="Ide"
           version="1.0"
            shared-library="libide-1.0.so,libegg-private.so.0,libtemplate-glib-1.0.so.0"
           c:identifier-prefixes="Ide"
           c:symbol-prefixes="ide">
```

The Autotools version is correct and are explicitly passed dependencies by the user (effectively equal to `link_with` in Meson). The problem is the automatic dependencies only needed for building also get shoved there.

As a naive approach I just stopped passing the flags as they didn't seem to matter for my project but it is correct to be passed as #947 indicates. We just need to figure out why us passing them has the incorrect result and in autotools it has the correct one.


Discussion:

Resolving patch:
diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py
--- a/mesonbuild/modules/gnome.py
+++ b/mesonbuild/modules/gnome.py
@@ -36,6 +36,20 @@
 native_glib_version = None
 girwarning_printed = False
 gresource_warning_printed = False
+_gir_has_extra_lib_arg = None
+
+def gir_has_extra_lib_arg():
+    global _gir_has_extra_lib_arg
+    if _gir_has_extra_lib_arg is not None:
+        return _gir_has_extra_lib_arg
+
+    _gir_has_extra_lib_arg = False
+    try:
+        scanner_options = subprocess.check_output(['g-ir-scanner', '--help']).decode()
+        _gir_has_extra_lib_arg = '--extra-library' in scanner_options
+    except (FileNotFound, subprocess.CalledProcessError):
+        pass
+    return _gir_has_extra_lib_arg
 
 class GnomeModule:
 
@@ -206,9 +220,13 @@ def exists_in_srcdir(f):
 
         return dep_files, depends, subdirs
 
-    @staticmethod
-    def _get_link_args(state, lib, depends=None):
-        link_command = ['-l%s' % lib.name]
+
+    def _get_link_args(self, state, lib, depends=None):
+        if gir_has_extra_lib_arg():
+            link_command = ['--extra-library=%s' % lib.name]
+        else:
+            link_command = ['-l%s' % lib.name]
+            print('lib: %s - %s' % (lib.name, link_command))
         if isinstance(lib, build.SharedLibrary):
             link_command += ['-L%s' %
                     os.path.join(state.environment.get_build_dir(),
@@ -288,6 +306,8 @@ def _get_dependencies_flags(self, deps, state, depends=None):
                     # Hack to avoid passing some compiler options in
                     if lib.startswith("-W"):
                         continue
+                    if gir_has_extra_lib_arg():
+                        lib = lib.replace('-l', '--extra-library=')
                     ldflags.update([lib])
 
                 if isinstance(dep, dependencies.PkgConfigDependency):


Test patch: