3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
7 # Thomas Nagy, 2006-2018 (ita)
10 Support for GLib2 tools:
20 from waflib import Context, Task, Utils, Options, Errors, Logs
21 from waflib.TaskGen import taskgen_method, before_method, feature, extension
22 from waflib.Configure import conf
24 ################## marshal files
27 def add_marshal_file(self, filename, prefix):
29 Adds a file to the list of marshal files to process. Store them in the attribute *marshal_list*.
31 :param filename: xml file to compile
32 :type filename: string
33 :param prefix: marshal prefix (--prefix=prefix)
36 if not hasattr(self, 'marshal_list'):
37 self.marshal_list = []
38 self.meths.append('process_marshal')
39 self.marshal_list.append((filename, prefix))
41 @before_method('process_source')
42 def process_marshal(self):
44 Processes the marshal files stored in the attribute *marshal_list* to create :py:class:`waflib.Tools.glib2.glib_genmarshal` instances.
45 Adds the c file created to the list of source to process.
47 for f, prefix in getattr(self, 'marshal_list', []):
48 node = self.path.find_resource(f)
51 raise Errors.WafError('file not found %r' % f)
53 h_node = node.change_ext('.h')
54 c_node = node.change_ext('.c')
56 task = self.create_task('glib_genmarshal', node, [h_node, c_node])
57 task.env.GLIB_GENMARSHAL_PREFIX = prefix
58 self.source = self.to_nodes(getattr(self, 'source', []))
59 self.source.append(c_node)
61 class glib_genmarshal(Task.Task):
62 vars = ['GLIB_GENMARSHAL_PREFIX', 'GLIB_GENMARSHAL']
66 bld = self.generator.bld
68 get = self.env.get_flat
69 cmd1 = "%s %s --prefix=%s --header > %s" % (
70 get('GLIB_GENMARSHAL'),
71 self.inputs[0].srcpath(),
72 get('GLIB_GENMARSHAL_PREFIX'),
73 self.outputs[0].abspath()
76 ret = bld.exec_command(cmd1)
80 #print self.outputs[1].abspath()
81 c = '''#include "%s"\n''' % self.outputs[0].name
82 self.outputs[1].write(c)
84 cmd2 = "%s %s --prefix=%s --body >> %s" % (
85 get('GLIB_GENMARSHAL'),
86 self.inputs[0].srcpath(),
87 get('GLIB_GENMARSHAL_PREFIX'),
88 self.outputs[1].abspath()
90 return bld.exec_command(cmd2)
92 ########################## glib-mkenums
95 def add_enums_from_template(self, source='', target='', template='', comments=''):
97 Adds a file to the list of enum files to process. Stores them in the attribute *enums_list*.
99 :param source: enum file to process
101 :param target: target file
103 :param template: template file
104 :type template: string
105 :param comments: comments
106 :type comments: string
108 if not hasattr(self, 'enums_list'):
110 self.meths.append('process_enums')
111 self.enums_list.append({'source': source,
113 'template': template,
121 'comments': comments})
124 def add_enums(self, source='', target='',
125 file_head='', file_prod='', file_tail='', enum_prod='',
126 value_head='', value_prod='', value_tail='', comments=''):
128 Adds a file to the list of enum files to process. Stores them in the attribute *enums_list*.
130 :param source: enum file to process
132 :param target: target file
134 :param file_head: unused
135 :param file_prod: unused
136 :param file_tail: unused
137 :param enum_prod: unused
138 :param value_head: unused
139 :param value_prod: unused
140 :param value_tail: unused
141 :param comments: comments
142 :type comments: string
144 if not hasattr(self, 'enums_list'):
146 self.meths.append('process_enums')
147 self.enums_list.append({'source': source,
150 'file-head': file_head,
151 'file-prod': file_prod,
152 'file-tail': file_tail,
153 'enum-prod': enum_prod,
154 'value-head': value_head,
155 'value-prod': value_prod,
156 'value-tail': value_tail,
157 'comments': comments})
159 @before_method('process_source')
160 def process_enums(self):
162 Processes the enum files stored in the attribute *enum_list* to create :py:class:`waflib.Tools.glib2.glib_mkenums` instances.
164 for enum in getattr(self, 'enums_list', []):
165 task = self.create_task('glib_mkenums')
171 source_list = self.to_list(enum['source'])
173 raise Errors.WafError('missing source ' + str(enum))
174 source_list = [self.path.find_resource(k) for k in source_list]
175 inputs += source_list
176 env.GLIB_MKENUMS_SOURCE = [k.abspath() for k in source_list]
179 if not enum['target']:
180 raise Errors.WafError('missing target ' + str(enum))
181 tgt_node = self.path.find_or_declare(enum['target'])
182 if tgt_node.name.endswith('.c'):
183 self.source.append(tgt_node)
184 env.GLIB_MKENUMS_TARGET = tgt_node.abspath()
189 if enum['template']: # template, if provided
190 template_node = self.path.find_resource(enum['template'])
191 options.append('--template %s' % (template_node.abspath()))
192 inputs.append(template_node)
193 params = {'file-head' : '--fhead',
194 'file-prod' : '--fprod',
195 'file-tail' : '--ftail',
196 'enum-prod' : '--eprod',
197 'value-head' : '--vhead',
198 'value-prod' : '--vprod',
199 'value-tail' : '--vtail',
200 'comments': '--comments'}
201 for param, option in params.items():
203 options.append('%s %r' % (option, enum[param]))
205 env.GLIB_MKENUMS_OPTIONS = ' '.join(options)
207 # update the task instance
208 task.set_inputs(inputs)
209 task.set_outputs(tgt_node)
211 class glib_mkenums(Task.Task):
215 run_str = '${GLIB_MKENUMS} ${GLIB_MKENUMS_OPTIONS} ${GLIB_MKENUMS_SOURCE} > ${GLIB_MKENUMS_TARGET}'
219 ######################################### gsettings
222 def add_settings_schemas(self, filename_list):
224 Adds settings files to process to *settings_schema_files*
226 :param filename_list: files
227 :type filename_list: list of string
229 if not hasattr(self, 'settings_schema_files'):
230 self.settings_schema_files = []
232 if not isinstance(filename_list, list):
233 filename_list = [filename_list]
235 self.settings_schema_files.extend(filename_list)
238 def add_settings_enums(self, namespace, filename_list):
240 Called only once by task generator to set the enums namespace.
242 :param namespace: namespace
243 :type namespace: string
244 :param filename_list: enum files to process
245 :type filename_list: file list
247 if hasattr(self, 'settings_enum_namespace'):
248 raise Errors.WafError("Tried to add gsettings enums to %r more than once" % self.name)
249 self.settings_enum_namespace = namespace
251 if not isinstance(filename_list, list):
252 filename_list = [filename_list]
253 self.settings_enum_files = filename_list
256 def process_settings(self):
258 Processes the schema files in *settings_schema_files* to create :py:class:`waflib.Tools.glib2.glib_mkenums` instances. The
259 same files are validated through :py:class:`waflib.Tools.glib2.glib_validate_schema` tasks.
265 settings_schema_files = getattr(self, 'settings_schema_files', [])
266 if settings_schema_files and not self.env.GLIB_COMPILE_SCHEMAS:
267 raise Errors.WafError ("Unable to process GSettings schemas - glib-compile-schemas was not found during configure")
269 # 1. process gsettings_enum_files (generate .enums.xml)
271 if hasattr(self, 'settings_enum_files'):
272 enums_task = self.create_task('glib_mkenums')
274 source_list = self.settings_enum_files
275 source_list = [self.path.find_resource(k) for k in source_list]
276 enums_task.set_inputs(source_list)
277 enums_task.env.GLIB_MKENUMS_SOURCE = [k.abspath() for k in source_list]
279 target = self.settings_enum_namespace + '.enums.xml'
280 tgt_node = self.path.find_or_declare(target)
281 enums_task.set_outputs(tgt_node)
282 enums_task.env.GLIB_MKENUMS_TARGET = tgt_node.abspath()
283 enums_tgt_node = [tgt_node]
285 install_files.append(tgt_node)
287 options = '--comments "<!-- @comment@ -->" --fhead "<schemalist>" --vhead " <@type@ id=\\"%s.@EnumName@\\">" --vprod " <value nick=\\"@valuenick@\\" value=\\"@valuenum@\\"/>" --vtail " </@type@>" --ftail "</schemalist>" ' % (self.settings_enum_namespace)
288 enums_task.env.GLIB_MKENUMS_OPTIONS = options
290 # 2. process gsettings_schema_files (validate .gschema.xml files)
292 for schema in settings_schema_files:
293 schema_task = self.create_task ('glib_validate_schema')
295 schema_node = self.path.find_resource(schema)
297 raise Errors.WafError("Cannot find the schema file %r" % schema)
298 install_files.append(schema_node)
299 source_list = enums_tgt_node + [schema_node]
301 schema_task.set_inputs (source_list)
302 schema_task.env.GLIB_COMPILE_SCHEMAS_OPTIONS = [("--schema-file=" + k.abspath()) for k in source_list]
304 target_node = schema_node.change_ext('.xml.valid')
305 schema_task.set_outputs (target_node)
306 schema_task.env.GLIB_VALIDATE_SCHEMA_OUTPUT = target_node.abspath()
308 # 3. schemas install task
309 def compile_schemas_callback(bld):
310 if not bld.is_install:
312 compile_schemas = Utils.to_list(bld.env.GLIB_COMPILE_SCHEMAS)
313 destdir = Options.options.destdir
314 paths = bld._compile_schemas_registered
316 paths = (os.path.join(destdir, path.lstrip(os.sep)) for path in paths)
318 Logs.pprint('YELLOW', 'Updating GSettings schema cache %r' % path)
319 if self.bld.exec_command(compile_schemas + [path]):
320 Logs.warn('Could not update GSettings schema cache %r' % path)
322 if self.bld.is_install:
323 schemadir = self.env.GSETTINGSSCHEMADIR
325 raise Errors.WafError ('GSETTINGSSCHEMADIR not defined (should have been set up automatically during configure)')
328 self.add_install_files(install_to=schemadir, install_from=install_files)
329 registered_schemas = getattr(self.bld, '_compile_schemas_registered', None)
330 if not registered_schemas:
331 registered_schemas = self.bld._compile_schemas_registered = set()
332 self.bld.add_post_fun(compile_schemas_callback)
333 registered_schemas.add(schemadir)
335 class glib_validate_schema(Task.Task):
337 Validates schema files
339 run_str = 'rm -f ${GLIB_VALIDATE_SCHEMA_OUTPUT} && ${GLIB_COMPILE_SCHEMAS} --dry-run ${GLIB_COMPILE_SCHEMAS_OPTIONS} && touch ${GLIB_VALIDATE_SCHEMA_OUTPUT}'
342 ################## gresource
344 @extension('.gresource.xml')
345 def process_gresource_source(self, node):
347 Creates tasks that turn ``.gresource.xml`` files to C code
349 if not self.env.GLIB_COMPILE_RESOURCES:
350 raise Errors.WafError ("Unable to process GResource file - glib-compile-resources was not found during configure")
352 if 'gresource' in self.features:
355 h_node = node.change_ext('_xml.h')
356 c_node = node.change_ext('_xml.c')
357 self.create_task('glib_gresource_source', node, [h_node, c_node])
358 self.source.append(c_node)
360 @feature('gresource')
361 def process_gresource_bundle(self):
363 Creates tasks to turn ``.gresource`` files from ``.gresource.xml`` files::
367 features='gresource',
368 source=['resources1.gresource.xml', 'resources2.gresource.xml'],
369 install_path='${LIBDIR}/${PACKAGE}'
372 :param source: XML files to process
373 :type source: list of string
374 :param install_path: installation path
375 :type install_path: string
377 for i in self.to_list(self.source):
378 node = self.path.find_resource(i)
380 task = self.create_task('glib_gresource_bundle', node, node.change_ext(''))
381 inst_to = getattr(self, 'install_path', None)
383 self.add_install_files(install_to=inst_to, install_from=task.outputs)
385 class glib_gresource_base(Task.Task):
387 Base class for gresource based tasks
390 base_cmd = '${GLIB_COMPILE_RESOURCES} --sourcedir=${SRC[0].parent.srcpath()} --sourcedir=${SRC[0].bld_dir()}'
394 Scans gresource dependencies through ``glib-compile-resources --generate-dependencies command``
396 bld = self.generator.bld
398 kw['cwd'] = self.get_cwd()
399 kw['quiet'] = Context.BOTH
401 cmd = Utils.subst_vars('${GLIB_COMPILE_RESOURCES} --sourcedir=%s --sourcedir=%s --generate-dependencies %s' % (
402 self.inputs[0].parent.srcpath(),
403 self.inputs[0].bld_dir(),
404 self.inputs[0].bldpath()
407 output = bld.cmd_and_log(cmd, **kw)
411 for dep in output.splitlines():
413 node = bld.bldnode.find_node(dep)
419 return (nodes, names)
421 class glib_gresource_source(glib_gresource_base):
423 Task to generate C source code (.h and .c files) from a gresource.xml file
425 vars = ['GLIB_COMPILE_RESOURCES']
426 fun_h = Task.compile_fun_shell(glib_gresource_base.base_cmd + ' --target=${TGT[0].abspath()} --generate-header ${SRC}')
427 fun_c = Task.compile_fun_shell(glib_gresource_base.base_cmd + ' --target=${TGT[1].abspath()} --generate-source ${SRC}')
431 return self.fun_h[0](self) or self.fun_c[0](self)
433 class glib_gresource_bundle(glib_gresource_base):
435 Task to generate a .gresource binary file from a gresource.xml file
437 run_str = glib_gresource_base.base_cmd + ' --target=${TGT} ${SRC}'
438 shell = True # temporary workaround for #795
441 def find_glib_genmarshal(conf):
442 conf.find_program('glib-genmarshal', var='GLIB_GENMARSHAL')
445 def find_glib_mkenums(conf):
446 if not conf.env.PERL:
447 conf.find_program('perl', var='PERL')
448 conf.find_program('glib-mkenums', interpreter='PERL', var='GLIB_MKENUMS')
451 def find_glib_compile_schemas(conf):
452 # when cross-compiling, gsettings.m4 locates the program with the following:
453 # pkg-config --variable glib_compile_schemas gio-2.0
454 conf.find_program('glib-compile-schemas', var='GLIB_COMPILE_SCHEMAS')
457 return getattr(Options.options, varname, getattr(conf.env,varname, ''))
459 gsettingsschemadir = getstr('GSETTINGSSCHEMADIR')
460 if not gsettingsschemadir:
461 datadir = getstr('DATADIR')
463 prefix = conf.env.PREFIX
464 datadir = os.path.join(prefix, 'share')
465 gsettingsschemadir = os.path.join(datadir, 'glib-2.0', 'schemas')
467 conf.env.GSETTINGSSCHEMADIR = gsettingsschemadir
470 def find_glib_compile_resources(conf):
471 conf.find_program('glib-compile-resources', var='GLIB_COMPILE_RESOURCES')
475 Finds the following programs:
477 * *glib-genmarshal* and set *GLIB_GENMARSHAL*
478 * *glib-mkenums* and set *GLIB_MKENUMS*
479 * *glib-compile-schemas* and set *GLIB_COMPILE_SCHEMAS* (not mandatory)
480 * *glib-compile-resources* and set *GLIB_COMPILE_RESOURCES* (not mandatory)
482 conf.find_glib_genmarshal()
483 conf.find_glib_mkenums()
484 conf.find_glib_compile_schemas(mandatory=False)
485 conf.find_glib_compile_resources(mandatory=False)
489 Adds the ``--gsettingsschemadir`` command-line option
491 gr = opt.add_option_group('Installation directories')
492 gr.add_option('--gsettingsschemadir', help='GSettings schema location [DATADIR/glib-2.0/schemas]', default='', dest='GSETTINGSSCHEMADIR')