-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomaker.c
356 lines (311 loc) · 8.6 KB
/
automaker.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
/*
%use buffer_0;
%use buffer_1;
%use buffer_2;
%use buffer_init;
%use buffer_put;
%use buffer_get;
%use buffer_read;
%use critbit0_insert;
%use critbit0_contains;
%use critbit0_allprefixed;
%use critbit0_clear;
%use getln;
%use open_read;
%use str_start;
%use stralloc_copys;
%use stralloc_cats;
%use stralloc_append;
%use strerr_sys;
%use strerr_die;
*/
#include "critbit0.h"
#include "str0.h"
#include "buffer.h"
#include "stralloc.h"
#include "getln.h"
#include "strerr.h"
#include "error.h"
#include "readwrite.h"
#include "open.h"
#include "exit.h"
#include "str.h"
#define puts(s) buffer_putsalign(buffer_1,(s))
#define putflush() buffer_flush(buffer_1)
#define FATAL "automaker: error: "
static critbit0_tree modules;
static critbit0_tree nextup;
static critbit0_tree libs;
static critbit0_tree objfiles;
static critbit0_tree allmodules;
static critbit0_tree executables;
static limitmalloc_pool pool = { 65536 };
static stralloc line = {0};
static stralloc modc = {0};
static char buffer_f_space[BUFFER_INSIZE];
static buffer buffer_f;
static void cleanup()
{
critbit0_clear(&executables,&pool);
critbit0_clear(&modules,&pool);
critbit0_clear(&allmodules,&pool);
critbit0_clear(&nextup,&pool);
critbit0_clear(&libs,&pool);
}
static void err_readfailed(str0 dep)
{
strerr_die4sys(111,FATAL,"failed to open '",dep,"': ");
}
static void err_memsoft()
{
strerr_die2x(111,FATAL,"memory limit exceeded");
}
static void err_memhard()
{
_exit(111);
}
static void forceclose(int fd)
{
if(close(fd)==-1)
{
strerr_die2sys(111,FATAL,"failed to close file descriptor: ");
}
}
/* Read the file {m}.c and read all of its dependencies into the tree */
static int dependon(str0 m)
{
int fd;
int rc;
int match;
str0 newmod;
/* No use in reading a module twice */
if(critbit0_contains(&modules,&m)) return 0;
/* Add the module to the tree */
if(!critbit0_insert(&modules,&pool,&m)) err_memsoft();
if(!critbit0_insert(&allmodules,&pool,&m)) err_memsoft();
/* {m}.c is a new module */
if(!stralloc_copys(&modc,m)) err_memhard();
if(!stralloc_cats(&modc,".c")) err_memhard();
if(!stralloc_0(&modc)) err_memhard();
/* Open module source file */
for(;;) {
fd = open_read(modc.s);
if(fd>=0) break;
if(errno == error_intr) { sleep(1); continue; }
err_readfailed(modc.s);
}
buffer_init(&buffer_f,buffer_unixread,fd,buffer_f_space,sizeof buffer_f_space);
/* Read first line */
rc = getln(&buffer_f,&line,&match,'\n');
if(rc<0) { forceclose(fd); return 1; }
/* Make sure first line is a comment start */
if(!str_start(line.s,"/*")) { forceclose(fd); return 0; }
for(;;) {
/* Read next line */
rc = getln(&buffer_f,&line,&match,'\n');
if(rc<0 || !match) break;
/* If line is a comment ender, we're done with this file */
if(str_start(line.s,"*/")) break;
/* Make sure line is of format "%use MODULE;\n" */
if(line.len<8) continue;
if(!str_start(line.s,"%use ")
&& !str_start(line.s,"%lib ")) continue;
if(line.s[line.len-2]!=';') continue;
if(line.s[line.len-4]=='.' && line.s[line.len-3]=='o')
{
/* extract just the module name */
line.s[line.len-2] = 0;
newmod = line.s + 5;
if(!critbit0_contains(&objfiles,&newmod))
if(!critbit0_insert(&objfiles,&pool,&newmod)) err_memsoft();
continue;
}
/* extract just the module name */
line.s[line.len-2] = 0;
newmod = line.s + 5;
/* Put the name in the tree */
if(str_start(line.s,"%use "))
{
if(!critbit0_contains(&nextup,&newmod))
if(!critbit0_insert(&nextup,&pool,&newmod)) err_memsoft();
}
else if(str_start(line.s,"%lib "))
{
if(!critbit0_contains(&libs,&newmod))
if(!critbit0_insert(&libs,&pool,&newmod)) err_memsoft();
}
}
/* Done reading this file */
forceclose(fd);
return 0;
}
/* moredepends */
static int newcount;
static str0 moredepends_arg;
int moredepends_callback(void)
{
++newcount;
if(dependon(moredepends_arg)!=0) return 0;
str0_free(&moredepends_arg,&pool);
return 1;
}
static int moredepends()
{
int oldcount;
str0 empty = "";
oldcount = 0;
newcount = 0;
do {
oldcount = newcount;
newcount = 0;
if(critbit0_allprefixed(&nextup, &pool, &moredepends_arg, &empty, moredepends_callback)!=1)
err_memsoft();
} while(newcount!=oldcount);
return 0;
}
/* loadall */
static str0 loadall_arg;
static int loadall_callback(void)
{
puts(" "); puts(loadall_arg); puts(".o");
str0_free(&loadall_arg,&pool);
return 1;
}
static int loadallibs_callback(void)
{
puts(" -l"); puts(loadall_arg);
str0_free(&loadall_arg,&pool);
return 1;
}
static int loadallobjfiles_callback(void)
{
puts(" "); puts(loadall_arg);
str0_free(&loadall_arg,&pool);
return 1;
}
static int loadall(str0 modname)
{
str0 empty = "";
/* First line */
puts(modname); puts(" : load ");
puts(modname); puts(".o");
if(critbit0_allprefixed(&nextup, &pool, &loadall_arg, &empty, loadall_callback)!=1)
err_memsoft();
if(critbit0_allprefixed(&objfiles, &pool, &loadall_arg, &empty, loadallobjfiles_callback)!=1)
err_memsoft();
puts("\n");
/* Second line */
puts(" ./load "); puts(modname);
if(critbit0_allprefixed(&nextup, &pool, &loadall_arg, &empty, loadall_callback)!=1)
err_memsoft();
if(critbit0_allprefixed(&objfiles, &pool, &loadall_arg, &empty, loadallobjfiles_callback)!=1)
err_memsoft();
if(critbit0_allprefixed(&libs, &pool, &loadall_arg, &empty, loadallibs_callback)!=1)
err_memsoft();
puts("\n");
return 0;
}
/* compileall */
static str0 compileall_arg;
static int compileall_callback(void)
{
puts(compileall_arg); puts(".o : compile "); puts(compileall_arg); puts(".c\n");
puts(" ./compile "); puts(compileall_arg); puts(".c\n");
str0_free(&compileall_arg,&pool);
return 1;
}
static int compileall()
{
str0 empty = "";
if(critbit0_allprefixed(&allmodules, &pool, &compileall_arg, &empty, compileall_callback)!=1)
err_memsoft();
return 0;
}
/* itall */
static str0 itall_arg;
static int itall_callback(void)
{
puts(" "); puts(itall_arg);
str0_free(&itall_arg,&pool);
return 1;
}
static int itall()
{
str0 empty = "";
puts("it :");
if(critbit0_allprefixed(&executables, &pool, &itall_arg, &empty, itall_callback)!=1)
err_memsoft();
puts("\n");
return 0;
}
/* cleanall */
static str0 cleanall_arg;
static int cleanall_callback(void)
{
puts(" ");
puts(cleanall_arg);
str0_free(&cleanall_arg,&pool);
return 1;
}
static int cleanall()
{
str0 empty = "";
puts("clean : \n rm -f *.o");
if(critbit0_allprefixed(&executables, &pool, &cleanall_arg, &empty, cleanall_callback)!=1)
err_memsoft();
puts("\n");
return 0;
}
int main(int argc, char*argv[])
{
int i,len,rc;
char *p;
if(argc<=1) {
strerr_die1x(100,"automaker: usage: automaker [files ...]");
}
puts("default : it\n");
puts("load :\n"
" echo '#!/bin/sh' > load\n"
" echo 'main=\"$$1\"; shift' >> load\n"
" echo 'exec cc -o \"$$main\" \"$$main\".o $${1+\"$$@\"}' >> load\n"
" chmod +x load\n");
puts("compile :\n"
" echo '#!/bin/sh' > compile\n"
" echo 'exec cc -c $${1+\"$$@\"}' >> compile\n"
" chmod +x compile\n");
for(i=1;i<argc;i++)
{
/* grow a new tree */
critbit0_clear(&modules,&pool);
critbit0_clear(&nextup,&pool);
critbit0_clear(&libs,&pool);
critbit0_clear(&objfiles,&pool);
/* chop off .c suffix */
p = argv[i];
len = str0_length(&p);
if((len > 2
&& p[len-2] == '.'
&& p[len-1] == 'c'))
{
p[len-2] = 0;
}
if(!critbit0_contains(&executables,&p))
if(!critbit0_insert(&executables,&pool,&p)) return 1;
/* add module to tree along with its dependents */
if((rc=dependon(argv[i]))!=0) return rc;
/* recursively get more dependencies */
if((rc=moredepends())!=0) return rc;
/* list all executable modules to load them */
if((rc=loadall(argv[i]))!=0) return rc;
}
/* list all modules to compile them */
if((rc=compileall())!=0) return rc;
/* make it */
if((rc=itall())!=0) return rc;
/* make clean */
if((rc=cleanall())!=0) return rc;
/* Cleanup time */
cleanup();
putflush();
return 0;
}