Understanding multiple `meson.build` and `gresource.xml` files

Hi, I am new to GNOME development. I installed Builder a couple of days ago and I have been playing around with the tutorials on its website.

I created a project using the default “GNOME Application” template, and I added a couple of symbolic icons to use on the app. Right now, the hierarchy of my project is as follows:

data/
    - my icons
    - meson.build
src/
      - .ui and .c files
      - gresource.xml
      - meson.build
...
meson.build

The meson file inside the src folder uses the gresource.xml file of the same folder. At the moment, that file defines the paths for the .ui files as well as the icons I included, which are located at an odd relative location ../data/icons/....

My question is, how can I make it so that the meson file inside the data folder adds the icons during the build process instead?

You’ll want a gresource.xml in data too, and then compile it in data/meson.build the same way you compile src/gresource.xml in src/meson.build.

Make sure to install the icons to <prefix>/icons/scalable/action :slight_smile:

@monster I tried your suggested solution, but the project won’t compile then. I think I am halfway there, but I still cannot figure out what I am doing wrong.

Maybe I should share my meson.build files, as perhaps something is going on there that I simply don’t get.

There are a total of 3 meson.build files in the project, one at the root of it and three on each of the child directories src, data, po. The one at the root looks like this:

project('gramola', 'c',
          version: '0.1.0',
    meson_version: '>= 1.0.0',
  default_options: [ 'warning_level=2', 'werror=false', 'c_std=gnu11', ],
)

i18n = import('i18n')
gnome = import('gnome')
cc = meson.get_compiler('c')

config_h = configuration_data()
config_h.set_quoted('PACKAGE_VERSION', meson.project_version())
config_h.set_quoted('GETTEXT_PACKAGE', 'gramola')
config_h.set_quoted('LOCALEDIR', get_option('prefix') / get_option('localedir'))
configure_file(output: 'config.h', configuration: config_h)
add_project_arguments(['-I' + meson.project_build_root()], language: 'c')


project_c_args = []
test_c_args = [
  '-Wcast-align',
  '-Wdeclaration-after-statement',
  '-Werror=address',
  '-Werror=array-bounds',
  '-Werror=empty-body',
  '-Werror=implicit',
  '-Werror=implicit-function-declaration',
  '-Werror=incompatible-pointer-types',
  '-Werror=init-self',
  '-Werror=int-conversion',
  '-Werror=int-to-pointer-cast',
  '-Werror=main',
  '-Werror=misleading-indentation',
  '-Werror=missing-braces',
  '-Werror=missing-include-dirs',
  '-Werror=nonnull',
  '-Werror=overflow',
  '-Werror=parenthesis',
  '-Werror=pointer-arith',
  '-Werror=pointer-to-int-cast',
  '-Werror=redundant-decls',
  '-Werror=return-type',
  '-Werror=sequence-point',
  '-Werror=shadow',
  '-Werror=strict-prototypes',
  '-Werror=trigraphs',
  '-Werror=undef',
  '-Werror=write-strings',
  '-Wformat-nonliteral',
  '-Wignored-qualifiers',
  '-Wimplicit-function-declaration',
  '-Wlogical-op',
  '-Wmissing-declarations',
  '-Wmissing-format-attribute',
  '-Wmissing-include-dirs',
  '-Wmissing-noreturn',
  '-Wnested-externs',
  '-Wno-cast-function-type',
  '-Wno-dangling-pointer',
  '-Wno-missing-field-initializers',
  '-Wno-sign-compare',
  '-Wno-unused-parameter',
  '-Wold-style-definition',
  '-Wpointer-arith',
  '-Wredundant-decls',
  '-Wstrict-prototypes',
  '-Wswitch-default',
  '-Wswitch-enum',
  '-Wundef',
  '-Wuninitialized',
  '-Wunused',
  '-fno-strict-aliasing',
  ['-Werror=format-security', '-Werror=format=2'],
]
if get_option('buildtype') != 'plain'
  test_c_args += '-fstack-protector-strong'
endif
foreach arg: test_c_args
  if cc.has_multi_arguments(arg)
    project_c_args += arg
  endif
endforeach
add_project_arguments(project_c_args, language: 'c')

subdir('data')
subdir('src')
subdir('po')

gnome.post_install(
     glib_compile_schemas: true,
    gtk_update_icon_cache: true,
  update_desktop_database: true,
)

The one on the src folder, which I understand compiles the the gresource currently:

gramola_sources = [
  'main.c',
  'gramola-application.c',
  'gramola-window.c',
]

gramola_deps = [
  dependency('gtk4'),
  dependency('libadwaita-1', version: '>= 1.4'),
]

blueprints = custom_target('blueprints',
  input: files(
    'gramola-window.blp',
    'shortcuts-dialog.blp',
  ),
  output: '.',
  command: [find_program('blueprint-compiler'), 'batch-compile', '@OUTPUT@', '@CURRENT_SOURCE_DIR@', '@INPUT@'],
)

gramola_sources += gnome.compile_resources('gramola-resources',
  'gramola.gresource.xml',
  c_name: 'gramola',
  dependencies: blueprints
)

executable('gramola', gramola_sources,
  dependencies: gramola_deps,
       install: true,
)

And then there is the one in data, which is where I want to add the lines of code to compile a new file called data-gresources.xml:

desktop_file = i18n.merge_file(
        input: 'com.camperotactico.Gramola.desktop.in',
       output: 'com.camperotactico.Gramola.desktop',
         type: 'desktop',
       po_dir: '../po',
      install: true,
  install_dir: get_option('datadir') / 'applications'
)

desktop_utils = find_program('desktop-file-validate', required: false)
if desktop_utils.found()
  test('Validate desktop file', desktop_utils, args: [desktop_file])
endif

appstream_file = i18n.merge_file(
        input: 'com.camperotactico.Gramola.metainfo.xml.in',
       output: 'com.camperotactico.Gramola.metainfo.xml',
       po_dir: '../po',
      install: true,
  install_dir: get_option('datadir') / 'metainfo'
)

appstreamcli = find_program('appstreamcli', required: false, disabler: true)
test('Validate appstream file', appstreamcli,
     args: ['validate', '--no-net', '--explain', appstream_file])

install_data('com.camperotactico.Gramola.gschema.xml',
  install_dir: get_option('datadir') / 'glib-2.0' / 'schemas'
)

compile_schemas = find_program('glib-compile-schemas', required: false, disabler: true)
test('Validate schema file',
     compile_schemas,
     args: ['--strict', '--dry-run', meson.current_source_dir()])


service_conf = configuration_data()
service_conf.set('bindir', get_option('prefix') / get_option('bindir'))
configure_file(
  input: 'com.camperotactico.Gramola.service.in',
  output: 'com.camperotactico.Gramola.service',
  configuration: service_conf,
  install_dir: get_option('datadir') / 'dbus-1' / 'services'
)

subdir('icons')

I tried looking at some of the repositories that GNOME Builder features, but they are too different from the default one that I am not able to understand what is happening.