1 /* $NetBSD: suff.c,v 1.54 2006/06/29 22:02:06 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1989 by Berkeley Softworks 37 * All rights reserved. 38 * 39 * This code is derived from software contributed to Berkeley by 40 * Adam de Boor. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #ifndef MAKE_NATIVE 72 static char rcsid[] = "$NetBSD: suff.c,v 1.54 2006/06/29 22:02:06 rillig Exp $"; 73 #else 74 #include <sys/cdefs.h> 75 #ifndef lint 76 #if 0 77 static char sccsid[] = "@(#)suff.c 8.4 (Berkeley) 3/21/94"; 78 #else 79 __RCSID("$NetBSD: suff.c,v 1.54 2006/06/29 22:02:06 rillig Exp $"); 80 #endif 81 #endif /* not lint */ 82 #endif 83 84 /*- 85 * suff.c -- 86 * Functions to maintain suffix lists and find implicit dependents 87 * using suffix transformation rules 88 * 89 * Interface: 90 * Suff_Init Initialize all things to do with suffixes. 91 * 92 * Suff_End Cleanup the module 93 * 94 * Suff_DoPaths This function is used to make life easier 95 * when searching for a file according to its 96 * suffix. It takes the global search path, 97 * as defined using the .PATH: target, and appends 98 * its directories to the path of each of the 99 * defined suffixes, as specified using 100 * .PATH<suffix>: targets. In addition, all 101 * directories given for suffixes labeled as 102 * include files or libraries, using the .INCLUDES 103 * or .LIBS targets, are played with using 104 * Dir_MakeFlags to create the .INCLUDES and 105 * .LIBS global variables. 106 * 107 * Suff_ClearSuffixes Clear out all the suffixes and defined 108 * transformations. 109 * 110 * Suff_IsTransform Return TRUE if the passed string is the lhs 111 * of a transformation rule. 112 * 113 * Suff_AddSuffix Add the passed string as another known suffix. 114 * 115 * Suff_GetPath Return the search path for the given suffix. 116 * 117 * Suff_AddInclude Mark the given suffix as denoting an include 118 * file. 119 * 120 * Suff_AddLib Mark the given suffix as denoting a library. 121 * 122 * Suff_AddTransform Add another transformation to the suffix 123 * graph. Returns GNode suitable for framing, I 124 * mean, tacking commands, attributes, etc. on. 125 * 126 * Suff_SetNull Define the suffix to consider the suffix of 127 * any file that doesn't have a known one. 128 * 129 * Suff_FindDeps Find implicit sources for and the location of 130 * a target based on its suffix. Returns the 131 * bottom-most node added to the graph or NILGNODE 132 * if the target had no implicit sources. 133 * 134 * Suff_FindPath Return the appropriate path to search in 135 * order to find the node. 136 */ 137 138 #include <stdio.h> 139 #include "make.h" 140 #include "hash.h" 141 #include "dir.h" 142 143 static Lst sufflist; /* Lst of suffixes */ 144 #ifdef CLEANUP 145 static Lst suffClean; /* Lst of suffixes to be cleaned */ 146 #endif 147 static Lst srclist; /* Lst of sources */ 148 static Lst transforms; /* Lst of transformation rules */ 149 150 static int sNum = 0; /* Counter for assigning suffix numbers */ 151 152 /* 153 * Structure describing an individual suffix. 154 */ 155 typedef struct _Suff { 156 char *name; /* The suffix itself */ 157 int nameLen; /* Length of the suffix */ 158 short flags; /* Type of suffix */ 159 #define SUFF_INCLUDE 0x01 /* One which is #include'd */ 160 #define SUFF_LIBRARY 0x02 /* One which contains a library */ 161 #define SUFF_NULL 0x04 /* The empty suffix */ 162 Lst searchPath; /* The path along which files of this suffix 163 * may be found */ 164 int sNum; /* The suffix number */ 165 int refCount; /* Reference count of list membership */ 166 Lst parents; /* Suffixes we have a transformation to */ 167 Lst children; /* Suffixes we have a transformation from */ 168 Lst ref; /* List of lists this suffix is referenced */ 169 } Suff; 170 171 /* 172 * for SuffSuffIsSuffix 173 */ 174 typedef struct { 175 char *ename; /* The end of the name */ 176 int len; /* Length of the name */ 177 } SuffixCmpData; 178 179 /* 180 * Structure used in the search for implied sources. 181 */ 182 typedef struct _Src { 183 char *file; /* The file to look for */ 184 char *pref; /* Prefix from which file was formed */ 185 Suff *suff; /* The suffix on the file */ 186 struct _Src *parent; /* The Src for which this is a source */ 187 GNode *node; /* The node describing the file */ 188 int children; /* Count of existing children (so we don't free 189 * this thing too early or never nuke it) */ 190 #ifdef DEBUG_SRC 191 Lst cp; /* Debug; children list */ 192 #endif 193 } Src; 194 195 /* 196 * A structure for passing more than one argument to the Lst-library-invoked 197 * function... 198 */ 199 typedef struct { 200 Lst l; 201 Src *s; 202 } LstSrc; 203 204 typedef struct { 205 GNode **gn; 206 Suff *s; 207 Boolean r; 208 } GNodeSuff; 209 210 static Suff *suffNull; /* The NULL suffix for this run */ 211 static Suff *emptySuff; /* The empty suffix required for POSIX 212 * single-suffix transformation rules */ 213 214 215 static char *SuffStrIsPrefix(char *, char *); 216 static char *SuffSuffIsSuffix(Suff *, SuffixCmpData *); 217 static int SuffSuffIsSuffixP(ClientData, ClientData); 218 static int SuffSuffHasNameP(ClientData, ClientData); 219 static int SuffSuffIsPrefix(ClientData, ClientData); 220 static int SuffGNHasNameP(ClientData, ClientData); 221 static void SuffUnRef(ClientData, ClientData); 222 static void SuffFree(ClientData); 223 static void SuffInsert(Lst, Suff *); 224 static void SuffRemove(Lst, Suff *); 225 static Boolean SuffParseTransform(char *, Suff **, Suff **); 226 static int SuffRebuildGraph(ClientData, ClientData); 227 static int SuffScanTargets(ClientData, ClientData); 228 static int SuffAddSrc(ClientData, ClientData); 229 static int SuffRemoveSrc(Lst); 230 static void SuffAddLevel(Lst, Src *); 231 static Src *SuffFindThem(Lst, Lst); 232 static Src *SuffFindCmds(Src *, Lst); 233 static int SuffExpandChildren(LstNode, GNode *); 234 static Boolean SuffApplyTransform(GNode *, GNode *, Suff *, Suff *); 235 static void SuffFindDeps(GNode *, Lst); 236 static void SuffFindArchiveDeps(GNode *, Lst); 237 static void SuffFindNormalDeps(GNode *, Lst); 238 static int SuffPrintName(ClientData, ClientData); 239 static int SuffPrintSuff(ClientData, ClientData); 240 static int SuffPrintTrans(ClientData, ClientData); 241 242 /*************** Lst Predicates ****************/ 243 /*- 244 *----------------------------------------------------------------------- 245 * SuffStrIsPrefix -- 246 * See if pref is a prefix of str. 247 * 248 * Input: 249 * pref possible prefix 250 * str string to check 251 * 252 * Results: 253 * NULL if it ain't, pointer to character in str after prefix if so 254 * 255 * Side Effects: 256 * None 257 *----------------------------------------------------------------------- 258 */ 259 static char * 260 SuffStrIsPrefix(char *pref, char *str) 261 { 262 while (*str && *pref == *str) { 263 pref++; 264 str++; 265 } 266 267 return (*pref ? NULL : str); 268 } 269 270 /*- 271 *----------------------------------------------------------------------- 272 * SuffSuffIsSuffix -- 273 * See if suff is a suffix of str. sd->ename should point to THE END 274 * of the string to check. (THE END == the null byte) 275 * 276 * Input: 277 * s possible suffix 278 * sd string to examine 279 * 280 * Results: 281 * NULL if it ain't, pointer to character in str before suffix if 282 * it is. 283 * 284 * Side Effects: 285 * None 286 *----------------------------------------------------------------------- 287 */ 288 static char * 289 SuffSuffIsSuffix(Suff *s, SuffixCmpData *sd) 290 { 291 char *p1; /* Pointer into suffix name */ 292 char *p2; /* Pointer into string being examined */ 293 294 if (sd->len < s->nameLen) 295 return NULL; /* this string is shorter than the suffix */ 296 297 p1 = s->name + s->nameLen; 298 p2 = sd->ename; 299 300 while (p1 >= s->name && *p1 == *p2) { 301 p1--; 302 p2--; 303 } 304 305 return (p1 == s->name - 1 ? p2 : NULL); 306 } 307 308 /*- 309 *----------------------------------------------------------------------- 310 * SuffSuffIsSuffixP -- 311 * Predicate form of SuffSuffIsSuffix. Passed as the callback function 312 * to Lst_Find. 313 * 314 * Results: 315 * 0 if the suffix is the one desired, non-zero if not. 316 * 317 * Side Effects: 318 * None. 319 * 320 *----------------------------------------------------------------------- 321 */ 322 static int 323 SuffSuffIsSuffixP(ClientData s, ClientData sd) 324 { 325 return(!SuffSuffIsSuffix((Suff *)s, (SuffixCmpData *)sd)); 326 } 327 328 /*- 329 *----------------------------------------------------------------------- 330 * SuffSuffHasNameP -- 331 * Callback procedure for finding a suffix based on its name. Used by 332 * Suff_GetPath. 333 * 334 * Input: 335 * s Suffix to check 336 * sd Desired name 337 * 338 * Results: 339 * 0 if the suffix is of the given name. non-zero otherwise. 340 * 341 * Side Effects: 342 * None 343 *----------------------------------------------------------------------- 344 */ 345 static int 346 SuffSuffHasNameP(ClientData s, ClientData sname) 347 { 348 return (strcmp((char *)sname, ((Suff *)s)->name)); 349 } 350 351 /*- 352 *----------------------------------------------------------------------- 353 * SuffSuffIsPrefix -- 354 * See if the suffix described by s is a prefix of the string. Care 355 * must be taken when using this to search for transformations and 356 * what-not, since there could well be two suffixes, one of which 357 * is a prefix of the other... 358 * 359 * Input: 360 * s suffix to compare 361 * str string to examine 362 * 363 * Results: 364 * 0 if s is a prefix of str. non-zero otherwise 365 * 366 * Side Effects: 367 * None 368 *----------------------------------------------------------------------- 369 */ 370 static int 371 SuffSuffIsPrefix(ClientData s, ClientData str) 372 { 373 return (SuffStrIsPrefix(((Suff *)s)->name, (char *)str) == NULL ? 1 : 0); 374 } 375 376 /*- 377 *----------------------------------------------------------------------- 378 * SuffGNHasNameP -- 379 * See if the graph node has the desired name 380 * 381 * Input: 382 * gn current node we're looking at 383 * name name we're looking for 384 * 385 * Results: 386 * 0 if it does. non-zero if it doesn't 387 * 388 * Side Effects: 389 * None 390 *----------------------------------------------------------------------- 391 */ 392 static int 393 SuffGNHasNameP(ClientData gn, ClientData name) 394 { 395 return (strcmp((char *)name, ((GNode *)gn)->name)); 396 } 397 398 /*********** Maintenance Functions ************/ 399 400 static void 401 SuffUnRef(ClientData lp, ClientData sp) 402 { 403 Lst l = (Lst) lp; 404 405 LstNode ln = Lst_Member(l, sp); 406 if (ln != NILLNODE) { 407 Lst_Remove(l, ln); 408 ((Suff *)sp)->refCount--; 409 } 410 } 411 412 /*- 413 *----------------------------------------------------------------------- 414 * SuffFree -- 415 * Free up all memory associated with the given suffix structure. 416 * 417 * Results: 418 * none 419 * 420 * Side Effects: 421 * the suffix entry is detroyed 422 *----------------------------------------------------------------------- 423 */ 424 static void 425 SuffFree(ClientData sp) 426 { 427 Suff *s = (Suff *)sp; 428 429 if (s == suffNull) 430 suffNull = NULL; 431 432 if (s == emptySuff) 433 emptySuff = NULL; 434 435 #ifdef notdef 436 /* We don't delete suffixes in order, so we cannot use this */ 437 if (s->refCount) 438 Punt("Internal error deleting suffix `%s' with refcount = %d", s->name, 439 s->refCount); 440 #endif 441 442 Lst_Destroy(s->ref, NOFREE); 443 Lst_Destroy(s->children, NOFREE); 444 Lst_Destroy(s->parents, NOFREE); 445 Lst_Destroy(s->searchPath, Dir_Destroy); 446 447 free(s->name); 448 free(s); 449 } 450 451 /*- 452 *----------------------------------------------------------------------- 453 * SuffRemove -- 454 * Remove the suffix into the list 455 * 456 * Results: 457 * None 458 * 459 * Side Effects: 460 * The reference count for the suffix is decremented and the 461 * suffix is possibly freed 462 *----------------------------------------------------------------------- 463 */ 464 static void 465 SuffRemove(Lst l, Suff *s) 466 { 467 SuffUnRef((ClientData) l, (ClientData) s); 468 if (s->refCount == 0) { 469 SuffUnRef((ClientData) sufflist, (ClientData) s); 470 SuffFree((ClientData) s); 471 } 472 } 473 474 /*- 475 *----------------------------------------------------------------------- 476 * SuffInsert -- 477 * Insert the suffix into the list keeping the list ordered by suffix 478 * numbers. 479 * 480 * Input: 481 * l the list where in s should be inserted 482 * s the suffix to insert 483 * 484 * Results: 485 * None 486 * 487 * Side Effects: 488 * The reference count of the suffix is incremented 489 *----------------------------------------------------------------------- 490 */ 491 static void 492 SuffInsert(Lst l, Suff *s) 493 { 494 LstNode ln; /* current element in l we're examining */ 495 Suff *s2 = NULL; /* the suffix descriptor in this element */ 496 497 if (Lst_Open(l) == FAILURE) { 498 return; 499 } 500 while ((ln = Lst_Next(l)) != NILLNODE) { 501 s2 = (Suff *)Lst_Datum(ln); 502 if (s2->sNum >= s->sNum) { 503 break; 504 } 505 } 506 507 Lst_Close(l); 508 if (DEBUG(SUFF)) { 509 printf("inserting %s(%d)...", s->name, s->sNum); 510 } 511 if (ln == NILLNODE) { 512 if (DEBUG(SUFF)) { 513 printf("at end of list\n"); 514 } 515 (void)Lst_AtEnd(l, (ClientData)s); 516 s->refCount++; 517 (void)Lst_AtEnd(s->ref, (ClientData) l); 518 } else if (s2->sNum != s->sNum) { 519 if (DEBUG(SUFF)) { 520 printf("before %s(%d)\n", s2->name, s2->sNum); 521 } 522 (void)Lst_Insert(l, ln, (ClientData)s); 523 s->refCount++; 524 (void)Lst_AtEnd(s->ref, (ClientData) l); 525 } else if (DEBUG(SUFF)) { 526 printf("already there\n"); 527 } 528 } 529 530 /*- 531 *----------------------------------------------------------------------- 532 * Suff_ClearSuffixes -- 533 * This is gross. Nuke the list of suffixes but keep all transformation 534 * rules around. The transformation graph is destroyed in this process, 535 * but we leave the list of rules so when a new graph is formed the rules 536 * will remain. 537 * This function is called from the parse module when a 538 * .SUFFIXES:\n line is encountered. 539 * 540 * Results: 541 * none 542 * 543 * Side Effects: 544 * the sufflist and its graph nodes are destroyed 545 *----------------------------------------------------------------------- 546 */ 547 void 548 Suff_ClearSuffixes(void) 549 { 550 #ifdef CLEANUP 551 Lst_Concat(suffClean, sufflist, LST_CONCLINK); 552 #endif 553 sufflist = Lst_Init(FALSE); 554 sNum = 0; 555 suffNull = emptySuff; 556 } 557 558 /*- 559 *----------------------------------------------------------------------- 560 * SuffParseTransform -- 561 * Parse a transformation string to find its two component suffixes. 562 * 563 * Input: 564 * str String being parsed 565 * srcPtr Place to store source of trans. 566 * targPtr Place to store target of trans. 567 * 568 * Results: 569 * TRUE if the string is a valid transformation and FALSE otherwise. 570 * 571 * Side Effects: 572 * The passed pointers are overwritten. 573 * 574 *----------------------------------------------------------------------- 575 */ 576 static Boolean 577 SuffParseTransform(char *str, Suff **srcPtr, Suff **targPtr) 578 { 579 LstNode srcLn; /* element in suffix list of trans source*/ 580 Suff *src; /* Source of transformation */ 581 LstNode targLn; /* element in suffix list of trans target*/ 582 char *str2; /* Extra pointer (maybe target suffix) */ 583 LstNode singleLn; /* element in suffix list of any suffix 584 * that exactly matches str */ 585 Suff *single = NULL;/* Source of possible transformation to 586 * null suffix */ 587 588 srcLn = NILLNODE; 589 singleLn = NILLNODE; 590 591 /* 592 * Loop looking first for a suffix that matches the start of the 593 * string and then for one that exactly matches the rest of it. If 594 * we can find two that meet these criteria, we've successfully 595 * parsed the string. 596 */ 597 for (;;) { 598 if (srcLn == NILLNODE) { 599 srcLn = Lst_Find(sufflist, (ClientData)str, SuffSuffIsPrefix); 600 } else { 601 srcLn = Lst_FindFrom(sufflist, Lst_Succ(srcLn), (ClientData)str, 602 SuffSuffIsPrefix); 603 } 604 if (srcLn == NILLNODE) { 605 /* 606 * Ran out of source suffixes -- no such rule 607 */ 608 if (singleLn != NILLNODE) { 609 /* 610 * Not so fast Mr. Smith! There was a suffix that encompassed 611 * the entire string, so we assume it was a transformation 612 * to the null suffix (thank you POSIX). We still prefer to 613 * find a double rule over a singleton, hence we leave this 614 * check until the end. 615 * 616 * XXX: Use emptySuff over suffNull? 617 */ 618 *srcPtr = single; 619 *targPtr = suffNull; 620 return(TRUE); 621 } 622 return (FALSE); 623 } 624 src = (Suff *)Lst_Datum(srcLn); 625 str2 = str + src->nameLen; 626 if (*str2 == '\0') { 627 single = src; 628 singleLn = srcLn; 629 } else { 630 targLn = Lst_Find(sufflist, (ClientData)str2, SuffSuffHasNameP); 631 if (targLn != NILLNODE) { 632 *srcPtr = src; 633 *targPtr = (Suff *)Lst_Datum(targLn); 634 return (TRUE); 635 } 636 } 637 } 638 } 639 640 /*- 641 *----------------------------------------------------------------------- 642 * Suff_IsTransform -- 643 * Return TRUE if the given string is a transformation rule 644 * 645 * 646 * Input: 647 * str string to check 648 * 649 * Results: 650 * TRUE if the string is a concatenation of two known suffixes. 651 * FALSE otherwise 652 * 653 * Side Effects: 654 * None 655 *----------------------------------------------------------------------- 656 */ 657 Boolean 658 Suff_IsTransform(char *str) 659 { 660 Suff *src, *targ; 661 662 return (SuffParseTransform(str, &src, &targ)); 663 } 664 665 /*- 666 *----------------------------------------------------------------------- 667 * Suff_AddTransform -- 668 * Add the transformation rule described by the line to the 669 * list of rules and place the transformation itself in the graph 670 * 671 * Input: 672 * line name of transformation to add 673 * 674 * Results: 675 * The node created for the transformation in the transforms list 676 * 677 * Side Effects: 678 * The node is placed on the end of the transforms Lst and links are 679 * made between the two suffixes mentioned in the target name 680 *----------------------------------------------------------------------- 681 */ 682 GNode * 683 Suff_AddTransform(char *line) 684 { 685 GNode *gn; /* GNode of transformation rule */ 686 Suff *s, /* source suffix */ 687 *t; /* target suffix */ 688 LstNode ln; /* Node for existing transformation */ 689 690 ln = Lst_Find(transforms, (ClientData)line, SuffGNHasNameP); 691 if (ln == NILLNODE) { 692 /* 693 * Make a new graph node for the transformation. It will be filled in 694 * by the Parse module. 695 */ 696 gn = Targ_NewGN(line); 697 (void)Lst_AtEnd(transforms, (ClientData)gn); 698 } else { 699 /* 700 * New specification for transformation rule. Just nuke the old list 701 * of commands so they can be filled in again... We don't actually 702 * free the commands themselves, because a given command can be 703 * attached to several different transformations. 704 */ 705 gn = (GNode *)Lst_Datum(ln); 706 Lst_Destroy(gn->commands, NOFREE); 707 Lst_Destroy(gn->children, NOFREE); 708 gn->commands = Lst_Init(FALSE); 709 gn->children = Lst_Init(FALSE); 710 } 711 712 gn->type = OP_TRANSFORM; 713 714 (void)SuffParseTransform(line, &s, &t); 715 716 /* 717 * link the two together in the proper relationship and order 718 */ 719 if (DEBUG(SUFF)) { 720 printf("defining transformation from `%s' to `%s'\n", 721 s->name, t->name); 722 } 723 SuffInsert(t->children, s); 724 SuffInsert(s->parents, t); 725 726 return (gn); 727 } 728 729 /*- 730 *----------------------------------------------------------------------- 731 * Suff_EndTransform -- 732 * Handle the finish of a transformation definition, removing the 733 * transformation from the graph if it has neither commands nor 734 * sources. This is a callback procedure for the Parse module via 735 * Lst_ForEach 736 * 737 * Input: 738 * gnp Node for transformation 739 * dummy Node for transformation 740 * 741 * Results: 742 * === 0 743 * 744 * Side Effects: 745 * If the node has no commands or children, the children and parents 746 * lists of the affected suffixes are altered. 747 * 748 *----------------------------------------------------------------------- 749 */ 750 int 751 Suff_EndTransform(ClientData gnp, ClientData dummy) 752 { 753 GNode *gn = (GNode *)gnp; 754 755 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) 756 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts)); 757 if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) && 758 Lst_IsEmpty(gn->children)) 759 { 760 Suff *s, *t; 761 762 /* 763 * SuffParseTransform() may fail for special rules which are not 764 * actual transformation rules. (e.g. .DEFAULT) 765 */ 766 if (SuffParseTransform(gn->name, &s, &t)) { 767 Lst p; 768 769 if (DEBUG(SUFF)) { 770 printf("deleting transformation from `%s' to `%s'\n", 771 s->name, t->name); 772 } 773 774 /* 775 * Store s->parents because s could be deleted in SuffRemove 776 */ 777 p = s->parents; 778 779 /* 780 * Remove the source from the target's children list. We check for a 781 * nil return to handle a beanhead saying something like 782 * .c.o .c.o: 783 * 784 * We'll be called twice when the next target is seen, but .c and .o 785 * are only linked once... 786 */ 787 SuffRemove(t->children, s); 788 789 /* 790 * Remove the target from the source's parents list 791 */ 792 SuffRemove(p, t); 793 } 794 } else if ((gn->type & OP_TRANSFORM) && DEBUG(SUFF)) { 795 printf("transformation %s complete\n", gn->name); 796 } 797 798 return(dummy ? 0 : 0); 799 } 800 801 /*- 802 *----------------------------------------------------------------------- 803 * SuffRebuildGraph -- 804 * Called from Suff_AddSuffix via Lst_ForEach to search through the 805 * list of existing transformation rules and rebuild the transformation 806 * graph when it has been destroyed by Suff_ClearSuffixes. If the 807 * given rule is a transformation involving this suffix and another, 808 * existing suffix, the proper relationship is established between 809 * the two. 810 * 811 * Input: 812 * transformp Transformation to test 813 * sp Suffix to rebuild 814 * 815 * Results: 816 * Always 0. 817 * 818 * Side Effects: 819 * The appropriate links will be made between this suffix and 820 * others if transformation rules exist for it. 821 * 822 *----------------------------------------------------------------------- 823 */ 824 static int 825 SuffRebuildGraph(ClientData transformp, ClientData sp) 826 { 827 GNode *transform = (GNode *)transformp; 828 Suff *s = (Suff *)sp; 829 char *cp; 830 LstNode ln; 831 Suff *s2; 832 SuffixCmpData sd; 833 834 /* 835 * First see if it is a transformation from this suffix. 836 */ 837 cp = SuffStrIsPrefix(s->name, transform->name); 838 if (cp != NULL) { 839 ln = Lst_Find(sufflist, (ClientData)cp, SuffSuffHasNameP); 840 if (ln != NILLNODE) { 841 /* 842 * Found target. Link in and return, since it can't be anything 843 * else. 844 */ 845 s2 = (Suff *)Lst_Datum(ln); 846 SuffInsert(s2->children, s); 847 SuffInsert(s->parents, s2); 848 return(0); 849 } 850 } 851 852 /* 853 * Not from, maybe to? 854 */ 855 sd.len = strlen(transform->name); 856 sd.ename = transform->name + sd.len; 857 cp = SuffSuffIsSuffix(s, &sd); 858 if (cp != NULL) { 859 /* 860 * Null-terminate the source suffix in order to find it. 861 */ 862 cp[1] = '\0'; 863 ln = Lst_Find(sufflist, (ClientData)transform->name, SuffSuffHasNameP); 864 /* 865 * Replace the start of the target suffix 866 */ 867 cp[1] = s->name[0]; 868 if (ln != NILLNODE) { 869 /* 870 * Found it -- establish the proper relationship 871 */ 872 s2 = (Suff *)Lst_Datum(ln); 873 SuffInsert(s->children, s2); 874 SuffInsert(s2->parents, s); 875 } 876 } 877 return(0); 878 } 879 880 /*- 881 *----------------------------------------------------------------------- 882 * SuffScanTargets -- 883 * Called from Suff_AddSuffix via Lst_ForEach to search through the 884 * list of existing targets and find if any of the existing targets 885 * can be turned into a transformation rule. 886 * 887 * Results: 888 * 1 if a new main target has been selected, 0 otherwise. 889 * 890 * Side Effects: 891 * If such a target is found and the target is the current main 892 * target, the main target is set to NULL and the next target 893 * examined (if that exists) becomes the main target. 894 * 895 *----------------------------------------------------------------------- 896 */ 897 static int 898 SuffScanTargets(ClientData targetp, ClientData gsp) 899 { 900 GNode *target = (GNode *)targetp; 901 GNodeSuff *gs = (GNodeSuff *)gsp; 902 Suff *s, *t; 903 char *ptr; 904 905 if (*gs->gn == NILGNODE && gs->r && (target->type & OP_NOTARGET) == 0) { 906 *gs->gn = target; 907 Targ_SetMain(target); 908 return 1; 909 } 910 911 if ((unsigned int)target->type == OP_TRANSFORM) 912 return 0; 913 914 if ((ptr = strstr(target->name, gs->s->name)) == NULL || 915 ptr == target->name) 916 return 0; 917 918 if (SuffParseTransform(target->name, &s, &t)) { 919 if (*gs->gn == target) { 920 gs->r = TRUE; 921 *gs->gn = NILGNODE; 922 Targ_SetMain(NILGNODE); 923 } 924 Lst_Destroy(target->children, NOFREE); 925 target->children = Lst_Init(FALSE); 926 target->type = OP_TRANSFORM; 927 /* 928 * link the two together in the proper relationship and order 929 */ 930 if (DEBUG(SUFF)) { 931 printf("defining transformation from `%s' to `%s'\n", 932 s->name, t->name); 933 } 934 SuffInsert(t->children, s); 935 SuffInsert(s->parents, t); 936 } 937 return 0; 938 } 939 940 /*- 941 *----------------------------------------------------------------------- 942 * Suff_AddSuffix -- 943 * Add the suffix in string to the end of the list of known suffixes. 944 * Should we restructure the suffix graph? Make doesn't... 945 * 946 * Input: 947 * str the name of the suffix to add 948 * 949 * Results: 950 * None 951 * 952 * Side Effects: 953 * A GNode is created for the suffix and a Suff structure is created and 954 * added to the suffixes list unless the suffix was already known. 955 * The mainNode passed can be modified if a target mutated into a 956 * transform and that target happened to be the main target. 957 *----------------------------------------------------------------------- 958 */ 959 void 960 Suff_AddSuffix(char *str, GNode **gn) 961 { 962 Suff *s; /* new suffix descriptor */ 963 LstNode ln; 964 GNodeSuff gs; 965 966 ln = Lst_Find(sufflist, (ClientData)str, SuffSuffHasNameP); 967 if (ln == NILLNODE) { 968 s = emalloc(sizeof(Suff)); 969 970 s->name = estrdup(str); 971 s->nameLen = strlen(s->name); 972 s->searchPath = Lst_Init(FALSE); 973 s->children = Lst_Init(FALSE); 974 s->parents = Lst_Init(FALSE); 975 s->ref = Lst_Init(FALSE); 976 s->sNum = sNum++; 977 s->flags = 0; 978 s->refCount = 1; 979 980 (void)Lst_AtEnd(sufflist, (ClientData)s); 981 /* 982 * We also look at our existing targets list to see if adding 983 * this suffix will make one of our current targets mutate into 984 * a suffix rule. This is ugly, but other makes treat all targets 985 * that start with a . as suffix rules. 986 */ 987 gs.gn = gn; 988 gs.s = s; 989 gs.r = FALSE; 990 Lst_ForEach(Targ_List(), SuffScanTargets, (ClientData) &gs); 991 /* 992 * Look for any existing transformations from or to this suffix. 993 * XXX: Only do this after a Suff_ClearSuffixes? 994 */ 995 Lst_ForEach(transforms, SuffRebuildGraph, (ClientData) s); 996 } 997 } 998 999 /*- 1000 *----------------------------------------------------------------------- 1001 * Suff_GetPath -- 1002 * Return the search path for the given suffix, if it's defined. 1003 * 1004 * Results: 1005 * The searchPath for the desired suffix or NILLST if the suffix isn't 1006 * defined. 1007 * 1008 * Side Effects: 1009 * None 1010 *----------------------------------------------------------------------- 1011 */ 1012 Lst 1013 Suff_GetPath(char *sname) 1014 { 1015 LstNode ln; 1016 Suff *s; 1017 1018 ln = Lst_Find(sufflist, (ClientData)sname, SuffSuffHasNameP); 1019 if (ln == NILLNODE) { 1020 return (NILLST); 1021 } else { 1022 s = (Suff *)Lst_Datum(ln); 1023 return (s->searchPath); 1024 } 1025 } 1026 1027 /*- 1028 *----------------------------------------------------------------------- 1029 * Suff_DoPaths -- 1030 * Extend the search paths for all suffixes to include the default 1031 * search path. 1032 * 1033 * Results: 1034 * None. 1035 * 1036 * Side Effects: 1037 * The searchPath field of all the suffixes is extended by the 1038 * directories in dirSearchPath. If paths were specified for the 1039 * ".h" suffix, the directories are stuffed into a global variable 1040 * called ".INCLUDES" with each directory preceded by a -I. The same 1041 * is done for the ".a" suffix, except the variable is called 1042 * ".LIBS" and the flag is -L. 1043 *----------------------------------------------------------------------- 1044 */ 1045 void 1046 Suff_DoPaths(void) 1047 { 1048 Suff *s; 1049 LstNode ln; 1050 char *ptr; 1051 Lst inIncludes; /* Cumulative .INCLUDES path */ 1052 Lst inLibs; /* Cumulative .LIBS path */ 1053 1054 if (Lst_Open(sufflist) == FAILURE) { 1055 return; 1056 } 1057 1058 inIncludes = Lst_Init(FALSE); 1059 inLibs = Lst_Init(FALSE); 1060 1061 while ((ln = Lst_Next(sufflist)) != NILLNODE) { 1062 s = (Suff *)Lst_Datum(ln); 1063 if (!Lst_IsEmpty (s->searchPath)) { 1064 #ifdef INCLUDES 1065 if (s->flags & SUFF_INCLUDE) { 1066 Dir_Concat(inIncludes, s->searchPath); 1067 } 1068 #endif /* INCLUDES */ 1069 #ifdef LIBRARIES 1070 if (s->flags & SUFF_LIBRARY) { 1071 Dir_Concat(inLibs, s->searchPath); 1072 } 1073 #endif /* LIBRARIES */ 1074 Dir_Concat(s->searchPath, dirSearchPath); 1075 } else { 1076 Lst_Destroy(s->searchPath, Dir_Destroy); 1077 s->searchPath = Lst_Duplicate(dirSearchPath, Dir_CopyDir); 1078 } 1079 } 1080 1081 Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL, 0); 1082 free(ptr); 1083 Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL, 0); 1084 free(ptr); 1085 1086 Lst_Destroy(inIncludes, Dir_Destroy); 1087 Lst_Destroy(inLibs, Dir_Destroy); 1088 1089 Lst_Close(sufflist); 1090 } 1091 1092 /*- 1093 *----------------------------------------------------------------------- 1094 * Suff_AddInclude -- 1095 * Add the given suffix as a type of file which gets included. 1096 * Called from the parse module when a .INCLUDES line is parsed. 1097 * The suffix must have already been defined. 1098 * 1099 * Input: 1100 * sname Name of the suffix to mark 1101 * 1102 * Results: 1103 * None. 1104 * 1105 * Side Effects: 1106 * The SUFF_INCLUDE bit is set in the suffix's flags field 1107 * 1108 *----------------------------------------------------------------------- 1109 */ 1110 void 1111 Suff_AddInclude(char *sname) 1112 { 1113 LstNode ln; 1114 Suff *s; 1115 1116 ln = Lst_Find(sufflist, (ClientData)sname, SuffSuffHasNameP); 1117 if (ln != NILLNODE) { 1118 s = (Suff *)Lst_Datum(ln); 1119 s->flags |= SUFF_INCLUDE; 1120 } 1121 } 1122 1123 /*- 1124 *----------------------------------------------------------------------- 1125 * Suff_AddLib -- 1126 * Add the given suffix as a type of file which is a library. 1127 * Called from the parse module when parsing a .LIBS line. The 1128 * suffix must have been defined via .SUFFIXES before this is 1129 * called. 1130 * 1131 * Input: 1132 * sname Name of the suffix to mark 1133 * 1134 * Results: 1135 * None. 1136 * 1137 * Side Effects: 1138 * The SUFF_LIBRARY bit is set in the suffix's flags field 1139 * 1140 *----------------------------------------------------------------------- 1141 */ 1142 void 1143 Suff_AddLib(char *sname) 1144 { 1145 LstNode ln; 1146 Suff *s; 1147 1148 ln = Lst_Find(sufflist, (ClientData)sname, SuffSuffHasNameP); 1149 if (ln != NILLNODE) { 1150 s = (Suff *)Lst_Datum(ln); 1151 s->flags |= SUFF_LIBRARY; 1152 } 1153 } 1154 1155 /********** Implicit Source Search Functions *********/ 1156 1157 /*- 1158 *----------------------------------------------------------------------- 1159 * SuffAddSrc -- 1160 * Add a suffix as a Src structure to the given list with its parent 1161 * being the given Src structure. If the suffix is the null suffix, 1162 * the prefix is used unaltered as the file name in the Src structure. 1163 * 1164 * Input: 1165 * sp suffix for which to create a Src structure 1166 * lsp list and parent for the new Src 1167 * 1168 * Results: 1169 * always returns 0 1170 * 1171 * Side Effects: 1172 * A Src structure is created and tacked onto the end of the list 1173 *----------------------------------------------------------------------- 1174 */ 1175 static int 1176 SuffAddSrc(ClientData sp, ClientData lsp) 1177 { 1178 Suff *s = (Suff *)sp; 1179 LstSrc *ls = (LstSrc *)lsp; 1180 Src *s2; /* new Src structure */ 1181 Src *targ; /* Target structure */ 1182 1183 targ = ls->s; 1184 1185 if ((s->flags & SUFF_NULL) && (*s->name != '\0')) { 1186 /* 1187 * If the suffix has been marked as the NULL suffix, also create a Src 1188 * structure for a file with no suffix attached. Two birds, and all 1189 * that... 1190 */ 1191 s2 = emalloc(sizeof(Src)); 1192 s2->file = estrdup(targ->pref); 1193 s2->pref = targ->pref; 1194 s2->parent = targ; 1195 s2->node = NILGNODE; 1196 s2->suff = s; 1197 s->refCount++; 1198 s2->children = 0; 1199 targ->children += 1; 1200 (void)Lst_AtEnd(ls->l, (ClientData)s2); 1201 #ifdef DEBUG_SRC 1202 s2->cp = Lst_Init(FALSE); 1203 Lst_AtEnd(targ->cp, (ClientData) s2); 1204 printf("1 add %x %x to %x:", targ, s2, ls->l); 1205 Lst_ForEach(ls->l, PrintAddr, (ClientData) 0); 1206 printf("\n"); 1207 #endif 1208 } 1209 s2 = emalloc(sizeof(Src)); 1210 s2->file = str_concat(targ->pref, s->name, 0); 1211 s2->pref = targ->pref; 1212 s2->parent = targ; 1213 s2->node = NILGNODE; 1214 s2->suff = s; 1215 s->refCount++; 1216 s2->children = 0; 1217 targ->children += 1; 1218 (void)Lst_AtEnd(ls->l, (ClientData)s2); 1219 #ifdef DEBUG_SRC 1220 s2->cp = Lst_Init(FALSE); 1221 Lst_AtEnd(targ->cp, (ClientData) s2); 1222 printf("2 add %x %x to %x:", targ, s2, ls->l); 1223 Lst_ForEach(ls->l, PrintAddr, (ClientData) 0); 1224 printf("\n"); 1225 #endif 1226 1227 return(0); 1228 } 1229 1230 /*- 1231 *----------------------------------------------------------------------- 1232 * SuffAddLevel -- 1233 * Add all the children of targ as Src structures to the given list 1234 * 1235 * Input: 1236 * l list to which to add the new level 1237 * targ Src structure to use as the parent 1238 * 1239 * Results: 1240 * None 1241 * 1242 * Side Effects: 1243 * Lots of structures are created and added to the list 1244 *----------------------------------------------------------------------- 1245 */ 1246 static void 1247 SuffAddLevel(Lst l, Src *targ) 1248 { 1249 LstSrc ls; 1250 1251 ls.s = targ; 1252 ls.l = l; 1253 1254 Lst_ForEach(targ->suff->children, SuffAddSrc, (ClientData)&ls); 1255 } 1256 1257 /*- 1258 *---------------------------------------------------------------------- 1259 * SuffRemoveSrc -- 1260 * Free all src structures in list that don't have a reference count 1261 * 1262 * Results: 1263 * Ture if an src was removed 1264 * 1265 * Side Effects: 1266 * The memory is free'd. 1267 *---------------------------------------------------------------------- 1268 */ 1269 static int 1270 SuffRemoveSrc(Lst l) 1271 { 1272 LstNode ln; 1273 Src *s; 1274 int t = 0; 1275 1276 if (Lst_Open(l) == FAILURE) { 1277 return 0; 1278 } 1279 #ifdef DEBUG_SRC 1280 printf("cleaning %lx: ", (unsigned long) l); 1281 Lst_ForEach(l, PrintAddr, (ClientData) 0); 1282 printf("\n"); 1283 #endif 1284 1285 1286 while ((ln = Lst_Next(l)) != NILLNODE) { 1287 s = (Src *)Lst_Datum(ln); 1288 if (s->children == 0) { 1289 free(s->file); 1290 if (!s->parent) 1291 free(s->pref); 1292 else { 1293 #ifdef DEBUG_SRC 1294 LstNode ln = Lst_Member(s->parent->cp, (ClientData)s); 1295 if (ln != NILLNODE) 1296 Lst_Remove(s->parent->cp, ln); 1297 #endif 1298 --s->parent->children; 1299 } 1300 #ifdef DEBUG_SRC 1301 printf("free: [l=%x] p=%x %d\n", l, s, s->children); 1302 Lst_Destroy(s->cp, NOFREE); 1303 #endif 1304 Lst_Remove(l, ln); 1305 free(s); 1306 t |= 1; 1307 Lst_Close(l); 1308 return TRUE; 1309 } 1310 #ifdef DEBUG_SRC 1311 else { 1312 printf("keep: [l=%x] p=%x %d: ", l, s, s->children); 1313 Lst_ForEach(s->cp, PrintAddr, (ClientData) 0); 1314 printf("\n"); 1315 } 1316 #endif 1317 } 1318 1319 Lst_Close(l); 1320 1321 return t; 1322 } 1323 1324 /*- 1325 *----------------------------------------------------------------------- 1326 * SuffFindThem -- 1327 * Find the first existing file/target in the list srcs 1328 * 1329 * Input: 1330 * srcs list of Src structures to search through 1331 * 1332 * Results: 1333 * The lowest structure in the chain of transformations 1334 * 1335 * Side Effects: 1336 * None 1337 *----------------------------------------------------------------------- 1338 */ 1339 static Src * 1340 SuffFindThem(Lst srcs, Lst slst) 1341 { 1342 Src *s; /* current Src */ 1343 Src *rs; /* returned Src */ 1344 char *ptr; 1345 1346 rs = NULL; 1347 1348 while (!Lst_IsEmpty (srcs)) { 1349 s = (Src *)Lst_DeQueue(srcs); 1350 1351 if (DEBUG(SUFF)) { 1352 printf("\ttrying %s...", s->file); 1353 } 1354 1355 /* 1356 * A file is considered to exist if either a node exists in the 1357 * graph for it or the file actually exists. 1358 */ 1359 if (Targ_FindNode(s->file, TARG_NOCREATE) != NILGNODE) { 1360 #ifdef DEBUG_SRC 1361 printf("remove %x from %x\n", s, srcs); 1362 #endif 1363 rs = s; 1364 break; 1365 } 1366 1367 if ((ptr = Dir_FindFile(s->file, s->suff->searchPath)) != NULL) { 1368 rs = s; 1369 #ifdef DEBUG_SRC 1370 printf("remove %x from %x\n", s, srcs); 1371 #endif 1372 free(ptr); 1373 break; 1374 } 1375 1376 if (DEBUG(SUFF)) { 1377 printf("not there\n"); 1378 } 1379 1380 SuffAddLevel(srcs, s); 1381 Lst_AtEnd(slst, (ClientData) s); 1382 } 1383 1384 if (DEBUG(SUFF) && rs) { 1385 printf("got it\n"); 1386 } 1387 return (rs); 1388 } 1389 1390 /*- 1391 *----------------------------------------------------------------------- 1392 * SuffFindCmds -- 1393 * See if any of the children of the target in the Src structure is 1394 * one from which the target can be transformed. If there is one, 1395 * a Src structure is put together for it and returned. 1396 * 1397 * Input: 1398 * targ Src structure to play with 1399 * 1400 * Results: 1401 * The Src structure of the "winning" child, or NIL if no such beast. 1402 * 1403 * Side Effects: 1404 * A Src structure may be allocated. 1405 * 1406 *----------------------------------------------------------------------- 1407 */ 1408 static Src * 1409 SuffFindCmds(Src *targ, Lst slst) 1410 { 1411 LstNode ln; /* General-purpose list node */ 1412 GNode *t, /* Target GNode */ 1413 *s; /* Source GNode */ 1414 int prefLen;/* The length of the defined prefix */ 1415 Suff *suff; /* Suffix on matching beastie */ 1416 Src *ret; /* Return value */ 1417 char *cp; 1418 1419 t = targ->node; 1420 (void)Lst_Open(t->children); 1421 prefLen = strlen(targ->pref); 1422 1423 for (;;) { 1424 ln = Lst_Next(t->children); 1425 if (ln == NILLNODE) { 1426 Lst_Close(t->children); 1427 return NULL; 1428 } 1429 s = (GNode *)Lst_Datum(ln); 1430 1431 if (s->type & OP_OPTIONAL && Lst_IsEmpty(t->commands)) { 1432 /* 1433 * We haven't looked to see if .OPTIONAL files exist yet, so 1434 * don't use one as the implicit source. 1435 * This allows us to use .OPTIONAL in .depend files so make won't 1436 * complain "don't know how to make xxx.h' when a dependant file 1437 * has been moved/deleted. 1438 */ 1439 continue; 1440 } 1441 1442 cp = strrchr(s->name, '/'); 1443 if (cp == NULL) { 1444 cp = s->name; 1445 } else { 1446 cp++; 1447 } 1448 if (strncmp(cp, targ->pref, prefLen) != 0) 1449 continue; 1450 /* 1451 * The node matches the prefix ok, see if it has a known 1452 * suffix. 1453 */ 1454 ln = Lst_Find(sufflist, (ClientData)&cp[prefLen], 1455 SuffSuffHasNameP); 1456 if (ln == NILLNODE) 1457 continue; 1458 /* 1459 * It even has a known suffix, see if there's a transformation 1460 * defined between the node's suffix and the target's suffix. 1461 * 1462 * XXX: Handle multi-stage transformations here, too. 1463 */ 1464 suff = (Suff *)Lst_Datum(ln); 1465 1466 if (Lst_Member(suff->parents, (ClientData)targ->suff) != NILLNODE) 1467 break; 1468 } 1469 1470 /* 1471 * Hot Damn! Create a new Src structure to describe 1472 * this transformation (making sure to duplicate the 1473 * source node's name so Suff_FindDeps can free it 1474 * again (ick)), and return the new structure. 1475 */ 1476 ret = emalloc(sizeof(Src)); 1477 ret->file = estrdup(s->name); 1478 ret->pref = targ->pref; 1479 ret->suff = suff; 1480 suff->refCount++; 1481 ret->parent = targ; 1482 ret->node = s; 1483 ret->children = 0; 1484 targ->children += 1; 1485 #ifdef DEBUG_SRC 1486 ret->cp = Lst_Init(FALSE); 1487 printf("3 add %x %x\n", targ, ret); 1488 Lst_AtEnd(targ->cp, (ClientData) ret); 1489 #endif 1490 Lst_AtEnd(slst, (ClientData) ret); 1491 if (DEBUG(SUFF)) { 1492 printf("\tusing existing source %s\n", s->name); 1493 } 1494 return (ret); 1495 } 1496 1497 /*- 1498 *----------------------------------------------------------------------- 1499 * SuffExpandChildren -- 1500 * Expand the names of any children of a given node that contain 1501 * variable invocations or file wildcards into actual targets. 1502 * 1503 * Input: 1504 * prevLN Child to examine 1505 * pgn Parent node being processed 1506 * 1507 * Results: 1508 * === 0 (continue) 1509 * 1510 * Side Effects: 1511 * The expanded node is removed from the parent's list of children, 1512 * and the parent's unmade counter is decremented, but other nodes 1513 * may be added. 1514 * 1515 *----------------------------------------------------------------------- 1516 */ 1517 static int 1518 SuffExpandChildren(LstNode prevLN, GNode *pgn) 1519 { 1520 GNode *cgn = (GNode *)Lst_Datum(prevLN); 1521 GNode *gn; /* New source 8) */ 1522 char *cp; /* Expanded value */ 1523 1524 /* 1525 * First do variable expansion -- this takes precedence over 1526 * wildcard expansion. If the result contains wildcards, they'll be gotten 1527 * to later since the resulting words are tacked on to the end of 1528 * the children list. 1529 */ 1530 if (strchr(cgn->name, '$') != NULL) { 1531 if (DEBUG(SUFF)) { 1532 printf("Expanding \"%s\"...", cgn->name); 1533 } 1534 cp = Var_Subst(NULL, cgn->name, pgn, TRUE); 1535 1536 if (cp != NULL) { 1537 Lst members = Lst_Init(FALSE); 1538 1539 if (cgn->type & OP_ARCHV) { 1540 /* 1541 * Node was an archive(member) target, so we want to call 1542 * on the Arch module to find the nodes for us, expanding 1543 * variables in the parent's context. 1544 */ 1545 char *sacrifice = cp; 1546 1547 (void)Arch_ParseArchive(&sacrifice, members, pgn); 1548 } else { 1549 /* 1550 * Break the result into a vector of strings whose nodes 1551 * we can find, then add those nodes to the members list. 1552 * Unfortunately, we can't use brk_string b/c it 1553 * doesn't understand about variable specifications with 1554 * spaces in them... 1555 */ 1556 char *start; 1557 char *initcp = cp; /* For freeing... */ 1558 1559 for (start = cp; *start == ' ' || *start == '\t'; start++) 1560 continue; 1561 for (cp = start; *cp != '\0'; cp++) { 1562 if (*cp == ' ' || *cp == '\t') { 1563 /* 1564 * White-space -- terminate element, find the node, 1565 * add it, skip any further spaces. 1566 */ 1567 *cp++ = '\0'; 1568 gn = Targ_FindNode(start, TARG_CREATE); 1569 (void)Lst_AtEnd(members, (ClientData)gn); 1570 while (*cp == ' ' || *cp == '\t') { 1571 cp++; 1572 } 1573 /* 1574 * Adjust cp for increment at start of loop, but 1575 * set start to first non-space. 1576 */ 1577 start = cp--; 1578 } else if (*cp == '$') { 1579 /* 1580 * Start of a variable spec -- contact variable module 1581 * to find the end so we can skip over it. 1582 */ 1583 char *junk; 1584 int len; 1585 void *freeIt; 1586 1587 junk = Var_Parse(cp, pgn, TRUE, &len, &freeIt); 1588 if (junk != var_Error) { 1589 cp += len - 1; 1590 } 1591 1592 if (freeIt) 1593 free(freeIt); 1594 } else if (*cp == '\\' && *cp != '\0') { 1595 /* 1596 * Escaped something -- skip over it 1597 */ 1598 cp++; 1599 } 1600 } 1601 1602 if (cp != start) { 1603 /* 1604 * Stuff left over -- add it to the list too 1605 */ 1606 gn = Targ_FindNode(start, TARG_CREATE); 1607 (void)Lst_AtEnd(members, (ClientData)gn); 1608 } 1609 /* 1610 * Point cp back at the beginning again so the variable value 1611 * can be freed. 1612 */ 1613 cp = initcp; 1614 } 1615 /* 1616 * Add all elements of the members list to the parent node. 1617 */ 1618 while(!Lst_IsEmpty(members)) { 1619 gn = (GNode *)Lst_DeQueue(members); 1620 1621 if (DEBUG(SUFF)) { 1622 printf("%s...", gn->name); 1623 } 1624 if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) { 1625 (void)Lst_Append(pgn->children, prevLN, (ClientData)gn); 1626 prevLN = Lst_Succ(prevLN); 1627 (void)Lst_AtEnd(gn->parents, (ClientData)pgn); 1628 pgn->unmade++; 1629 } 1630 } 1631 Lst_Destroy(members, NOFREE); 1632 /* 1633 * Free the result 1634 */ 1635 free(cp); 1636 } 1637 /* 1638 * Now the source is expanded, remove it from the list of children to 1639 * keep it from being processed. 1640 */ 1641 if (DEBUG(SUFF)) { 1642 printf("\n"); 1643 } 1644 return(1); 1645 } else if (Dir_HasWildcards(cgn->name)) { 1646 Lst explist; /* List of expansions */ 1647 1648 /* 1649 * Expand the word along the chosen path 1650 */ 1651 explist = Lst_Init(FALSE); 1652 Dir_Expand(cgn->name, Suff_FindPath(cgn), explist); 1653 1654 while (!Lst_IsEmpty(explist)) { 1655 /* 1656 * Fetch next expansion off the list and find its GNode 1657 */ 1658 cp = (char *)Lst_DeQueue(explist); 1659 1660 if (DEBUG(SUFF)) { 1661 printf("%s...", cp); 1662 } 1663 gn = Targ_FindNode(cp, TARG_CREATE); 1664 1665 /* 1666 * If gn isn't already a child of the parent, make it so and 1667 * up the parent's count of unmade children. 1668 */ 1669 if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) { 1670 (void)Lst_Append(pgn->children, prevLN, (ClientData)gn); 1671 prevLN = Lst_Succ(prevLN); 1672 (void)Lst_AtEnd(gn->parents, (ClientData)pgn); 1673 pgn->unmade++; 1674 } 1675 } 1676 1677 /* 1678 * Nuke what's left of the list 1679 */ 1680 Lst_Destroy(explist, NOFREE); 1681 1682 /* 1683 * Now the source is expanded, remove it from the list of children to 1684 * keep it from being processed. 1685 */ 1686 if (DEBUG(SUFF)) { 1687 printf("\n"); 1688 } 1689 return(1); 1690 } 1691 1692 return(0); 1693 } 1694 1695 /*- 1696 *----------------------------------------------------------------------- 1697 * Suff_FindPath -- 1698 * Find a path along which to expand the node. 1699 * 1700 * If the word has a known suffix, use that path. 1701 * If it has no known suffix, use the default system search path. 1702 * 1703 * Input: 1704 * gn Node being examined 1705 * 1706 * Results: 1707 * The appropriate path to search for the GNode. 1708 * 1709 * Side Effects: 1710 * XXX: We could set the suffix here so that we don't have to scan 1711 * again. 1712 * 1713 *----------------------------------------------------------------------- 1714 */ 1715 Lst 1716 Suff_FindPath(GNode* gn) 1717 { 1718 Suff *suff = gn->suffix; 1719 1720 if (suff == NULL) { 1721 SuffixCmpData sd; /* Search string data */ 1722 LstNode ln; 1723 sd.len = strlen(gn->name); 1724 sd.ename = gn->name + sd.len; 1725 ln = Lst_Find(sufflist, (ClientData)&sd, SuffSuffIsSuffixP); 1726 1727 if (DEBUG(SUFF)) { 1728 printf("Wildcard expanding \"%s\"...", gn->name); 1729 } 1730 if (ln != NILLNODE) 1731 suff = (Suff *)Lst_Datum(ln); 1732 /* XXX: Here we can save the suffix so we don't have to do this again */ 1733 } 1734 1735 if (suff != NULL) { 1736 if (DEBUG(SUFF)) { 1737 printf("suffix is \"%s\"...", suff->name); 1738 } 1739 return suff->searchPath; 1740 } else { 1741 /* 1742 * Use default search path 1743 */ 1744 return dirSearchPath; 1745 } 1746 } 1747 1748 /*- 1749 *----------------------------------------------------------------------- 1750 * SuffApplyTransform -- 1751 * Apply a transformation rule, given the source and target nodes 1752 * and suffixes. 1753 * 1754 * Input: 1755 * tGn Target node 1756 * sGn Source node 1757 * t Target suffix 1758 * s Source suffix 1759 * 1760 * Results: 1761 * TRUE if successful, FALSE if not. 1762 * 1763 * Side Effects: 1764 * The source and target are linked and the commands from the 1765 * transformation are added to the target node's commands list. 1766 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied 1767 * to the target. The target also inherits all the sources for 1768 * the transformation rule. 1769 * 1770 *----------------------------------------------------------------------- 1771 */ 1772 static Boolean 1773 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s) 1774 { 1775 LstNode ln, nln; /* General node */ 1776 char *tname; /* Name of transformation rule */ 1777 GNode *gn; /* Node for same */ 1778 1779 /* 1780 * Form the proper links between the target and source. 1781 */ 1782 (void)Lst_AtEnd(tGn->children, (ClientData)sGn); 1783 (void)Lst_AtEnd(sGn->parents, (ClientData)tGn); 1784 tGn->unmade += 1; 1785 1786 /* 1787 * Locate the transformation rule itself 1788 */ 1789 tname = str_concat(s->name, t->name, 0); 1790 ln = Lst_Find(transforms, (ClientData)tname, SuffGNHasNameP); 1791 free(tname); 1792 1793 if (ln == NILLNODE) { 1794 /* 1795 * Not really such a transformation rule (can happen when we're 1796 * called to link an OP_MEMBER and OP_ARCHV node), so return 1797 * FALSE. 1798 */ 1799 return(FALSE); 1800 } 1801 1802 gn = (GNode *)Lst_Datum(ln); 1803 1804 if (DEBUG(SUFF)) { 1805 printf("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name); 1806 } 1807 1808 /* 1809 * Record last child for expansion purposes 1810 */ 1811 ln = Lst_Last(tGn->children); 1812 1813 /* 1814 * Pass the buck to Make_HandleUse to apply the rule 1815 */ 1816 (void)Make_HandleUse(gn, tGn); 1817 1818 /* 1819 * Deal with wildcards and variables in any acquired sources 1820 */ 1821 ln = Lst_Succ(ln); 1822 while (ln != NILLNODE) { 1823 if (SuffExpandChildren(ln, tGn)) { 1824 nln = Lst_Succ(ln); 1825 tGn->unmade--; 1826 Lst_Remove(tGn->children, ln); 1827 ln = nln; 1828 } else 1829 ln = Lst_Succ(ln); 1830 } 1831 1832 /* 1833 * Keep track of another parent to which this beast is transformed so 1834 * the .IMPSRC variable can be set correctly for the parent. 1835 */ 1836 (void)Lst_AtEnd(sGn->iParents, (ClientData)tGn); 1837 1838 return(TRUE); 1839 } 1840 1841 1842 /*- 1843 *----------------------------------------------------------------------- 1844 * SuffFindArchiveDeps -- 1845 * Locate dependencies for an OP_ARCHV node. 1846 * 1847 * Input: 1848 * gn Node for which to locate dependencies 1849 * 1850 * Results: 1851 * None 1852 * 1853 * Side Effects: 1854 * Same as Suff_FindDeps 1855 * 1856 *----------------------------------------------------------------------- 1857 */ 1858 static void 1859 SuffFindArchiveDeps(GNode *gn, Lst slst) 1860 { 1861 char *eoarch; /* End of archive portion */ 1862 char *eoname; /* End of member portion */ 1863 GNode *mem; /* Node for member */ 1864 static const char *copy[] = { 1865 /* Variables to be copied from the member node */ 1866 TARGET, /* Must be first */ 1867 PREFIX, /* Must be second */ 1868 }; 1869 int i; /* Index into copy and vals */ 1870 Suff *ms; /* Suffix descriptor for member */ 1871 char *name; /* Start of member's name */ 1872 1873 /* 1874 * The node is an archive(member) pair. so we must find a 1875 * suffix for both of them. 1876 */ 1877 eoarch = strchr(gn->name, '('); 1878 eoname = strchr(eoarch, ')'); 1879 1880 *eoname = '\0'; /* Nuke parentheses during suffix search */ 1881 *eoarch = '\0'; /* So a suffix can be found */ 1882 1883 name = eoarch + 1; 1884 1885 /* 1886 * To simplify things, call Suff_FindDeps recursively on the member now, 1887 * so we can simply compare the member's .PREFIX and .TARGET variables 1888 * to locate its suffix. This allows us to figure out the suffix to 1889 * use for the archive without having to do a quadratic search over the 1890 * suffix list, backtracking for each one... 1891 */ 1892 mem = Targ_FindNode(name, TARG_CREATE); 1893 SuffFindDeps(mem, slst); 1894 1895 /* 1896 * Create the link between the two nodes right off 1897 */ 1898 (void)Lst_AtEnd(gn->children, (ClientData)mem); 1899 (void)Lst_AtEnd(mem->parents, (ClientData)gn); 1900 gn->unmade += 1; 1901 1902 /* 1903 * Copy in the variables from the member node to this one. 1904 */ 1905 for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) { 1906 char *p1; 1907 Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn, 0); 1908 if (p1) 1909 free(p1); 1910 1911 } 1912 1913 ms = mem->suffix; 1914 if (ms == NULL) { 1915 /* 1916 * Didn't know what it was -- use .NULL suffix if not in make mode 1917 */ 1918 if (DEBUG(SUFF)) { 1919 printf("using null suffix\n"); 1920 } 1921 ms = suffNull; 1922 } 1923 1924 1925 /* 1926 * Set the other two local variables required for this target. 1927 */ 1928 Var_Set(MEMBER, name, gn, 0); 1929 Var_Set(ARCHIVE, gn->name, gn, 0); 1930 1931 if (ms != NULL) { 1932 /* 1933 * Member has a known suffix, so look for a transformation rule from 1934 * it to a possible suffix of the archive. Rather than searching 1935 * through the entire list, we just look at suffixes to which the 1936 * member's suffix may be transformed... 1937 */ 1938 LstNode ln; 1939 SuffixCmpData sd; /* Search string data */ 1940 1941 /* 1942 * Use first matching suffix... 1943 */ 1944 sd.len = eoarch - gn->name; 1945 sd.ename = eoarch; 1946 ln = Lst_Find(ms->parents, &sd, SuffSuffIsSuffixP); 1947 1948 if (ln != NILLNODE) { 1949 /* 1950 * Got one -- apply it 1951 */ 1952 if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms) && 1953 DEBUG(SUFF)) 1954 { 1955 printf("\tNo transformation from %s -> %s\n", 1956 ms->name, ((Suff *)Lst_Datum(ln))->name); 1957 } 1958 } 1959 } 1960 1961 /* 1962 * Replace the opening and closing parens now we've no need of the separate 1963 * pieces. 1964 */ 1965 *eoarch = '('; *eoname = ')'; 1966 1967 /* 1968 * Pretend gn appeared to the left of a dependency operator so 1969 * the user needn't provide a transformation from the member to the 1970 * archive. 1971 */ 1972 if (OP_NOP(gn->type)) { 1973 gn->type |= OP_DEPENDS; 1974 } 1975 1976 /* 1977 * Flag the member as such so we remember to look in the archive for 1978 * its modification time. 1979 */ 1980 mem->type |= OP_MEMBER; 1981 } 1982 1983 /*- 1984 *----------------------------------------------------------------------- 1985 * SuffFindNormalDeps -- 1986 * Locate implicit dependencies for regular targets. 1987 * 1988 * Input: 1989 * gn Node for which to find sources 1990 * 1991 * Results: 1992 * None. 1993 * 1994 * Side Effects: 1995 * Same as Suff_FindDeps... 1996 * 1997 *----------------------------------------------------------------------- 1998 */ 1999 static void 2000 SuffFindNormalDeps(GNode *gn, Lst slst) 2001 { 2002 char *eoname; /* End of name */ 2003 char *sopref; /* Start of prefix */ 2004 LstNode ln, nln; /* Next suffix node to check */ 2005 Lst srcs; /* List of sources at which to look */ 2006 Lst targs; /* List of targets to which things can be 2007 * transformed. They all have the same file, 2008 * but different suff and pref fields */ 2009 Src *bottom; /* Start of found transformation path */ 2010 Src *src; /* General Src pointer */ 2011 char *pref; /* Prefix to use */ 2012 Src *targ; /* General Src target pointer */ 2013 SuffixCmpData sd; /* Search string data */ 2014 2015 2016 sd.len = strlen(gn->name); 2017 sd.ename = eoname = gn->name + sd.len; 2018 2019 sopref = gn->name; 2020 2021 /* 2022 * Begin at the beginning... 2023 */ 2024 ln = Lst_First(sufflist); 2025 srcs = Lst_Init(FALSE); 2026 targs = Lst_Init(FALSE); 2027 2028 /* 2029 * We're caught in a catch-22 here. On the one hand, we want to use any 2030 * transformation implied by the target's sources, but we can't examine 2031 * the sources until we've expanded any variables/wildcards they may hold, 2032 * and we can't do that until we've set up the target's local variables 2033 * and we can't do that until we know what the proper suffix for the 2034 * target is (in case there are two suffixes one of which is a suffix of 2035 * the other) and we can't know that until we've found its implied 2036 * source, which we may not want to use if there's an existing source 2037 * that implies a different transformation. 2038 * 2039 * In an attempt to get around this, which may not work all the time, 2040 * but should work most of the time, we look for implied sources first, 2041 * checking transformations to all possible suffixes of the target, 2042 * use what we find to set the target's local variables, expand the 2043 * children, then look for any overriding transformations they imply. 2044 * Should we find one, we discard the one we found before. 2045 */ 2046 2047 while (ln != NILLNODE) { 2048 /* 2049 * Look for next possible suffix... 2050 */ 2051 ln = Lst_FindFrom(sufflist, ln, &sd, SuffSuffIsSuffixP); 2052 2053 if (ln != NILLNODE) { 2054 int prefLen; /* Length of the prefix */ 2055 2056 /* 2057 * Allocate a Src structure to which things can be transformed 2058 */ 2059 targ = emalloc(sizeof(Src)); 2060 targ->file = estrdup(gn->name); 2061 targ->suff = (Suff *)Lst_Datum(ln); 2062 targ->suff->refCount++; 2063 targ->node = gn; 2064 targ->parent = NULL; 2065 targ->children = 0; 2066 #ifdef DEBUG_SRC 2067 targ->cp = Lst_Init(FALSE); 2068 #endif 2069 2070 /* 2071 * Allocate room for the prefix, whose end is found by subtracting 2072 * the length of the suffix from the end of the name. 2073 */ 2074 prefLen = (eoname - targ->suff->nameLen) - sopref; 2075 targ->pref = emalloc(prefLen + 1); 2076 memcpy(targ->pref, sopref, prefLen); 2077 targ->pref[prefLen] = '\0'; 2078 2079 /* 2080 * Add nodes from which the target can be made 2081 */ 2082 SuffAddLevel(srcs, targ); 2083 2084 /* 2085 * Record the target so we can nuke it 2086 */ 2087 (void)Lst_AtEnd(targs, (ClientData)targ); 2088 2089 /* 2090 * Search from this suffix's successor... 2091 */ 2092 ln = Lst_Succ(ln); 2093 } 2094 } 2095 2096 /* 2097 * Handle target of unknown suffix... 2098 */ 2099 if (Lst_IsEmpty(targs) && suffNull != NULL) { 2100 if (DEBUG(SUFF)) { 2101 printf("\tNo known suffix on %s. Using .NULL suffix\n", gn->name); 2102 } 2103 2104 targ = emalloc(sizeof(Src)); 2105 targ->file = estrdup(gn->name); 2106 targ->suff = suffNull; 2107 targ->suff->refCount++; 2108 targ->node = gn; 2109 targ->parent = NULL; 2110 targ->children = 0; 2111 targ->pref = estrdup(sopref); 2112 #ifdef DEBUG_SRC 2113 targ->cp = Lst_Init(FALSE); 2114 #endif 2115 2116 /* 2117 * Only use the default suffix rules if we don't have commands 2118 * defined for this gnode; traditional make programs used to 2119 * not define suffix rules if the gnode had children but we 2120 * don't do this anymore. 2121 */ 2122 if (Lst_IsEmpty(gn->commands)) 2123 SuffAddLevel(srcs, targ); 2124 else { 2125 if (DEBUG(SUFF)) 2126 printf("not "); 2127 } 2128 2129 if (DEBUG(SUFF)) 2130 printf("adding suffix rules\n"); 2131 2132 (void)Lst_AtEnd(targs, (ClientData)targ); 2133 } 2134 2135 /* 2136 * Using the list of possible sources built up from the target suffix(es), 2137 * try and find an existing file/target that matches. 2138 */ 2139 bottom = SuffFindThem(srcs, slst); 2140 2141 if (bottom == NULL) { 2142 /* 2143 * No known transformations -- use the first suffix found for setting 2144 * the local variables. 2145 */ 2146 if (!Lst_IsEmpty(targs)) { 2147 targ = (Src *)Lst_Datum(Lst_First(targs)); 2148 } else { 2149 targ = NULL; 2150 } 2151 } else { 2152 /* 2153 * Work up the transformation path to find the suffix of the 2154 * target to which the transformation was made. 2155 */ 2156 for (targ = bottom; targ->parent != NULL; targ = targ->parent) 2157 continue; 2158 } 2159 2160 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0); 2161 2162 pref = (targ != NULL) ? targ->pref : gn->name; 2163 Var_Set(PREFIX, pref, gn, 0); 2164 2165 /* 2166 * Now we've got the important local variables set, expand any sources 2167 * that still contain variables or wildcards in their names. 2168 */ 2169 ln = Lst_First(gn->children); 2170 while (ln != NILLNODE) { 2171 if (SuffExpandChildren(ln, gn)) { 2172 nln = Lst_Succ(ln); 2173 gn->unmade--; 2174 Lst_Remove(gn->children, ln); 2175 ln = nln; 2176 } else 2177 ln = Lst_Succ(ln); 2178 } 2179 2180 if (targ == NULL) { 2181 if (DEBUG(SUFF)) { 2182 printf("\tNo valid suffix on %s\n", gn->name); 2183 } 2184 2185 sfnd_abort: 2186 /* 2187 * Deal with finding the thing on the default search path. We 2188 * always do that, not only if the node is only a source (not 2189 * on the lhs of a dependency operator or [XXX] it has neither 2190 * children or commands) as the old pmake did. 2191 */ 2192 if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) { 2193 free(gn->path); 2194 gn->path = Dir_FindFile(gn->name, 2195 (targ == NULL ? dirSearchPath : 2196 targ->suff->searchPath)); 2197 if (gn->path != NULL) { 2198 char *ptr; 2199 Var_Set(TARGET, gn->path, gn, 0); 2200 2201 if (targ != NULL) { 2202 /* 2203 * Suffix known for the thing -- trim the suffix off 2204 * the path to form the proper .PREFIX variable. 2205 */ 2206 int savep = strlen(gn->path) - targ->suff->nameLen; 2207 char savec; 2208 2209 if (gn->suffix) 2210 gn->suffix->refCount--; 2211 gn->suffix = targ->suff; 2212 gn->suffix->refCount++; 2213 2214 savec = gn->path[savep]; 2215 gn->path[savep] = '\0'; 2216 2217 if ((ptr = strrchr(gn->path, '/')) != NULL) 2218 ptr++; 2219 else 2220 ptr = gn->path; 2221 2222 Var_Set(PREFIX, ptr, gn, 0); 2223 2224 gn->path[savep] = savec; 2225 } else { 2226 /* 2227 * The .PREFIX gets the full path if the target has 2228 * no known suffix. 2229 */ 2230 if (gn->suffix) 2231 gn->suffix->refCount--; 2232 gn->suffix = NULL; 2233 2234 if ((ptr = strrchr(gn->path, '/')) != NULL) 2235 ptr++; 2236 else 2237 ptr = gn->path; 2238 2239 Var_Set(PREFIX, ptr, gn, 0); 2240 } 2241 } 2242 } 2243 2244 goto sfnd_return; 2245 } 2246 2247 /* 2248 * If the suffix indicates that the target is a library, mark that in 2249 * the node's type field. 2250 */ 2251 if (targ->suff->flags & SUFF_LIBRARY) { 2252 gn->type |= OP_LIB; 2253 } 2254 2255 /* 2256 * Check for overriding transformation rule implied by sources 2257 */ 2258 if (!Lst_IsEmpty(gn->children)) { 2259 src = SuffFindCmds(targ, slst); 2260 2261 if (src != NULL) { 2262 /* 2263 * Free up all the Src structures in the transformation path 2264 * up to, but not including, the parent node. 2265 */ 2266 while (bottom && bottom->parent != NULL) { 2267 if (Lst_Member(slst, (ClientData) bottom) == NILLNODE) { 2268 Lst_AtEnd(slst, (ClientData) bottom); 2269 } 2270 bottom = bottom->parent; 2271 } 2272 bottom = src; 2273 } 2274 } 2275 2276 if (bottom == NULL) { 2277 /* 2278 * No idea from where it can come -- return now. 2279 */ 2280 goto sfnd_abort; 2281 } 2282 2283 /* 2284 * We now have a list of Src structures headed by 'bottom' and linked via 2285 * their 'parent' pointers. What we do next is create links between 2286 * source and target nodes (which may or may not have been created) 2287 * and set the necessary local variables in each target. The 2288 * commands for each target are set from the commands of the 2289 * transformation rule used to get from the src suffix to the targ 2290 * suffix. Note that this causes the commands list of the original 2291 * node, gn, to be replaced by the commands of the final 2292 * transformation rule. Also, the unmade field of gn is incremented. 2293 * Etc. 2294 */ 2295 if (bottom->node == NILGNODE) { 2296 bottom->node = Targ_FindNode(bottom->file, TARG_CREATE); 2297 } 2298 2299 for (src = bottom; src->parent != NULL; src = src->parent) { 2300 targ = src->parent; 2301 2302 if (src->node->suffix) 2303 src->node->suffix->refCount--; 2304 src->node->suffix = src->suff; 2305 src->node->suffix->refCount++; 2306 2307 if (targ->node == NILGNODE) { 2308 targ->node = Targ_FindNode(targ->file, TARG_CREATE); 2309 } 2310 2311 SuffApplyTransform(targ->node, src->node, 2312 targ->suff, src->suff); 2313 2314 if (targ->node != gn) { 2315 /* 2316 * Finish off the dependency-search process for any nodes 2317 * between bottom and gn (no point in questing around the 2318 * filesystem for their implicit source when it's already 2319 * known). Note that the node can't have any sources that 2320 * need expanding, since SuffFindThem will stop on an existing 2321 * node, so all we need to do is set the standard and System V 2322 * variables. 2323 */ 2324 targ->node->type |= OP_DEPS_FOUND; 2325 2326 Var_Set(PREFIX, targ->pref, targ->node, 0); 2327 2328 Var_Set(TARGET, targ->node->name, targ->node, 0); 2329 } 2330 } 2331 2332 if (gn->suffix) 2333 gn->suffix->refCount--; 2334 gn->suffix = src->suff; 2335 gn->suffix->refCount++; 2336 2337 /* 2338 * Nuke the transformation path and the Src structures left over in the 2339 * two lists. 2340 */ 2341 sfnd_return: 2342 if (bottom) 2343 if (Lst_Member(slst, (ClientData) bottom) == NILLNODE) 2344 Lst_AtEnd(slst, (ClientData) bottom); 2345 2346 while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs)) 2347 continue; 2348 2349 Lst_Concat(slst, srcs, LST_CONCLINK); 2350 Lst_Concat(slst, targs, LST_CONCLINK); 2351 } 2352 2353 2354 /*- 2355 *----------------------------------------------------------------------- 2356 * Suff_FindDeps -- 2357 * Find implicit sources for the target described by the graph node 2358 * gn 2359 * 2360 * Results: 2361 * Nothing. 2362 * 2363 * Side Effects: 2364 * Nodes are added to the graph below the passed-in node. The nodes 2365 * are marked to have their IMPSRC variable filled in. The 2366 * PREFIX variable is set for the given node and all its 2367 * implied children. 2368 * 2369 * Notes: 2370 * The path found by this target is the shortest path in the 2371 * transformation graph, which may pass through non-existent targets, 2372 * to an existing target. The search continues on all paths from the 2373 * root suffix until a file is found. I.e. if there's a path 2374 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but 2375 * the .c and .l files don't, the search will branch out in 2376 * all directions from .o and again from all the nodes on the 2377 * next level until the .l,v node is encountered. 2378 * 2379 *----------------------------------------------------------------------- 2380 */ 2381 2382 void 2383 Suff_FindDeps(GNode *gn) 2384 { 2385 2386 SuffFindDeps(gn, srclist); 2387 while (SuffRemoveSrc(srclist)) 2388 continue; 2389 } 2390 2391 2392 /* 2393 * Input: 2394 * gn node we're dealing with 2395 * 2396 */ 2397 static void 2398 SuffFindDeps(GNode *gn, Lst slst) 2399 { 2400 if (gn->type & (OP_DEPS_FOUND|OP_PHONY)) { 2401 /* 2402 * If dependencies already found, no need to do it again... 2403 * If this is a .PHONY target, we do not apply suffix rules. 2404 */ 2405 return; 2406 } else { 2407 gn->type |= OP_DEPS_FOUND; 2408 } 2409 2410 if (DEBUG(SUFF)) { 2411 printf("SuffFindDeps (%s)\n", gn->name); 2412 } 2413 2414 if (gn->type & OP_ARCHV) { 2415 SuffFindArchiveDeps(gn, slst); 2416 } else if (gn->type & OP_LIB) { 2417 /* 2418 * If the node is a library, it is the arch module's job to find it 2419 * and set the TARGET variable accordingly. We merely provide the 2420 * search path, assuming all libraries end in ".a" (if the suffix 2421 * hasn't been defined, there's nothing we can do for it, so we just 2422 * set the TARGET variable to the node's name in order to give it a 2423 * value). 2424 */ 2425 LstNode ln; 2426 Suff *s; 2427 2428 ln = Lst_Find(sufflist, (ClientData)UNCONST(LIBSUFF), 2429 SuffSuffHasNameP); 2430 if (gn->suffix) 2431 gn->suffix->refCount--; 2432 if (ln != NILLNODE) { 2433 gn->suffix = s = (Suff *)Lst_Datum(ln); 2434 gn->suffix->refCount++; 2435 Arch_FindLib(gn, s->searchPath); 2436 } else { 2437 gn->suffix = NULL; 2438 Var_Set(TARGET, gn->name, gn, 0); 2439 } 2440 /* 2441 * Because a library (-lfoo) target doesn't follow the standard 2442 * filesystem conventions, we don't set the regular variables for 2443 * the thing. .PREFIX is simply made empty... 2444 */ 2445 Var_Set(PREFIX, "", gn, 0); 2446 } else { 2447 SuffFindNormalDeps(gn, slst); 2448 } 2449 } 2450 2451 /*- 2452 *----------------------------------------------------------------------- 2453 * Suff_SetNull -- 2454 * Define which suffix is the null suffix. 2455 * 2456 * Input: 2457 * name Name of null suffix 2458 * 2459 * Results: 2460 * None. 2461 * 2462 * Side Effects: 2463 * 'suffNull' is altered. 2464 * 2465 * Notes: 2466 * Need to handle the changing of the null suffix gracefully so the 2467 * old transformation rules don't just go away. 2468 * 2469 *----------------------------------------------------------------------- 2470 */ 2471 void 2472 Suff_SetNull(char *name) 2473 { 2474 Suff *s; 2475 LstNode ln; 2476 2477 ln = Lst_Find(sufflist, (ClientData)name, SuffSuffHasNameP); 2478 if (ln != NILLNODE) { 2479 s = (Suff *)Lst_Datum(ln); 2480 if (suffNull != NULL) { 2481 suffNull->flags &= ~SUFF_NULL; 2482 } 2483 s->flags |= SUFF_NULL; 2484 /* 2485 * XXX: Here's where the transformation mangling would take place 2486 */ 2487 suffNull = s; 2488 } else { 2489 Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.", 2490 name); 2491 } 2492 } 2493 2494 /*- 2495 *----------------------------------------------------------------------- 2496 * Suff_Init -- 2497 * Initialize suffixes module 2498 * 2499 * Results: 2500 * None 2501 * 2502 * Side Effects: 2503 * Many 2504 *----------------------------------------------------------------------- 2505 */ 2506 void 2507 Suff_Init(void) 2508 { 2509 sufflist = Lst_Init(FALSE); 2510 #ifdef CLEANUP 2511 suffClean = Lst_Init(FALSE); 2512 #endif 2513 srclist = Lst_Init(FALSE); 2514 transforms = Lst_Init(FALSE); 2515 2516 sNum = 0; 2517 /* 2518 * Create null suffix for single-suffix rules (POSIX). The thing doesn't 2519 * actually go on the suffix list or everyone will think that's its 2520 * suffix. 2521 */ 2522 emptySuff = suffNull = emalloc(sizeof(Suff)); 2523 2524 suffNull->name = estrdup(""); 2525 suffNull->nameLen = 0; 2526 suffNull->searchPath = Lst_Init(FALSE); 2527 Dir_Concat(suffNull->searchPath, dirSearchPath); 2528 suffNull->children = Lst_Init(FALSE); 2529 suffNull->parents = Lst_Init(FALSE); 2530 suffNull->ref = Lst_Init(FALSE); 2531 suffNull->sNum = sNum++; 2532 suffNull->flags = SUFF_NULL; 2533 suffNull->refCount = 1; 2534 2535 } 2536 2537 2538 /*- 2539 *---------------------------------------------------------------------- 2540 * Suff_End -- 2541 * Cleanup the this module 2542 * 2543 * Results: 2544 * None 2545 * 2546 * Side Effects: 2547 * The memory is free'd. 2548 *---------------------------------------------------------------------- 2549 */ 2550 2551 void 2552 Suff_End(void) 2553 { 2554 #ifdef CLEANUP 2555 Lst_Destroy(sufflist, SuffFree); 2556 Lst_Destroy(suffClean, SuffFree); 2557 if (suffNull) 2558 SuffFree(suffNull); 2559 Lst_Destroy(srclist, NOFREE); 2560 Lst_Destroy(transforms, NOFREE); 2561 #endif 2562 } 2563 2564 2565 /********************* DEBUGGING FUNCTIONS **********************/ 2566 2567 static int SuffPrintName(ClientData s, ClientData dummy) 2568 { 2569 printf("%s ", ((Suff *)s)->name); 2570 return (dummy ? 0 : 0); 2571 } 2572 2573 static int 2574 SuffPrintSuff(ClientData sp, ClientData dummy) 2575 { 2576 Suff *s = (Suff *)sp; 2577 int flags; 2578 int flag; 2579 2580 printf("# `%s' [%d] ", s->name, s->refCount); 2581 2582 flags = s->flags; 2583 if (flags) { 2584 fputs(" (", stdout); 2585 while (flags) { 2586 flag = 1 << (ffs(flags) - 1); 2587 flags &= ~flag; 2588 switch (flag) { 2589 case SUFF_NULL: 2590 printf("NULL"); 2591 break; 2592 case SUFF_INCLUDE: 2593 printf("INCLUDE"); 2594 break; 2595 case SUFF_LIBRARY: 2596 printf("LIBRARY"); 2597 break; 2598 } 2599 fputc(flags ? '|' : ')', stdout); 2600 } 2601 } 2602 fputc('\n', stdout); 2603 printf("#\tTo: "); 2604 Lst_ForEach(s->parents, SuffPrintName, (ClientData)0); 2605 fputc('\n', stdout); 2606 printf("#\tFrom: "); 2607 Lst_ForEach(s->children, SuffPrintName, (ClientData)0); 2608 fputc('\n', stdout); 2609 printf("#\tSearch Path: "); 2610 Dir_PrintPath(s->searchPath); 2611 fputc('\n', stdout); 2612 return (dummy ? 0 : 0); 2613 } 2614 2615 static int 2616 SuffPrintTrans(ClientData tp, ClientData dummy) 2617 { 2618 GNode *t = (GNode *)tp; 2619 2620 printf("%-16s: ", t->name); 2621 Targ_PrintType(t->type); 2622 fputc('\n', stdout); 2623 Lst_ForEach(t->commands, Targ_PrintCmd, (ClientData)0); 2624 fputc('\n', stdout); 2625 return(dummy ? 0 : 0); 2626 } 2627 2628 void 2629 Suff_PrintAll(void) 2630 { 2631 printf("#*** Suffixes:\n"); 2632 Lst_ForEach(sufflist, SuffPrintSuff, (ClientData)0); 2633 2634 printf("#*** Transformations:\n"); 2635 Lst_ForEach(transforms, SuffPrintTrans, (ClientData)0); 2636 } 2637