jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

489 lines
15KB

  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2006-2010 (ita)
  4. """
  5. Support for GLib2 tools:
  6. * marshal
  7. * enums
  8. * gsettings
  9. * gresource
  10. """
  11. import os
  12. from waflib import Context, Task, Utils, Options, Errors, Logs
  13. from waflib.TaskGen import taskgen_method, before_method, after_method, feature, extension
  14. from waflib.Configure import conf
  15. ################## marshal files
  16. @taskgen_method
  17. def add_marshal_file(self, filename, prefix):
  18. """
  19. Add a file to the list of marshal files to process. Store them in the attribute *marshal_list*.
  20. :param filename: xml file to compile
  21. :type filename: string
  22. :param prefix: marshal prefix (--prefix=prefix)
  23. :type prefix: string
  24. """
  25. if not hasattr(self, 'marshal_list'):
  26. self.marshal_list = []
  27. self.meths.append('process_marshal')
  28. self.marshal_list.append((filename, prefix))
  29. @before_method('process_source')
  30. def process_marshal(self):
  31. """
  32. Process the marshal files stored in the attribute *marshal_list* to create :py:class:`waflib.Tools.glib2.glib_genmarshal` instances.
  33. Add the c file created to the list of source to process.
  34. """
  35. for f, prefix in getattr(self, 'marshal_list', []):
  36. node = self.path.find_resource(f)
  37. if not node:
  38. raise Errors.WafError('file not found %r' % f)
  39. h_node = node.change_ext('.h')
  40. c_node = node.change_ext('.c')
  41. task = self.create_task('glib_genmarshal', node, [h_node, c_node])
  42. task.env.GLIB_GENMARSHAL_PREFIX = prefix
  43. self.source = self.to_nodes(getattr(self, 'source', []))
  44. self.source.append(c_node)
  45. class glib_genmarshal(Task.Task):
  46. def run(self):
  47. bld = self.inputs[0].__class__.ctx
  48. get = self.env.get_flat
  49. cmd1 = "%s %s --prefix=%s --header > %s" % (
  50. get('GLIB_GENMARSHAL'),
  51. self.inputs[0].srcpath(),
  52. get('GLIB_GENMARSHAL_PREFIX'),
  53. self.outputs[0].abspath()
  54. )
  55. ret = bld.exec_command(cmd1)
  56. if ret: return ret
  57. #print self.outputs[1].abspath()
  58. c = '''#include "%s"\n''' % self.outputs[0].name
  59. self.outputs[1].write(c)
  60. cmd2 = "%s %s --prefix=%s --body >> %s" % (
  61. get('GLIB_GENMARSHAL'),
  62. self.inputs[0].srcpath(),
  63. get('GLIB_GENMARSHAL_PREFIX'),
  64. self.outputs[1].abspath()
  65. )
  66. return bld.exec_command(cmd2)
  67. vars = ['GLIB_GENMARSHAL_PREFIX', 'GLIB_GENMARSHAL']
  68. color = 'BLUE'
  69. ext_out = ['.h']
  70. ########################## glib-mkenums
  71. @taskgen_method
  72. def add_enums_from_template(self, source='', target='', template='', comments=''):
  73. """
  74. Add a file to the list of enum files to process. Store them in the attribute *enums_list*.
  75. :param source: enum file to process
  76. :type source: string
  77. :param target: target file
  78. :type target: string
  79. :param template: template file
  80. :type template: string
  81. :param comments: comments
  82. :type comments: string
  83. """
  84. if not hasattr(self, 'enums_list'):
  85. self.enums_list = []
  86. self.meths.append('process_enums')
  87. self.enums_list.append({'source': source,
  88. 'target': target,
  89. 'template': template,
  90. 'file-head': '',
  91. 'file-prod': '',
  92. 'file-tail': '',
  93. 'enum-prod': '',
  94. 'value-head': '',
  95. 'value-prod': '',
  96. 'value-tail': '',
  97. 'comments': comments})
  98. @taskgen_method
  99. def add_enums(self, source='', target='',
  100. file_head='', file_prod='', file_tail='', enum_prod='',
  101. value_head='', value_prod='', value_tail='', comments=''):
  102. """
  103. Add a file to the list of enum files to process. Store them in the attribute *enums_list*.
  104. :param source: enum file to process
  105. :type source: string
  106. :param target: target file
  107. :type target: string
  108. :param file_head: unused
  109. :param file_prod: unused
  110. :param file_tail: unused
  111. :param enum_prod: unused
  112. :param value_head: unused
  113. :param value_prod: unused
  114. :param value_tail: unused
  115. :param comments: comments
  116. :type comments: string
  117. """
  118. if not hasattr(self, 'enums_list'):
  119. self.enums_list = []
  120. self.meths.append('process_enums')
  121. self.enums_list.append({'source': source,
  122. 'template': '',
  123. 'target': target,
  124. 'file-head': file_head,
  125. 'file-prod': file_prod,
  126. 'file-tail': file_tail,
  127. 'enum-prod': enum_prod,
  128. 'value-head': value_head,
  129. 'value-prod': value_prod,
  130. 'value-tail': value_tail,
  131. 'comments': comments})
  132. @before_method('process_source')
  133. def process_enums(self):
  134. """
  135. Process the enum files stored in the attribute *enum_list* to create :py:class:`waflib.Tools.glib2.glib_mkenums` instances.
  136. """
  137. for enum in getattr(self, 'enums_list', []):
  138. task = self.create_task('glib_mkenums')
  139. env = task.env
  140. inputs = []
  141. # process the source
  142. source_list = self.to_list(enum['source'])
  143. if not source_list:
  144. raise Errors.WafError('missing source ' + str(enum))
  145. source_list = [self.path.find_resource(k) for k in source_list]
  146. inputs += source_list
  147. env['GLIB_MKENUMS_SOURCE'] = [k.abspath() for k in source_list]
  148. # find the target
  149. if not enum['target']:
  150. raise Errors.WafError('missing target ' + str(enum))
  151. tgt_node = self.path.find_or_declare(enum['target'])
  152. if tgt_node.name.endswith('.c'):
  153. self.source.append(tgt_node)
  154. env['GLIB_MKENUMS_TARGET'] = tgt_node.abspath()
  155. options = []
  156. if enum['template']: # template, if provided
  157. template_node = self.path.find_resource(enum['template'])
  158. options.append('--template %s' % (template_node.abspath()))
  159. inputs.append(template_node)
  160. params = {'file-head' : '--fhead',
  161. 'file-prod' : '--fprod',
  162. 'file-tail' : '--ftail',
  163. 'enum-prod' : '--eprod',
  164. 'value-head' : '--vhead',
  165. 'value-prod' : '--vprod',
  166. 'value-tail' : '--vtail',
  167. 'comments': '--comments'}
  168. for param, option in params.items():
  169. if enum[param]:
  170. options.append('%s %r' % (option, enum[param]))
  171. env['GLIB_MKENUMS_OPTIONS'] = ' '.join(options)
  172. # update the task instance
  173. task.set_inputs(inputs)
  174. task.set_outputs(tgt_node)
  175. class glib_mkenums(Task.Task):
  176. """
  177. Process enum files
  178. """
  179. run_str = '${GLIB_MKENUMS} ${GLIB_MKENUMS_OPTIONS} ${GLIB_MKENUMS_SOURCE} > ${GLIB_MKENUMS_TARGET}'
  180. color = 'PINK'
  181. ext_out = ['.h']
  182. ######################################### gsettings
  183. @taskgen_method
  184. def add_settings_schemas(self, filename_list):
  185. """
  186. Add settings files to process, add them to *settings_schema_files*
  187. :param filename_list: files
  188. :type filename_list: list of string
  189. """
  190. if not hasattr(self, 'settings_schema_files'):
  191. self.settings_schema_files = []
  192. if not isinstance(filename_list, list):
  193. filename_list = [filename_list]
  194. self.settings_schema_files.extend(filename_list)
  195. @taskgen_method
  196. def add_settings_enums(self, namespace, filename_list):
  197. """
  198. This function may be called only once by task generator to set the enums namespace.
  199. :param namespace: namespace
  200. :type namespace: string
  201. :param filename_list: enum files to process
  202. :type filename_list: file list
  203. """
  204. if hasattr(self, 'settings_enum_namespace'):
  205. raise Errors.WafError("Tried to add gsettings enums to '%s' more than once" % self.name)
  206. self.settings_enum_namespace = namespace
  207. if type(filename_list) != 'list':
  208. filename_list = [filename_list]
  209. self.settings_enum_files = filename_list
  210. @feature('glib2')
  211. def process_settings(self):
  212. """
  213. Process the schema files in *settings_schema_files* to create :py:class:`waflib.Tools.glib2.glib_mkenums` instances. The
  214. same files are validated through :py:class:`waflib.Tools.glib2.glib_validate_schema` tasks.
  215. """
  216. enums_tgt_node = []
  217. install_files = []
  218. settings_schema_files = getattr(self, 'settings_schema_files', [])
  219. if settings_schema_files and not self.env['GLIB_COMPILE_SCHEMAS']:
  220. raise Errors.WafError ("Unable to process GSettings schemas - glib-compile-schemas was not found during configure")
  221. # 1. process gsettings_enum_files (generate .enums.xml)
  222. #
  223. if hasattr(self, 'settings_enum_files'):
  224. enums_task = self.create_task('glib_mkenums')
  225. source_list = self.settings_enum_files
  226. source_list = [self.path.find_resource(k) for k in source_list]
  227. enums_task.set_inputs(source_list)
  228. enums_task.env['GLIB_MKENUMS_SOURCE'] = [k.abspath() for k in source_list]
  229. target = self.settings_enum_namespace + '.enums.xml'
  230. tgt_node = self.path.find_or_declare(target)
  231. enums_task.set_outputs(tgt_node)
  232. enums_task.env['GLIB_MKENUMS_TARGET'] = tgt_node.abspath()
  233. enums_tgt_node = [tgt_node]
  234. install_files.append (tgt_node)
  235. options = '--comments "<!-- @comment@ -->" --fhead "<schemalist>" --vhead " <@type@ id=\\"%s.@EnumName@\\">" --vprod " <value nick=\\"@valuenick@\\" value=\\"@valuenum@\\"/>" --vtail " </@type@>" --ftail "</schemalist>" ' % (self.settings_enum_namespace)
  236. enums_task.env['GLIB_MKENUMS_OPTIONS'] = options
  237. # 2. process gsettings_schema_files (validate .gschema.xml files)
  238. #
  239. for schema in settings_schema_files:
  240. schema_task = self.create_task ('glib_validate_schema')
  241. schema_node = self.path.find_resource(schema)
  242. if not schema_node:
  243. raise Errors.WafError("Cannot find the schema file '%s'" % schema)
  244. install_files.append(schema_node)
  245. source_list = enums_tgt_node + [schema_node]
  246. schema_task.set_inputs (source_list)
  247. schema_task.env['GLIB_COMPILE_SCHEMAS_OPTIONS'] = [("--schema-file=" + k.abspath()) for k in source_list]
  248. target_node = schema_node.change_ext('.xml.valid')
  249. schema_task.set_outputs (target_node)
  250. schema_task.env['GLIB_VALIDATE_SCHEMA_OUTPUT'] = target_node.abspath()
  251. # 3. schemas install task
  252. def compile_schemas_callback(bld):
  253. if not bld.is_install: return
  254. Logs.pprint ('YELLOW','Updating GSettings schema cache')
  255. command = Utils.subst_vars("${GLIB_COMPILE_SCHEMAS} ${GSETTINGSSCHEMADIR}", bld.env)
  256. ret = self.bld.exec_command(command)
  257. if self.bld.is_install:
  258. if not self.env['GSETTINGSSCHEMADIR']:
  259. raise Errors.WafError ('GSETTINGSSCHEMADIR not defined (should have been set up automatically during configure)')
  260. if install_files:
  261. self.bld.install_files (self.env['GSETTINGSSCHEMADIR'], install_files)
  262. if not hasattr(self.bld, '_compile_schemas_registered'):
  263. self.bld.add_post_fun (compile_schemas_callback)
  264. self.bld._compile_schemas_registered = True
  265. class glib_validate_schema(Task.Task):
  266. """
  267. Validate schema files
  268. """
  269. run_str = 'rm -f ${GLIB_VALIDATE_SCHEMA_OUTPUT} && ${GLIB_COMPILE_SCHEMAS} --dry-run ${GLIB_COMPILE_SCHEMAS_OPTIONS} && touch ${GLIB_VALIDATE_SCHEMA_OUTPUT}'
  270. color = 'PINK'
  271. ################## gresource
  272. @extension('.gresource.xml')
  273. def process_gresource_source(self, node):
  274. """
  275. Hook to process .gresource.xml to generate C source files
  276. """
  277. if not self.env['GLIB_COMPILE_RESOURCES']:
  278. raise Errors.WafError ("Unable to process GResource file - glib-compile-resources was not found during configure")
  279. if 'gresource' in self.features:
  280. return
  281. h_node = node.change_ext('_xml.h')
  282. c_node = node.change_ext('_xml.c')
  283. self.create_task('glib_gresource_source', node, [h_node, c_node])
  284. self.source.append(c_node)
  285. @feature('gresource')
  286. def process_gresource_bundle(self):
  287. """
  288. Generate a binary .gresource files from .gresource.xml files::
  289. def build(bld):
  290. bld(
  291. features='gresource',
  292. source=['resources1.gresource.xml', 'resources2.gresource.xml'],
  293. install_path='${LIBDIR}/${PACKAGE}'
  294. )
  295. :param source: XML files to process
  296. :type source: list of string
  297. :param install_path: installation path
  298. :type install_path: string
  299. """
  300. for i in self.to_list(self.source):
  301. node = self.path.find_resource(i)
  302. task = self.create_task('glib_gresource_bundle', node, node.change_ext(''))
  303. inst_to = getattr(self, 'install_path', None)
  304. if inst_to:
  305. self.bld.install_files(inst_to, task.outputs)
  306. class glib_gresource_base(Task.Task):
  307. """
  308. Base class for gresource based tasks, it implements the implicit dependencies scan.
  309. """
  310. color = 'BLUE'
  311. base_cmd = '${GLIB_COMPILE_RESOURCES} --sourcedir=${SRC[0].parent.srcpath()} --sourcedir=${SRC[0].bld_dir()}'
  312. def scan(self):
  313. """
  314. Scan gresource dependencies through ``glib-compile-resources --generate-dependencies command``
  315. """
  316. bld = self.generator.bld
  317. kw = {}
  318. try:
  319. if not kw.get('cwd', None):
  320. kw['cwd'] = bld.cwd
  321. except AttributeError:
  322. bld.cwd = kw['cwd'] = bld.variant_dir
  323. kw['quiet'] = Context.BOTH
  324. cmd = Utils.subst_vars('${GLIB_COMPILE_RESOURCES} --sourcedir=%s --sourcedir=%s --generate-dependencies %s' % (
  325. self.inputs[0].parent.srcpath(),
  326. self.inputs[0].bld_dir(),
  327. self.inputs[0].bldpath()
  328. ), self.env)
  329. output = bld.cmd_and_log(cmd, **kw)
  330. nodes = []
  331. names = []
  332. for dep in output.splitlines():
  333. if dep:
  334. node = bld.bldnode.find_node(dep)
  335. if node:
  336. nodes.append(node)
  337. else:
  338. names.append(dep)
  339. return (nodes, names)
  340. class glib_gresource_source(glib_gresource_base):
  341. """
  342. Task to generate C source code (.h and .c files) from a gresource.xml file
  343. """
  344. vars = ['GLIB_COMPILE_RESOURCES']
  345. fun_h = Task.compile_fun_shell(glib_gresource_base.base_cmd + ' --target=${TGT[0].abspath()} --generate-header ${SRC}')
  346. fun_c = Task.compile_fun_shell(glib_gresource_base.base_cmd + ' --target=${TGT[1].abspath()} --generate-source ${SRC}')
  347. ext_out = ['.h']
  348. def run(self):
  349. return self.fun_h[0](self) or self.fun_c[0](self)
  350. class glib_gresource_bundle(glib_gresource_base):
  351. """
  352. Task to generate a .gresource binary file from a gresource.xml file
  353. """
  354. run_str = glib_gresource_base.base_cmd + ' --target=${TGT} ${SRC}'
  355. shell = True # temporary workaround for #795
  356. @conf
  357. def find_glib_genmarshal(conf):
  358. conf.find_program('glib-genmarshal', var='GLIB_GENMARSHAL')
  359. @conf
  360. def find_glib_mkenums(conf):
  361. if not conf.env.PERL:
  362. conf.find_program('perl', var='PERL')
  363. conf.find_program('glib-mkenums', interpreter='PERL', var='GLIB_MKENUMS')
  364. @conf
  365. def find_glib_compile_schemas(conf):
  366. # when cross-compiling, gsettings.m4 locates the program with the following:
  367. # pkg-config --variable glib_compile_schemas gio-2.0
  368. conf.find_program('glib-compile-schemas', var='GLIB_COMPILE_SCHEMAS')
  369. def getstr(varname):
  370. return getattr(Options.options, varname, getattr(conf.env,varname, ''))
  371. # TODO make this dependent on the gnu_dirs tool?
  372. gsettingsschemadir = getstr('GSETTINGSSCHEMADIR')
  373. if not gsettingsschemadir:
  374. datadir = getstr('DATADIR')
  375. if not datadir:
  376. prefix = conf.env['PREFIX']
  377. datadir = os.path.join(prefix, 'share')
  378. gsettingsschemadir = os.path.join(datadir, 'glib-2.0', 'schemas')
  379. conf.env['GSETTINGSSCHEMADIR'] = gsettingsschemadir
  380. @conf
  381. def find_glib_compile_resources(conf):
  382. conf.find_program('glib-compile-resources', var='GLIB_COMPILE_RESOURCES')
  383. def configure(conf):
  384. """
  385. Find the following programs:
  386. * *glib-genmarshal* and set *GLIB_GENMARSHAL*
  387. * *glib-mkenums* and set *GLIB_MKENUMS*
  388. * *glib-compile-schemas* and set *GLIB_COMPILE_SCHEMAS* (not mandatory)
  389. * *glib-compile-resources* and set *GLIB_COMPILE_RESOURCES* (not mandatory)
  390. And set the variable *GSETTINGSSCHEMADIR*
  391. """
  392. conf.find_glib_genmarshal()
  393. conf.find_glib_mkenums()
  394. conf.find_glib_compile_schemas(mandatory=False)
  395. conf.find_glib_compile_resources(mandatory=False)
  396. def options(opt):
  397. """
  398. Add the ``--gsettingsschemadir`` command-line option
  399. """
  400. gr = opt.add_option_group('Installation directories')
  401. gr.add_option('--gsettingsschemadir', help='GSettings schema location [DATADIR/glib-2.0/schemas]', default='', dest='GSETTINGSSCHEMADIR')