jbsrc/lib/src/extras/jb-dbus.c (7496B) - raw
1 /*
2 * JB, the Jean-Yves Lefort's Build System
3 * Copyright (C) 2008 Jean-Yves Lefort <jylefort@brutele.be>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "jb-dbus.h"
21 #include "jb-tests.h"
22 #include "jb-util.h"
23 #include "jb-group.h"
24 #include "jb-action.h"
25
26 static void
27 dbus_init (void)
28 {
29 jb_register_program("dbus-binding-tool", 0);
30 }
31
32 static void
33 dbus_configure (void)
34 {
35 jb_require_program("dbus-binding-tool");
36 }
37
38 JBFeature jb_dbus_feature = {
39 dbus_init,
40 dbus_configure
41 };
42
43 G_DEFINE_TYPE(JBDBusInterfaceClient, jb_dbus_interface_client, JB_TYPE_GROUP_RESOURCE)
44
45 JBDBusInterfaceClient *
46 jb_dbus_interface_client_new (const char *name, const char *client)
47 {
48 JBDBusInterfaceClient *self;
49
50 g_return_val_if_fail(name != NULL, NULL);
51 g_return_val_if_fail(client != NULL, NULL);
52 g_return_val_if_fail(jb_feature_is_enabled(&jb_dbus_feature), NULL);
53
54 self = g_object_new(JB_TYPE_DBUS_INTERFACE_CLIENT, NULL);
55 self->name = g_strdup(name);
56 self->client = g_strdup(client);
57
58 return self;
59 }
60
61 static char *
62 dbus_interface_client_to_string (JBResource *res)
63 {
64 JBDBusInterfaceClient *self = JB_DBUS_INTERFACE_CLIENT(res);
65
66 return g_strdup_printf("client of D-Bus interface %s", self->name);
67 }
68
69 static void
70 dbus_interface_client_get_files (JBDBusInterfaceClient *self,
71 char **xmlfile,
72 char **clientfile)
73 {
74 JBGroupResource *gres = JB_GROUP_RESOURCE(self);
75 char *cprefix;
76
77 cprefix = jb_strdelimit(self->name, ".", '-');
78 if (xmlfile != NULL)
79 *xmlfile = g_strdup_printf("%s/%s.xml", gres->group->srcdir, cprefix);
80 if (clientfile != NULL)
81 *clientfile = g_strdup_printf("%s/%s", gres->group->builddir, self->client);
82 g_free(cprefix);
83 }
84
85 static void
86 dbus_interface_client_pre_build (JBResource *res)
87 {
88 JBDBusInterfaceClient *self = JB_DBUS_INTERFACE_CLIENT(res);
89 char *xmlfile;
90 char *clientfile;
91
92 dbus_interface_client_get_files(self, &xmlfile, &clientfile);
93
94 if (! jb_is_uptodate(clientfile, xmlfile))
95 {
96 JBGroupResource *gres = JB_GROUP_RESOURCE(res);
97
98 jb_resource_message_building(res);
99
100 jb_mkdir(gres->group->builddir);
101
102 jb_action_exec("$dbus-binding-tool --mode=glib-client $xmlfile > $clientfile.tmp && mv -f $clientfile.tmp $clientfile",
103 "xmlfile", xmlfile,
104 "clientfile", clientfile,
105 NULL);
106 }
107
108 g_free(xmlfile);
109 g_free(clientfile);
110 }
111
112 static void
113 dbus_interface_client_makedist (JBResource *res)
114 {
115 JBDBusInterfaceClient *self = JB_DBUS_INTERFACE_CLIENT(res);
116 char *xmlfile;
117
118 dbus_interface_client_get_files(self, &xmlfile, NULL);
119
120 jb_action_add_to_dist(xmlfile);
121
122 g_free(xmlfile);
123 }
124
125 static void
126 dbus_interface_client_clean (JBResource *res)
127 {
128 JBDBusInterfaceClient *self = JB_DBUS_INTERFACE_CLIENT(res);
129 char *clientfile;
130
131 dbus_interface_client_get_files(self, NULL, &clientfile);
132
133 jb_action_rm(clientfile);
134
135 g_free(clientfile);
136 }
137
138 static void
139 jb_dbus_interface_client_init (JBDBusInterfaceClient *self)
140 {
141 }
142
143 static void
144 jb_dbus_interface_client_class_init (JBDBusInterfaceClientClass *class)
145 {
146 JBResourceClass *rclass = JB_RESOURCE_CLASS(class);
147
148 rclass->to_string = dbus_interface_client_to_string;
149 rclass->pre_build = dbus_interface_client_pre_build;
150 rclass->makedist = dbus_interface_client_makedist;
151 rclass->clean = dbus_interface_client_clean;
152 }
153
154 G_DEFINE_TYPE(JBDBusInterfaceServer, jb_dbus_interface_server, JB_TYPE_GROUP_RESOURCE)
155
156 JBDBusInterfaceServer *
157 jb_dbus_interface_server_new (const char *name,
158 const char *server,
159 const char *server_prefix)
160 {
161 JBDBusInterfaceServer *self;
162
163 g_return_val_if_fail(name != NULL, NULL);
164 g_return_val_if_fail(server != NULL, NULL);
165 g_return_val_if_fail(server_prefix != NULL, NULL);
166 g_return_val_if_fail(jb_feature_is_enabled(&jb_dbus_feature), NULL);
167
168 self = g_object_new(JB_TYPE_DBUS_INTERFACE_SERVER, NULL);
169 self->name = g_strdup(name);
170 self->server = g_strdup(server);
171 self->server_prefix = g_strdup(server_prefix);
172
173 return self;
174 }
175
176 static char *
177 dbus_interface_server_to_string (JBResource *res)
178 {
179 JBDBusInterfaceServer *self = JB_DBUS_INTERFACE_SERVER(res);
180
181 return g_strdup_printf("server of D-Bus interface %s", self->name);
182 }
183
184 static void
185 dbus_interface_server_get_files (JBDBusInterfaceServer *self,
186 char **xmlfile,
187 char **serverfile)
188 {
189 JBGroupResource *gres = JB_GROUP_RESOURCE(self);
190 char *cprefix;
191
192 cprefix = jb_strdelimit(self->name, ".", '-');
193 if (xmlfile != NULL)
194 *xmlfile = g_strdup_printf("%s/%s.xml", gres->group->srcdir, cprefix);
195 if (serverfile != NULL)
196 *serverfile = g_strdup_printf("%s/%s", gres->group->builddir, self->server);
197 g_free(cprefix);
198 }
199
200 static void
201 dbus_interface_server_pre_build (JBResource *res)
202 {
203 JBDBusInterfaceServer *self = JB_DBUS_INTERFACE_SERVER(res);
204 char *xmlfile;
205 char *serverfile;
206
207 dbus_interface_server_get_files(self, &xmlfile, &serverfile);
208
209 if (! jb_is_uptodate(serverfile, xmlfile))
210 {
211 JBGroupResource *gres = JB_GROUP_RESOURCE(res);
212
213 jb_resource_message_building(res);
214
215 jb_mkdir(gres->group->builddir);
216
217 jb_action_exec("$dbus-binding-tool --mode=glib-server --prefix=$server-prefix $xmlfile > $serverfile.tmp && mv -f $serverfile.tmp $serverfile",
218 "server-prefix", self->server_prefix,
219 "xmlfile", xmlfile,
220 "serverfile", serverfile,
221 NULL);
222 }
223
224 g_free(xmlfile);
225 g_free(serverfile);
226 }
227
228 static void
229 dbus_interface_server_makedist (JBResource *res)
230 {
231 JBDBusInterfaceServer *self = JB_DBUS_INTERFACE_SERVER(res);
232 char *xmlfile;
233
234 dbus_interface_server_get_files(self, &xmlfile, NULL);
235
236 jb_action_add_to_dist(xmlfile);
237
238 g_free(xmlfile);
239 }
240
241 static void
242 dbus_interface_server_clean (JBResource *res)
243 {
244 JBDBusInterfaceServer *self = JB_DBUS_INTERFACE_SERVER(res);
245 char *serverfile;
246
247 dbus_interface_server_get_files(self, NULL, &serverfile);
248
249 jb_action_rm(serverfile);
250
251 g_free(serverfile);
252 }
253
254 static void
255 jb_dbus_interface_server_init (JBDBusInterfaceServer *self)
256 {
257 }
258
259 static void
260 jb_dbus_interface_server_class_init (JBDBusInterfaceServerClass *class)
261 {
262 JBResourceClass *rclass = JB_RESOURCE_CLASS(class);
263
264 rclass->to_string = dbus_interface_server_to_string;
265 rclass->pre_build = dbus_interface_server_pre_build;
266 rclass->makedist = dbus_interface_server_makedist;
267 rclass->clean = dbus_interface_server_clean;
268 }
269
270 void
271 jb_group_add_dbus_interface (JBGroup *self,
272 const char *name,
273 const char *client,
274 const char *server,
275 const char *server_prefix)
276 {
277 g_return_if_fail(JB_IS_GROUP(self));
278 g_return_if_fail(name != NULL);
279
280 if (client != NULL)
281 jb_group_add_resource(self, JB_GROUP_RESOURCE(jb_dbus_interface_client_new(name, client)));
282 if (server != NULL)
283 jb_group_add_resource(self, JB_GROUP_RESOURCE(jb_dbus_interface_server_new(name, server, server_prefix)));
284 }