Skip to content

Commit

Permalink
Vala: fixes to gresource handling (#12418)
Browse files Browse the repository at this point in the history
* Vala: depend on gresources

Valac uses gresource at compile time to look up .ui files

* Automatically pass `--gresourcesdir` to valac

* gnome.compile_resources: clean up duplicate paths better

* Add a test for improved gresouce handling
  • Loading branch information
v1993 authored Jan 11, 2024
1 parent a5fdd37 commit ae857e8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
12 changes: 11 additions & 1 deletion mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,11 +1691,21 @@ def generate_vala_compile(self, target: build.BuildTarget) -> \
# Install GIR to default location if requested by user
if len(target.install_dir) > 3 and target.install_dir[3] is True:
target.install_dir[3] = os.path.join(self.environment.get_datadir(), 'gir-1.0')
# Detect gresources and add --gresources arguments for each
# Detect gresources and add --gresources/--gresourcesdir arguments for each
gres_dirs = []
for gensrc in other_src[1].values():
if isinstance(gensrc, modules.GResourceTarget):
gres_xml, = self.get_custom_target_sources(gensrc)
args += ['--gresources=' + gres_xml]
for source_dir in gensrc.source_dirs:
gres_dirs += [os.path.join(self.get_target_dir(gensrc), source_dir)]
# Ensure that resources are built before vala sources
# This is required since vala code using [GtkTemplate] effectively depends on .ui files
# GResourceHeaderTarget is not suitable due to lacking depfile
gres_c, = gensrc.get_outputs()
extra_dep_files += [os.path.join(self.get_target_dir(gensrc), gres_c)]
for gres_dir in OrderedSet(gres_dirs):
args += [f'--gresourcesdir={gres_dir}']
dependency_vapis = self.determine_dep_vapis(target)
extra_dep_files += dependency_vapis
extra_dep_files.extend(self.get_target_depend_files(target))
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def __init__(self, return_value: T.Optional['TYPE_var'],
self.new_objects: T.List[T.Union['TYPE_var', 'mesonlib.ExecutableSerialisation']] = new_objects

class GResourceTarget(build.CustomTarget):
pass
source_dirs: T.List[str] = []

class GResourceHeaderTarget(build.CustomTarget):
pass
Expand Down
6 changes: 5 additions & 1 deletion mesonbuild/modules/gnome.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,10 @@ def compile_resources(self, state: 'ModuleState', args: T.Tuple[str, 'FileOrStri
# Always include current directory, but after paths set by user
source_dirs.append(os.path.join(state.build_to_src, state.subdir))

for source_dir in OrderedSet(source_dirs):
# Clean up duplicate directories
source_dirs = list(OrderedSet(os.path.normpath(dir) for dir in source_dirs))

for source_dir in source_dirs:
cmd += ['--sourcedir', source_dir]

if kwargs['c_name']:
Expand Down Expand Up @@ -507,6 +510,7 @@ def compile_resources(self, state: 'ModuleState', args: T.Tuple[str, 'FileOrStri
install_dir=[kwargs['install_dir']] if kwargs['install_dir'] else [],
install_tag=['runtime'],
)
target_c.source_dirs = source_dirs

if gresource: # Only one target for .gresource files
return ModuleReturnValue(target_c, [target_c])
Expand Down
6 changes: 6 additions & 0 deletions test cases/vala/28 generated ui file/TestBox.ui.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="3.0"/>
<template class="TestBox" parent="GtkBox">
</template>
</interface>
34 changes: 34 additions & 0 deletions test cases/vala/28 generated ui file/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
project('demo', 'c', 'vala')

gnome = import('gnome', required: false)

if not gnome.found()
error('MESON_SKIP_TEST: gnome module not supported')
endif

deps = [
dependency('glib-2.0', version : '>=2.50'),
dependency('gobject-2.0'),
dependency('gtk+-3.0'),
]

ui_tgt = custom_target(
input: 'TestBox.ui.in',
output: 'TestBox.ui',
command: [find_program('cat')],
feed: true,
capture: true,
)

resources = gnome.compile_resources('test-resources',
'test.gresource.xml',
c_name: 'test_res',
dependencies: ui_tgt,
)

executable(
'demo',
'test.vala',
resources,
dependencies: deps,
)
6 changes: 6 additions & 0 deletions test cases/vala/28 generated ui file/test.gresource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/mesonbuild/test">
<file>TestBox.ui</file>
</gresource>
</gresources>
7 changes: 7 additions & 0 deletions test cases/vala/28 generated ui file/test.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[GtkTemplate (ui = "/com/mesonbuild/test/TestBox.ui")]
class TestBox: Gtk.Box {
}

int main() {
return 0;
}

0 comments on commit ae857e8

Please sign in to comment.