jbsrc/lib/src/core/jb-main.c (8449B) - 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 <locale.h>
21 #include <string.h>
22 #include "jb-action.h"
23 #include "jb-feature.h"
24 #include "jb-util.h"
25 #include "jb-variable.h"
26 #include "jb-compile-options.h"
27
28 char *jb_topsrcdir = NULL;
29
30 static gboolean
31 parse_variable (const char *pair, char **name, char **value)
32 {
33 char *p;
34 char *_name = NULL;
35
36 p = strchr(pair, '=');
37 if (! p)
38 return FALSE;
39
40 _name = g_strndup(pair, p - pair);
41 if (*_name == '\0')
42 return FALSE;
43
44 *name = _name;
45 *value = g_strdup(p + 1);
46
47 return TRUE;
48 }
49
50 static void
51 handle_variable (const char *pair)
52 {
53 char *name;
54 char *value;
55 JBVariable *variable;
56 GError *err = NULL;
57
58 if (! parse_variable(pair, &name, &value))
59 jb_error("invalid variable specification \"%s\"", pair);
60
61 variable = jb_variable_get_variable(name);
62 if (variable == NULL || (variable->flags & JB_VARIABLE_USER_SETTABLE) == 0)
63 jb_error("unknown variable \"%s\"", name);
64
65 if (! jb_variable_set_from_string(variable, value, &err))
66 jb_error("invalid value \"%s\" for %s variable \"%s\": %s",
67 value,
68 variable->type->name,
69 name,
70 err->message);
71
72 variable->user_set = TRUE;
73 }
74
75 static GSList *
76 parse_group_args (int argc, char **argv)
77 {
78 GSList *group_names = NULL;
79 int i;
80
81 for (i = 0; i < argc; i++)
82 group_names = g_slist_append(group_names, g_strdup(argv[i]));
83
84 return group_names;
85 }
86
87 static void
88 parse_args (int argc, char **argv)
89 {
90 GSList *group_names;
91 int i = 1;
92
93 if (argc >= 2)
94 {
95 if (! strcmp(argv[1], "help")
96 || ! strcmp(argv[1], "--help")
97 || ! strcmp(argv[1], "-h")
98 || ! strcmp(argv[1], "-?"))
99 {
100 if (argc != 2)
101 jb_error("too many arguments for \"help\" action; run \"./jb help\" for an usage summary");
102
103 jb_action_help();
104 }
105 else if (! strcmp(argv[1], "configure"))
106 {
107 for (i++; i < argc; i++)
108 handle_variable(argv[i]);
109
110 jb_action_configure();
111 }
112 else if (! strcmp(argv[1], "build"))
113 {
114 group_names = parse_group_args(argc - 2, argv + 2);
115 jb_action_build(group_names);
116 }
117 else if (! strcmp(argv[1], "install"))
118 {
119 group_names = parse_group_args(argc - 2, argv + 2);
120 jb_action_install(group_names);
121 }
122 else if (! strcmp(argv[1], "makedist"))
123 {
124 if (argc != 2)
125 jb_error("too many arguments for \"makedist\" action; run \"./jb help\" for an usage summary");
126
127 jb_action_makedist();
128 }
129 else if (! strcmp(argv[1], "clean"))
130 {
131 group_names = parse_group_args(argc - 2, argv + 2);
132 jb_action_clean(group_names);
133 }
134 else if (! strcmp(argv[1], "distclean"))
135 {
136 group_names = parse_group_args(argc - 2, argv + 2);
137 jb_action_distclean(group_names);
138 }
139 else if (! strcmp(argv[1], "maintainerclean"))
140 {
141 group_names = parse_group_args(argc - 2, argv + 2);
142 jb_action_maintainerclean(group_names);
143 }
144 else
145 jb_error("unknown action \"%s\"; run \"./jb help\" for an usage summary", argv[1]);
146 }
147 else
148 jb_error("not enough arguments; run \"./jb help\" for an usage summary");
149 }
150
151 void
152 jb_main (int argc,
153 char **argv,
154 const char *package,
155 const char *version,
156 const char *human_package,
157 const JBFeature **features,
158 int num_features)
159 {
160 setlocale(LC_ALL, "");
161
162 g_log_set_fatal_mask(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL);
163
164 g_type_init();
165
166 jb_topsrcdir = g_get_current_dir();
167
168 jb_set_log_file("build/jb.log");
169
170 jb_variable_init();
171
172 jb_compile_options = jb_compile_options_new("package");
173
174 jb_feature_set_list(features, num_features);
175
176 /* these flags are needed by the JB GOB2 build mechanism */
177 jb_compile_options_add_gob2flags(jb_compile_options, "--always-private-header --no-touch");
178
179 jb_variable_set_string("package", package);
180 jb_variable_set_string("version", version);
181 jb_variable_set_string("human-package", human_package);
182
183 jb_variable_add_string("cc",
184 "C compiler command",
185 jb_variable_group_compiler_options,
186 0,
187 "cc");
188 jb_variable_add_string("cflags",
189 "C compiler flags",
190 jb_variable_group_compiler_options,
191 0,
192 "-O2");
193 jb_variable_add_string("cppflags",
194 "C preprocessor flags",
195 jb_variable_group_compiler_options,
196 0,
197 NULL);
198 jb_variable_add_string("ldflags",
199 "C linker flags",
200 jb_variable_group_compiler_options,
201 0,
202 NULL);
203 jb_variable_add_string("libs",
204 "C -l flags",
205 jb_variable_group_compiler_options,
206 0,
207 NULL);
208 jb_variable_add_bool("cc-dependency-tracking",
209 "enable C compiler dependency tracking",
210 jb_variable_group_compiler_options,
211 0,
212 TRUE);
213
214 jb_variable_add_string("destdir",
215 "destination directory",
216 jb_variable_group_installation_options,
217 0,
218 NULL);
219 jb_variable_add_string("prefix",
220 "installation prefix",
221 jb_variable_group_installation_options,
222 0,
223 "/usr/local");
224
225 jb_variable_add_string("bindir",
226 "user executables directory",
227 jb_variable_group_installation_options,
228 0,
229 "$prefix/bin");
230 jb_variable_add_string("libdir",
231 "shared libraries directory",
232 jb_variable_group_installation_options,
233 0,
234 "$prefix/lib");
235 jb_variable_add_string("libexecdir",
236 "private executables directory",
237 jb_variable_group_installation_options,
238 0,
239 "$prefix/libexec");
240 jb_variable_add_string("datadir",
241 "read-only architecture-independent data directory",
242 jb_variable_group_installation_options,
243 0,
244 "$prefix/share");
245 jb_variable_add_string("sysconfdir",
246 "per-machine configuration directory",
247 jb_variable_group_installation_options,
248 0,
249 "$prefix/etc");
250 jb_variable_add_string("localstatedir",
251 "per-machine state directory",
252 jb_variable_group_installation_options,
253 0,
254 "$prefix/var");
255
256 jb_variable_set_string("pkglibdir", "$libdir/$package");
257 jb_variable_set_string("pkgdatadir", "$datadir/$package");
258 jb_variable_set_string("pkgsysconfdir", "$sysconfdir/$package");
259
260 jb_variable_add_mode("data-mode",
261 "data file permissions (octal)",
262 jb_variable_group_installation_options,
263 0,
264 0644);
265 jb_variable_add_string("data-owner",
266 "data file owner",
267 jb_variable_group_installation_options,
268 0,
269 NULL);
270 jb_variable_add_string("data-group",
271 "data file group",
272 jb_variable_group_installation_options,
273 0,
274 NULL);
275 jb_variable_add_mode("program-mode",
276 "program file permissions (octal)",
277 jb_variable_group_installation_options,
278 0,
279 0755);
280 jb_variable_add_string("program-owner",
281 "program file owner",
282 jb_variable_group_installation_options,
283 0,
284 NULL);
285 jb_variable_add_string("program-group",
286 "program file group",
287 jb_variable_group_installation_options,
288 0,
289 NULL);
290 jb_variable_add_mode("library-mode",
291 "library file permissions (octal)",
292 jb_variable_group_installation_options,
293 0,
294 0644);
295 jb_variable_add_string("library-owner",
296 "library file owner",
297 jb_variable_group_installation_options,
298 0,
299 NULL);
300 jb_variable_add_string("library-group",
301 "library file group",
302 jb_variable_group_installation_options,
303 0,
304 NULL);
305
306 jb_feature_init();
307 jb_package_init();
308
309 parse_args(argc, argv);
310 }
311
312 void
313 jb_set_prefix_from_program (const char *name)
314 {
315 char *program;
316
317 g_return_if_fail(name != NULL);
318
319 program = g_find_program_in_path(name);
320 if (program != NULL)
321 {
322 char *parent;
323 char *prefix;
324
325 parent = g_path_get_dirname(program);
326 g_free(program);
327
328 prefix = g_path_get_dirname(parent);
329 g_free(parent);
330
331 jb_variable_set_string("prefix", prefix);
332 g_free(prefix);
333 }
334 }