Repository: mesonbuild/meson

Issue:
gtkdoc dependencies produce invalid search paths
If I add a `print` at the beginning of [`gtkdoc_run_check`](https://github.com/mesonbuild/meson/blob/master/mesonbuild/scripts/gtkdochelper.py#L48):
```
['gtkdoc-scangobj',
 '--types=finch.types',
 '--module=finch',
 '--cflags=-I@BUILD_ROOT@/. -I@SOURCE_ROOT@/. -I@BUILD_ROOT@/finch/. -I@SOURCE_ROOT@/finch/. -pthread -I/usr/include/gstreamer-1.0 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/gplugin-1.0/ -I@BUILD_ROOT@/libpurple/. -I@SOURCE_ROOT@/libpurple/. -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include -I/usr/include/libxml2 -I/usr/include/farstream-0.2 -I/usr/include/json-glib-1.0 -I/usr/include/p11-kit-1 -I/usr/include/nss3 -I/usr/include/nspr4 -I@BUILD_ROOT@/finch/libgnt/. -I@SOURCE_ROOT@/finch/libgnt/. -I/usr/include/ncursesw', '--ldflags=-lfinch -Lfinch -L/home/elliott/code/pidgin-hg/build/finch -L/home/elliott/code/pidgin-hg/build/libpurple -L/home/elliott/code/pidgin-hg/build/finch/libgnt -Wl,-rpath,finch -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0 -lgplugin -lgmodule-2.0 -pthread -lgio-2.0 -lncursesw -lpanelw -lpurple -Llibpurple -Wl,-rpath,libpurple -ldbus-1 -ldbus-glib-1 -lxml2 -lfarstream-0.2 -lgstbase-1.0 -lgstvideo-1.0 -lgstapp-1.0 -lidn -ljson-glib-1.0 -lgnutls -lssl3 -lsmime3 -lnss3 -lnssutil3 -lplds4 -lplc4 -lnspr4 -lpthread -ldl -lz -lm -lgnt -Lfinch/libgnt -Wl,-rpath,finch/libgnt']
```
where `gtkdoc` is called approximately like:
```meson
libfinch_inc = include_directories('somewhere')
libfinch_dep = declare_dependency(
    link_with : library('finch', ...),
    include_directories : [libfinch_inc, other stuff],
    dependencies : [gstreamer, glib, etc.])

DOC_MODULE = 'finch'
gnome.gtkdoc(DOC_MODULE,
    main_xml : DOC_MODULE + '-docs.xml',
    src_dir : libfinch_inc,
    dependencies : libfinch_dep,
    gobject_typesfile : 'finch.types',
    scan_args : scan_args)
```

You can see that `@BUILD_ROOT@` and `@SOURCE_ROOT@` are not replaced, and the linker paths are relative to the source instead of build+source. I think the problem is it creates a `RunTarget` which does no substitutions and runs out an unspecified directory.


Discussion:
The first part of this, the substitutions, is a dupe of #1681. The second part, the linker paths, is a separate issue and is yet to be fixed.

Both linker search path and rpath are relative (to top of build or source) and so built libraries are not found.

Resolving patch:
diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py
--- a/mesonbuild/modules/gnome.py
+++ b/mesonbuild/modules/gnome.py
@@ -294,7 +294,7 @@ def _get_link_args(self, state, lib, depends=None, include_rpath=False,
         else:
             link_command = ['-l' + lib.name]
         if isinstance(lib, build.SharedLibrary):
-            libdir = state.backend.get_target_dir(lib)
+            libdir = os.path.join(state.environment.get_build_dir(), state.backend.get_target_dir(lib))
             link_command.append('-L' + libdir)
             # Needed for the following binutils bug:
             # https://github.com/mesonbuild/meson/issues/1911
@@ -303,6 +303,8 @@ def _get_link_args(self, state, lib, depends=None, include_rpath=False,
             for d in state.backend.determine_rpath_dirs(lib):
                 d = os.path.join(state.environment.get_build_dir(), d)
                 link_command.append('-L' + d)
+                if include_rpath:
+                    link_command.append('-Wl,-rpath,' + d)
             if include_rpath:
                 link_command.append('-Wl,-rpath,' + libdir)
             if depends:
@@ -700,6 +702,8 @@ def gtkdoc(self, state, args, kwargs):
                 for inc_dir in src_dir.get_incdirs():
                     header_dirs.append(os.path.join(state.environment.get_source_dir(),
                                                     src_dir.get_curdir(), inc_dir))
+                    header_dirs.append(os.path.join(state.environment.get_build_dir(),
+                                                    src_dir.get_curdir(), inc_dir))
             else:
                 header_dirs.append(src_dir)
 


Test patch:
diff --git a/test cases/frameworks/10 gtk-doc/doc/foobar-docs.sgml b/test cases/frameworks/10 gtk-doc/doc/foobar-docs.sgml
--- a/test cases/frameworks/10 gtk-doc/doc/foobar-docs.sgml	
+++ b/test cases/frameworks/10 gtk-doc/doc/foobar-docs.sgml	
@@ -34,6 +34,7 @@
       </para>
     </partintro>
     <xi:include href="xml/foo.xml"/>
+    <xi:include href="xml/foo-version.xml"/>
   </reference>
 
 </book>
diff --git a/test cases/frameworks/10 gtk-doc/include/foo-version.h.in b/test cases/frameworks/10 gtk-doc/include/foo-version.h.in
new file mode 100644
--- /dev/null
+++ b/test cases/frameworks/10 gtk-doc/include/foo-version.h.in	
@@ -0,0 +1,29 @@
+#pragma once
+
+/**
+ * SECTION:version
+ * @section_id: foo-version
+ * @short_description: <filename>foo-version.h</filename>
+ * @title: Foo Versioning
+ */
+
+/**
+ * FOO_MAJOR_VERSION:
+ *
+ * The major version of foo.
+ */
+#define FOO_MAJOR_VERSION (@FOO_MAJOR_VERSION@)
+
+/**
+ * FOO_MINOR_VERSION:
+ *
+ * The minor version of foo.
+ */
+#define FOO_MINOR_VERSION (@FOO_MINOR_VERSION@)
+
+/**
+ * FOO_MICRO_VERSION:
+ *
+ * The micro version of foo.
+ */
+#define FOO_MICRO_VERSION (@FOO_MICRO_VERSION@)
diff --git a/test cases/frameworks/10 gtk-doc/include/meson.build b/test cases/frameworks/10 gtk-doc/include/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/frameworks/10 gtk-doc/include/meson.build	
@@ -0,0 +1,10 @@
+cdata = configuration_data()
+parts = meson.project_version().split('.')
+cdata.set('FOO_MAJOR_VERSION', parts[0])
+cdata.set('FOO_MINOR_VERSION', parts[1])
+cdata.set('FOO_MICRO_VERSION', parts[2])
+configure_file(input : 'foo-version.h.in',
+  output : 'foo-version.h',
+  configuration : cdata,
+  install : true,
+  install_dir : get_option('includedir'))
diff --git a/test cases/frameworks/10 gtk-doc/meson.build b/test cases/frameworks/10 gtk-doc/meson.build
--- a/test cases/frameworks/10 gtk-doc/meson.build	
+++ b/test cases/frameworks/10 gtk-doc/meson.build	
@@ -1,4 +1,4 @@
-project('gtkdoctest', 'c')
+project('gtkdoctest', 'c', version : '1.0.0')
 
 gnome = import('gnome')
 
@@ -6,6 +6,8 @@ assert(gnome.gtkdoc_html_dir('foobar') == 'share/gtkdoc/html/foobar', 'Gtkdoc in
 
 inc = include_directories('include')
 
+subdir('include')
+
 # We have to disable this test until this bug fix has landed to
 # distros https://bugzilla.gnome.org/show_bug.cgi?id=753145
 error('MESON_SKIP_TEST can not enable gtk-doc test until upstream fixes have landed.')