Repository: mesonbuild/meson

Issue:
How to pass include directories / CFlags to gtkdoc-scangobj ?
I'm probably not understanding something here, but I need to pass additionnal flags to scangobj:
* `include_directories` to specific dirs of the project
* `link_with` helper/static libraries of the project

It seems that the only way to do that is to `declare_dependency` with those infos and pass this dependency to `gnome.gtkdoc`.
Isn't there an easier way ?

EDIT: That doesn't work, because `meson.source_root()` is a relative path, and the scanobj call is equivalent to:

    cd devel-docs/libgimp && gtkdoc-scangobj --cflags=... --ldflags=...
That seems to break the path to `meson.source_root() == ../.`.
Or am i missing something ?
  


Discussion:
I'm having the same issue when porting [evince to meson](https://gitlab.gnome.org/GNOME/evince/merge_requests/80). Most of headers include [guards](https://gitlab.gnome.org/inigomartinez/evince/blob/meson/libview/ev-page-cache.h#L20) to force including just two general headers, [evince-view.h](https://gitlab.gnome.org/inigomartinez/evince/blob/meson/evince-view.h) and [evince-document.h](https://gitlab.gnome.org/inigomartinez/evince/blob/meson/evince-document.h), unless `EVINCE_COMPILATION` macro is defined.

This macro definition has to be passed as `CFLAGS` in both [libview](https://gitlab.gnome.org/inigomartinez/evince/blob/meson/help/reference/libview/Makefile.am#L111) and [shell](https://gitlab.gnome.org/inigomartinez/evince/blob/meson/help/reference/shell/Makefile.am#L103) GTK documentation generation.

Using the `scanobjs_args` parameter to include it does not work, so there doesn't seem to be any way to override the `cflags` already included by meson.
@inigomartinez i've already started and almost finished to port evince... I'll send you my work when I can if you're interested. 
I didn't know someone else was also working on porting ```evince``` to meson. ```evince``` was marked as ```to do``` in the [meson porting initiative](https://wiki.gnome.org/Initiatives/GnomeGoals/MesonPorting) until German recently updated it. If I had known it, I would have moved to another package to avoid duplicating work. Please, if you had any other meson port in progress, update it in the list.

Regarding ```evince```'s meson port, there is already an open [merge request](https://gitlab.gnome.org/GNOME/evince/merge_requests/80) and it's only missing two GTK documentation generation that are not working due to this issue, not to be able to append extra ```CFLAGS```. However, any suggestion is very appreciated and any improvement would be welcome.
I'm having the same issue, I've to say that related only to `include_directories` the problem can be fixed as you said using `declare_dependency` with something like `dependencies: [LIB_DEPS, declare_dependency(include_directories: extra_include)]`, however, this could even just be fixed by passing the `include_directories` **undocomented** parameter to `gnome.gtkdoc` (see https://github.com/mesonbuild/meson/blob/master/mesonbuild/modules/gnome.py#L888).

However, this is indeed a bug, even using `add_project_arguments(cflags, language: 'c')` doesn't work (well, I've a fixed for this case, though).

In the mean time that I'm looking at how to fix properly meson, however these [gtk-doc patches](https://gitlab.gnome.org/GNOME/gtk-doc/merge_requests/7) should allow to use a syntax such as:

```
scanobjs_args: [
     '--cflags=' + ' '.join(cflags),
     '--ldflags=' + ' '.join(libs)
  ]
```

Resolving patch:
diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py
--- a/mesonbuild/modules/gnome.py
+++ b/mesonbuild/modules/gnome.py
@@ -326,7 +326,7 @@ def _get_dependencies_flags(self, deps, state, depends, include_rpath=False,
 
         for dep in deps:
             if isinstance(dep, InternalDependency):
-                cflags.update(dep.compile_args)
+                cflags.update(dep.get_compile_args())
                 cflags.update(get_include_args(dep.include_directories))
                 for lib in dep.libraries:
                     if hasattr(lib, 'held_object'):
@@ -883,12 +883,13 @@ def yelp(self, state, args, kwargs):
         return ModuleReturnValue(None, rv)
 
     @FeatureNewKwargs('gnome.gtkdoc', '0.48.0', ['c_args'])
+    @FeatureNewKwargs('gnome.gtkdoc', '0.48.0', ['module_version'])
     @FeatureNewKwargs('gnome.gtkdoc', '0.37.0', ['namespace', 'mode'])
     @permittedKwargs({'main_xml', 'main_sgml', 'src_dir', 'dependencies', 'install',
                       'install_dir', 'scan_args', 'scanobjs_args', 'gobject_typesfile',
                       'fixxref_args', 'html_args', 'html_assets', 'content_files',
                       'mkdb_args', 'ignore_headers', 'include_directories',
-                      'namespace', 'mode', 'expand_content_files'})
+                      'namespace', 'mode', 'expand_content_files', 'module_version'})
     def gtkdoc(self, state, args, kwargs):
         if len(args) != 1:
             raise MesonException('Gtkdoc must have one positional argument.')
