jbsrc/lib/src/core/jb-group.c (7117B) - 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 <string.h> 21 #include "jb-group.h" 22 #include "jb-feature.h" 23 #include "jb-variable.h" 24 #include "jb-util.h" 25 #include "jb-resource.h" 26 #include "jb-main.h" 27 28 GSList *jb_groups = NULL; 29 30 JBStringHashSet *jb_templates = NULL; 31 32 G_DEFINE_TYPE(JBGroup, jb_group, G_TYPE_OBJECT) 33 34 static void 35 jb_group_init (JBGroup *self) 36 { 37 } 38 39 static void 40 jb_group_class_init (JBGroupClass *class) 41 { 42 } 43 44 JBGroup * 45 jb_group_new (const char *name) 46 { 47 JBGroup *self; 48 char *cppflags; 49 char *gob2flags; 50 51 g_return_val_if_fail(name != NULL, NULL); 52 53 self = g_object_new(JB_TYPE_GROUP, NULL); 54 55 self->name = g_strdup(name); 56 57 self->srcdir = g_strdup(name); 58 self->builddir = g_strdup_printf("build/%s", self->srcdir); 59 60 self->compile_options = jb_compile_options_new(name); 61 62 /* srcdir for the C headers, builddir for the generated C headers */ 63 cppflags = g_strdup_printf("-I%s -I%s", self->srcdir, self->builddir); 64 jb_compile_options_add_cppflags(self->compile_options, cppflags); 65 g_free(cppflags); 66 67 gob2flags = g_strdup_printf("--output-dir=%s", self->builddir); 68 jb_compile_options_add_gob2flags(self->compile_options, gob2flags); 69 g_free(gob2flags); 70 71 return self; 72 } 73 74 void 75 jb_group_add (JBGroup *group) 76 { 77 GSList *l; 78 79 g_return_if_fail(JB_IS_GROUP(group)); 80 81 JB_LIST_FOREACH(l, group->resources) 82 { 83 JBResource *res = l->data; 84 85 if (JB_IS_TEMPLATE(res)) 86 { 87 JBTemplate *template = JB_TEMPLATE(res); 88 char *filename; 89 90 filename = g_strdup_printf("%s/%s", group->srcdir, template->filename); 91 92 if (jb_templates == NULL) 93 jb_templates = jb_string_hash_set_new(); 94 95 if (! jb_string_hash_set_add(jb_templates, filename)) 96 g_error("template file \"%s\" specified multiple times", filename); 97 98 g_free(filename); 99 } 100 } 101 102 jb_groups = g_slist_append(jb_groups, group); 103 } 104 105 JBGroup * 106 jb_group_get (const char *name) 107 { 108 GSList *l; 109 110 g_return_val_if_fail(name != NULL, NULL); 111 112 JB_LIST_FOREACH(l, jb_groups) 113 { 114 JBGroup *group = l->data; 115 116 if (! strcmp(group->name, name)) 117 return group; 118 } 119 120 return NULL; 121 } 122 123 void 124 jb_group_add_resource (JBGroup *self, JBGroupResource *res) 125 { 126 g_return_if_fail(JB_IS_GROUP(self)); 127 g_return_if_fail(JB_IS_GROUP_RESOURCE(res)); 128 g_return_if_fail(res->group == NULL); 129 130 res->group = self; 131 132 self->resources = g_slist_append(self->resources, res); 133 } 134 135 void 136 jb_group_add_data_file (JBGroup *self, 137 const char *file, 138 const char *installdir) 139 { 140 JBDataFile *data_file; 141 142 g_return_if_fail(JB_IS_GROUP(self)); 143 g_return_if_fail(file != NULL); 144 145 data_file = jb_data_file_new(file); 146 147 jb_install_options_set_installdir(data_file->install_options, installdir); 148 149 jb_group_add_resource(self, JB_GROUP_RESOURCE(data_file)); 150 } 151 152 void 153 jb_group_add_data_files (JBGroup *self, const char *file, ...) 154 { 155 va_list args; 156 157 g_return_if_fail(JB_IS_GROUP(self)); 158 159 va_start(args, file); 160 161 while (file != NULL) 162 { 163 const char *installdir; 164 165 installdir = va_arg(args, const char *); 166 g_assert(installdir != NULL); 167 168 jb_group_add_data_file(self, file, installdir); 169 170 file = va_arg(args, const char *); 171 } 172 173 va_end(args); 174 } 175 176 void 177 jb_group_add_data_files_list (JBGroup *self, 178 GSList *files, 179 const char *installdir) 180 { 181 GSList *l; 182 183 g_return_if_fail(JB_IS_GROUP(self)); 184 185 JB_LIST_FOREACH(l, files) 186 { 187 const char *file = l->data; 188 189 jb_group_add_data_file(self, file, installdir); 190 } 191 } 192 193 void 194 jb_group_add_data_files_pattern (JBGroup *self, 195 const char *pattern, 196 const char *installdir) 197 { 198 GSList *files; 199 200 g_return_if_fail(JB_IS_GROUP(self)); 201 g_return_if_fail(pattern != NULL); 202 203 /* 204 * We must chdir to srcdir for matching, because the file list must 205 * be relative to srcdir. 206 */ 207 jb_chdir(self->srcdir); 208 files = jb_match_files(pattern); 209 jb_chdir(jb_topsrcdir); 210 211 jb_group_add_data_files_list(self, files, installdir); 212 jb_g_slist_free_deep(files); 213 } 214 215 void 216 jb_group_add_dist_file (JBGroup *self, const char *file) 217 { 218 g_return_if_fail(JB_IS_GROUP(self)); 219 g_return_if_fail(file != NULL); 220 221 jb_group_add_data_file(self, file, NULL); 222 } 223 224 void 225 jb_group_add_dist_files (JBGroup *self, const char *file, ...) 226 { 227 va_list args; 228 229 g_return_if_fail(JB_IS_GROUP(self)); 230 231 va_start(args, file); 232 233 while (file != NULL) 234 { 235 jb_group_add_dist_file(self, file); 236 237 file = va_arg(args, const char *); 238 } 239 240 va_end(args); 241 } 242 243 static void 244 add_intltool_file (JBGroup *self, 245 const char *type, 246 const char *filename, 247 const char *merge_flags, 248 const char *installdir) 249 { 250 JBIntltoolFile *file; 251 252 file = jb_intltool_file_new(type, filename, merge_flags); 253 254 jb_install_options_set_installdir(file->install_options, installdir); 255 256 jb_group_add_resource(self, JB_GROUP_RESOURCE(file)); 257 } 258 259 void 260 jb_group_add_desktop_file (JBGroup *self, 261 const char *filename, 262 const char *installdir) 263 { 264 g_return_if_fail(JB_IS_GROUP(self)); 265 g_return_if_fail(filename != NULL); 266 g_return_if_fail(installdir != NULL); 267 268 add_intltool_file(self, 269 "desktop file", 270 filename, 271 "-d", 272 installdir); 273 } 274 275 void 276 jb_group_add_gconf_schemas (JBGroup *self, const char *filename) 277 { 278 JBGConfSchemas *schemas; 279 280 g_return_if_fail(JB_IS_GROUP(self)); 281 g_return_if_fail(filename != NULL); 282 g_return_if_fail(jb_feature_is_enabled(&jb_gconf_feature)); 283 g_return_if_fail(jb_feature_is_enabled(&jb_intltool_feature)); 284 g_return_if_fail(jb_intltool_use_xml); 285 286 schemas = jb_gconf_schemas_new(filename); 287 288 jb_group_add_resource(self, JB_GROUP_RESOURCE(schemas)); 289 } 290 291 void 292 jb_group_add_bonobo_server (JBGroup *self, const char *filename) 293 { 294 g_return_if_fail(JB_IS_GROUP(self)); 295 g_return_if_fail(filename != NULL); 296 g_return_if_fail(jb_feature_is_enabled(&jb_intltool_feature)); 297 g_return_if_fail(jb_intltool_use_xml); 298 299 add_intltool_file(self, 300 "Bonobo server", 301 filename, 302 "-b", 303 "$libdir/bonobo/servers"); 304 } 305 306 void 307 jb_group_add_translations (JBGroup *self, const char *languages) 308 { 309 char **array; 310 int i; 311 312 g_return_if_fail(self != NULL); 313 g_return_if_fail(jb_feature_is_enabled(&jb_gettext_feature)); 314 315 array = g_strsplit(languages, " ", 0); 316 for (i = 0; array[i] != NULL; i++) 317 jb_group_add_resource(self, JB_GROUP_RESOURCE(jb_translations_new(array[i]))); 318 g_strfreev(array); 319 }