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