third_party:waf: update to upstream 2.0.4 release
[vlendec/samba-autobuild/.git] / third_party / waf / waflib / Tools / glib2.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
4
5 #! /usr/bin/env python
6 # encoding: utf-8
7 # Thomas Nagy, 2006-2018 (ita)
8
9 """
10 Support for GLib2 tools:
11
12 * marshal
13 * enums
14 * gsettings
15 * gresource
16 """
17
18 import os
19 import functools
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
23
24 ################## marshal files
25
26 @taskgen_method
27 def add_marshal_file(self, filename, prefix):
28         """
29         Adds a file to the list of marshal files to process. Store them in the attribute *marshal_list*.
30
31         :param filename: xml file to compile
32         :type filename: string
33         :param prefix: marshal prefix (--prefix=prefix)
34         :type prefix: string
35         """
36         if not hasattr(self, 'marshal_list'):
37                 self.marshal_list = []
38         self.meths.append('process_marshal')
39         self.marshal_list.append((filename, prefix))
40
41 @before_method('process_source')
42 def process_marshal(self):
43         """
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.
46         """
47         for f, prefix in getattr(self, 'marshal_list', []):
48                 node = self.path.find_resource(f)
49
50                 if not node:
51                         raise Errors.WafError('file not found %r' % f)
52
53                 h_node = node.change_ext('.h')
54                 c_node = node.change_ext('.c')
55
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)
60
61 class glib_genmarshal(Task.Task):
62         vars    = ['GLIB_GENMARSHAL_PREFIX', 'GLIB_GENMARSHAL']
63         color   = 'BLUE'
64         ext_out = ['.h']
65         def run(self):
66                 bld = self.generator.bld
67
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()
74                 )
75
76                 ret = bld.exec_command(cmd1)
77                 if ret:
78                         return ret
79
80                 #print self.outputs[1].abspath()
81                 c = '''#include "%s"\n''' % self.outputs[0].name
82                 self.outputs[1].write(c)
83
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()
89                 )
90                 return bld.exec_command(cmd2)
91
92 ########################## glib-mkenums
93
94 @taskgen_method
95 def add_enums_from_template(self, source='', target='', template='', comments=''):
96         """
97         Adds a file to the list of enum files to process. Stores them in the attribute *enums_list*.
98
99         :param source: enum file to process
100         :type source: string
101         :param target: target file
102         :type target: string
103         :param template: template file
104         :type template: string
105         :param comments: comments
106         :type comments: string
107         """
108         if not hasattr(self, 'enums_list'):
109                 self.enums_list = []
110         self.meths.append('process_enums')
111         self.enums_list.append({'source': source,
112                                 'target': target,
113                                 'template': template,
114                                 'file-head': '',
115                                 'file-prod': '',
116                                 'file-tail': '',
117                                 'enum-prod': '',
118                                 'value-head': '',
119                                 'value-prod': '',
120                                 'value-tail': '',
121                                 'comments': comments})
122
123 @taskgen_method
124 def add_enums(self, source='', target='',
125               file_head='', file_prod='', file_tail='', enum_prod='',
126               value_head='', value_prod='', value_tail='', comments=''):
127         """
128         Adds a file to the list of enum files to process. Stores them in the attribute *enums_list*.
129
130         :param source: enum file to process
131         :type source: string
132         :param target: target file
133         :type target: string
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
143         """
144         if not hasattr(self, 'enums_list'):
145                 self.enums_list = []
146         self.meths.append('process_enums')
147         self.enums_list.append({'source': source,
148                                 'template': '',
149                                 'target': target,
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})
158
159 @before_method('process_source')
160 def process_enums(self):
161         """
162         Processes the enum files stored in the attribute *enum_list* to create :py:class:`waflib.Tools.glib2.glib_mkenums` instances.
163         """
164         for enum in getattr(self, 'enums_list', []):
165                 task = self.create_task('glib_mkenums')
166                 env = task.env
167
168                 inputs = []
169
170                 # process the source
171                 source_list = self.to_list(enum['source'])
172                 if not source_list:
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]
177
178                 # find the target
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()
185
186
187                 options = []
188
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():
202                         if enum[param]:
203                                 options.append('%s %r' % (option, enum[param]))
204
205                 env.GLIB_MKENUMS_OPTIONS = ' '.join(options)
206
207                 # update the task instance
208                 task.set_inputs(inputs)
209                 task.set_outputs(tgt_node)
210
211 class glib_mkenums(Task.Task):
212         """
213         Processes enum files
214         """
215         run_str = '${GLIB_MKENUMS} ${GLIB_MKENUMS_OPTIONS} ${GLIB_MKENUMS_SOURCE} > ${GLIB_MKENUMS_TARGET}'
216         color   = 'PINK'
217         ext_out = ['.h']
218
219 ######################################### gsettings
220
221 @taskgen_method
222 def add_settings_schemas(self, filename_list):
223         """
224         Adds settings files to process to *settings_schema_files*
225
226         :param filename_list: files
227         :type filename_list: list of string
228         """
229         if not hasattr(self, 'settings_schema_files'):
230                 self.settings_schema_files = []
231
232         if not isinstance(filename_list, list):
233                 filename_list = [filename_list]
234
235         self.settings_schema_files.extend(filename_list)
236
237 @taskgen_method
238 def add_settings_enums(self, namespace, filename_list):
239         """
240         Called only once by task generator to set the enums namespace.
241
242         :param namespace: namespace
243         :type namespace: string
244         :param filename_list: enum files to process
245         :type filename_list: file list
246         """
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
250
251         if not isinstance(filename_list, list):
252                 filename_list = [filename_list]
253         self.settings_enum_files = filename_list
254
255 @feature('glib2')
256 def process_settings(self):
257         """
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.
260
261         """
262         enums_tgt_node = []
263         install_files = []
264
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")
268
269         # 1. process gsettings_enum_files (generate .enums.xml)
270         #
271         if hasattr(self, 'settings_enum_files'):
272                 enums_task = self.create_task('glib_mkenums')
273
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]
278
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]
284
285                 install_files.append(tgt_node)
286
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
289
290         # 2. process gsettings_schema_files (validate .gschema.xml files)
291         #
292         for schema in settings_schema_files:
293                 schema_task = self.create_task ('glib_validate_schema')
294
295                 schema_node = self.path.find_resource(schema)
296                 if not schema_node:
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]
300
301                 schema_task.set_inputs (source_list)
302                 schema_task.env.GLIB_COMPILE_SCHEMAS_OPTIONS = [("--schema-file=" + k.abspath()) for k in source_list]
303
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()
307
308         # 3. schemas install task
309         def compile_schemas_callback(bld):
310                 if not bld.is_install:
311                         return
312                 compile_schemas = Utils.to_list(bld.env.GLIB_COMPILE_SCHEMAS)
313                 destdir = Options.options.destdir
314                 paths = bld._compile_schemas_registered
315                 if destdir:
316                         paths = (os.path.join(destdir, path.lstrip(os.sep)) for path in paths)
317                 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)
321
322         if self.bld.is_install:
323                 schemadir = self.env.GSETTINGSSCHEMADIR
324                 if not schemadir:
325                         raise Errors.WafError ('GSETTINGSSCHEMADIR not defined (should have been set up automatically during configure)')
326
327                 if install_files:
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)
334
335 class glib_validate_schema(Task.Task):
336         """
337         Validates schema files
338         """
339         run_str = 'rm -f ${GLIB_VALIDATE_SCHEMA_OUTPUT} && ${GLIB_COMPILE_SCHEMAS} --dry-run ${GLIB_COMPILE_SCHEMAS_OPTIONS} && touch ${GLIB_VALIDATE_SCHEMA_OUTPUT}'
340         color   = 'PINK'
341
342 ################## gresource
343
344 @extension('.gresource.xml')
345 def process_gresource_source(self, node):
346         """
347         Creates tasks that turn ``.gresource.xml`` files to C code
348         """
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")
351
352         if 'gresource' in self.features:
353                 return
354
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)
359
360 @feature('gresource')
361 def process_gresource_bundle(self):
362         """
363         Creates tasks to turn ``.gresource`` files from ``.gresource.xml`` files::
364
365                 def build(bld):
366                         bld(
367                                 features='gresource',
368                                 source=['resources1.gresource.xml', 'resources2.gresource.xml'],
369                                 install_path='${LIBDIR}/${PACKAGE}'
370                         )
371
372         :param source: XML files to process
373         :type source: list of string
374         :param install_path: installation path
375         :type install_path: string
376         """
377         for i in self.to_list(self.source):
378                 node = self.path.find_resource(i)
379
380                 task = self.create_task('glib_gresource_bundle', node, node.change_ext(''))
381                 inst_to = getattr(self, 'install_path', None)
382                 if inst_to:
383                         self.add_install_files(install_to=inst_to, install_from=task.outputs)
384
385 class glib_gresource_base(Task.Task):
386         """
387         Base class for gresource based tasks
388         """
389         color    = 'BLUE'
390         base_cmd = '${GLIB_COMPILE_RESOURCES} --sourcedir=${SRC[0].parent.srcpath()} --sourcedir=${SRC[0].bld_dir()}'
391
392         def scan(self):
393                 """
394                 Scans gresource dependencies through ``glib-compile-resources --generate-dependencies command``
395                 """
396                 bld = self.generator.bld
397                 kw = {}
398                 kw['cwd'] = self.get_cwd()
399                 kw['quiet'] = Context.BOTH
400
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()
405                 ), self.env)
406
407                 output = bld.cmd_and_log(cmd, **kw)
408
409                 nodes = []
410                 names = []
411                 for dep in output.splitlines():
412                         if dep:
413                                 node = bld.bldnode.find_node(dep)
414                                 if node:
415                                         nodes.append(node)
416                                 else:
417                                         names.append(dep)
418
419                 return (nodes, names)
420
421 class glib_gresource_source(glib_gresource_base):
422         """
423         Task to generate C source code (.h and .c files) from a gresource.xml file
424         """
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}')
428         ext_out = ['.h']
429
430         def run(self):
431                 return self.fun_h[0](self) or self.fun_c[0](self)
432
433 class glib_gresource_bundle(glib_gresource_base):
434         """
435         Task to generate a .gresource binary file from a gresource.xml file
436         """
437         run_str = glib_gresource_base.base_cmd + ' --target=${TGT} ${SRC}'
438         shell   = True # temporary workaround for #795
439
440 @conf
441 def find_glib_genmarshal(conf):
442         conf.find_program('glib-genmarshal', var='GLIB_GENMARSHAL')
443
444 @conf
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')
449
450 @conf
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')
455
456         def getstr(varname):
457                 return getattr(Options.options, varname, getattr(conf.env,varname, ''))
458
459         gsettingsschemadir = getstr('GSETTINGSSCHEMADIR')
460         if not gsettingsschemadir:
461                 datadir = getstr('DATADIR')
462                 if not datadir:
463                         prefix = conf.env.PREFIX
464                         datadir = os.path.join(prefix, 'share')
465                 gsettingsschemadir = os.path.join(datadir, 'glib-2.0', 'schemas')
466
467         conf.env.GSETTINGSSCHEMADIR = gsettingsschemadir
468
469 @conf
470 def find_glib_compile_resources(conf):
471         conf.find_program('glib-compile-resources', var='GLIB_COMPILE_RESOURCES')
472
473 def configure(conf):
474         """
475         Finds the following programs:
476
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)
481         """
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)
486
487 def options(opt):
488         """
489         Adds the ``--gsettingsschemadir`` command-line option
490         """
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')
493