gtk2/gtk/gen-gtk-gresources-xml.py
Nirbheek Chauhan 777435c470 meson: Reduce useless relinking on reconfigure
When we reconfigure, `configure_file()` is called again, and
`*.gresource.xml` files are regenerated, which causes many (all?)
binaries to be relinked. Now we only write those out if the contents
actually changed (or if the output didn't already exist).

This is exactly what Meson already does with `configure_file()` when
`command:` is not used.

While we're at it, also do the same for `gen-c-array.py` and
`gentypefuncs.py` for completeness. Now even if the input to those
changes, re-building of those custom targets may not result in
relinking if the outputted C files have the same contents.
2020-04-03 18:41:55 +05:30

101 lines
2.9 KiB
Python

#!/usr/bin/env python3
#
# Generate gtk.gresources.xml
#
# Usage: gen-gtk-gresources-xml SRCDIR_GTK [OUTPUT-FILE]
import os, sys
import filecmp
def replace_if_changed(new, old):
'''
Compare contents and only replace if changed to avoid triggering a rebuild.
'''
try:
changed = not filecmp.cmp(new, old, shallow=False)
except FileNotFoundError:
changed = True
if changed:
os.replace(new, old)
else:
os.remove(new)
srcdir = sys.argv[1]
xml = '''<?xml version='1.0' encoding='UTF-8'?>
<gresources>
<gresource prefix='/org/gtk/libgtk'>
'''
def get_files(subdir,extension):
return sorted(filter(lambda x: x.endswith((extension)), os.listdir(os.path.join(srcdir,subdir))))
xml += '''
<file>theme/Empty/gtk.css</file>
<file>theme/Adwaita/gtk.css</file>
<file>theme/Adwaita/gtk-dark.css</file>
<file>theme/Adwaita/Adwaita.css</file>
<file>theme/Adwaita/Adwaita-dark.css</file>
'''
for f in get_files('theme/Adwaita/assets', '.png'):
xml += ' <file>theme/Adwaita/assets/{0}</file>\n'.format(f)
xml += '\n'
for f in get_files('theme/Adwaita/assets', '.svg'):
xml += ' <file>theme/Adwaita/assets/{0}</file>\n'.format(f)
xml += '''
<file>theme/HighContrast/gtk.css</file>
<file alias='theme/HighContrastInverse/gtk.css'>theme/HighContrast/gtk-inverse.css</file>
<file>theme/HighContrast/HighContrast.css</file>
<file>theme/HighContrast/HighContrast-inverse.css</file>
'''
for f in get_files('theme/HighContrast/assets', '.png'):
xml += ' <file>theme/HighContrast/assets/{0}</file>\n'.format(f)
xml += '\n'
for f in get_files('theme/HighContrast/assets', '.svg'):
xml += ' <file>theme/HighContrast/assets/{0}</file>\n'.format(f)
for f in get_files('gesture', '.symbolic.png'):
xml += ' <file alias=\'icons/64x64/actions/{0}\'>gesture/{0}</file>\n'.format(f)
xml += '\n'
for f in get_files('ui', '.ui'):
xml += ' <file preprocess=\'xml-stripblanks\'>ui/{0}</file>\n'.format(f)
xml += '\n'
for s in ['16x16', '24x24', '32x32', '48x48', 'scalable']:
for c in ['actions', 'categories', 'emblems', 'emotes', 'devices', 'mimetypes', 'places', 'status']:
icons_dir = 'icons/{0}/{1}'.format(s,c)
if os.path.exists(os.path.join(srcdir,icons_dir)):
for f in get_files(icons_dir, '.png'):
xml += ' <file>icons/{0}/{1}/{2}</file>\n'.format(s,c,f)
for f in get_files(icons_dir, '.svg'):
xml += ' <file>icons/{0}/{1}/{2}</file>\n'.format(s,c,f)
for f in get_files('inspector', '.ui'):
xml += ' <file preprocess=\'xml-stripblanks\'>inspector/{0}</file>\n'.format(f)
xml += '''
<file>inspector/logo.png</file>
<file>inspector/inspector.css</file>
<file>emoji/emoji.data</file>
</gresource>
</gresources>'''
if len(sys.argv) > 2:
outfile = sys.argv[2]
tmpfile = outfile + '~'
with open(tmpfile, 'w') as f:
f.write(xml)
replace_if_changed(tmpfile, outfile)
else:
print(xml)