Include waf as an extracted source directory, rather than as a one-in-a-file script.
[samba.git] / buildtools / wafadmin / Tools / glib2.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # Thomas Nagy, 2006-2008 (ita)
4
5 "GLib2 support"
6
7 import Task, Utils
8 from TaskGen import taskgen, before, after, feature
9
10 #
11 # glib-genmarshal
12 #
13
14 @taskgen
15 def add_marshal_file(self, filename, prefix):
16         if not hasattr(self, 'marshal_list'):
17                 self.marshal_list = []
18         self.meths.append('process_marshal')
19         self.marshal_list.append((filename, prefix))
20
21 @before('apply_core')
22 def process_marshal(self):
23         for f, prefix in getattr(self, 'marshal_list', []):
24                 node = self.path.find_resource(f)
25
26                 if not node:
27                         raise Utils.WafError('file not found %r' % f)
28
29                 h_node = node.change_ext('.h')
30                 c_node = node.change_ext('.c')
31
32                 task = self.create_task('glib_genmarshal', node, [h_node, c_node])
33                 task.env.GLIB_GENMARSHAL_PREFIX = prefix
34         self.allnodes.append(c_node)
35
36 def genmarshal_func(self):
37
38         bld = self.inputs[0].__class__.bld
39
40         get = self.env.get_flat
41         cmd1 = "%s %s --prefix=%s --header > %s" % (
42                 get('GLIB_GENMARSHAL'),
43                 self.inputs[0].srcpath(self.env),
44                 get('GLIB_GENMARSHAL_PREFIX'),
45                 self.outputs[0].abspath(self.env)
46         )
47
48         ret = bld.exec_command(cmd1)
49         if ret: return ret
50
51         #print self.outputs[1].abspath(self.env)
52         f = open(self.outputs[1].abspath(self.env), 'wb')
53         c = '''#include "%s"\n''' % self.outputs[0].name
54         f.write(c)
55         f.close()
56
57         cmd2 = "%s %s --prefix=%s --body >> %s" % (
58                 get('GLIB_GENMARSHAL'),
59                 self.inputs[0].srcpath(self.env),
60                 get('GLIB_GENMARSHAL_PREFIX'),
61                 self.outputs[1].abspath(self.env)
62         )
63         ret = Utils.exec_command(cmd2)
64         if ret: return ret
65
66 #
67 # glib-mkenums
68 #
69
70 @taskgen
71 def add_enums_from_template(self, source='', target='', template='', comments=''):
72         if not hasattr(self, 'enums_list'):
73                 self.enums_list = []
74         self.meths.append('process_enums')
75         self.enums_list.append({'source': source,
76                                 'target': target,
77                                 'template': template,
78                                 'file-head': '',
79                                 'file-prod': '',
80                                 'file-tail': '',
81                                 'enum-prod': '',
82                                 'value-head': '',
83                                 'value-prod': '',
84                                 'value-tail': '',
85                                 'comments': comments})
86
87 @taskgen
88 def add_enums(self, source='', target='',
89               file_head='', file_prod='', file_tail='', enum_prod='',
90               value_head='', value_prod='', value_tail='', comments=''):
91         if not hasattr(self, 'enums_list'):
92                 self.enums_list = []
93         self.meths.append('process_enums')
94         self.enums_list.append({'source': source,
95                                 'template': '',
96                                 'target': target,
97                                 'file-head': file_head,
98                                 'file-prod': file_prod,
99                                 'file-tail': file_tail,
100                                 'enum-prod': enum_prod,
101                                 'value-head': value_head,
102                                 'value-prod': value_prod,
103                                 'value-tail': value_tail,
104                                 'comments': comments})
105
106 @before('apply_core')
107 def process_enums(self):
108         for enum in getattr(self, 'enums_list', []):
109                 task = self.create_task('glib_mkenums')
110                 env = task.env
111
112                 inputs = []
113
114                 # process the source
115                 source_list = self.to_list(enum['source'])
116                 if not source_list:
117                         raise Utils.WafError('missing source ' + str(enum))
118                 source_list = [self.path.find_resource(k) for k in source_list]
119                 inputs += source_list
120                 env['GLIB_MKENUMS_SOURCE'] = [k.srcpath(env) for k in source_list]
121
122                 # find the target
123                 if not enum['target']:
124                         raise Utils.WafError('missing target ' + str(enum))
125                 tgt_node = self.path.find_or_declare(enum['target'])
126                 if tgt_node.name.endswith('.c'):
127                         self.allnodes.append(tgt_node)
128                 env['GLIB_MKENUMS_TARGET'] = tgt_node.abspath(env)
129
130
131                 options = []
132
133                 if enum['template']: # template, if provided
134                         template_node = self.path.find_resource(enum['template'])
135                         options.append('--template %s' % (template_node.abspath(env)))
136                         inputs.append(template_node)
137                 params = {'file-head' : '--fhead',
138                            'file-prod' : '--fprod',
139                            'file-tail' : '--ftail',
140                            'enum-prod' : '--eprod',
141                            'value-head' : '--vhead',
142                            'value-prod' : '--vprod',
143                            'value-tail' : '--vtail',
144                            'comments': '--comments'}
145                 for param, option in params.iteritems():
146                         if enum[param]:
147                                 options.append('%s %r' % (option, enum[param]))
148
149                 env['GLIB_MKENUMS_OPTIONS'] = ' '.join(options)
150
151                 # update the task instance
152                 task.set_inputs(inputs)
153                 task.set_outputs(tgt_node)
154
155 Task.task_type_from_func('glib_genmarshal', func=genmarshal_func, vars=['GLIB_GENMARSHAL_PREFIX', 'GLIB_GENMARSHAL'],
156         color='BLUE', before='cc cxx')
157 Task.simple_task_type('glib_mkenums',
158         '${GLIB_MKENUMS} ${GLIB_MKENUMS_OPTIONS} ${GLIB_MKENUMS_SOURCE} > ${GLIB_MKENUMS_TARGET}',
159         color='PINK', before='cc cxx')
160
161 def detect(conf):
162         glib_genmarshal = conf.find_program('glib-genmarshal', var='GLIB_GENMARSHAL')
163         mk_enums_tool = conf.find_program('glib-mkenums', var='GLIB_MKENUMS')
164