-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamgen.c
404 lines (361 loc) · 9.75 KB
/
namgen.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
393
394
395
396
397
398
399
400
401
402
403
404
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <getopt.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "myio.h"
#include "utlist.h"
#include "ngtemplate.h"
#include "stringbuilder.h"
#include "dirscanner.h"
#include "target.h"
#include "template.h"
#include "logging.h"
static ngt_template *template = NULL;
static ngt_template *template_goto = NULL;
static char *template_file = COMPILED_IN_TEMPLATE_FILE;
char *top_dir = NULL;
static int process_module(module_entry *module)
{
DEBUG("Creating %s/makefile\n", module->directory);
char *output_str = NULL;
int res = 0;
if (chdir(module->directory))
return 1;
ngt_dictionary *dict = dict_for_module(module);
ngt_set_dictionary(template, dict);
ngt_expand(template, &output_str);
int fd = open("makefile.ng", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
int wl = strlen(output_str);
if (write(fd, output_str, wl) != wl)
{
fprintf(stderr, "Error while writing '%s/makefile.ng'\n",
module->directory);
res = 1;
}
close(fd);
if (output_str)
free(output_str);
ngt_set_dictionary(template_goto, dict);
ngt_expand(template_goto, &output_str);
fd = open("makefile", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
wl = strlen(output_str);
if (write(fd, output_str, wl) != wl)
{
fprintf(stderr, "Error while writing '%s/makefile'\n",
module->directory);
res = 1;
}
close(fd);
if (dict)
ngt_dictionary_destroy(dict);
if (output_str)
free(output_str);
return res;
}
static void usage(const char * prg)
{
char *_prg = strdup(prg);
char *name = basename(_prg);
printf("USAGE: %s [flags]\n", name);
free(_prg);
name = load_file("usage.txt");
if (name)
{
printf("%s\n", name);
free(name);
} else
{
fprintf(stderr, "Failed to load usage.txt\n");
}
}
static void show_documentation(void)
{
char *doc = load_file("documentation.txt");
if (doc)
{
printf("%s\n", doc);
free(doc);
} else
{
fprintf(stderr, "Failed to load documentation.txt\n");
}
}
struct charp_list_entry;
typedef struct charp_list_entry {
char *dir;
struct charp_list_entry *next;
} charp_list_entry;
static charp_list_entry* make_list_entry(char *dir)
{
charp_list_entry *e = malloc(sizeof(charp_list_entry));
int k = strlen(dir) - 1;
if (dir[k] == '/')
dir[k] = 0;
e->dir = dir;
e->next = NULL;
return e;
}
static int module_cmp(module_entry *a, module_entry *b)
{
return strcmp(a->path_from_top, b->path_from_top);
}
static void create_master_include(void)
{
if (chdir(top_dir))
{
fprintf(stderr, "Could not change to dir: %s\n", top_dir);
return;
}
FILE *f = fopen("makefile.dirs", "w");
fprintf(f, "# This file was generated by namgen; do not edit!\n\n");
stringbuilder *sb = sb_new();
sb_reset(sb);
module_entry *modules, *me;
modules = get_module_list();
LL_SORT(modules, module_cmp);
LL_FOREACH(modules, me)
{
if (me->skip_condition)
{
fprintf(f, "ifndef %s\n", me->skip_condition);
}
fprintf(f, "include %s/makefile.ng\n", me->path_from_top);
fprintf(f, "ALL_CLEAN_TARGETS += %s_clean\n", me->dir_name);
fprintf(f, "ALL_INSTALL_TARGETS += %s_install\n", me->dir_name);
fprintf(f, "ALL_UNINSTALL_TARGETS += %s_uninstall\n", me->dir_name);
if (me->skip_condition)
{
fprintf(f, "endif\n");
}
fprintf(f, "\n");
target_entry *target;
LL_FOREACH(me->targets, target)
{
sb_append_str(sb, "$(DESTDIR)/");
sb_append_str(sb, get_target_install_subdir(target));
sb_append_str(sb, " ");
}
}
fprintf(f, "\n");
sb_append_str(sb, "$(DESTDIR)/lib");
char *tmp = sb_make_cstring(sb);
fprintf(f, "DISTINCT_DEST_DIR := $(sort %s)\n", tmp);
free(tmp);
fprintf(f, "$(DISTINCT_DEST_DIR):\n\t@mkdir -p $@\n\n");
fprintf(f, "clean: $(ALL_CLEAN_TARGETS)\n");
fprintf(f, "install: $(ALL_INSTALL_TARGETS)\n");
fprintf(f, "uninstall: $(ALL_UNINSTALL_TARGETS)\n");
fprintf(f, ".PHONY: $(ALL_CLEAN_TARGETS) $(ALL_INSTALL_TARGETS) $(ALL_UNINSTALL_TARGETS)\n");
fclose(f);
sb_destroy(sb, 1);
}
static void safe_export_file(const char *name, int make_executable)
{
// assume that we are back in top_dir
// don't overwrite if already exists
if (!access(name, F_OK))
{
DEBUG("File %s exists, skipping\n", name);
return;
}
FILE *f = fopen(name, "w");
if (!f)
{
fprintf(stderr, "Error opening file %s for writing\n", name);
return;
}
char *data = load_file(name);
if (data)
{
fprintf(f, "%s", data);
} else
{
fprintf(stderr, "Error loading src file %s\n", name);
}
fclose(f);
if (make_executable)
{
chmod(name, S_IRWXU | S_IRWXG | S_IROTH);
}
DEBUG("Created base-file %s\n", name);
if (data)
free(data);
}
static void print_loaded_rules(void)
{
module_entry *modules, *me;
modules = get_module_list();
LL_FOREACH(modules, me)
{
target_entry *te;
LL_FOREACH(me->targets, te)
{
print_target_decl(te);
}
if (me->verbatim)
{
printf("Module '%s' has verbatim data:\n%s------------------------\n",
me->dir_name, me->verbatim);
}
}
}
static int namgen_version_ok = 1;
static const int current_namgen_version =
#include "VERSION"
;
void check_namgen_version(const char *requested_version)
{
int req = atoi(requested_version);
if (req > current_namgen_version) {
namgen_version_ok = 0;
fprintf(stderr, "Error: Requested namgen version (%i) is too old (> %i)\n",
req, current_namgen_version);
}
}
extern void install_bt_handler(void);
int main(int argc, char *argv[])
{
int c, opt_idx;
int print_rules = 0;
int work_mode = 1;
static struct option long_opts[] = {
{"add-dir", 1, 0, 'a'},
{"print", 0, 0, 'p'},
{"help", 0, 0, 'h'},
{"show-doc", 0, 0, 'H'},
{"debug", 0, 0, 'd'},
{"clean", 0, 0, 'C'},
{0, 0, 0, 0}
};
charp_list_entry *additional_dir_list_head = NULL;
if (io_init(argv[0]))
{
fprintf(stderr, "Error loading IO subsystem for %s\n", argv[0]);
exit(1);
}
while ((c = getopt_long(argc, argv, "a:dpCh?H", long_opts, &opt_idx)) != -1) {
switch(c) {
case 'a':
LL_APPEND(additional_dir_list_head, make_list_entry(optarg));
break;
case 'p':
print_rules = 1;
break;
case 'd':
set_debug_on(1);
break;
case 'C':
work_mode = -1;
break;
case 'h':
case '?':
usage(argv[0]);
exit(0);
break;
case 'H':
show_documentation();
exit(0);
break;
default:
usage(argv[1]);
exit(1);
}
};
install_bt_handler();
top_dir = getenv("PWD");
iterate_directories(top_dir, work_mode);
if (additional_dir_list_head)
{
charp_list_entry *e, *n;
LL_FOREACH(additional_dir_list_head, e)
{
iterate_directories(e->dir, work_mode);
}
// cleanup
e = additional_dir_list_head;
while (e) {
n = e->next;
free(e);
e = n;
}
}
if (!namgen_version_ok) {
fprintf(stderr, "Please update namgen\n");
exit(1);
}
if (work_mode == -1) {
// cleanup other files
if (chdir(top_dir))
{
fprintf(stderr, "Could not change to dir: %s\n", top_dir);
} else
{
if (!access("makefile.dirs", R_OK))
{
unlink("makefile.dirs");
unlink("install");
unlink("makefile");
}
}
goto work_skipped;
}
int module_name_clashes = module_check_name_unique();
if (module_name_clashes)
{
fprintf(stderr, "Continuing in spite of possible name clashes!\n");
}
int missing = resolve_all_dependencies();
if (!missing)
{
printf("Loaded: %i program rules and %i library rules in %i modules\n",
program_count, library_count, module_count);
} else
{
fprintf(stderr, "Error: missing %i dependencies\n", missing);
if (!print_rules)
exit(1);
}
if (print_rules)
{
print_loaded_rules();
goto work_skipped;
}
template = ngt_new();
template_goto = ngt_new();
if (load_template(template, template_file))
{
fprintf(stderr, "Error loading template %s\n", template_file);
goto work_skipped;
}
if (load_template(template_goto, "sub_goto.tmpl"))
{
fprintf(stderr, "Error loading template %s\n", template_file);
goto work_skipped;
}
module_entry *modules, *me;
modules = get_module_list();
LL_FOREACH(modules, me)
{
if (process_module(me))
goto work_skipped;
}
create_master_include();
safe_export_file("makefile", 0);
safe_export_file("install", 1);
work_skipped:
cleanup_data();
template_cleanup();
if (template)
ngt_destroy(template);
if (template_goto)
ngt_destroy(template_goto);
io_quit();
return 0;
}