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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
|
/* $OpenBSD: crunchgen.c,v 1.6 1996/09/28 04:30:39 etheisen Exp $ */
/*
* Copyright (c) 1994 University of Maryland
* All Rights Reserved.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of U.M. not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. U.M. makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: James da Silva, Systems Design and Analysis Group
* Computer Science Department
* University of Maryland at College Park
*/
/*
* ========================================================================
* crunchgen.c
*
* Generates a Makefile and main C file for a crunched executable,
* from specs given in a .conf file.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#define CRUNCH_VERSION "0.2"
#define MAXLINELEN 16384
#define MAXFIELDS 2048
/* XXX - This should be runtime configurable */
/*
* We might have more than one makefile
* name on any given platform. Make sure
* default name is last though.
*/
char *mf_name[] = {
#if defined(MF_NAMES)
MF_NAMES,
#else
"Makefile",
#endif
NULL
};
/* internal representation of conf file: */
/* simple lists of strings suffice for most parms */
typedef struct strlst {
struct strlst *next;
char *str;
} strlst_t;
/* progs have structure, each field can be set with "special" or calculated */
typedef struct prog {
struct prog *next;
char *name, *ident, *mf_name;
char *srcdir, *objdir;
strlst_t *objs, *objpaths;
strlst_t *links;
int goterror;
} prog_t;
/* global state */
strlst_t *srcdirs = NULL;
strlst_t *libs = NULL;
prog_t *progs = NULL;
char line[MAXLINELEN];
char confname[MAXPATHLEN], infilename[MAXPATHLEN];
char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
char topdir[MAXPATHLEN];
char libdir[MAXPATHLEN] = "/usr/lib";
int linenum = -1;
int goterror = 0;
char *pname = "crunchgen";
int verbose, readcache; /* options */
int reading_cache;
/* general library routines */
void status(char *str);
void out_of_memory(void);
void add_string(strlst_t **listp, char *str);
int is_dir(char *pathname);
int is_nonempty_file(char *pathname);
/* helper routines for main() */
void usage(void);
void parse_conf_file(void);
void gen_outputs(void);
int main(int argc, char **argv)
{
char *p;
int optc;
extern int optind;
extern char *optarg;
verbose = 1;
readcache = 1;
*outmkname = *outcfname = *execfname = '\0';
if(argc > 0) pname = argv[0];
while((optc = getopt(argc, argv, "m:c:e:fqD:L:")) != -1) {
switch(optc) {
case 'f': readcache = 0; break;
case 'q': verbose = 0; break;
case 'm': strcpy(outmkname, optarg); break;
case 'c': strcpy(outcfname, optarg); break;
case 'e': strcpy(execfname, optarg); break;
case 'D': strcpy(topdir, optarg); break;
case 'L': strcpy(libdir, optarg); break;
case '?':
default: usage();
}
}
argc -= optind;
argv += optind;
if(argc != 1) usage();
/*
* generate filenames
*/
strcpy(infilename, argv[0]);
/* confname = `basename infilename .conf` */
if((p=strrchr(infilename, '/')) != NULL) strcpy(confname, p+1);
else strcpy(confname, infilename);
if((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) *p = '\0';
if(!*outmkname) sprintf(outmkname, "%s.mk", confname);
if(!*outcfname) sprintf(outcfname, "%s.c", confname);
if(!*execfname) sprintf(execfname, "%s", confname);
sprintf(cachename, "%s.cache", confname);
sprintf(tempfname, ".tmp_%sXXXXXX", confname);
if(mktemp(tempfname) == NULL) {
perror(tempfname);
exit(1);
}
parse_conf_file();
gen_outputs();
exit(goterror);
}
void usage(void)
{
fprintf(stderr,
"%s [-fq] [-m <makefile>] [-c <c file>] [-e <exec file>] <conffile>\n",
pname);
exit(1);
}
/*
* ========================================================================
* parse_conf_file subsystem
*
*/
/* helper routines for parse_conf_file */
void parse_one_file(char *filename);
void parse_line(char *line, int *fc, char **fv, int nf);
void add_srcdirs(int argc, char **argv);
void add_progs(int argc, char **argv);
void add_link(int argc, char **argv);
void add_libs(int argc, char **argv);
void add_special(int argc, char **argv);
prog_t *find_prog(char *str);
void add_prog(char *progname);
void parse_conf_file(void)
{
if(!is_nonempty_file(infilename)) {
fprintf(stderr, "%s: fatal: input file \"%s\" not found.\n",
pname, infilename);
exit(1);
}
parse_one_file(infilename);
if(readcache && is_nonempty_file(cachename)) {
reading_cache = 1;
parse_one_file(cachename);
}
}
void parse_one_file(char *filename)
{
char *fieldv[MAXFIELDS];
int fieldc;
void (*f)(int c, char **v);
FILE *cf;
sprintf(line, "reading %s", filename);
status(line);
strcpy(curfilename, filename);
if((cf = fopen(curfilename, "r")) == NULL) {
perror(curfilename);
goterror = 1;
return;
}
linenum = 0;
while(fgets(line, MAXLINELEN, cf) != NULL) {
linenum++;
parse_line(line, &fieldc, fieldv, MAXFIELDS);
if(fieldc < 1) continue;
if(!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs;
else if(!strcmp(fieldv[0], "progs")) f = add_progs;
else if(!strcmp(fieldv[0], "ln")) f = add_link;
else if(!strcmp(fieldv[0], "libs")) f = add_libs;
else if(!strcmp(fieldv[0], "special")) f = add_special;
else {
fprintf(stderr, "%s:%d: skipping unknown command `%s'.\n",
curfilename, linenum, fieldv[0]);
goterror = 1;
continue;
}
if(fieldc < 2) {
fprintf(stderr,
"%s:%d: %s command needs at least 1 argument, skipping.\n",
curfilename, linenum, fieldv[0]);
goterror = 1;
continue;
}
f(fieldc, fieldv);
}
if(ferror(cf)) {
perror(curfilename);
goterror = 1;
}
fclose(cf);
}
void parse_line(char *line, int *fc, char **fv, int nf)
{
char *p;
p = line;
*fc = 0;
while(1) {
while(isspace(*p)) p++;
if(*p == '\0' || *p == '#') break;
if(*fc < nf) fv[(*fc)++] = p;
while(*p && !isspace(*p) && *p != '#') p++;
if(*p == '\0' || *p == '#') break;
*p++ = '\0';
}
if(*p) *p = '\0'; /* needed for '#' case */
}
void add_srcdirs(int argc, char **argv)
{
int i;
char tmppath[MAXPATHLEN];
for(i=1;i<argc;i++) {
if (argv[i][0] == '/' || topdir[0] == '\0')
strcpy(tmppath, argv[i]);
else {
strcpy(tmppath, topdir);
strcat(tmppath, "/");
strcat(tmppath, argv[i]);
}
if(is_dir(tmppath))
add_string(&srcdirs, tmppath);
else {
fprintf(stderr, "%s:%d: `%s' is not a directory, skipping it.\n",
curfilename, linenum, tmppath);
goterror = 1;
}
}
}
void add_progs(int argc, char **argv)
{
int i;
for(i=1;i<argc;i++)
add_prog(argv[i]);
}
void add_prog(char *progname)
{
prog_t *p1, *p2;
/* add to end, but be smart about dups */
for(p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
if(!strcmp(p2->name, progname)) return;
p2 = malloc(sizeof(prog_t));
if(p2) p2->name = strdup(progname);
if(!p2 || !p2->name)
out_of_memory();
p2->next = NULL;
if(p1 == NULL) progs = p2;
else p1->next = p2;
p2->ident = p2->srcdir = p2->objdir = NULL;
p2->links = p2->objs = NULL;
p2->goterror = 0;
}
void add_link(int argc, char **argv)
{
int i;
prog_t *p = find_prog(argv[1]);
if(p == NULL) {
fprintf(stderr,
"%s:%d: no prog %s previously declared, skipping link.\n",
curfilename, linenum, argv[1]);
goterror = 1;
return;
}
for(i=2;i<argc;i++)
add_string(&p->links, argv[i]);
}
void add_libs(int argc, char **argv)
{
int i;
for(i=1;i<argc;i++)
add_string(&libs, argv[i]);
}
void add_special(int argc, char **argv)
{
int i;
prog_t *p = find_prog(argv[1]);
if(p == NULL) {
if(reading_cache) return;
fprintf(stderr,
"%s:%d: no prog %s previously declared, skipping special.\n",
curfilename, linenum, argv[1]);
goterror = 1;
return;
}
if(!strcmp(argv[2], "ident")) {
if(argc != 4) goto argcount;
if((p->ident = strdup(argv[3])) == NULL)
out_of_memory();
}
else if(!strcmp(argv[2], "srcdir")) {
if(argc != 4) goto argcount;
if((p->srcdir = strdup(argv[3])) == NULL)
out_of_memory();
}
else if(!strcmp(argv[2], "objdir")) {
if(argc != 4) goto argcount;
if((p->objdir = strdup(argv[3])) == NULL)
out_of_memory();
}
else if(!strcmp(argv[2], "objs")) {
p->objs = NULL;
for(i=3;i<argc;i++)
add_string(&p->objs, argv[i]);
}
else if(!strcmp(argv[2], "objpaths")) {
p->objpaths = NULL;
for(i=3;i<argc;i++)
add_string(&p->objpaths, argv[i]);
}
else {
fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n",
curfilename, linenum, argv[2]);
goterror = 1;
}
return;
argcount:
fprintf(stderr,
"%s:%d: too %s arguments, expected \"special %s %s <string>\".\n",
curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
goterror = 1;
}
prog_t *find_prog(char *str)
{
prog_t *p;
for(p = progs; p != NULL; p = p->next)
if(!strcmp(p->name, str)) return p;
return NULL;
}
/*
* ========================================================================
* gen_outputs subsystem
*
*/
/* helper subroutines */
void remove_error_progs(void);
void fillin_program(prog_t *p);
void gen_specials_cache(void);
void gen_output_makefile(void);
void gen_output_cfile(void);
void fillin_program_objs(prog_t *p, char *path);
void top_makefile_rules(FILE *outmk);
void prog_makefile_rules(FILE *outmk, prog_t *p);
void output_strlst(FILE *outf, strlst_t *lst);
char *genident(char *str);
char *dir_search(char *progname);
void gen_outputs(void)
{
prog_t *p;
for(p = progs; p != NULL; p = p->next)
fillin_program(p);
remove_error_progs();
gen_specials_cache();
gen_output_cfile();
gen_output_makefile();
status("");
fprintf(stderr,
"Run \"make -f %s objs exe\" to build crunched binary.\n",
outmkname);
}
void fillin_program(prog_t *p)
{
char path[MAXPATHLEN];
char *srcparent;
strlst_t *s;
int i;
sprintf(line, "filling in parms for %s", p->name);
status(line);
if(!p->ident)
p->ident = genident(p->name);
if(!p->srcdir) {
srcparent = dir_search(p->name);
if(srcparent)
sprintf(path, "%s/%s", srcparent, p->name);
if(is_dir(path))
p->srcdir = strdup(path);
}
if(!p->objdir && p->srcdir) {
sprintf(path, "%s/obj", p->srcdir);
if(is_dir(path))
p->objdir = strdup(path);
else {
sprintf(path, "%s/obj.%s", p->srcdir, MACHINE);
if(is_dir(path))
p->objdir = strdup(path);
else
p->objdir = p->srcdir;
}
}
/* We have a sourcedir and no explict objs, try */
/* to find makefile and get objs from it. */
if (p->srcdir && !p->objs) {
for (i = 0; mf_name[i] != NULL; i++) {
sprintf(path, "%s/%s", p->srcdir, mf_name[i]);
if (is_nonempty_file(path)) {
p->mf_name = mf_name[i];
fillin_program_objs(p, path);
break;
}
}
}
if(!p->objpaths && p->objdir && p->objs)
for(s = p->objs; s != NULL; s = s->next) {
sprintf(line, "%s/%s", p->objdir, s->str);
add_string(&p->objpaths, line);
}
if(!p->srcdir && verbose)
fprintf(stderr, "%s: %s: warning: could not find source directory.\n",
infilename, p->name);
if(!p->objs && verbose)
fprintf(stderr, "%s: %s: warning: could not find any .o files.\n",
infilename, p->name);
if(!p->objpaths) {
fprintf(stderr,
"%s: %s: error: no objpaths specified or calculated.\n",
infilename, p->name);
p->goterror = goterror = 1;
}
}
void fillin_program_objs(prog_t *p, char *path)
{
char *obj, *cp;
int rc;
FILE *f;
/* discover the objs from the srcdir Makefile */
if((f = fopen(tempfname, "w")) == NULL) {
perror(tempfname);
goterror = 1;
return;
}
fprintf(f, ".include \"%s\"\n", path);
fprintf(f, ".if defined(PROG) && !defined(OBJS)\n");
fprintf(f, "OBJS=${PROG}.o\n");
fprintf(f, ".endif\n");
fprintf(f, "crunchgen_objs:\n\t@echo 'OBJS= '${OBJS}\n");
fclose(f);
sprintf(line, "make -f %s crunchgen_objs 2>&1", tempfname);
if((f = popen(line, "r")) == NULL) {
perror("submake pipe");
goterror = 1;
return;
}
while(fgets(line, MAXLINELEN, f)) {
if(strncmp(line, "OBJS= ", 6)) {
if (strcmp(line,
"sh: warning: running as root with dot in PATH\n") == 0)
continue;
fprintf(stderr, "make error: %s", line);
goterror = 1;
continue;
}
cp = line + 6;
while(isspace(*cp)) cp++;
while(*cp) {
obj = cp;
while(*cp && !isspace(*cp)) cp++;
if(*cp) *cp++ = '\0';
add_string(&p->objs, obj);
while(isspace(*cp)) cp++;
}
}
if((rc=pclose(f)) != 0) {
fprintf(stderr, "make error: make returned %d\n", rc);
goterror = 1;
}
unlink(tempfname);
}
void remove_error_progs(void)
{
prog_t *p1, *p2;
p1 = NULL; p2 = progs;
while(p2 != NULL) {
if(!p2->goterror)
p1 = p2, p2 = p2->next;
else {
/* delete it from linked list */
fprintf(stderr, "%s: %s: ignoring program because of errors.\n",
infilename, p2->name);
if(p1) p1->next = p2->next;
else progs = p2->next;
p2 = p2->next;
}
}
}
void gen_specials_cache(void)
{
FILE *cachef;
prog_t *p;
sprintf(line, "generating %s", cachename);
status(line);
if((cachef = fopen(cachename, "w")) == NULL) {
perror(cachename);
goterror = 1;
return;
}
fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n",
cachename, infilename, CRUNCH_VERSION);
for(p = progs; p != NULL; p = p->next) {
fprintf(cachef, "\n");
if(p->srcdir)
fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir);
if(p->objdir)
fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir);
if(p->objs) {
fprintf(cachef, "special %s objs", p->name);
output_strlst(cachef, p->objs);
}
fprintf(cachef, "special %s objpaths", p->name);
output_strlst(cachef, p->objpaths);
}
fclose(cachef);
}
void gen_output_makefile(void)
{
prog_t *p;
FILE *outmk;
sprintf(line, "generating %s", outmkname);
status(line);
if((outmk = fopen(outmkname, "w")) == NULL) {
perror(outmkname);
goterror = 1;
return;
}
fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
outmkname, infilename, CRUNCH_VERSION);
top_makefile_rules(outmk);
for(p = progs; p != NULL; p = p->next)
prog_makefile_rules(outmk, p);
fprintf(outmk, "\n# ========\n");
fclose(outmk);
}
void gen_output_cfile(void)
{
extern char *crunched_skel[];
char **cp;
FILE *outcf;
prog_t *p;
strlst_t *s;
sprintf(line, "generating %s", outcfname);
status(line);
if((outcf = fopen(outcfname, "w")) == NULL) {
perror(outcfname);
goterror = 1;
return;
}
fprintf(outcf,
"/* %s - generated from %s by crunchgen %s */\n",
outcfname, infilename, CRUNCH_VERSION);
fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
for(cp = crunched_skel; *cp != NULL; cp++)
fprintf(outcf, "%s\n", *cp);
for(p = progs; p != NULL; p = p->next)
fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
fprintf(outcf, "\nstruct stub entry_points[] = {\n");
for(p = progs; p != NULL; p = p->next) {
fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
p->name, p->ident);
for(s = p->links; s != NULL; s = s->next)
fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
s->str, p->ident);
}
fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
fprintf(outcf, "\t{ NULL, NULL }\n};\n");
fclose(outcf);
}
char *genident(char *str)
{
char *n,*s,*d;
/*
* generates a Makefile/C identifier from a program name, mapping '-' to
* '_' and ignoring all other non-identifier characters. This leads to
* programs named "foo.bar" and "foobar" to map to the same identifier.
*/
if((n = strdup(str)) == NULL)
return NULL;
for(d = s = n; *s != '\0'; s++) {
if(*s == '-') *d++ = '_';
else if(*s == '_' || isalnum(*s)) *d++ = *s;
}
*d = '\0';
return n;
}
char *dir_search(char *progname)
{
char path[MAXPATHLEN];
strlst_t *dir;
for(dir=srcdirs; dir != NULL; dir=dir->next) {
sprintf(path, "%s/%s", dir->str, progname);
if(is_dir(path)) return dir->str;
}
return NULL;
}
void top_makefile_rules(FILE *outmk)
{
prog_t *p;
fprintf(outmk, "STRIP=strip\n");
fprintf(outmk, "LIBS=");
fprintf(outmk, "-L%s ", libdir);
output_strlst(outmk, libs);
fprintf(outmk, "CRUNCHED_OBJS=");
for(p = progs; p != NULL; p = p->next)
fprintf(outmk, " %s.lo", p->name);
fprintf(outmk, "\n");
fprintf(outmk, "SUBMAKE_TARGETS=");
for(p = progs; p != NULL; p = p->next)
fprintf(outmk, " %s_make", p->ident);
fprintf(outmk, "\n\n");
fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\n",
execfname, execfname);
fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
execfname, execfname);
fprintf(outmk, "\t$(STRIP) %s\n", execfname);
fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
fprintf(outmk, "exe: %s\n", execfname);
fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n",
execfname);
}
void prog_makefile_rules(FILE *outmk, prog_t *p)
{
fprintf(outmk, "\n# -------- %s\n\n", p->name);
if(p->srcdir && p->objs) {
fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
fprintf(outmk, "%s_OBJS=", p->ident);
output_strlst(outmk, p->objs);
fprintf(outmk, "%s_make:\n", p->ident);
fprintf(outmk, "\t(cd $(%s_SRCDIR); make -f %s $(%s_OBJS))\n\n",
p->ident, p->mf_name, p->ident);
}
else
fprintf(outmk, "%s_make:\n\t@echo \"** cannot make objs for %s\"\n\n",
p->ident, p->name);
fprintf(outmk, "%s_OBJPATHS=", p->ident);
output_strlst(outmk, p->objpaths);
fprintf(outmk, "%s_stub.c:\n", p->name);
fprintf(outmk, "\techo \""
"int _crunched_%s_stub(int argc, char **argv, char **envp)"
"{return main(argc,argv,envp);}\" >%s_stub.c\n",
p->ident, p->name);
fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)\n",
p->name, p->name, p->ident);
fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)\n",
p->name, p->name, p->ident);
fprintf(outmk, "\tcrunchide -k __crunched_%s_stub %s.lo\n",
p->ident, p->name);
}
void output_strlst(FILE *outf, strlst_t *lst)
{
for(; lst != NULL; lst = lst->next)
fprintf(outf, " %s", lst->str);
fprintf(outf, "\n");
}
/*
* ========================================================================
* general library routines
*
*/
void status(char *str)
{
static int lastlen = 0;
int len, spaces;
if(!verbose) return;
len = strlen(str);
spaces = lastlen - len;
if(spaces < 1) spaces = 1;
fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
fflush(stderr);
lastlen = len;
}
void out_of_memory(void)
{
fprintf(stderr, "%s: %d: out of memory, stopping.\n", infilename, linenum);
exit(1);
}
void add_string(strlst_t **listp, char *str)
{
strlst_t *p1, *p2;
/* add to end, but be smart about dups */
for(p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
if(!strcmp(p2->str, str)) return;
p2 = malloc(sizeof(strlst_t));
if(p2) p2->str = strdup(str);
if(!p2 || !p2->str)
out_of_memory();
p2->next = NULL;
if(p1 == NULL) *listp = p2;
else p1->next = p2;
}
int is_dir(char *pathname)
{
struct stat buf;
if(stat(pathname, &buf) == -1)
return 0;
return S_ISDIR(buf.st_mode);
}
int is_nonempty_file(char *pathname)
{
struct stat buf;
if(stat(pathname, &buf) == -1)
return 0;
return S_ISREG(buf.st_mode) && buf.st_size > 0;
}
|