Repository: mesonbuild/meson

Issue:
add option to build ".c" or ".gresource" out of gnome.compile_resources()
It's not always needed to get `.c` + `.h`. Sometimes it's enough to get only `.c` or `.gresource.

```
(08:21:42 PM) ignatenkobrain: jpakkane: probably not, problem is that glib-compile-resources can generate `.c`, `.c + .h` or `.gresource`
(08:21:57 PM) ignatenkobrain: I was thinking what's the best option to give user what exactly he wants
(08:23:46 PM) jpakkane: No, that's too magic. Maybe something like have two methods, the current one and compile_gresource that produces a gresource file. Developers could the mix and match based on preference.
(08:28:06 PM) ignatenkobrain: or having option: `output_mode` in (`source`, `header`, `sourceheader` (default), `gresource`), wdyt?
(08:39:14 AM) TingPing: ignatenkobrain, i agree with simply adding another kwarg for it
```


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
@@ -108,8 +108,19 @@ def compile_resources(self, state, args, kwargs):
 
         cmd += mesonlib.stringlistify(kwargs.pop('extra_args', []))
 
+        gresource = kwargs.pop('gresource_bundle', False)
+        if gresource:
+            output = args[0] + '.gresource'
+            name = args[0] + '_gresource'
+        else:
+            output = args[0] + '.c'
+            name = args[0] + '_c'
+
+        if kwargs.get('install', False) and not gresource:
+            raise MesonException('Only gresource files can be installed')
+
         kwargs['input'] = args[1]
-        kwargs['output'] = args[0] + '.c'
+        kwargs['output'] = output
         kwargs['depends'] = depends
         if not mesonlib.version_compare(glib_version, gresource_dep_needed_version):
             # This will eventually go out of sync if dependencies are added
@@ -119,7 +130,10 @@ def compile_resources(self, state, args, kwargs):
             depfile = kwargs['output'] + '.d'
             kwargs['depfile'] = depfile
             kwargs['command'] = copy.copy(cmd) + ['--dependency-file', '@DEPFILE@']
-        target_c = build.CustomTarget(args[0] + '_c', state.subdir, kwargs)
+        target_c = build.CustomTarget(name, state.subdir, kwargs)
+
+        if gresource: # Only one target for .gresource files
+            return [target_c]
 
         h_kwargs = {
             'command': cmd,


Test patch:
diff --git a/test cases/frameworks/7 gnome/installed_files.txt b/test cases/frameworks/7 gnome/installed_files.txt
--- a/test cases/frameworks/7 gnome/installed_files.txt	
+++ b/test cases/frameworks/7 gnome/installed_files.txt	
@@ -12,3 +12,4 @@ usr/share/gir-1.0/Meson-1.0.gir
 usr/share/gir-1.0/MesonDep1-1.0.gir
 usr/share/gir-1.0/MesonDep2-1.0.gir
 usr/share/glib-2.0/schemas/com.github.meson.gschema.xml
+usr/share/simple-resources.gresource
diff --git a/test cases/frameworks/7 gnome/resources/meson.build b/test cases/frameworks/7 gnome/resources/meson.build
--- a/test cases/frameworks/7 gnome/resources/meson.build	
+++ b/test cases/frameworks/7 gnome/resources/meson.build	
@@ -11,6 +11,15 @@ simple_res_exe = executable('simple-resources-test',
   dependencies: gio)
 test('simple resource test', simple_res_exe)
 
+gnome.compile_resources('simple-resources',
+  'simple.gresource.xml',
+  gresource_bundle: true,
+  install: true,
+  install_dir: get_option('datadir'),
+  source_dir : '../resources-data',
+)
+test('simple resource test (gresource)', find_program('resources.py'))
+
 if glib.version() >= '2.52.0'
   # This test cannot pass if GLib version is older than 9.99.9.
   # Meson will raise an error if the user tries to use the 'dependencies'
diff --git a/test cases/frameworks/7 gnome/resources/resources.py b/test cases/frameworks/7 gnome/resources/resources.py
new file mode 100644
--- /dev/null
+++ b/test cases/frameworks/7 gnome/resources/resources.py	
@@ -0,0 +1,10 @@
+#!/usr/bin/env python3
+import os
+from gi.repository import Gio
+
+if __name__ == '__main__':
+    res = Gio.resource_load(os.path.join('resources', 'simple-resources.gresource'))
+    Gio.Resource._register(res)
+
+    data = Gio.resources_lookup_data('/com/example/myprog/res1.txt', Gio.ResourceLookupFlags.NONE)
+    assert(data.get_data() == b'This is a resource.\n')