7 standard options: -c, -D name[=val], -E (preprocess to stdout),
8 -g, -L dir, -o outfile, -O, -s, -U name
9 (and operands can have -l lib interspersed)
11 nonstandard but specified options: -S (assembly language left in .s),
12 -Wx,arg1[,arg2...] (pass arg(s) to phase x, where x is p (cpp)
13 0 (compiler), or l (loader)
14 nonstandard options: -v (echo real commands to stdout as they execute)
15 -A: turn on ANSI prototype warnings
18 typedef struct Objtype {
26 {"68020", "2c", "2l", "2"},
27 {"arm", "5c", "5l", "5"},
28 {"amd64", "6c", "6l", "6"},
29 {"alpha", "7c", "7l", "7"},
30 {"386", "8c", "8l", "8"},
31 {"sparc", "kc", "kl", "k"},
32 {"power", "qc", "ql", "q"},
33 {"mips", "vc", "vl", "v"},
37 Nobjs = (sizeof objtype)/(sizeof objtype[0]),
42 char *strings[Maxlist];
46 List srcs, objs, cpp, cc, ld, ldargs, srchlibs;
47 int cflag, vflag, Eflag, Sflag, Aflag;
48 char *allos = "2678kqv";
50 void append(List *, char *);
51 char *changeext(char *, char *);
52 void doexec(char *, List *);
53 void dopipe(char *, List *, char *, List *);
55 Objtype *findoty(void);
56 void printlist(List *);
57 char *searchlib(char *, char*);
60 main(int argc, char *argv[])
62 char *s, *suf, *ccpath, *lib;
71 append(&cpp, "-D__STDC__=1"); /* ANSI says so */
72 append(&cpp, "-D_POSIX_SOURCE=");
73 append(&cpp, "-N"); /* turn off standard includes */
76 append(&srchlibs, smprint("/%s/lib/ape", ot->name));
83 lib = searchlib(ARGF(), ot->name);
85 fprint(2, "cc: can't find library for -l\n");
93 fatal("cc: no -o argument");
98 append(&cpp, smprint("-%c%s", ARGC(), ARGF()));
110 fprint(2, "cc: no -L argument\n");
112 append(&srchlibs, lib);
117 append(&cc, smprint("-%c", ARGC()));
132 append(&ldargs, s+2);
135 fprint(2, "cc: pass letter after -W should be one of p0l; ignored\n");
138 fprint(2, "cc: bad option after -W; ignored\n");
142 append(&ldargs, "-v");
151 fprint(2, "cc: flag -%c ignored\n", ARGC());
155 append(&cc, "-J"); /* old/new decl mixture hack */
156 append(&cc, "-B"); /* turn off non-prototype warnings */
160 suf = utfrrune(s, '.');
163 if(strcmp(suf, "c") == 0) {
165 append(&objs, changeext(s, "o"));
166 } else if(strcmp(suf, "o") == 0 ||
167 strcmp(suf, ot->o) == 0 ||
168 strcmp(suf, "a") == 0 ||
169 (suf[0] == 'a' && strcmp(suf+1, ot->o) == 0)) {
171 } else if(utfrune(allos, suf[0]) != 0) {
172 fprint(2, "cc: argument %s ignored: wrong architecture\n",
179 fatal("no files to compile or load");
180 ccpath = smprint("/bin/%s", ot->cc);
181 append(&cpp, smprint("-I/%s/include/ape", ot->name));
182 append(&cpp, "-I/sys/include/ape");
185 for(i = 0; i < srcs.n; i++) {
186 append(&cpp, srcs.strings[i]);
188 doexec("/bin/cpp", &cpp);
194 if (haveoname && cflag)
197 append(&cc, changeext(srcs.strings[i], "o"));
199 dopipe("/bin/cpp", &cpp, ccpath, &cc);
207 for(i = 0; i < ldargs.n; i++)
208 append(&ld, ldargs.strings[i]);
209 for(i = 0; i < objs.n; i++)
210 append(&ld, objs.strings[i]);
211 append(&ld, smprint("/%s/lib/ape/libap.a", ot->name));
212 doexec(smprint("/bin/%s", ot->ld), &ld);
214 remove(objs.strings[0]);
221 searchlib(char *s, char *objtype)
228 for(i = srchlibs.n-1; i>=0; i--) {
229 l = smprint("%s/lib%s.a", srchlibs.strings[i], s);
230 if(access(l, 0) >= 0)
236 l = smprint("/%s/lib/ape/libap.a", objtype);
239 l = smprint("/%s/lib/ape/libap.a", objtype);
242 l = smprint("/%s/lib/ape/libl.a", objtype);
245 l = smprint("/%s/lib/ape/liby.a", objtype);
256 append(List *l, char *s)
258 if(l->n >= Maxlist-1)
259 fatal("too many arguments");
260 l->strings[l->n++] = s;
261 l->strings[l->n] = 0;
265 doexec(char *c, List *a)
275 fatal("fork failed");
278 fatal("exec failed");
280 if((w = wait()) == nil)
281 fatal("wait failed");
283 fatal(smprint("%s: %s", a->strings[0], w->msg));
288 dopipe(char *c1, List *a1, char *c2, List *a2)
301 fatal("pipe failed");
302 switch((pid1 = fork())) {
304 fatal("fork failed");
309 exec(c2, a2->strings);
310 fatal("exec failed");
314 fatal("fork failed");
320 exec(c1, a1->strings);
321 fatal("exec failed");
325 for(got = 0; got < 2; got++) {
326 if((w = wait()) == nil)
327 fatal("wait failed");
329 fatal(smprint("%s: %s", (w->pid == pid1) ? a1->strings[0] : a2->strings[0], w->msg));
340 o = getenv("objtype");
342 fatal("no $objtype in environment");
343 for(oty = objtype; oty < &objtype[Nobjs]; oty++)
344 if(strcmp(o, oty->name) == 0)
346 fatal("unknown $objtype");
347 return 0; /* shut compiler up */
353 fprint(2, "cc: %s\n", msg);
357 /* src ends in .something; return copy of basename with .ext added */
359 changeext(char *src, char *ext)
363 b = utfrrune(src, '/');
368 e = utfrrune(src, '.');
372 ans = smprint("%s.%s", b, ext);
382 for(i = 0; i < l->n; i++) {
383 fprint(2, "%s", l->strings[i]);