@@ -903,11 +904,14 @@ def gtkdoc(self, state, args, kwargs):
         main_xml = kwargs.get('main_xml', '')
         if not isinstance(main_xml, str):
             raise MesonException('Main xml keyword argument must be a string.')
+        moduleversion = kwargs.get('module_version', '')
+        if not isinstance(moduleversion, str):
+            raise MesonException('Module version keyword argument must be a string.')
         if main_xml != '':
             if main_file != '':
                 raise MesonException('You can only specify main_xml or main_sgml, not both.')
             main_file = main_xml
-        targetname = modulename + '-doc'
+        targetname = modulename + ('-' + moduleversion if moduleversion else '') + '-doc'
         command = state.environment.get_build_command()
 
         namespace = kwargs.get('namespace', '')
@@ -938,6 +942,7 @@ def gtkdoc(self, state, args, kwargs):
                 '--headerdirs=' + '@@'.join(header_dirs),
                 '--mainfile=' + main_file,
                 '--modulename=' + modulename,
+                '--moduleversion=' + moduleversion,
                 '--mode=' + mode]
         if namespace:
             args.append('--namespace=' + namespace)
@@ -1013,6 +1018,10 @@ def _get_build_args(self, kwargs, state, depends):
             ldflags.update(state.environment.coredata.get_external_link_args('c'))
             compiler = state.environment.coredata.compilers.get('c')
 
+        compiler_flags = self._get_langs_compilers_flags(state, [('c', compiler)])
+        cflags.update(compiler_flags[0])
+        ldflags.update(compiler_flags[1])
+        ldflags.update(compiler_flags[2])
         if compiler:
             args += ['--cc=%s' % ' '.join(compiler.get_exelist())]
             args += ['--ld=%s' % ' '.join(compiler.get_linker_exelist())]
diff --git a/mesonbuild/scripts/gtkdochelper.py b/mesonbuild/scripts/gtkdochelper.py
--- a/mesonbuild/scripts/gtkdochelper.py
+++ b/mesonbuild/scripts/gtkdochelper.py
@@ -28,6 +28,7 @@
 parser.add_argument('--headerdirs', dest='headerdirs')
 parser.add_argument('--mainfile', dest='mainfile')
 parser.add_argument('--modulename', dest='modulename')
+parser.add_argument('--moduleversion', dest='moduleversion')
 parser.add_argument('--htmlargs', dest='htmlargs', default='')
 parser.add_argument('--scanargs', dest='scanargs', default='')
 parser.add_argument('--scanobjsargs', dest='scanobjsargs', default='')
@@ -73,7 +74,7 @@ def gtkdoc_run_check(cmd, cwd, library_paths=None):
         print(out)
 
 def build_gtkdoc(source_root, build_root, doc_subdir, src_subdirs,
-                 main_file, module,
+                 main_file, module, module_version,
                  html_args, scan_args, fixxref_args, mkdb_args,
                  gobject_typesfile, scanobjs_args, run, ld, cc, ldflags, cflags,
                  html_assets, content_files, ignore_headers, namespace,
@@ -191,7 +192,7 @@ def build_gtkdoc(source_root, build_root, doc_subdir, src_subdirs,
     else:
         mkhtml_cmd.append('%s-docs.xml' % module)
     # html gen must be run in the HTML dir
-    gtkdoc_run_check(mkhtml_cmd, os.path.join(abs_out, 'html'))
+    gtkdoc_run_check(mkhtml_cmd, htmldir)
 
     # Fix cross-references in HTML files
     fixref_cmd = ['gtkdoc-fixxref',
@@ -199,6 +200,10 @@ def build_gtkdoc(source_root, build_root, doc_subdir, src_subdirs,
                   '--module-dir=html'] + fixxref_args
     gtkdoc_run_check(fixref_cmd, abs_out)
 
+    if module_version:
+        shutil.move(os.path.join(htmldir, '{}.devhelp2'.format(module)),
+                    os.path.join(htmldir, '{}-{}.devhelp2'.format(module, module_version)))
+
 def install_gtkdoc(build_root, doc_subdir, install_prefix, datadir, module):
     source = os.path.join(build_root, doc_subdir, 'html')
     final_destination = os.path.join(install_prefix, datadir, module)
@@ -234,6 +239,7 @@ def run(args):
         options.headerdirs.split('@@'),
         options.mainfile,
         options.modulename,
+        options.moduleversion,
         htmlargs,
         scanargs,
         fixxrefargs,
@@ -255,7 +261,12 @@ def run(args):
     if 'MESON_INSTALL_PREFIX' in os.environ:
         destdir = os.environ.get('DESTDIR', '')
         install_prefix = destdir_join(destdir, os.environ['MESON_INSTALL_PREFIX'])
-        install_dir = options.install_dir if options.install_dir else options.modulename
+        if options.install_dir:
+            install_dir = options.install_dir
+        else:
+            install_dir = options.modulename
+            if options.moduleversion:
+                install_dir += '-' + options.moduleversion
         if os.path.isabs(install_dir):
             install_dir = destdir_join(destdir, install_dir)
         install_gtkdoc(options.builddir,


Test patch:
diff --git a/test cases/frameworks/10 gtk-doc/doc/meson.build b/test cases/frameworks/10 gtk-doc/doc/meson.build
--- a/test cases/frameworks/10 gtk-doc/doc/meson.build	
+++ b/test cases/frameworks/10 gtk-doc/doc/meson.build	
@@ -16,3 +16,18 @@ gnome.gtkdoc('foobar2',
   content_files : [docbook, version_xml],
   install : true,
   install_dir : 'foobar2')
+
+gnome.gtkdoc('foobar',
+  module_version : '3.0',
+  src_dir : inc,
+  main_sgml : 'foobar-docs.sgml',
+  content_files : [docbook, version_xml],
+  install : true)
+
+gnome.gtkdoc('foobar2',
+  module_version : '3.0',
+  src_dir : inc,
+  main_sgml : 'foobar-docs.sgml',
+  content_files : [docbook, version_xml],
+  install : true,
+  install_dir : 'foobar3')
diff --git a/test cases/frameworks/10 gtk-doc/installed_files.txt b/test cases/frameworks/10 gtk-doc/installed_files.txt
--- a/test cases/frameworks/10 gtk-doc/installed_files.txt	
+++ b/test cases/frameworks/10 gtk-doc/installed_files.txt	
@@ -27,3 +27,31 @@ usr/share/gtk-doc/html/foobar2/right-insensitive.png
 usr/share/gtk-doc/html/foobar2/style.css
 usr/share/gtk-doc/html/foobar2/up.png
 usr/share/gtk-doc/html/foobar2/up-insensitive.png
+usr/share/gtk-doc/html/foobar-3.0/BAR.html
+usr/share/gtk-doc/html/foobar-3.0/foobar-3.0.devhelp2
+usr/share/gtk-doc/html/foobar-3.0/foobar.html
+usr/share/gtk-doc/html/foobar-3.0/foobar-foo.html
+usr/share/gtk-doc/html/foobar-3.0/foobar-foo-version.html
+usr/share/gtk-doc/html/foobar-3.0/home.png
+usr/share/gtk-doc/html/foobar-3.0/index.html
+usr/share/gtk-doc/html/foobar-3.0/left.png
+usr/share/gtk-doc/html/foobar-3.0/left-insensitive.png
+usr/share/gtk-doc/html/foobar-3.0/right.png
+usr/share/gtk-doc/html/foobar-3.0/right-insensitive.png
+usr/share/gtk-doc/html/foobar-3.0/style.css
+usr/share/gtk-doc/html/foobar-3.0/up.png
+usr/share/gtk-doc/html/foobar-3.0/up-insensitive.png
+usr/share/gtk-doc/html/foobar3/BAR.html
+usr/share/gtk-doc/html/foobar3/foobar2-3.0.devhelp2
+usr/share/gtk-doc/html/foobar3/foobar.html
+usr/share/gtk-doc/html/foobar3/foobar2-foo.html
+usr/share/gtk-doc/html/foobar3/foobar2-foo-version.html
+usr/share/gtk-doc/html/foobar3/home.png
+usr/share/gtk-doc/html/foobar3/index.html
+usr/share/gtk-doc/html/foobar3/left.png
+usr/share/gtk-doc/html/foobar3/left-insensitive.png
+usr/share/gtk-doc/html/foobar3/right.png
+usr/share/gtk-doc/html/foobar3/right-insensitive.png
+usr/share/gtk-doc/html/foobar3/style.css
+usr/share/gtk-doc/html/foobar3/up.png
+usr/share/gtk-doc/html/foobar3/up-insensitive.png