Repository: mesonbuild/meson

Issue:
Internal static library leaks to the ldflags when using gnome.gtkdoc()
Similar to #3934: additional uninstalled static libraries end up on the linker flags for shared libraries, causing build failures when running `gtkdoc-scangobj`:

```
ERROR: 'gtkdoc-scangobj' failed with status 1
/usr/bin/ld: cannot find -lgeoclue-public-api
collect2: error: ld returned 1 exit status
2018-07-24 18:53:45,120:scangobj.py:execute_command:1196:WARNING:Linking scanner failed: 1, command: ccache cc libgeoclue-scan.o -L/home/ebassi/gnome/source/geoclue/_build/libgeoclue -L/home/ebassi/gnome/install/lib -Wl,-rpath,/home/ebassi/gnome/install/lib -Wl,-rpath,/home/ebassi/gnome/source/geoclue/_build/libgeoclue -lgeoclue-2 -lgeoclue-public-api -lglib-2.0 -lgio-2.0 -lgobject-2.0 -o libgeoclue-scan
```

`geoclue-public-api` is a static library defined as an internal dependency of `geoclue-2`.


Discussion:
"Note that meson's pc generator special case shared_library() to not pull its dependencies, exactly for the case you're describing. But since geoclue uses library() it means it wants to provide the static library." — I guess the `gnome.gtkdoc()` method will need to special case shared_library() as well, then.
Can you paste the call to `gnome.gtkdoc` you're doing? I guess you pass libgeoclue_lib in dependencies?

I think _get_dependencies_flags() in gnome module should indeed know that when you link on a shared library you don't need to link to the static libraries it depends on. That's yet another reason why I would like to stop letting gtkdoc-scangobj compile executable, and let ninja do it. See https://gitlab.gnome.org/GNOME/gtk-doc/merge_requests/1. I'll try to find some time to actually try that idea.
```meson
content_files = [ '../../COPYING.LIB' ]
scan_options = [ '--ignore-headers=config.h', '--rebuild-types' ]
# Extra options to supply to gtkdoc-mkdb
mkdb_options = [ '--output-format=xml' ]
src_dir = [
  include_directories('../../public-api'),
  include_directories('../../libgeoclue'),
]

gnome.gtkdoc('libgeoclue',
  main_xml: 'libgeoclue-docs.xml',
  content_files: content_files,
  gobject_typesfile: 'libgeoclue.types',
  dependencies: libgeoclue,
  scan_args: scan_options,
  src_dir: src_dir,
  mkdb_args: mkdb_options,
  install: true,
)
```
Reading the code in gnome.py, when there is a SharedLibrary in dependencies, it does not even seems to add a ldflag for it:
```meson
            elif isinstance(dep, (build.StaticLibrary, build.SharedLibrary)):
                cflags.update(get_include_args(dep.get_include_dirs()))
                depends.append(dep)
```
That seems wrong to me, but also I don't understand how you can have the error you report here, since you pass libgeoclue which is the library object itself and not a declare_dependency() wrapper, right?
I have the same issue with the [wip evince's meson port](https://gitlab.gnome.org/GNOME/evince/merge_requests/80).

`evince` binary dependencies include [4 static libraries](https://gitlab.gnome.org/GNOME/evince/merge_requests/80/diffs?commit_id=be173beae37452ee989eb928c9307bc1d3594c1c#949ff4ad2adba0d5a2c089cb7ad6a430eabf853c_0_67): [libevmisc](https://gitlab.gnome.org/GNOME/evince/merge_requests/80/diffs?commit_id=be173beae37452ee989eb928c9307bc1d3594c1c#b803f5b061c0abbb2f39adafc658e2795982ad43_0_7), [libevproperties](https://gitlab.gnome.org/GNOME/evince/merge_requests/80/diffs?commit_id=be173beae37452ee989eb928c9307bc1d3594c1c#cbba70be4c8e1bbb014c7d4d256910678a4300c6_0_1), [libgd](https://gitlab.gnome.org/GNOME/evince/merge_requests/80/diffs?commit_id=be173beae37452ee989eb928c9307bc1d3594c1c#6680ab60a248fd798053abc72673eb8a89283593_0_11) and [libgimpcellrenderertoggle](https://gitlab.gnome.org/GNOME/evince/merge_requests/80/diffs?commit_id=be173beae37452ee989eb928c9307bc1d3594c1c#cdf62a975807839fb7938675aa6c9ed871d8c0a1_0_1).

These are also used when generating [gtkdoc documentation](https://gitlab.gnome.org/GNOME/evince/merge_requests/80/diffs?commit_id=be173beae37452ee989eb928c9307bc1d3594c1c#c5e3299722c471b70f0186425f2cc673f98f00f4_0_30), that produces the following error:

```
ERROR: 'gtkdoc-scangobj' failed with status 1
/usr/bin/ld: cannot find -levmisc
/usr/bin/ld: cannot find -levproperties
/usr/bin/ld: cannot find -lgd
/usr/bin/ld: cannot find -lgimpcellrenderertoggle
collect2: error: ld returned 1 exit status
```

Resolving patch:
diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py
--- a/mesonbuild/modules/gnome.py
+++ b/mesonbuild/modules/gnome.py
@@ -331,14 +331,15 @@ def _get_dependencies_flags(self, deps, state, depends, include_rpath=False,
                 for lib in dep.libraries:
                     if hasattr(lib, 'held_object'):
                         lib = lib.held_object
-                    internal_ldflags.update(self._get_link_args(state, lib, depends, include_rpath))
-                    libdepflags = self._get_dependencies_flags(lib.get_external_deps(), state, depends, include_rpath,
-                                                               use_gir_args, True)
-                    cflags.update(libdepflags[0])
-                    internal_ldflags.update(libdepflags[1])
-                    external_ldflags.update(libdepflags[2])
-                    external_ldflags_nodedup += libdepflags[3]
-                    gi_includes.update(libdepflags[4])
+                    if isinstance(lib, build.SharedLibrary):
+                        internal_ldflags.update(self._get_link_args(state, lib, depends, include_rpath))
+                        libdepflags = self._get_dependencies_flags(lib.get_external_deps(), state, depends, include_rpath,
+                                                                   use_gir_args, True)
+                        cflags.update(libdepflags[0])
+                        internal_ldflags.update(libdepflags[1])
+                        external_ldflags.update(libdepflags[2])
+                        external_ldflags_nodedup += libdepflags[3]
+                        gi_includes.update(libdepflags[4])
                 extdepflags = self._get_dependencies_flags(dep.ext_deps, state, depends, include_rpath,
                                                            use_gir_args, True)
                 cflags.update(extdepflags[0])


Test patch: