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