Repository: mesonbuild/meson

Issue:
gnome.gtkdoc doesn't include linker flags set by LDFLAGS
This causes problems when building graphene with gtk-doc enabled in a jhbuild prefix on FreeBSD.
LDFLAGS set by jhbuild is `-L/home/lantw44/gnome/devinstall/lib -L/usr/local/lib`, but `-L/usr/local/lib` is missing from the gtkdoc helper command line.

```
Building documentation for graphene

Error in gtkdoc helper script:
'gtkdoc-scangobj' failed with status 1
/usr/bin/ld: cannot find -lintl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Traceback (most recent call last):
  File "/home/lantw44/gnome/devinstall/bin/gtkdoc-scangobj", line 69, in <module>
    sys.exit(scangobj.run(options))
  File "/home/lantw44/gnome/devinstall/share/gtk-doc/python/gtkdoc/scangobj.py", line 1262, in run
    res = subprocess.check_call(command, shell=True)
  File "/usr/local/lib/python2.7/subprocess.py", line 186, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'clang -std=gnu11 -pedantic >/dev/null graphene-scan.o -lgraphene-1.0 -L/home/lantw44/gnome/build/graphene/src -Wl,-rpath /home/lantw44/gnome/build/graphene/src -lm -L/home/lantw44/gnome/devinstall/lib -lgobject-2.0 -lglib-2.0 -lintl -o graphene-scan' returned non-zero exit status 1

Failed to run install script '/usr/local/bin/python3 /home/lantw44/gnome/devinstall/bin/meson --internal gtkdoc --sourcedir=/home/lantw44/gnome/source/graphene --builddir=/home/lantw44/gnome/build/graphene --subdir=doc --headerdirs=/home/lantw44/gnome/source/graphene/src@@/home/lantw44/gnome/build/graphene/src --mainfile=graphene-docs.xml --modulename=graphene --mode=auto --scanargs=--rebuild-types@@--ignore-decorators=_GRAPHENE_PUBLIC@@--ignore-headers=config.h graphene.h graphene-alloc-private.h graphene-bench-utils.h graphene-config.h graphene-line-segment-private.h graphene-macros.h graphene-private.h graphene-test-compat.h graphene-version-macros.h graphene-vectors-private.h --gobjects-types-file=graphene.types --fixxrefargs=--html-dir=/home/lantw44/gnome/devinstall/share/gtk-doc/html@@--extra-dir=/home/lantw44/gnome/devinstall/share/gtk-doc/html/glib@@--extra-dir=/home/lantw44/gnome/devinstall/share/gtk-doc/html/gobject --html-assets=rectangle-intersection.png@@rectangle-union.png@@rectangle.png@@triangle-barycentric.png --cflags=-I/home/lantw44/gnome/build/graphene/src/. -I/home/lantw44/gnome/source/graphene/src/. -I/home/lantw44/gnome/devinstall/include/glib-2.0 -I/home/lantw44/gnome/devinstall/lib/glib-2.0/include --ldflags=-lgraphene-1.0 -L/home/lantw44/gnome/build/graphene/src -Wl,-rpath /home/lantw44/gnome/build/graphene/src -lm -L/home/lantw44/gnome/devinstall/lib -lgobject-2.0 -lglib-2.0 -lintl --cc=clang -std=gnu11 -pedantic --ld=clang -std=gnu11 -pedantic'
FAILED: install 
'/usr/local/bin/python3' '/home/lantw44/gnome/devinstall/bin/meson' '--internal' 'install' '/home/lantw44/gnome/build/graphene/meson-private/install.dat'
ninja: build stopped: subcommand failed.
```




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
@@ -372,7 +372,7 @@ def _get_dependencies_flags(self, deps, state, depends=None, include_rpath=False
                 continue
 
         if gir_has_extra_lib_arg() and use_gir_args:
-            fixed_ldflags = set()
+            fixed_ldflags = OrderedSet()
             for ldflag in ldflags:
                 if ldflag.startswith("-l"):
                     fixed_ldflags.add(ldflag.replace('-l', '--extra-library=', 1))
@@ -565,6 +565,7 @@ def generate_gir(self, state, args, kwargs):
         ldflags += list(dep_ldflags)
         scan_command += ['--cflags-begin']
         scan_command += cflags
+        scan_command += state.environment.coredata.external_args[lang]
         scan_command += ['--cflags-end']
         # need to put our output directory first as we need to use the
         # generated libraries instead of any possibly installed system/prefix
@@ -595,6 +596,11 @@ def generate_gir(self, state, args, kwargs):
                 d = os.path.join(state.environment.get_build_dir(), d)
                 scan_command.append('-L' + d)
             scan_command += ['--library', libname]
+
+        for link_arg in state.environment.coredata.external_link_args[lang]:
+            if link_arg.startswith('-L'):
+                scan_command.append(link_arg)
+
         scankwargs = {'output': girfile,
                       'command': scan_command,
                       'depends': depends}
@@ -823,6 +829,8 @@ def _get_build_args(self, kwargs, state):
                 raise MesonException(
                     'Gir include dirs should be include_directories().')
         cflags.update(get_include_args(inc_dirs))
+        cflags.update(state.environment.coredata.external_args['c'])
+        ldflags.update(state.environment.coredata.external_link_args['c'])
         if cflags:
             args += ['--cflags=%s' % ' '.join(cflags)]
         if ldflags:
diff --git a/mesonbuild/scripts/gtkdochelper.py b/mesonbuild/scripts/gtkdochelper.py
--- a/mesonbuild/scripts/gtkdochelper.py
+++ b/mesonbuild/scripts/gtkdochelper.py
@@ -14,6 +14,7 @@
 
 import sys, os
 import subprocess
+import shlex
 import shutil
 import argparse
 from ..mesonlib import MesonException, Popen_safe
@@ -45,10 +46,13 @@
 parser.add_argument('--mode', dest='mode', default='')
 parser.add_argument('--installdir', dest='install_dir')
 
-def gtkdoc_run_check(cmd, cwd):
+def gtkdoc_run_check(cmd, cwd, library_path=None):
+    env = dict(os.environ)
+    if library_path:
+        env['LD_LIBRARY_PATH'] = library_path
     # Put stderr into stdout since we want to print it out anyway.
     # This preserves the order of messages.
-    p, out = Popen_safe(cmd, cwd=cwd, stderr=subprocess.STDOUT)[0:2]
+    p, out = Popen_safe(cmd, cwd=cwd, env=env, stderr=subprocess.STDOUT)[0:2]
     if p.returncode != 0:
         err_msg = ["{!r} failed with status {:d}".format(cmd[0], p.returncode)]
         if out:
@@ -115,7 +119,15 @@ def build_gtkdoc(source_root, build_root, doc_subdir, src_subdirs,
                                                               '--ldflags=' + ldflags,
                                                               '--ld=' + ld]
 
-        gtkdoc_run_check(scanobjs_cmd, abs_out)
+        library_paths = []
+        for ldflag in shlex.split(ldflags):
+            if ldflag.startswith('-Wl,-rpath,'):
+                library_paths.append(ldflag[11:])
+        if 'LD_LIBRARY_PATH' in os.environ:
+            library_paths.append(os.environ['LD_LIBRARY_PATH'])
+        library_path = ':'.join(library_paths)
+
+        gtkdoc_run_check(scanobjs_cmd, abs_out, library_path)
 
     # Make docbook files
     if mode == 'auto':


Test patch: