-
Notifications
You must be signed in to change notification settings - Fork 1
/
target.c
392 lines (350 loc) · 9.02 KB
/
target.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "target.h"
#include "utlist.h"
#include "logging.h"
#define SF(x) if (x) free(x); x = NULL
#define TARGET_RESOLVED_MASK 0x20
static module_entry *all_modules = NULL;
static module_entry *current_module = NULL;
static target_lookup_entry *all_targets_hash = NULL;
extern int module_count;
extern int program_count;
extern int library_count;
static depend_list_entry* depend_list_entry_new(const char *s)
{
depend_list_entry *e = malloc(sizeof(depend_list_entry));
memset(e, 0, sizeof(depend_list_entry));
e->str = strdup(s);
e->ptr = NULL;
return e;
}
static void depend_list_entry_free(depend_list_entry *e)
{
SF(e->str);
e->ptr = NULL;
free(e);
}
void target_add_dependency(target_entry *e, const char *s)
{
depend_list_entry *de = depend_list_entry_new(s);
LL_APPEND(e->dependencies, de);
}
module_entry* module_find_by(const char *name)
{
module_entry *me = NULL, *found = NULL;
LL_FOREACH(all_modules, me)
{
if (strcmp(name, me->dir_name) == 0)
{
found = me;
break;
}
}
return found;
}
int module_check_name_unique(void)
{
int num_clashes = 0;
module_entry *outer= NULL, *inner = NULL;
/* FIXME: this warns twice about each name clash - once might be enough */
LL_FOREACH(all_modules, outer)
{
LL_FOREACH(all_modules, inner)
{
if (outer == inner)
continue;
if (strcmp(outer->dir_name, inner->dir_name) == 0)
{
++num_clashes;
fprintf(stderr, "Warning: directory name clash '%s' for rules in\n"
" * %s\n * %s\n", outer->dir_name,
outer->directory, inner->directory);
}
}
}
return num_clashes;
}
module_entry* module_entry_new(const char *name, char *directory, char *pft)
{
module_entry *e = malloc(sizeof(module_entry));
memset(e, 0, sizeof(module_entry));
e->dir_name = strdup(name);
e->directory = directory;
e->path_from_top = strdup(pft);
LL_APPEND(all_modules, e);
current_module = e;
++module_count;
return e;
}
void module_set_skip_condition(char *s)
{
current_module->skip_condition = s;
}
target_entry* target_entry_new(target_type t, const char *name)
{
target_entry *e = malloc(sizeof(target_entry));
memset(e, 0, sizeof(target_entry));
e->type = t;
e->target_name = strdup(name);
switch(e->type)
{
case TYPE_PROGRAM:
++program_count;
break;
case TYPE_LIBRARY:
++library_count;
break;
case TYPE_WORKER:
break;
default:
break;
}
return e;
}
static void target_entry_free(target_entry *e)
{
SF(e->target_name);
e->dir_name = NULL;
e->directory = NULL;
e->path_from_top = NULL;
SF(e->src);
SF(e->src_ext);
SF(e->extra_obj);
SF(e->extra_clean);
SF(e->compile_flags);
SF(e->link_flags);
SF(e->libs);
SF(e->cmd);
SF(e->dest_sub_path);
SF(e->lib_version_num);
SF(e->export_include);
depend_list_entry *a, *b;
a = e->dependencies;
while (a)
{
b = a->next;
depend_list_entry_free(a);
a = b;
}
e->dependencies = NULL;
free(e);
}
static void module_entry_free(module_entry *e)
{
target_entry *a, *b;
a = e->targets;
while (a)
{
b = a->next;
target_entry_free(a);
a = b;
}
SF(e->dir_name);
SF(e->directory);
SF(e->path_from_top);
SF(e->verbatim);
SF(e->skip_condition);
free(e);
}
static depend_list_entry* depend_list_copy(depend_list_entry *l)
{
depend_list_entry *s, *d, *dh;
s = l;
dh = NULL;
while (s)
{
d = depend_list_entry_new(s->str);
LL_APPEND(dh, d);
s = s->next;
}
return dh;
}
#define SC(s) if (prog->s) lib->s = strdup(prog->s);
static target_entry* libtarget_for_worker(target_entry *prog)
{
char *name_as_lib = malloc(strlen(prog->target_name) + 4);
sprintf(name_as_lib, "lib%s", prog->target_name);
target_entry *lib = target_entry_new(TYPE_LIBRARY, name_as_lib);
free(name_as_lib);
SC(src);
SC(src_ext);
SC(extra_obj);
SC(extra_clean);
SC(compile_flags);
SC(link_flags);
SC(libs);
SC(cmd);
SC(lib_version_num);
SC(export_include);
lib->dest_sub_path = strdup("lib/embrace");
lib->dependencies = depend_list_copy(prog->dependencies);
lib->other_flags = prog->other_flags;
lib->other_flags &= ~SKIP_SHARED_MASK;
lib->other_flags |= SKIP_STATIC_MASK;
return lib;
}
#undef SC
target_entry* module_add_target(target_entry *e)
{
// check for target name collision
target_lookup_entry *_e = NULL;
HASH_FIND_STR(all_targets_hash, e->target_name, _e);
if (_e)
{
fprintf(stderr, "Warning: target name clash '%s' declared in\n"
" * %s\n * %s\n", e->target_name,
current_module->directory, _e->ptr->directory);
return _e->ptr;
}
if (e->type == TYPE_WORKER)
{
// register this decl twice
e->type = TYPE_PROGRAM; // _this_ is a program
++program_count;
// register a copy as a lib
// FIXME: name clashes of lib
module_add_target(libtarget_for_worker(e));
}
// copy ptrs from module entry
e->dir_name = current_module->dir_name;
e->directory = current_module->directory;
e->path_from_top = current_module->path_from_top;
// add to current module
LL_APPEND(current_module->targets, e);
// hash lib-names for dependency lookup
_e = malloc(sizeof(target_lookup_entry));
_e->target_name = e->target_name;
_e->ptr = e;
HASH_ADD_KEYPTR(hh, all_targets_hash, _e->target_name, strlen(_e->target_name), _e);
return NULL;
}
int resolve_all_dependencies(void)
{
printf("Resolving module dependencies\n");
int missing = 0;
module_entry *me = NULL;
LL_FOREACH(all_modules, me)
{
target_entry *te = NULL;
LL_FOREACH(me->targets, te)
{
depend_list_entry *de = NULL;
LL_FOREACH(te->dependencies, de)
{
target_lookup_entry *tle;
HASH_FIND_STR(all_targets_hash, de->str, tle);
if (tle)
{
DEBUG("Found '%s' dependency: %s\n", te->target_name, de->str);
de->ptr = tle->ptr;
} else
{
missing++;
fprintf(stderr, " Could not resolve '%s' requested by target '%s'\n",
de->str, te->target_name);
}
}
}
}
return missing;
}
inline int is_true(const char *true_str)
{
return !strcmp(true_str, "true");
}
void target_set_destdir_path(target_entry *e, const char *sub_path)
{
e->dest_sub_path = strdup(sub_path);
int k = strlen(sub_path);
if (sub_path[k-1] == '/')
{
e->dest_sub_path[k-1] = '\0';
}
}
void target_set_skip_install(target_entry *e, const char *true_str)
{
if (is_true(true_str))
e->other_flags |= SKIP_INSTALL_MASK;
}
void target_set_skip_shared(target_entry *e, const char *true_str)
{
if (is_true(true_str))
e->other_flags |= SKIP_SHARED_MASK;
}
void target_set_skip_static(target_entry *e, const char *true_str)
{
if (is_true(true_str))
e->other_flags |= SKIP_STATIC_MASK;
}
void target_set_convencience(target_entry *e, const char *true_str)
{
if (is_true(true_str))
{
e->other_flags |= SKIP_INSTALL_MASK;
e->other_flags &= ~SKIP_SHARED_MASK;
e->other_flags &= ~SKIP_STATIC_MASK;
e->other_flags |= CONVENIENCE_MASK;
}
}
module_entry* get_module_list(void)
{
return all_modules;
}
void cleanup_data(void)
{
// clear the lookup table
target_lookup_entry *x;
while (all_targets_hash)
{
x = all_targets_hash;
HASH_DEL(all_targets_hash, x);
free(x);
}
// clear all modules
module_entry *a, *b;
a = all_modules;
while (a)
{
b = a->next;
module_entry_free(a);
a = b;
}
all_modules = NULL;
current_module = NULL;
}
void print_target_decl(target_entry *e)
{
printf("Target: %s [%s]\n", e->target_name,
e->type == TYPE_PROGRAM ? "program" : "library");
printf(" directory: %s\n\tshort: %s\n\tpath-from-top: %s\n",
e->directory, e->dir_name, e->path_from_top);
printf(" src: %s\n", e->src);
if (e->compile_flags)
printf(" compile-flags: %s\n", e->compile_flags);
if (e->link_flags)
printf(" link-flags: %s\n", e->link_flags);
if (e->libs)
printf(" libs: %s\n", e->libs);
if (e->dest_sub_path)
printf(" DESTDIR-sub: %s\n", e->dest_sub_path);
if (e->other_flags & SKIP_INSTALL_MASK)
printf(" skip-install: true\n");
if (e->other_flags & SKIP_SHARED_MASK)
printf(" skip-shared: true\n");
if (e->lib_version_num)
printf(" version-%s: %s\n",
(e->other_flags & VERSION_INFO_SCHEMA) ?
"info" : "number", e->lib_version_num);
if (e->dependencies)
{
printf(" dependencies: ");
depend_list_entry *d;
LL_FOREACH(e->dependencies, d)
{
printf("%s%s ", d->str, d->ptr ? "*" : "!");
}
printf("\n");
}
}