1 /* 2 * Copyright (c) 1994 University of Maryland 3 * All Rights Reserved. 4 * 5 * Permission to use, copy, modify, distribute, and sell this software and its 6 * documentation for any purpose is hereby granted without fee, provided that 7 * the above copyright notice appear in all copies and that both that 8 * copyright notice and this permission notice appear in supporting 9 * documentation, and that the name of U.M. not be used in advertising or 10 * publicity pertaining to distribution of the software without specific, 11 * written prior permission. U.M. makes no representations about the 12 * suitability of this software for any purpose. It is provided "as is" 13 * without express or implied warranty. 14 * 15 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. 17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 19 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21 * 22 * Author: James da Silva, Systems Design and Analysis Group 23 * Computer Science Department 24 * University of Maryland at College Park 25 */ 26 /* 27 * ======================================================================== 28 * crunchgen.c 29 * 30 * Generates a Makefile and main C file for a crunched executable, 31 * from specs given in a .conf file. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/stat.h> 39 40 #include <ctype.h> 41 #include <err.h> 42 #include <paths.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #define CRUNCH_VERSION "0.2" 49 50 #define MAXLINELEN 16384 51 #define MAXFIELDS 2048 52 53 54 /* internal representation of conf file: */ 55 56 /* simple lists of strings suffice for most parms */ 57 58 typedef struct strlst { 59 struct strlst *next; 60 char *str; 61 } strlst_t; 62 63 /* progs have structure, each field can be set with "special" or calculated */ 64 65 typedef struct prog { 66 struct prog *next; /* link field */ 67 char *name; /* program name */ 68 char *ident; /* C identifier for the program name */ 69 char *srcdir; 70 char *realsrcdir; 71 char *objdir; 72 char *objvar; /* Makefile variable to replace OBJS */ 73 strlst_t *objs, *objpaths; 74 strlst_t *buildopts; 75 strlst_t *keeplist; 76 strlst_t *links; 77 strlst_t *libs; 78 strlst_t *libs_so; 79 int goterror; 80 } prog_t; 81 82 83 /* global state */ 84 85 strlst_t *buildopts = NULL; 86 strlst_t *srcdirs = NULL; 87 strlst_t *libs = NULL; 88 strlst_t *libs_so = NULL; 89 prog_t *progs = NULL; 90 91 char confname[MAXPATHLEN], infilename[MAXPATHLEN]; 92 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN]; 93 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN]; 94 char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */ 95 char *objprefix; /* where are the objects ? */ 96 char *path_make; 97 int linenum = -1; 98 int goterror = 0; 99 100 int verbose, readcache; /* options */ 101 int reading_cache; 102 int makeobj = 0; /* add 'make obj' rules to the makefile */ 103 104 int list_mode; 105 106 /* general library routines */ 107 108 void status(char *str); 109 void out_of_memory(void); 110 void add_string(strlst_t **listp, char *str); 111 int is_dir(char *pathname); 112 int is_nonempty_file(char *pathname); 113 int subtract_strlst(strlst_t **lista, strlst_t **listb); 114 int in_list(strlst_t **listp, char *str); 115 116 /* helper routines for main() */ 117 118 void usage(void); 119 void parse_conf_file(void); 120 void gen_outputs(void); 121 122 123 int 124 main(int argc, char **argv) 125 { 126 char *p; 127 int optc; 128 129 verbose = 1; 130 readcache = 1; 131 *outmkname = *outcfname = *execfname = '\0'; 132 133 path_make = getenv("MAKE"); 134 if (path_make == NULL || *path_make == '\0') 135 path_make = "make"; 136 137 p = getenv("MAKEOBJDIRPREFIX"); 138 if (p == NULL || *p == '\0') 139 objprefix = "/usr/obj"; /* default */ 140 else 141 if ((objprefix = strdup(p)) == NULL) 142 out_of_memory(); 143 144 while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) { 145 switch(optc) { 146 case 'f': 147 readcache = 0; 148 break; 149 case 'o': 150 makeobj = 1; 151 break; 152 case 'q': 153 verbose = 0; 154 break; 155 156 case 'm': 157 strlcpy(outmkname, optarg, sizeof(outmkname)); 158 break; 159 case 'p': 160 if ((objprefix = strdup(optarg)) == NULL) 161 out_of_memory(); 162 break; 163 164 case 'h': 165 strlcpy(outhdrname, optarg, sizeof(outhdrname)); 166 break; 167 case 'c': 168 strlcpy(outcfname, optarg, sizeof(outcfname)); 169 break; 170 case 'e': 171 strlcpy(execfname, optarg, sizeof(execfname)); 172 break; 173 174 case 'l': 175 list_mode++; 176 verbose = 0; 177 break; 178 179 case '?': 180 default: 181 usage(); 182 } 183 } 184 185 argc -= optind; 186 argv += optind; 187 188 if (argc != 1) 189 usage(); 190 191 /* 192 * generate filenames 193 */ 194 195 strlcpy(infilename, argv[0], sizeof(infilename)); 196 197 /* confname = `basename infilename .conf` */ 198 199 if ((p=strrchr(infilename, '/')) != NULL) 200 strlcpy(confname, p + 1, sizeof(confname)); 201 else 202 strlcpy(confname, infilename, sizeof(confname)); 203 204 if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) 205 *p = '\0'; 206 207 if (!*outmkname) 208 snprintf(outmkname, sizeof(outmkname), "%s.mk", confname); 209 if (!*outcfname) 210 snprintf(outcfname, sizeof(outcfname), "%s.c", confname); 211 if (!*execfname) 212 snprintf(execfname, sizeof(execfname), "%s", confname); 213 214 snprintf(cachename, sizeof(cachename), "%s.cache", confname); 215 snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX", 216 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname); 217 218 parse_conf_file(); 219 if (list_mode) 220 exit(goterror); 221 222 gen_outputs(); 223 224 exit(goterror); 225 } 226 227 228 void 229 usage(void) 230 { 231 fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ", 232 "[-h <makefile-header-name>] [-m <makefile>]", 233 "[-p <obj-prefix>] [-c <c-file-name>] [-e <exec-file>] ", 234 "<conffile>"); 235 exit(1); 236 } 237 238 239 /* 240 * ======================================================================== 241 * parse_conf_file subsystem 242 * 243 */ 244 245 /* helper routines for parse_conf_file */ 246 247 void parse_one_file(char *filename); 248 void parse_line(char *line, int *fc, char **fv, int nf); 249 void add_srcdirs(int argc, char **argv); 250 void add_progs(int argc, char **argv); 251 void add_link(int argc, char **argv); 252 void add_libs(int argc, char **argv); 253 void add_libs_so(int argc, char **argv); 254 void add_buildopts(int argc, char **argv); 255 void add_special(int argc, char **argv); 256 257 prog_t *find_prog(char *str); 258 void add_prog(char *progname); 259 260 261 void 262 parse_conf_file(void) 263 { 264 if (!is_nonempty_file(infilename)) 265 errx(1, "fatal: input file \"%s\" not found", infilename); 266 267 parse_one_file(infilename); 268 if (readcache && is_nonempty_file(cachename)) { 269 reading_cache = 1; 270 parse_one_file(cachename); 271 } 272 } 273 274 275 void 276 parse_one_file(char *filename) 277 { 278 char *fieldv[MAXFIELDS]; 279 int fieldc; 280 void (*f)(int c, char **v); 281 FILE *cf; 282 char line[MAXLINELEN]; 283 284 snprintf(line, sizeof(line), "reading %s", filename); 285 status(line); 286 strlcpy(curfilename, filename, sizeof(curfilename)); 287 288 if ((cf = fopen(curfilename, "r")) == NULL) { 289 warn("%s", curfilename); 290 goterror = 1; 291 return; 292 } 293 294 linenum = 0; 295 while (fgets(line, MAXLINELEN, cf) != NULL) { 296 linenum++; 297 parse_line(line, &fieldc, fieldv, MAXFIELDS); 298 299 if (fieldc < 1) 300 continue; 301 302 if (!strcmp(fieldv[0], "srcdirs")) 303 f = add_srcdirs; 304 else if(!strcmp(fieldv[0], "progs")) 305 f = add_progs; 306 else if(!strcmp(fieldv[0], "ln")) 307 f = add_link; 308 else if(!strcmp(fieldv[0], "libs")) 309 f = add_libs; 310 else if(!strcmp(fieldv[0], "libs_so")) 311 f = add_libs_so; 312 else if(!strcmp(fieldv[0], "buildopts")) 313 f = add_buildopts; 314 else if(!strcmp(fieldv[0], "special")) 315 f = add_special; 316 else { 317 warnx("%s:%d: skipping unknown command `%s'", 318 curfilename, linenum, fieldv[0]); 319 goterror = 1; 320 continue; 321 } 322 323 if (fieldc < 2) { 324 warnx("%s:%d: %s %s", 325 curfilename, linenum, fieldv[0], 326 "command needs at least 1 argument, skipping"); 327 goterror = 1; 328 continue; 329 } 330 331 f(fieldc, fieldv); 332 } 333 334 if (ferror(cf)) { 335 warn("%s", curfilename); 336 goterror = 1; 337 } 338 fclose(cf); 339 } 340 341 342 void 343 parse_line(char *line, int *fc, char **fv, int nf) 344 { 345 char *p; 346 347 p = line; 348 *fc = 0; 349 350 while (1) { 351 while (isspace(*p)) 352 p++; 353 354 if (*p == '\0' || *p == '#') 355 break; 356 357 if (*fc < nf) 358 fv[(*fc)++] = p; 359 360 while (*p && !isspace(*p) && *p != '#') 361 p++; 362 363 if (*p == '\0' || *p == '#') 364 break; 365 366 *p++ = '\0'; 367 } 368 369 if (*p) 370 *p = '\0'; /* needed for '#' case */ 371 } 372 373 374 void 375 add_srcdirs(int argc, char **argv) 376 { 377 int i; 378 379 for (i = 1; i < argc; i++) { 380 if (is_dir(argv[i])) 381 add_string(&srcdirs, argv[i]); 382 else { 383 warnx("%s:%d: `%s' is not a directory, skipping it", 384 curfilename, linenum, argv[i]); 385 goterror = 1; 386 } 387 } 388 } 389 390 391 void 392 add_progs(int argc, char **argv) 393 { 394 int i; 395 396 for (i = 1; i < argc; i++) 397 add_prog(argv[i]); 398 } 399 400 401 void 402 add_prog(char *progname) 403 { 404 prog_t *p1, *p2; 405 406 /* add to end, but be smart about dups */ 407 408 for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next) 409 if (!strcmp(p2->name, progname)) 410 return; 411 412 p2 = malloc(sizeof(prog_t)); 413 if(p2) { 414 memset(p2, 0, sizeof(prog_t)); 415 p2->name = strdup(progname); 416 } 417 if (!p2 || !p2->name) 418 out_of_memory(); 419 420 p2->next = NULL; 421 if (p1 == NULL) 422 progs = p2; 423 else 424 p1->next = p2; 425 426 p2->ident = NULL; 427 p2->srcdir = NULL; 428 p2->realsrcdir = NULL; 429 p2->objdir = NULL; 430 p2->links = NULL; 431 p2->libs = NULL; 432 p2->libs_so = NULL; 433 p2->objs = NULL; 434 p2->keeplist = NULL; 435 p2->buildopts = NULL; 436 p2->goterror = 0; 437 438 if (list_mode) 439 printf("%s\n",progname); 440 } 441 442 443 void 444 add_link(int argc, char **argv) 445 { 446 int i; 447 prog_t *p = find_prog(argv[1]); 448 449 if (p == NULL) { 450 warnx("%s:%d: no prog %s previously declared, skipping link", 451 curfilename, linenum, argv[1]); 452 goterror = 1; 453 return; 454 } 455 456 for (i = 2; i < argc; i++) { 457 if (list_mode) 458 printf("%s\n",argv[i]); 459 460 add_string(&p->links, argv[i]); 461 } 462 } 463 464 465 void 466 add_libs(int argc, char **argv) 467 { 468 int i; 469 470 for(i = 1; i < argc; i++) { 471 add_string(&libs, argv[i]); 472 if ( in_list(&libs_so, argv[i]) ) 473 warnx("%s:%d: " 474 "library `%s' specified as dynamic earlier", 475 curfilename, linenum, argv[i]); 476 } 477 } 478 479 480 void 481 add_libs_so(int argc, char **argv) 482 { 483 int i; 484 485 for(i = 1; i < argc; i++) { 486 add_string(&libs_so, argv[i]); 487 if ( in_list(&libs, argv[i]) ) 488 warnx("%s:%d: " 489 "library `%s' specified as static earlier", 490 curfilename, linenum, argv[i]); 491 } 492 } 493 494 495 void 496 add_buildopts(int argc, char **argv) 497 { 498 int i; 499 500 for (i = 1; i < argc; i++) 501 add_string(&buildopts, argv[i]); 502 } 503 504 505 void 506 add_special(int argc, char **argv) 507 { 508 int i; 509 prog_t *p = find_prog(argv[1]); 510 511 if (p == NULL) { 512 if (reading_cache) 513 return; 514 515 warnx("%s:%d: no prog %s previously declared, skipping special", 516 curfilename, linenum, argv[1]); 517 goterror = 1; 518 return; 519 } 520 521 if (!strcmp(argv[2], "ident")) { 522 if (argc != 4) 523 goto argcount; 524 if ((p->ident = strdup(argv[3])) == NULL) 525 out_of_memory(); 526 } else if (!strcmp(argv[2], "srcdir")) { 527 if (argc != 4) 528 goto argcount; 529 if ((p->srcdir = strdup(argv[3])) == NULL) 530 out_of_memory(); 531 } else if (!strcmp(argv[2], "objdir")) { 532 if(argc != 4) 533 goto argcount; 534 if((p->objdir = strdup(argv[3])) == NULL) 535 out_of_memory(); 536 } else if (!strcmp(argv[2], "objs")) { 537 p->objs = NULL; 538 for (i = 3; i < argc; i++) 539 add_string(&p->objs, argv[i]); 540 } else if (!strcmp(argv[2], "objpaths")) { 541 p->objpaths = NULL; 542 for (i = 3; i < argc; i++) 543 add_string(&p->objpaths, argv[i]); 544 } else if (!strcmp(argv[2], "keep")) { 545 p->keeplist = NULL; 546 for(i = 3; i < argc; i++) 547 add_string(&p->keeplist, argv[i]); 548 } else if (!strcmp(argv[2], "objvar")) { 549 if(argc != 4) 550 goto argcount; 551 if ((p->objvar = strdup(argv[3])) == NULL) 552 out_of_memory(); 553 } else if (!strcmp(argv[2], "buildopts")) { 554 p->buildopts = NULL; 555 for (i = 3; i < argc; i++) 556 add_string(&p->buildopts, argv[i]); 557 } else if (!strcmp(argv[2], "lib")) { 558 for (i = 3; i < argc; i++) 559 add_string(&p->libs, argv[i]); 560 } else { 561 warnx("%s:%d: bad parameter name `%s', skipping line", 562 curfilename, linenum, argv[2]); 563 goterror = 1; 564 } 565 return; 566 567 argcount: 568 warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"", 569 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]); 570 goterror = 1; 571 } 572 573 574 prog_t *find_prog(char *str) 575 { 576 prog_t *p; 577 578 for (p = progs; p != NULL; p = p->next) 579 if (!strcmp(p->name, str)) 580 return p; 581 582 return NULL; 583 } 584 585 586 /* 587 * ======================================================================== 588 * gen_outputs subsystem 589 * 590 */ 591 592 /* helper subroutines */ 593 594 void remove_error_progs(void); 595 void fillin_program(prog_t *p); 596 void gen_specials_cache(void); 597 void gen_output_makefile(void); 598 void gen_output_cfile(void); 599 600 void fillin_program_objs(prog_t *p, char *path); 601 void top_makefile_rules(FILE *outmk); 602 void prog_makefile_rules(FILE *outmk, prog_t *p); 603 void output_strlst(FILE *outf, strlst_t *lst); 604 char *genident(char *str); 605 char *dir_search(char *progname); 606 607 608 void 609 gen_outputs(void) 610 { 611 prog_t *p; 612 613 for (p = progs; p != NULL; p = p->next) 614 fillin_program(p); 615 616 remove_error_progs(); 617 gen_specials_cache(); 618 gen_output_cfile(); 619 gen_output_makefile(); 620 status(""); 621 fprintf(stderr, 622 "Run \"%s -f %s\" to build crunched binary.\n", 623 path_make, outmkname); 624 } 625 626 /* 627 * run the makefile for the program to find which objects are necessary 628 */ 629 void 630 fillin_program(prog_t *p) 631 { 632 char path[MAXPATHLEN]; 633 char line[MAXLINELEN]; 634 FILE *f; 635 636 snprintf(line, MAXLINELEN, "filling in parms for %s", p->name); 637 status(line); 638 639 if (!p->ident) 640 p->ident = genident(p->name); 641 642 /* look for the source directory if one wasn't specified by a special */ 643 if (!p->srcdir) { 644 p->srcdir = dir_search(p->name); 645 } 646 647 /* Determine the actual srcdir (maybe symlinked). */ 648 if (p->srcdir) { 649 snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`", 650 p->srcdir); 651 f = popen(line,"r"); 652 if (!f) 653 errx(1, "Can't execute: %s\n", line); 654 655 path[0] = '\0'; 656 fgets(path, sizeof path, f); 657 if (pclose(f)) 658 errx(1, "Can't execute: %s\n", line); 659 660 if (!*path) 661 errx(1, "Can't perform pwd on: %s\n", p->srcdir); 662 663 p->realsrcdir = strdup(path); 664 } 665 666 /* Unless the option to make object files was specified the 667 * the objects will be built in the source directory unless 668 * an object directory already exists. 669 */ 670 if (!makeobj && !p->objdir && p->srcdir) { 671 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir); 672 if (is_dir(line)) { 673 if ((p->objdir = strdup(line)) == NULL) 674 out_of_memory(); 675 } else 676 p->objdir = p->realsrcdir; 677 } 678 679 /* 680 * XXX look for a Makefile.{name} in local directory first. 681 * This lets us override the original Makefile. 682 */ 683 snprintf(path, sizeof(path), "Makefile.%s", p->name); 684 if (is_nonempty_file(path)) { 685 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name); 686 status(line); 687 } else 688 if (p->srcdir) 689 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir); 690 if (!p->objs && p->srcdir && is_nonempty_file(path)) 691 fillin_program_objs(p, path); 692 693 if (!p->srcdir && !p->objdir && verbose) 694 warnx("%s: %s: %s", 695 "warning: could not find source directory", 696 infilename, p->name); 697 if (!p->objs && verbose) 698 warnx("%s: %s: warning: could not find any .o files", 699 infilename, p->name); 700 701 if ((!p->srcdir || !p->objdir) && !p->objs) 702 p->goterror = 1; 703 } 704 705 void 706 fillin_program_objs(prog_t *p, char *path) 707 { 708 char *obj, *cp; 709 int fd, rc; 710 FILE *f; 711 char *objvar="OBJS"; 712 strlst_t *s; 713 char line[MAXLINELEN]; 714 715 /* discover the objs from the srcdir Makefile */ 716 717 if ((fd = mkstemp(tempfname)) == -1) { 718 perror(tempfname); 719 exit(1); 720 } 721 if ((f = fdopen(fd, "w")) == NULL) { 722 warn("%s", tempfname); 723 goterror = 1; 724 return; 725 } 726 if (p->objvar) 727 objvar = p->objvar; 728 729 /* 730 * XXX include outhdrname (e.g. to contain Make variables) 731 */ 732 if (outhdrname[0] != '\0') 733 fprintf(f, ".include \"%s\"\n", outhdrname); 734 fprintf(f, ".include \"%s\"\n", path); 735 fprintf(f, ".POSIX:\n"); 736 if (buildopts) { 737 fprintf(f, "BUILDOPTS+="); 738 output_strlst(f, buildopts); 739 } 740 fprintf(f, ".if defined(PROG)\n"); 741 fprintf(f, "%s?=${PROG}.o\n", objvar); 742 fprintf(f, ".endif\n"); 743 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar); 744 745 fprintf(f, "crunchgen_objs:\n" 746 "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)", 747 p->srcdir, path_make, tempfname, p->ident); 748 for (s = p->buildopts; s != NULL; s = s->next) 749 fprintf(f, " %s", s->str); 750 fprintf(f, " loop\n"); 751 752 fclose(f); 753 754 snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs", 755 p->srcdir, path_make, tempfname); 756 if ((f = popen(line, "r")) == NULL) { 757 warn("submake pipe"); 758 goterror = 1; 759 return; 760 } 761 762 while(fgets(line, MAXLINELEN, f)) { 763 if (strncmp(line, "OBJS= ", 6)) { 764 warnx("make error: %s", line); 765 goterror = 1; 766 continue; 767 } 768 769 cp = line + 6; 770 while (isspace(*cp)) 771 cp++; 772 773 while(*cp) { 774 obj = cp; 775 while (*cp && !isspace(*cp)) 776 cp++; 777 if (*cp) 778 *cp++ = '\0'; 779 add_string(&p->objs, obj); 780 while (isspace(*cp)) 781 cp++; 782 } 783 } 784 785 if ((rc=pclose(f)) != 0) { 786 warnx("make error: make returned %d", rc); 787 goterror = 1; 788 } 789 790 unlink(tempfname); 791 } 792 793 void 794 remove_error_progs(void) 795 { 796 prog_t *p1, *p2; 797 798 p1 = NULL; p2 = progs; 799 while (p2 != NULL) { 800 if (!p2->goterror) 801 p1 = p2, p2 = p2->next; 802 else { 803 /* delete it from linked list */ 804 warnx("%s: %s: ignoring program because of errors", 805 infilename, p2->name); 806 if (p1) 807 p1->next = p2->next; 808 else 809 progs = p2->next; 810 p2 = p2->next; 811 } 812 } 813 } 814 815 void 816 gen_specials_cache(void) 817 { 818 FILE *cachef; 819 prog_t *p; 820 char line[MAXLINELEN]; 821 822 snprintf(line, MAXLINELEN, "generating %s", cachename); 823 status(line); 824 825 if ((cachef = fopen(cachename, "w")) == NULL) { 826 warn("%s", cachename); 827 goterror = 1; 828 return; 829 } 830 831 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen " 832 " %s\n\n", 833 cachename, infilename, CRUNCH_VERSION); 834 835 for (p = progs; p != NULL; p = p->next) { 836 fprintf(cachef, "\n"); 837 if (p->srcdir) 838 fprintf(cachef, "special %s srcdir %s\n", 839 p->name, p->srcdir); 840 if (p->objdir) 841 fprintf(cachef, "special %s objdir %s\n", 842 p->name, p->objdir); 843 if (p->objs) { 844 fprintf(cachef, "special %s objs", p->name); 845 output_strlst(cachef, p->objs); 846 } 847 if (p->objpaths) { 848 fprintf(cachef, "special %s objpaths", p->name); 849 output_strlst(cachef, p->objpaths); 850 } 851 } 852 fclose(cachef); 853 } 854 855 856 void 857 gen_output_makefile(void) 858 { 859 prog_t *p; 860 FILE *outmk; 861 char line[MAXLINELEN]; 862 863 snprintf(line, MAXLINELEN, "generating %s", outmkname); 864 status(line); 865 866 if ((outmk = fopen(outmkname, "w")) == NULL) { 867 warn("%s", outmkname); 868 goterror = 1; 869 return; 870 } 871 872 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n", 873 outmkname, infilename, CRUNCH_VERSION); 874 875 if (outhdrname[0] != '\0') 876 fprintf(outmk, ".include \"%s\"\n", outhdrname); 877 878 top_makefile_rules(outmk); 879 for (p = progs; p != NULL; p = p->next) 880 prog_makefile_rules(outmk, p); 881 882 fprintf(outmk, "\n# ========\n"); 883 fclose(outmk); 884 } 885 886 887 void 888 gen_output_cfile(void) 889 { 890 extern char *crunched_skel[]; 891 char **cp; 892 FILE *outcf; 893 prog_t *p; 894 strlst_t *s; 895 char line[MAXLINELEN]; 896 897 snprintf(line, MAXLINELEN, "generating %s", outcfname); 898 status(line); 899 900 if((outcf = fopen(outcfname, "w")) == NULL) { 901 warn("%s", outcfname); 902 goterror = 1; 903 return; 904 } 905 906 fprintf(outcf, 907 "/* %s - generated from %s by crunchgen %s */\n", 908 outcfname, infilename, CRUNCH_VERSION); 909 910 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname); 911 for (cp = crunched_skel; *cp != NULL; cp++) 912 fprintf(outcf, "%s\n", *cp); 913 914 for (p = progs; p != NULL; p = p->next) 915 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident); 916 917 fprintf(outcf, "\nstruct stub entry_points[] = {\n"); 918 for (p = progs; p != NULL; p = p->next) { 919 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", 920 p->name, p->ident); 921 for (s = p->links; s != NULL; s = s->next) 922 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", 923 s->str, p->ident); 924 } 925 926 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n"); 927 fprintf(outcf, "\t{ NULL, NULL }\n};\n"); 928 fclose(outcf); 929 } 930 931 932 char *genident(char *str) 933 { 934 char *n, *s, *d; 935 936 /* 937 * generates a Makefile/C identifier from a program name, 938 * mapping '-' to '_' and ignoring all other non-identifier 939 * characters. This leads to programs named "foo.bar" and 940 * "foobar" to map to the same identifier. 941 */ 942 943 if ((n = strdup(str)) == NULL) 944 return NULL; 945 for (d = s = n; *s != '\0'; s++) { 946 if (*s == '-') 947 *d++ = '_'; 948 else if (*s == '_' || isalnum(*s)) 949 *d++ = *s; 950 } 951 *d = '\0'; 952 return n; 953 } 954 955 956 char *dir_search(char *progname) 957 { 958 char path[MAXPATHLEN]; 959 strlst_t *dir; 960 char *srcdir; 961 962 for (dir = srcdirs; dir != NULL; dir = dir->next) { 963 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname); 964 if (!is_dir(path)) 965 continue; 966 967 if ((srcdir = strdup(path)) == NULL) 968 out_of_memory(); 969 970 return srcdir; 971 } 972 return NULL; 973 } 974 975 976 void 977 top_makefile_rules(FILE *outmk) 978 { 979 prog_t *p; 980 981 if ( subtract_strlst(&libs, &libs_so) ) 982 fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n"); 983 984 fprintf(outmk, "LIBS+="); 985 output_strlst(outmk, libs); 986 987 fprintf(outmk, "LIBS_SO+="); 988 output_strlst(outmk, libs_so); 989 990 if (makeobj) { 991 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix); 992 fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n"); 993 fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n"); 994 } else { 995 fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n"); 996 } 997 998 if (buildopts) { 999 fprintf(outmk, "BUILDOPTS+="); 1000 output_strlst(outmk, buildopts); 1001 } 1002 1003 fprintf(outmk, "CRUNCHED_OBJS="); 1004 for (p = progs; p != NULL; p = p->next) 1005 fprintf(outmk, " %s.lo", p->name); 1006 fprintf(outmk, "\n"); 1007 1008 fprintf(outmk, "SUBMAKE_TARGETS="); 1009 for (p = progs; p != NULL; p = p->next) 1010 fprintf(outmk, " %s_make", p->ident); 1011 fprintf(outmk, "\nSUBCLEAN_TARGETS="); 1012 for (p = progs; p != NULL; p = p->next) 1013 fprintf(outmk, " %s_clean", p->ident); 1014 fprintf(outmk, "\n\n"); 1015 1016 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n"); 1017 fprintf(outmk, "exe: %s\n", execfname); 1018 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname); 1019 fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n"); 1020 fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n", 1021 execfname, execfname); 1022 fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n"); 1023 fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n"); 1024 fprintf(outmk, ".else\n"); 1025 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n", 1026 execfname, execfname); 1027 fprintf(outmk, ".endif\n"); 1028 fprintf(outmk, "\tstrip %s\n", execfname); 1029 fprintf(outmk, "realclean: clean subclean\n"); 1030 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname); 1031 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n"); 1032 } 1033 1034 1035 void 1036 prog_makefile_rules(FILE *outmk, prog_t *p) 1037 { 1038 strlst_t *lst; 1039 1040 fprintf(outmk, "\n# -------- %s\n\n", p->name); 1041 1042 fprintf(outmk, "%s_OBJDIR=", p->ident); 1043 if (p->objdir) 1044 fprintf(outmk, "%s", p->objdir); 1045 else 1046 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n", 1047 p->ident); 1048 fprintf(outmk, "\n"); 1049 1050 fprintf(outmk, "%s_OBJPATHS=", p->ident); 1051 if (p->objpaths) 1052 output_strlst(outmk, p->objpaths); 1053 else { 1054 for (lst = p->objs; lst != NULL; lst = lst->next) { 1055 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str); 1056 } 1057 fprintf(outmk, "\n"); 1058 } 1059 1060 if (p->srcdir && p->objs) { 1061 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir); 1062 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir); 1063 1064 fprintf(outmk, "%s_OBJS=", p->ident); 1065 output_strlst(outmk, p->objs); 1066 if (p->buildopts != NULL) { 1067 fprintf(outmk, "%s_OPTS+=", p->ident); 1068 output_strlst(outmk, p->buildopts); 1069 } 1070 #if 0 1071 fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident); 1072 #endif 1073 fprintf(outmk, "%s_make:\n", p->ident); 1074 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident); 1075 if (makeobj) 1076 fprintf(outmk, "$(CRUNCHMAKE) obj && "); 1077 fprintf(outmk, "\\\n"); 1078 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&", 1079 p->ident); 1080 fprintf(outmk, "\\\n"); 1081 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) " 1082 "$(%s_OBJS))", 1083 p->ident, p->ident); 1084 fprintf(outmk, "\n"); 1085 fprintf(outmk, "%s_clean:\n", p->ident); 1086 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n", 1087 p->ident); 1088 } else { 1089 fprintf(outmk, "%s_make:\n", p->ident); 1090 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n", 1091 p->name); 1092 } 1093 1094 if (p->libs) { 1095 fprintf(outmk, "%s_LIBS=", p->ident); 1096 output_strlst(outmk, p->libs); 1097 } 1098 1099 fprintf(outmk, "%s_stub.c:\n", p->name); 1100 fprintf(outmk, "\techo \"" 1101 "int _crunched_%s_stub(int argc, char **argv, char **envp)" 1102 "{return main(argc,argv,envp);}\" >%s_stub.c\n", 1103 p->ident, p->name); 1104 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)", 1105 p->name, p->name, p->ident); 1106 if (p->libs) 1107 fprintf(outmk, " $(%s_LIBS)", p->ident); 1108 1109 fprintf(outmk, "\n"); 1110 fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)", 1111 p->name, p->name, p->ident); 1112 if (p->libs) 1113 fprintf(outmk, " $(%s_LIBS)", p->ident); 1114 fprintf(outmk, "\n"); 1115 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident); 1116 for (lst = p->keeplist; lst != NULL; lst = lst->next) 1117 fprintf(outmk, "-k _%s ", lst->str); 1118 fprintf(outmk, "%s.lo\n", p->name); 1119 } 1120 1121 void 1122 output_strlst(FILE *outf, strlst_t *lst) 1123 { 1124 for (; lst != NULL; lst = lst->next) 1125 if ( strlen(lst->str) ) 1126 fprintf(outf, " %s", lst->str); 1127 fprintf(outf, "\n"); 1128 } 1129 1130 1131 /* 1132 * ======================================================================== 1133 * general library routines 1134 * 1135 */ 1136 1137 void 1138 status(char *str) 1139 { 1140 static int lastlen = 0; 1141 int len, spaces; 1142 1143 if (!verbose) 1144 return; 1145 1146 len = strlen(str); 1147 spaces = lastlen - len; 1148 if (spaces < 1) 1149 spaces = 1; 1150 1151 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " "); 1152 fflush(stderr); 1153 lastlen = len; 1154 } 1155 1156 1157 void 1158 out_of_memory(void) 1159 { 1160 err(1, "%s: %d: out of memory, stopping", infilename, linenum); 1161 } 1162 1163 1164 void 1165 add_string(strlst_t **listp, char *str) 1166 { 1167 strlst_t *p1, *p2; 1168 1169 /* add to end, but be smart about dups */ 1170 1171 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next) 1172 if (!strcmp(p2->str, str)) 1173 return; 1174 1175 p2 = malloc(sizeof(strlst_t)); 1176 if (p2) { 1177 p2->next = NULL; 1178 p2->str = strdup(str); 1179 } 1180 if (!p2 || !p2->str) 1181 out_of_memory(); 1182 1183 if (p1 == NULL) 1184 *listp = p2; 1185 else 1186 p1->next = p2; 1187 } 1188 1189 int 1190 subtract_strlst(strlst_t **lista, strlst_t **listb) 1191 { 1192 int subtract_count = 0; 1193 strlst_t *p1; 1194 for (p1 = *listb; p1 != NULL; p1 = p1->next) 1195 if ( in_list(lista, p1->str) ) { 1196 warnx("Will compile library `%s' dynamically", p1->str); 1197 strcat(p1->str, ""); 1198 subtract_count++; 1199 } 1200 return subtract_count; 1201 } 1202 1203 int 1204 in_list(strlst_t **listp, char *str) 1205 { 1206 strlst_t *p1; 1207 for (p1 = *listp; p1 != NULL; p1 = p1->next) 1208 if (!strcmp(p1->str, str)) 1209 return 1; 1210 return 0; 1211 } 1212 1213 int 1214 is_dir(char *pathname) 1215 { 1216 struct stat buf; 1217 1218 if (stat(pathname, &buf) == -1) 1219 return 0; 1220 1221 return S_ISDIR(buf.st_mode); 1222 } 1223 1224 int 1225 is_nonempty_file(char *pathname) 1226 { 1227 struct stat buf; 1228 1229 if (stat(pathname, &buf) == -1) 1230 return 0; 1231 1232 return S_ISREG(buf.st_mode) && buf.st_size > 0; 1233 } 1234