1 /* $NetBSD: suff.c,v 1.382 2024/07/07 07:50:57 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 /* 72 * Maintain suffix lists and find implicit dependents using suffix 73 * transformation rules such as ".c.o". 74 * 75 * Interface: 76 * Suff_Init Initialize the module. 77 * 78 * Suff_End Clean up the module. 79 * 80 * Suff_ExtendPaths 81 * Extend the search path of each suffix to include the 82 * default search path. 83 * 84 * Suff_ClearSuffixes 85 * Clear out all the suffixes and transformations. 86 * 87 * Suff_IsTransform 88 * See if the passed string is a transformation rule. 89 * 90 * Suff_AddSuffix Add the passed string as another known suffix. 91 * 92 * Suff_GetPath Return the search path for the given suffix. 93 * 94 * Suff_AddInclude 95 * Mark the given suffix as denoting an include file. 96 * 97 * Suff_AddLib Mark the given suffix as denoting a library. 98 * 99 * Suff_AddTransform 100 * Add another transformation to the suffix graph. 101 * 102 * Suff_SetNull Define the suffix to consider the suffix of 103 * any file that doesn't have a known one. 104 * 105 * Suff_FindDeps Find implicit sources for and the location of 106 * a target based on its suffix. Returns the 107 * bottom-most node added to the graph or NULL 108 * if the target had no implicit sources. 109 * 110 * Suff_FindPath Return the appropriate path to search in order to 111 * find the node. 112 */ 113 114 #include "make.h" 115 #include "dir.h" 116 117 /* "@(#)suff.c 8.4 (Berkeley) 3/21/94" */ 118 MAKE_RCSID("$NetBSD: suff.c,v 1.382 2024/07/07 07:50:57 rillig Exp $"); 119 120 typedef List SuffixList; 121 typedef ListNode SuffixListNode; 122 123 typedef List CandidateList; 124 typedef ListNode CandidateListNode; 125 126 /* The defined suffixes, such as '.c', '.o', '.l'. */ 127 static SuffixList sufflist = LST_INIT; 128 #ifdef CLEANUP 129 /* The suffixes to be cleaned up at the end. */ 130 static SuffixList suffClean = LST_INIT; 131 #endif 132 133 /* 134 * The transformation rules, such as '.c.o' to transform '.c' into '.o', 135 * or simply '.c' to transform 'file.c' into 'file'. 136 */ 137 static GNodeList transforms = LST_INIT; 138 139 /* 140 * Counter for assigning suffix numbers. 141 * TODO: What are these suffix numbers used for? 142 */ 143 static int sNum = 0; 144 145 /* 146 * A suffix such as ".c" or ".o" that may be used in suffix transformation 147 * rules such as ".c.o:". 148 */ 149 typedef struct Suffix { 150 /* The suffix itself, such as ".c" */ 151 char *name; 152 /* Length of the name, to avoid strlen calls */ 153 size_t nameLen; 154 /* 155 * This suffix marks include files. Their search path ends up in the 156 * undocumented special variable '.INCLUDES'. 157 */ 158 bool include:1; 159 /* 160 * This suffix marks library files. Their search path ends up in the 161 * undocumented special variable '.LIBS'. 162 */ 163 bool library:1; 164 /* 165 * The empty suffix. 166 * 167 * XXX: What is the difference between the empty suffix and the null 168 * suffix? 169 * 170 * XXX: Why is SUFF_NULL needed at all? Wouldn't nameLen == 0 mean 171 * the same? 172 */ 173 bool isNull:1; 174 /* The path along which files of this suffix may be found */ 175 SearchPath *searchPath; 176 177 /* The suffix number; TODO: document the purpose of this number */ 178 int sNum; 179 /* Reference count of list membership and several other places */ 180 int refCount; 181 182 /* Suffixes we have a transformation to */ 183 SuffixList parents; 184 /* Suffixes we have a transformation from */ 185 SuffixList children; 186 } Suffix; 187 188 /* 189 * A candidate when searching for implied sources. 190 * 191 * For example, when "src.o" is to be made, a typical candidate is "src.c" 192 * via the transformation rule ".c.o". If that doesn't exist, maybe there is 193 * another transformation rule ".pas.c" that would make "src.pas" an indirect 194 * candidate as well. The first such chain that leads to an existing file or 195 * node is finally chosen to be made. 196 */ 197 typedef struct Candidate { 198 /* The file or node to look for. */ 199 char *file; 200 /* 201 * The prefix from which file was formed. Its memory is shared among 202 * all candidates. 203 */ 204 char *prefix; 205 /* The suffix on the file. */ 206 Suffix *suff; 207 208 /* 209 * The candidate that can be made from this, or NULL for the 210 * top-level candidate. 211 */ 212 struct Candidate *parent; 213 /* The node describing the file. */ 214 GNode *node; 215 216 /* 217 * Count of existing children, only used for memory management, so we 218 * don't free this candidate too early or too late. 219 */ 220 int numChildren; 221 #ifdef DEBUG_SRC 222 CandidateList childrenList; 223 #endif 224 } Candidate; 225 226 typedef struct CandidateSearcher { 227 228 CandidateList list; 229 230 /* 231 * TODO: Add HashSet for seen entries, to avoid endless loops such as 232 * in suff-transform-endless.mk. 233 */ 234 235 } CandidateSearcher; 236 237 238 /* TODO: Document the difference between nullSuff and emptySuff. */ 239 /* The NULL suffix is used when a file has no known suffix */ 240 static Suffix *nullSuff; 241 /* The empty suffix required for POSIX single-suffix transformation rules */ 242 static Suffix *emptySuff; 243 244 245 static Suffix * 246 Suffix_Ref(Suffix *suff) 247 { 248 suff->refCount++; 249 return suff; 250 } 251 252 /* Change the value of a Suffix variable, adjusting the reference counts. */ 253 static void 254 Suffix_Reassign(Suffix **var, Suffix *suff) 255 { 256 if (*var != NULL) 257 (*var)->refCount--; 258 *var = suff; 259 suff->refCount++; 260 } 261 262 /* Set a Suffix variable to NULL, adjusting the reference count. */ 263 static void 264 Suffix_Unassign(Suffix **var) 265 { 266 if (*var != NULL) 267 (*var)->refCount--; 268 *var = NULL; 269 } 270 271 /* 272 * See if pref is a prefix of str. 273 * Return NULL if it ain't, pointer to character in str after prefix if so. 274 */ 275 static const char * 276 StrTrimPrefix(const char *pref, const char *str) 277 { 278 while (*str != '\0' && *pref == *str) { 279 pref++; 280 str++; 281 } 282 283 return *pref != '\0' ? NULL : str; 284 } 285 286 /* 287 * See if suff is a suffix of str, and if so, return the pointer to the suffix 288 * in str, which at the same time marks the end of the prefix. 289 */ 290 static const char * 291 StrTrimSuffix(const char *str, size_t strLen, const char *suff, size_t suffLen) 292 { 293 const char *suffInStr; 294 size_t i; 295 296 if (strLen < suffLen) 297 return NULL; 298 299 suffInStr = str + strLen - suffLen; 300 for (i = 0; i < suffLen; i++) 301 if (suff[i] != suffInStr[i]) 302 return NULL; 303 304 return suffInStr; 305 } 306 307 /* 308 * See if suff is a suffix of name, and if so, return the end of the prefix 309 * in name. 310 */ 311 static const char * 312 Suffix_TrimSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd) 313 { 314 return StrTrimSuffix(nameEnd - nameLen, nameLen, 315 suff->name, suff->nameLen); 316 } 317 318 static bool 319 Suffix_IsSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd) 320 { 321 return Suffix_TrimSuffix(suff, nameLen, nameEnd) != NULL; 322 } 323 324 static Suffix * 325 FindSuffixByNameLen(const char *name, size_t nameLen) 326 { 327 SuffixListNode *ln; 328 329 for (ln = sufflist.first; ln != NULL; ln = ln->next) { 330 Suffix *suff = ln->datum; 331 if (suff->nameLen == nameLen && 332 memcmp(suff->name, name, nameLen) == 0) 333 return suff; 334 } 335 return NULL; 336 } 337 338 static Suffix * 339 FindSuffixByName(const char *name) 340 { 341 return FindSuffixByNameLen(name, strlen(name)); 342 } 343 344 static GNode * 345 FindTransformByName(const char *name) 346 { 347 GNodeListNode *ln; 348 349 for (ln = transforms.first; ln != NULL; ln = ln->next) { 350 GNode *gn = ln->datum; 351 if (strcmp(gn->name, name) == 0) 352 return gn; 353 } 354 return NULL; 355 } 356 357 static void 358 SuffixList_Unref(SuffixList *list, Suffix *suff) 359 { 360 SuffixListNode *ln = Lst_FindDatum(list, suff); 361 if (ln != NULL) { 362 Lst_Remove(list, ln); 363 suff->refCount--; 364 } 365 } 366 367 static void 368 Suffix_Free(Suffix *suff) 369 { 370 371 if (suff == nullSuff) 372 nullSuff = NULL; 373 374 if (suff == emptySuff) 375 emptySuff = NULL; 376 377 #if 0 378 /* We don't delete suffixes in order, so we cannot use this */ 379 if (suff->refCount != 0) 380 Punt("Internal error deleting suffix `%s' with refcount = %d", 381 suff->name, suff->refCount); 382 #endif 383 384 Lst_Done(&suff->children); 385 Lst_Done(&suff->parents); 386 SearchPath_Free(suff->searchPath); 387 388 free(suff->name); 389 free(suff); 390 } 391 392 /* Remove the suffix from the list, and free if it is otherwise unused. */ 393 static void 394 SuffixList_Remove(SuffixList *list, Suffix *suff) 395 { 396 SuffixList_Unref(list, suff); 397 if (suff->refCount == 0) { 398 /* XXX: can lead to suff->refCount == -1 */ 399 SuffixList_Unref(&sufflist, suff); 400 DEBUG1(SUFF, "Removing suffix \"%s\"\n", suff->name); 401 Suffix_Free(suff); 402 } 403 } 404 405 /* 406 * Insert the suffix into the list, keeping the list ordered by suffix 407 * number. 408 */ 409 static void 410 SuffixList_Insert(SuffixList *list, Suffix *suff) 411 { 412 SuffixListNode *ln; 413 Suffix *listSuff = NULL; 414 415 for (ln = list->first; ln != NULL; ln = ln->next) { 416 listSuff = ln->datum; 417 if (listSuff->sNum >= suff->sNum) 418 break; 419 } 420 421 if (ln == NULL) { 422 DEBUG2(SUFF, "inserting \"%s\" (%d) at end of list\n", 423 suff->name, suff->sNum); 424 Lst_Append(list, Suffix_Ref(suff)); 425 } else if (listSuff->sNum != suff->sNum) { 426 DEBUG4(SUFF, "inserting \"%s\" (%d) before \"%s\" (%d)\n", 427 suff->name, suff->sNum, listSuff->name, listSuff->sNum); 428 Lst_InsertBefore(list, ln, Suffix_Ref(suff)); 429 } else { 430 DEBUG2(SUFF, "\"%s\" (%d) is already there\n", 431 suff->name, suff->sNum); 432 } 433 } 434 435 static void 436 Relate(Suffix *srcSuff, Suffix *targSuff) 437 { 438 SuffixList_Insert(&targSuff->children, srcSuff); 439 SuffixList_Insert(&srcSuff->parents, targSuff); 440 } 441 442 static Suffix * 443 Suffix_New(const char *name) 444 { 445 Suffix *suff = bmake_malloc(sizeof *suff); 446 447 suff->name = bmake_strdup(name); 448 suff->nameLen = strlen(suff->name); 449 suff->searchPath = SearchPath_New(); 450 Lst_Init(&suff->children); 451 Lst_Init(&suff->parents); 452 suff->sNum = sNum++; 453 suff->include = false; 454 suff->library = false; 455 suff->isNull = false; 456 suff->refCount = 1; /* XXX: why 1? It's not assigned anywhere yet. */ 457 458 return suff; 459 } 460 461 /* 462 * Nuke the list of suffixes but keep all transformation rules around. The 463 * transformation graph is destroyed in this process, but we leave the list 464 * of rules so when a new graph is formed, the rules will remain. This 465 * function is called when a line '.SUFFIXES:' with an empty suffixes list is 466 * encountered in a makefile. 467 */ 468 void 469 Suff_ClearSuffixes(void) 470 { 471 #ifdef CLEANUP 472 Lst_MoveAll(&suffClean, &sufflist); 473 #endif 474 DEBUG0(SUFF, "Clearing all suffixes\n"); 475 Lst_Init(&sufflist); 476 sNum = 0; 477 if (nullSuff != NULL) 478 Suffix_Free(nullSuff); 479 emptySuff = nullSuff = Suffix_New(""); 480 481 SearchPath_AddAll(nullSuff->searchPath, &dirSearchPath); 482 nullSuff->include = false; 483 nullSuff->library = false; 484 nullSuff->isNull = true; 485 } 486 487 /* 488 * Parse a transformation string such as ".c.o" to find its two component 489 * suffixes (the source ".c" and the target ".o"). If there are no such 490 * suffixes, try a single-suffix transformation as well. 491 * 492 * Return true if the string is a valid transformation. 493 */ 494 static bool 495 ParseTransform(const char *str, Suffix **out_src, Suffix **out_targ) 496 { 497 SuffixListNode *ln; 498 Suffix *single = NULL; 499 500 /* 501 * Loop looking first for a suffix that matches the start of the 502 * string and then for one that exactly matches the rest of it. If 503 * we can find two that meet these criteria, we've successfully 504 * parsed the string. 505 */ 506 for (ln = sufflist.first; ln != NULL; ln = ln->next) { 507 Suffix *src = ln->datum; 508 509 if (StrTrimPrefix(src->name, str) == NULL) 510 continue; 511 512 if (str[src->nameLen] == '\0') { 513 single = src; 514 } else { 515 Suffix *targ = FindSuffixByName(str + src->nameLen); 516 if (targ != NULL) { 517 *out_src = src; 518 *out_targ = targ; 519 return true; 520 } 521 } 522 } 523 524 if (single != NULL) { 525 /* 526 * There was a suffix that encompassed the entire string, so we 527 * assume it was a transformation to the null suffix (thank you 528 * POSIX; search for "single suffix" or "single-suffix"). 529 * 530 * We still prefer to find a double rule over a singleton, 531 * hence we leave this check until the end. 532 * 533 * XXX: Use emptySuff over nullSuff? 534 */ 535 *out_src = single; 536 *out_targ = nullSuff; 537 return true; 538 } 539 return false; 540 } 541 542 /* 543 * Return true if the given string is a transformation rule, that is, a 544 * concatenation of two known suffixes such as ".c.o" or a single suffix 545 * such as ".o". 546 */ 547 bool 548 Suff_IsTransform(const char *str) 549 { 550 Suffix *src, *targ; 551 552 return ParseTransform(str, &src, &targ); 553 } 554 555 /* 556 * Add the transformation rule to the list of rules and place the 557 * transformation itself in the graph. 558 * 559 * The transformation is linked to the two suffixes mentioned in the name. 560 * 561 * Input: 562 * name must have the form ".from.to" or just ".from" 563 * 564 * Results: 565 * The created or existing transformation node in the transforms list 566 */ 567 GNode * 568 Suff_AddTransform(const char *name) 569 { 570 Suffix *srcSuff; 571 Suffix *targSuff; 572 573 GNode *gn = FindTransformByName(name); 574 if (gn == NULL) { 575 /* 576 * Make a new graph node for the transformation. It will be 577 * filled in by the Parse module. 578 */ 579 gn = GNode_New(name); 580 Lst_Append(&transforms, gn); 581 } else { 582 /* 583 * New specification for transformation rule. Just nuke the 584 * old list of commands so they can be filled in again. We 585 * don't actually free the commands themselves, because a 586 * given command can be attached to several different 587 * transformations. 588 */ 589 Lst_Done(&gn->commands); 590 Lst_Init(&gn->commands); 591 Lst_Done(&gn->children); 592 Lst_Init(&gn->children); 593 } 594 595 gn->type = OP_TRANSFORM; 596 597 { 598 /* TODO: Avoid the redundant parsing here. */ 599 bool ok = ParseTransform(name, &srcSuff, &targSuff); 600 assert(ok); 601 /* LINTED 129 *//* expression has null effect */ 602 (void)ok; 603 } 604 605 /* Link the two together in the proper relationship and order. */ 606 DEBUG2(SUFF, "defining transformation from `%s' to `%s'\n", 607 srcSuff->name, targSuff->name); 608 Relate(srcSuff, targSuff); 609 610 return gn; 611 } 612 613 /* 614 * Handle the finish of a transformation definition, removing the 615 * transformation from the graph if it has neither commands nor sources. 616 * 617 * If the node has no commands or children, the children and parents lists 618 * of the affected suffixes are altered. 619 * 620 * Input: 621 * gn Node for transformation 622 */ 623 void 624 Suff_EndTransform(GNode *gn) 625 { 626 Suffix *srcSuff, *targSuff; 627 SuffixList *srcSuffParents; 628 629 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(&gn->cohorts)) 630 gn = gn->cohorts.last->datum; 631 632 if (!(gn->type & OP_TRANSFORM)) 633 return; 634 635 if (!Lst_IsEmpty(&gn->commands) || !Lst_IsEmpty(&gn->children)) { 636 DEBUG1(SUFF, "transformation %s complete\n", gn->name); 637 return; 638 } 639 640 /* 641 * SuffParseTransform() may fail for special rules which are not 642 * actual transformation rules. (e.g. .DEFAULT) 643 */ 644 if (!ParseTransform(gn->name, &srcSuff, &targSuff)) 645 return; 646 647 DEBUG2(SUFF, "deleting incomplete transformation from `%s' to `%s'\n", 648 srcSuff->name, targSuff->name); 649 650 /* 651 * Remember the parents since srcSuff could be deleted in 652 * SuffixList_Remove. 653 */ 654 srcSuffParents = &srcSuff->parents; 655 SuffixList_Remove(&targSuff->children, srcSuff); 656 SuffixList_Remove(srcSuffParents, targSuff); 657 } 658 659 /* 660 * Called from Suff_AddSuffix to search through the list of 661 * existing transformation rules and rebuild the transformation graph when 662 * it has been destroyed by Suff_ClearSuffixes. If the given rule is a 663 * transformation involving this suffix and another, existing suffix, the 664 * proper relationship is established between the two. 665 * 666 * The appropriate links will be made between this suffix and others if 667 * transformation rules exist for it. 668 * 669 * Input: 670 * transform Transformation to test 671 * suff Suffix to rebuild 672 */ 673 static void 674 RebuildGraph(GNode *transform, Suffix *suff) 675 { 676 const char *name = transform->name; 677 size_t nameLen = strlen(name); 678 const char *toName; 679 680 /* 681 * See if it is a transformation from this suffix to another suffix. 682 */ 683 toName = StrTrimPrefix(suff->name, name); 684 if (toName != NULL) { 685 Suffix *to = FindSuffixByName(toName); 686 if (to != NULL) { 687 Relate(suff, to); 688 return; 689 } 690 } 691 692 /* 693 * See if it is a transformation from another suffix to this suffix. 694 */ 695 toName = Suffix_TrimSuffix(suff, nameLen, name + nameLen); 696 if (toName != NULL) { 697 Suffix *from = FindSuffixByNameLen(name, 698 (size_t)(toName - name)); 699 if (from != NULL) 700 Relate(from, suff); 701 } 702 } 703 704 /* 705 * During Suff_AddSuffix, search through the list of existing targets and find 706 * if any of the existing targets can be turned into a transformation rule. 707 * 708 * If such a target is found and the target is the current main target, the 709 * main target is set to NULL and the next target examined (if that exists) 710 * becomes the main target. 711 * 712 * Results: 713 * true iff a new main target has been selected. 714 */ 715 static bool 716 UpdateTarget(GNode *target, Suffix *suff, bool *inout_removedMain) 717 { 718 Suffix *srcSuff, *targSuff; 719 char *ptr; 720 721 if (mainNode == NULL && *inout_removedMain && 722 GNode_IsMainCandidate(target)) { 723 DEBUG1(MAKE, "Setting main node to \"%s\"\n", target->name); 724 mainNode = target; 725 /* 726 * XXX: Why could it be a good idea to return true here? 727 * The main task of this function is to turn ordinary nodes 728 * into transformations, no matter whether or not a new .MAIN 729 * node has been found. 730 */ 731 /* 732 * XXX: Even when changing this to false, none of the existing 733 * unit tests fails. 734 */ 735 return true; 736 } 737 738 if (target->type == OP_TRANSFORM) 739 return false; 740 741 /* 742 * XXX: What about a transformation ".cpp.c"? If ".c" is added as 743 * a new suffix, it seems wrong that this transformation would be 744 * skipped just because ".c" happens to be a prefix of ".cpp". 745 */ 746 ptr = strstr(target->name, suff->name); 747 if (ptr == NULL) 748 return false; 749 750 /* 751 * XXX: In suff-rebuild.mk, in the line '.SUFFIXES: .c .b .a', this 752 * condition prevents the rule '.b.c' from being added again during 753 * Suff_AddSuffix(".b"). 754 * 755 * XXX: Removing this paragraph makes suff-add-later.mk use massive 756 * amounts of memory. 757 */ 758 if (ptr == target->name) 759 return false; 760 761 if (ParseTransform(target->name, &srcSuff, &targSuff)) { 762 if (mainNode == target) { 763 DEBUG1(MAKE, 764 "Setting main node from \"%s\" back to null\n", 765 target->name); 766 *inout_removedMain = true; 767 mainNode = NULL; 768 } 769 Lst_Done(&target->children); 770 Lst_Init(&target->children); 771 target->type = OP_TRANSFORM; 772 773 /* 774 * Link the two together in the proper relationship and order. 775 */ 776 DEBUG2(SUFF, "defining transformation from `%s' to `%s'\n", 777 srcSuff->name, targSuff->name); 778 Relate(srcSuff, targSuff); 779 } 780 return false; 781 } 782 783 /* 784 * Look at all existing targets to see if adding this suffix will make one 785 * of the current targets mutate into a suffix rule. 786 * 787 * This is ugly, but other makes treat all targets that start with a '.' as 788 * suffix rules. 789 */ 790 static void 791 UpdateTargets(Suffix *suff) 792 { 793 bool removedMain = false; 794 GNodeListNode *ln; 795 796 for (ln = Targ_List()->first; ln != NULL; ln = ln->next) { 797 GNode *gn = ln->datum; 798 if (UpdateTarget(gn, suff, &removedMain)) 799 break; 800 } 801 } 802 803 /* Add the suffix to the end of the list of known suffixes. */ 804 void 805 Suff_AddSuffix(const char *name) 806 { 807 GNodeListNode *ln; 808 809 Suffix *suff = FindSuffixByName(name); 810 if (suff != NULL) 811 return; 812 813 suff = Suffix_New(name); 814 Lst_Append(&sufflist, suff); 815 DEBUG1(SUFF, "Adding suffix \"%s\"\n", suff->name); 816 817 UpdateTargets(suff); 818 819 /* 820 * Look for any existing transformations from or to this suffix. 821 * XXX: Only do this after a Suff_ClearSuffixes? 822 */ 823 for (ln = transforms.first; ln != NULL; ln = ln->next) 824 RebuildGraph(ln->datum, suff); 825 } 826 827 /* Return the search path for the given suffix, or NULL. */ 828 SearchPath * 829 Suff_GetPath(const char *name) 830 { 831 Suffix *suff = FindSuffixByName(name); 832 return suff != NULL ? suff->searchPath : NULL; 833 } 834 835 /* 836 * Extend the search paths for all suffixes to include the default search 837 * path (dirSearchPath). 838 * 839 * The default search path can be defined using the special target '.PATH'. 840 * The search path of each suffix can be defined using the special target 841 * '.PATH<suffix>'. 842 * 843 * If paths were specified for the ".h" suffix, the directories are stuffed 844 * into a global variable called ".INCLUDES" with each directory preceded by 845 * '-I'. The same is done for the ".a" suffix, except the variable is called 846 * ".LIBS" and the flag is '-L'. 847 */ 848 void 849 Suff_ExtendPaths(void) 850 { 851 SuffixListNode *ln; 852 char *flags; 853 SearchPath *includesPath = SearchPath_New(); 854 SearchPath *libsPath = SearchPath_New(); 855 856 for (ln = sufflist.first; ln != NULL; ln = ln->next) { 857 Suffix *suff = ln->datum; 858 if (!Lst_IsEmpty(&suff->searchPath->dirs)) { 859 if (suff->include) 860 SearchPath_AddAll(includesPath, 861 suff->searchPath); 862 if (suff->library) 863 SearchPath_AddAll(libsPath, suff->searchPath); 864 SearchPath_AddAll(suff->searchPath, &dirSearchPath); 865 } else { 866 SearchPath_Free(suff->searchPath); 867 suff->searchPath = Dir_CopyDirSearchPath(); 868 } 869 } 870 871 flags = SearchPath_ToFlags(includesPath, "-I"); 872 Global_Set(".INCLUDES", flags); 873 free(flags); 874 875 flags = SearchPath_ToFlags(libsPath, "-L"); 876 Global_Set(".LIBS", flags); 877 free(flags); 878 879 SearchPath_Free(includesPath); 880 SearchPath_Free(libsPath); 881 } 882 883 /* 884 * Add the given suffix as a type of file which gets included. 885 * Called when a '.INCLUDES: .h' line is parsed. 886 * To have an effect, the suffix must already exist. 887 * This affects the magic variable '.INCLUDES'. 888 */ 889 void 890 Suff_AddInclude(const char *suffName) 891 { 892 Suffix *suff = FindSuffixByName(suffName); 893 if (suff != NULL) 894 suff->include = true; 895 } 896 897 /* 898 * Add the given suffix as a type of file which is a library. 899 * Called when a '.LIBS: .a' line is parsed. 900 * To have an effect, the suffix must already exist. 901 * This affects the magic variable '.LIBS'. 902 */ 903 void 904 Suff_AddLib(const char *suffName) 905 { 906 Suffix *suff = FindSuffixByName(suffName); 907 if (suff != NULL) 908 suff->library = true; 909 } 910 911 /********** Implicit Source Search Functions *********/ 912 913 static void 914 CandidateSearcher_Init(CandidateSearcher *cs) 915 { 916 Lst_Init(&cs->list); 917 } 918 919 static void 920 CandidateSearcher_Done(CandidateSearcher *cs) 921 { 922 Lst_Done(&cs->list); 923 } 924 925 static void 926 CandidateSearcher_Add(CandidateSearcher *cs, Candidate *cand) 927 { 928 /* TODO: filter duplicates */ 929 Lst_Append(&cs->list, cand); 930 } 931 932 static void 933 CandidateSearcher_AddIfNew(CandidateSearcher *cs, Candidate *cand) 934 { 935 /* TODO: filter duplicates */ 936 if (Lst_FindDatum(&cs->list, cand) == NULL) 937 Lst_Append(&cs->list, cand); 938 } 939 940 static void 941 CandidateSearcher_MoveAll(CandidateSearcher *cs, CandidateList *list) 942 { 943 /* TODO: filter duplicates */ 944 Lst_MoveAll(&cs->list, list); 945 } 946 947 948 #ifdef DEBUG_SRC 949 static void 950 CandidateList_PrintAddrs(CandidateList *list) 951 { 952 CandidateListNode *ln; 953 954 for (ln = list->first; ln != NULL; ln = ln->next) { 955 Candidate *cand = ln->datum; 956 debug_printf(" %p:%s", cand, cand->file); 957 } 958 debug_printf("\n"); 959 } 960 #endif 961 962 static Candidate * 963 Candidate_New(char *name, char *prefix, Suffix *suff, Candidate *parent, 964 GNode *gn) 965 { 966 Candidate *cand = bmake_malloc(sizeof *cand); 967 968 cand->file = name; 969 cand->prefix = prefix; 970 cand->suff = Suffix_Ref(suff); 971 cand->parent = parent; 972 cand->node = gn; 973 cand->numChildren = 0; 974 #ifdef DEBUG_SRC 975 Lst_Init(&cand->childrenList); 976 #endif 977 978 return cand; 979 } 980 981 /* Add a new candidate to the list. */ 982 static void 983 CandidateList_Add(CandidateList *list, char *srcName, Candidate *targ, 984 Suffix *suff, const char *debug_tag MAKE_ATTR_UNUSED) 985 { 986 Candidate *cand = Candidate_New(srcName, targ->prefix, suff, targ, 987 NULL); 988 targ->numChildren++; 989 Lst_Append(list, cand); 990 991 #ifdef DEBUG_SRC 992 Lst_Append(&targ->childrenList, cand); 993 debug_printf("%s add suff %p:%s candidate %p:%s to list %p:", 994 debug_tag, targ, targ->file, cand, cand->file, list); 995 CandidateList_PrintAddrs(list); 996 #endif 997 } 998 999 /* 1000 * Add all candidates to the list that can be formed by applying a suffix to 1001 * the candidate. 1002 */ 1003 static void 1004 CandidateList_AddCandidatesFor(CandidateList *list, Candidate *cand) 1005 { 1006 SuffixListNode *ln; 1007 for (ln = cand->suff->children.first; ln != NULL; ln = ln->next) { 1008 Suffix *suff = ln->datum; 1009 1010 if (suff->isNull && suff->name[0] != '\0') { 1011 /* 1012 * If the suffix has been marked as the NULL suffix, 1013 * also create a candidate for a file with no suffix 1014 * attached. 1015 */ 1016 CandidateList_Add(list, bmake_strdup(cand->prefix), 1017 cand, suff, "1"); 1018 } 1019 1020 CandidateList_Add(list, str_concat2(cand->prefix, suff->name), 1021 cand, suff, "2"); 1022 } 1023 } 1024 1025 /* 1026 * Free the first candidate in the list that is not referenced anymore. 1027 * Return whether a candidate was removed. 1028 */ 1029 static bool 1030 RemoveCandidate(CandidateList *srcs) 1031 { 1032 CandidateListNode *ln; 1033 1034 #ifdef DEBUG_SRC 1035 debug_printf("cleaning list %p:", srcs); 1036 CandidateList_PrintAddrs(srcs); 1037 #endif 1038 1039 for (ln = srcs->first; ln != NULL; ln = ln->next) { 1040 Candidate *src = ln->datum; 1041 1042 if (src->numChildren == 0) { 1043 if (src->parent == NULL) 1044 free(src->prefix); 1045 else { 1046 #ifdef DEBUG_SRC 1047 /* XXX: Lst_RemoveDatum */ 1048 CandidateListNode *ln2; 1049 ln2 = Lst_FindDatum(&src->parent->childrenList, 1050 src); 1051 if (ln2 != NULL) 1052 Lst_Remove(&src->parent->childrenList, 1053 ln2); 1054 #endif 1055 src->parent->numChildren--; 1056 } 1057 #ifdef DEBUG_SRC 1058 debug_printf("free: list %p src %p:%s children %d\n", 1059 srcs, src, src->file, src->numChildren); 1060 Lst_Done(&src->childrenList); 1061 #endif 1062 Lst_Remove(srcs, ln); 1063 free(src->file); 1064 free(src); 1065 return true; 1066 } 1067 #ifdef DEBUG_SRC 1068 else { 1069 debug_printf("keep: list %p src %p:%s children %d:", 1070 srcs, src, src->file, src->numChildren); 1071 CandidateList_PrintAddrs(&src->childrenList); 1072 } 1073 #endif 1074 } 1075 1076 return false; 1077 } 1078 1079 /* Find the first existing file/target in srcs. */ 1080 static Candidate * 1081 FindThem(CandidateList *srcs, CandidateSearcher *cs) 1082 { 1083 HashSet seen; 1084 1085 HashSet_Init(&seen); 1086 1087 while (!Lst_IsEmpty(srcs)) { 1088 Candidate *src = Lst_Dequeue(srcs); 1089 1090 #ifdef DEBUG_SRC 1091 debug_printf("remove from list %p src %p:%s\n", 1092 srcs, src, src->file); 1093 #endif 1094 DEBUG1(SUFF, "\ttrying %s...", src->file); 1095 1096 /* 1097 * A file is considered to exist if either a node exists in the 1098 * graph for it or the file actually exists. 1099 */ 1100 if (Targ_FindNode(src->file) != NULL) { 1101 found: 1102 HashSet_Done(&seen); 1103 DEBUG0(SUFF, "got it\n"); 1104 return src; 1105 } 1106 1107 { 1108 char *file = Dir_FindFile(src->file, 1109 src->suff->searchPath); 1110 if (file != NULL) { 1111 free(file); 1112 goto found; 1113 } 1114 } 1115 1116 DEBUG0(SUFF, "not there\n"); 1117 1118 if (HashSet_Add(&seen, src->file)) 1119 CandidateList_AddCandidatesFor(srcs, src); 1120 else { 1121 DEBUG1(SUFF, "FindThem: skipping duplicate \"%s\"\n", 1122 src->file); 1123 } 1124 1125 CandidateSearcher_Add(cs, src); 1126 } 1127 1128 HashSet_Done(&seen); 1129 return NULL; 1130 } 1131 1132 /* 1133 * See if any of the children of the candidate's GNode is one from which the 1134 * target can be transformed. If there is one, a candidate is put together 1135 * for it and returned. 1136 */ 1137 static Candidate * 1138 FindCmds(Candidate *targ, CandidateSearcher *cs) 1139 { 1140 GNodeListNode *gln; 1141 GNode *tgn; /* Target GNode */ 1142 GNode *sgn; /* Source GNode */ 1143 size_t prefLen; /* The length of the defined prefix */ 1144 Suffix *suff; /* Suffix of the matching candidate */ 1145 Candidate *ret; /* Return value */ 1146 1147 tgn = targ->node; 1148 prefLen = strlen(targ->prefix); 1149 1150 for (gln = tgn->children.first; gln != NULL; gln = gln->next) { 1151 const char *base; 1152 1153 sgn = gln->datum; 1154 1155 if (sgn->type & OP_OPTIONAL && Lst_IsEmpty(&tgn->commands)) { 1156 /* 1157 * We haven't looked to see if .OPTIONAL files exist 1158 * yet, so don't use one as the implicit source. 1159 * This allows us to use .OPTIONAL in .depend files so 1160 * make won't complain "don't know how to make xxx.h" 1161 * when a dependent file has been moved/deleted. 1162 */ 1163 continue; 1164 } 1165 1166 base = str_basename(sgn->name); 1167 if (strncmp(base, targ->prefix, prefLen) != 0) 1168 continue; 1169 /* 1170 * The node matches the prefix, see if it has a known suffix. 1171 */ 1172 suff = FindSuffixByName(base + prefLen); 1173 if (suff == NULL) 1174 continue; 1175 1176 /* 1177 * It even has a known suffix, see if there's a transformation 1178 * defined between the node's suffix and the target's suffix. 1179 * 1180 * XXX: Handle multi-stage transformations here, too. 1181 */ 1182 1183 if (Lst_FindDatum(&suff->parents, targ->suff) != NULL) 1184 break; 1185 } 1186 1187 if (gln == NULL) 1188 return NULL; 1189 1190 ret = Candidate_New(bmake_strdup(sgn->name), targ->prefix, suff, targ, 1191 sgn); 1192 targ->numChildren++; 1193 #ifdef DEBUG_SRC 1194 debug_printf("3 add targ %p:%s ret %p:%s\n", 1195 targ, targ->file, ret, ret->file); 1196 Lst_Append(&targ->childrenList, ret); 1197 #endif 1198 CandidateSearcher_Add(cs, ret); 1199 DEBUG1(SUFF, "\tusing existing source %s\n", sgn->name); 1200 return ret; 1201 } 1202 1203 static void 1204 ExpandWildcards(GNodeListNode *cln, GNode *pgn) 1205 { 1206 GNode *cgn = cln->datum; 1207 StringList expansions; 1208 1209 if (!Dir_HasWildcards(cgn->name)) 1210 return; 1211 1212 /* Expand the word along the chosen path. */ 1213 Lst_Init(&expansions); 1214 SearchPath_Expand(Suff_FindPath(cgn), cgn->name, &expansions); 1215 1216 while (!Lst_IsEmpty(&expansions)) { 1217 GNode *gn; 1218 /* 1219 * Fetch next expansion off the list and find its GNode 1220 */ 1221 char *name = Lst_Dequeue(&expansions); 1222 1223 DEBUG1(SUFF, "%s...", name); 1224 gn = Targ_GetNode(name); 1225 free(name); 1226 1227 /* Insert gn before the original child. */ 1228 Lst_InsertBefore(&pgn->children, cln, gn); 1229 Lst_Append(&gn->parents, pgn); 1230 pgn->unmade++; 1231 } 1232 1233 Lst_Done(&expansions); 1234 1235 DEBUG0(SUFF, "\n"); 1236 1237 /* 1238 * Now that the source is expanded, remove it from the list of 1239 * children, to keep it from being processed. 1240 */ 1241 pgn->unmade--; 1242 Lst_Remove(&pgn->children, cln); 1243 Lst_Remove(&cgn->parents, Lst_FindDatum(&cgn->parents, pgn)); 1244 } 1245 1246 /* 1247 * Break the result into a vector of strings whose nodes we can find, then 1248 * add those nodes to the members list. 1249 * 1250 * Unfortunately, we can't use Str_Words because it doesn't understand about 1251 * expressions with spaces in them. 1252 */ 1253 static void 1254 ExpandChildrenRegular(char *p, GNode *pgn, GNodeList *members) 1255 { 1256 char *start; 1257 1258 pp_skip_hspace(&p); 1259 start = p; 1260 while (*p != '\0') { 1261 if (*p == ' ' || *p == '\t') { 1262 GNode *gn; 1263 /* 1264 * White-space -- terminate element, find the node, 1265 * add it, skip any further spaces. 1266 */ 1267 *p++ = '\0'; 1268 gn = Targ_GetNode(start); 1269 Lst_Append(members, gn); 1270 pp_skip_hspace(&p); 1271 /* Continue at the next non-space. */ 1272 start = p; 1273 } else if (*p == '$') { 1274 /* Skip over the expression. */ 1275 const char *nested_p = p; 1276 FStr junk = Var_Parse(&nested_p, pgn, VARE_PARSE); 1277 /* TODO: handle errors */ 1278 if (junk.str == var_Error) { 1279 Parse_Error(PARSE_FATAL, 1280 "Malformed expression at \"%s\"", p); 1281 p++; 1282 } else { 1283 p += nested_p - p; 1284 } 1285 1286 FStr_Done(&junk); 1287 } else if (p[0] == '\\' && p[1] != '\0') { 1288 /* Escaped something -- skip over it. */ 1289 /* 1290 * XXX: In other places, escaping at this syntactical 1291 * position is done by a '$', not a '\'. The '\' is 1292 * only used in variable modifiers. 1293 */ 1294 p += 2; 1295 } else { 1296 p++; 1297 } 1298 } 1299 1300 if (p != start) { 1301 /* 1302 * Stuff left over -- add it to the list too 1303 */ 1304 GNode *gn = Targ_GetNode(start); 1305 Lst_Append(members, gn); 1306 } 1307 } 1308 1309 /* 1310 * Expand the names of any children of a given node that contain 1311 * expressions or file wildcards into actual targets. 1312 * 1313 * The expanded node is removed from the parent's list of children, and the 1314 * parent's unmade counter is decremented, but other nodes may be added. 1315 * 1316 * Input: 1317 * cln Child to examine 1318 * pgn Parent node being processed 1319 */ 1320 static void 1321 ExpandChildren(GNodeListNode *cln, GNode *pgn) 1322 { 1323 GNode *cgn = cln->datum; 1324 char *expanded; 1325 1326 if (!Lst_IsEmpty(&cgn->order_pred) || !Lst_IsEmpty(&cgn->order_succ)) 1327 /* It is all too hard to process the result of .ORDER */ 1328 return; 1329 1330 if (cgn->type & OP_WAIT) 1331 /* Ignore these (& OP_PHONY ?) */ 1332 return; 1333 1334 /* 1335 * First do variable expansion -- this takes precedence over wildcard 1336 * expansion. If the result contains wildcards, they'll be gotten to 1337 * later since the resulting words are tacked on to the end of the 1338 * children list. 1339 */ 1340 if (strchr(cgn->name, '$') == NULL) { 1341 ExpandWildcards(cln, pgn); 1342 return; 1343 } 1344 1345 DEBUG1(SUFF, "Expanding \"%s\"...", cgn->name); 1346 expanded = Var_Subst(cgn->name, pgn, VARE_EVAL_DEFINED); 1347 /* TODO: handle errors */ 1348 1349 { 1350 GNodeList members = LST_INIT; 1351 1352 if (cgn->type & OP_ARCHV) { 1353 /* 1354 * Node was an 'archive(member)' target, so 1355 * call on the Arch module to find the nodes for us, 1356 * expanding variables in the parent's scope. 1357 */ 1358 char *ap = expanded; 1359 (void)Arch_ParseArchive(&ap, &members, pgn); 1360 } else { 1361 ExpandChildrenRegular(expanded, pgn, &members); 1362 } 1363 1364 /* Add all members to the parent node. */ 1365 while (!Lst_IsEmpty(&members)) { 1366 GNode *gn = Lst_Dequeue(&members); 1367 1368 DEBUG1(SUFF, "%s...", gn->name); 1369 Lst_InsertBefore(&pgn->children, cln, gn); 1370 Lst_Append(&gn->parents, pgn); 1371 pgn->unmade++; 1372 ExpandWildcards(cln->prev, pgn); 1373 } 1374 Lst_Done(&members); 1375 1376 free(expanded); 1377 } 1378 1379 DEBUG0(SUFF, "\n"); 1380 1381 /* 1382 * The source is expanded now, so remove it from the list of children, 1383 * to keep it from being processed. 1384 */ 1385 pgn->unmade--; 1386 Lst_Remove(&pgn->children, cln); 1387 Lst_Remove(&cgn->parents, Lst_FindDatum(&cgn->parents, pgn)); 1388 } 1389 1390 static void 1391 ExpandAllChildren(GNode *gn) 1392 { 1393 GNodeListNode *ln, *nln; 1394 1395 for (ln = gn->children.first; ln != NULL; ln = nln) { 1396 nln = ln->next; 1397 ExpandChildren(ln, gn); 1398 } 1399 } 1400 1401 /* 1402 * Find a path along which to search or expand the node. 1403 * 1404 * If the node has a known suffix, use that path, 1405 * otherwise use the default system search path. 1406 */ 1407 SearchPath * 1408 Suff_FindPath(GNode *gn) 1409 { 1410 Suffix *suff = gn->suffix; 1411 1412 if (suff == NULL) { 1413 char *name = gn->name; 1414 size_t nameLen = strlen(gn->name); 1415 SuffixListNode *ln; 1416 for (ln = sufflist.first; ln != NULL; ln = ln->next) 1417 if (Suffix_IsSuffix(ln->datum, nameLen, name + nameLen)) 1418 break; 1419 1420 DEBUG1(SUFF, "Wildcard expanding \"%s\"...", gn->name); 1421 if (ln != NULL) 1422 suff = ln->datum; 1423 /* 1424 * XXX: Here we can save the suffix so we don't have to do 1425 * this again. 1426 */ 1427 } 1428 1429 if (suff != NULL) { 1430 DEBUG1(SUFF, "suffix is \"%s\"...\n", suff->name); 1431 return suff->searchPath; 1432 } else { 1433 DEBUG0(SUFF, "\n"); 1434 return &dirSearchPath; /* Use default search path */ 1435 } 1436 } 1437 1438 /* 1439 * Apply a transformation rule, given the source and target nodes and 1440 * suffixes. 1441 * 1442 * The source and target are linked and the commands from the transformation 1443 * are added to the target node's commands list. The target also inherits all 1444 * the sources for the transformation rule. 1445 * 1446 * Results: 1447 * true if successful, false if not. 1448 */ 1449 static bool 1450 ApplyTransform(GNode *tgn, GNode *sgn, Suffix *tsuff, Suffix *ssuff) 1451 { 1452 GNodeListNode *ln; 1453 char *tname; /* Name of transformation rule */ 1454 GNode *gn; /* Node for the transformation rule */ 1455 1456 /* Form the proper links between the target and source. */ 1457 Lst_Append(&tgn->children, sgn); 1458 Lst_Append(&sgn->parents, tgn); 1459 tgn->unmade++; 1460 1461 /* Locate the transformation rule itself. */ 1462 tname = str_concat2(ssuff->name, tsuff->name); 1463 gn = FindTransformByName(tname); 1464 free(tname); 1465 1466 /* This can happen when linking an OP_MEMBER and OP_ARCHV node. */ 1467 if (gn == NULL) 1468 return false; 1469 1470 DEBUG3(SUFF, "\tapplying %s -> %s to \"%s\"\n", 1471 ssuff->name, tsuff->name, tgn->name); 1472 1473 /* Record last child; Make_HandleUse may add child nodes. */ 1474 ln = tgn->children.last; 1475 1476 /* Apply the rule. */ 1477 Make_HandleUse(gn, tgn); 1478 1479 /* Deal with wildcards and expressions in any acquired sources. */ 1480 ln = ln != NULL ? ln->next : NULL; 1481 while (ln != NULL) { 1482 GNodeListNode *nln = ln->next; 1483 ExpandChildren(ln, tgn); 1484 ln = nln; 1485 } 1486 1487 /* 1488 * Keep track of another parent to which this node is transformed so 1489 * the .IMPSRC variable can be set correctly for the parent. 1490 */ 1491 Lst_Append(&sgn->implicitParents, tgn); 1492 1493 return true; 1494 } 1495 1496 /* 1497 * Member has a known suffix, so look for a transformation rule from 1498 * it to a possible suffix of the archive. 1499 * 1500 * Rather than searching through the entire list, we just look at 1501 * suffixes to which the member's suffix may be transformed. 1502 */ 1503 static void 1504 ExpandMember(GNode *gn, const char *eoarch, GNode *mem, Suffix *memSuff) 1505 { 1506 SuffixListNode *ln; 1507 size_t nameLen = (size_t)(eoarch - gn->name); 1508 1509 /* Use first matching suffix... */ 1510 for (ln = memSuff->parents.first; ln != NULL; ln = ln->next) 1511 if (Suffix_IsSuffix(ln->datum, nameLen, eoarch)) 1512 break; 1513 1514 if (ln != NULL) { 1515 Suffix *suff = ln->datum; 1516 if (!ApplyTransform(gn, mem, suff, memSuff)) { 1517 DEBUG2(SUFF, "\tNo transformation from %s -> %s\n", 1518 memSuff->name, suff->name); 1519 } 1520 } 1521 } 1522 1523 static void FindDeps(GNode *, CandidateSearcher *); 1524 1525 /* 1526 * Locate dependencies for an OP_ARCHV node. 1527 * 1528 * Side Effects: 1529 * Same as Suff_FindDeps 1530 */ 1531 static void 1532 FindDepsArchive(GNode *gn, CandidateSearcher *cs) 1533 { 1534 char *eoarch; /* End of archive portion */ 1535 char *eoname; /* End of member portion */ 1536 GNode *mem; /* Node for member */ 1537 Suffix *memSuff; 1538 const char *name; /* Start of member's name */ 1539 1540 /* 1541 * The node is an 'archive(member)' pair, so we must find a 1542 * suffix for both of them. 1543 */ 1544 eoarch = strchr(gn->name, '('); 1545 eoname = strchr(eoarch, ')'); 1546 1547 /* 1548 * Caller guarantees the format `libname(member)', via 1549 * Arch_ParseArchive. 1550 */ 1551 assert(eoarch != NULL); 1552 assert(eoname != NULL); 1553 1554 *eoname = '\0'; /* Nuke parentheses during suffix search */ 1555 *eoarch = '\0'; /* So a suffix can be found */ 1556 1557 name = eoarch + 1; 1558 1559 /* 1560 * To simplify things, call Suff_FindDeps recursively on the member 1561 * now, so we can simply compare the member's .PREFIX and .TARGET 1562 * variables to locate its suffix. This allows us to figure out the 1563 * suffix to use for the archive without having to do a quadratic 1564 * search over the suffix list, backtracking for each one. 1565 */ 1566 mem = Targ_GetNode(name); 1567 FindDeps(mem, cs); 1568 1569 /* Create the link between the two nodes right off. */ 1570 Lst_Append(&gn->children, mem); 1571 Lst_Append(&mem->parents, gn); 1572 gn->unmade++; 1573 1574 /* Copy in the variables from the member node to this one. */ 1575 Var_Set(gn, PREFIX, GNode_VarPrefix(mem)); 1576 Var_Set(gn, TARGET, GNode_VarTarget(mem)); 1577 1578 memSuff = mem->suffix; 1579 if (memSuff == NULL) { /* Didn't know what it was. */ 1580 DEBUG0(SUFF, "using null suffix\n"); 1581 memSuff = nullSuff; 1582 } 1583 1584 1585 /* Set the other two local variables required for this target. */ 1586 Var_Set(gn, MEMBER, name); 1587 Var_Set(gn, ARCHIVE, gn->name); 1588 /* Set $@ for compatibility with other makes. */ 1589 Var_Set(gn, TARGET, gn->name); 1590 1591 /* 1592 * Now we've got the important local variables set, expand any sources 1593 * that still contain variables or wildcards in their names. 1594 */ 1595 ExpandAllChildren(gn); 1596 1597 if (memSuff != NULL) 1598 ExpandMember(gn, eoarch, mem, memSuff); 1599 1600 /* 1601 * Replace the opening and closing parens now we've no need of the 1602 * separate pieces. 1603 */ 1604 *eoarch = '('; 1605 *eoname = ')'; 1606 1607 /* 1608 * Pretend gn appeared to the left of a dependency operator so the 1609 * user needn't provide a transformation from the member to the 1610 * archive. 1611 */ 1612 if (!GNode_IsTarget(gn)) 1613 gn->type |= OP_DEPENDS; 1614 1615 /* 1616 * Flag the member as such so we remember to look in the archive for 1617 * its modification time. The OP_JOIN | OP_MADE is needed because 1618 * this target should never get made. 1619 */ 1620 mem->type |= OP_MEMBER | OP_JOIN | OP_MADE; 1621 } 1622 1623 /* 1624 * If the node is a library, it is the arch module's job to find it 1625 * and set the TARGET variable accordingly. We merely provide the 1626 * search path, assuming all libraries end in ".a" (if the suffix 1627 * hasn't been defined, there's nothing we can do for it, so we just 1628 * set the TARGET variable to the node's name in order to give it a 1629 * value). 1630 */ 1631 static void 1632 FindDepsLib(GNode *gn) 1633 { 1634 Suffix *suff = FindSuffixByName(LIBSUFF); 1635 if (suff != NULL) { 1636 Suffix_Reassign(&gn->suffix, suff); 1637 Arch_FindLib(gn, suff->searchPath); 1638 } else { 1639 Suffix_Unassign(&gn->suffix); 1640 Var_Set(gn, TARGET, gn->name); 1641 } 1642 1643 /* 1644 * Because a library (-lfoo) target doesn't follow the standard 1645 * filesystem conventions, we don't set the regular variables for 1646 * the thing. .PREFIX is simply made empty. 1647 */ 1648 Var_Set(gn, PREFIX, ""); 1649 } 1650 1651 static void 1652 FindDepsRegularKnown(const char *name, size_t nameLen, GNode *gn, 1653 CandidateList *srcs, CandidateList *targs) 1654 { 1655 SuffixListNode *ln; 1656 Candidate *targ; 1657 char *pref; 1658 1659 for (ln = sufflist.first; ln != NULL; ln = ln->next) { 1660 Suffix *suff = ln->datum; 1661 if (!Suffix_IsSuffix(suff, nameLen, name + nameLen)) 1662 continue; 1663 1664 pref = bmake_strldup(name, (size_t)(nameLen - suff->nameLen)); 1665 targ = Candidate_New(bmake_strdup(gn->name), pref, suff, NULL, 1666 gn); 1667 1668 CandidateList_AddCandidatesFor(srcs, targ); 1669 1670 /* Record the target so we can nuke it. */ 1671 Lst_Append(targs, targ); 1672 } 1673 } 1674 1675 static void 1676 FindDepsRegularUnknown(GNode *gn, const char *sopref, 1677 CandidateList *srcs, CandidateList *targs) 1678 { 1679 Candidate *targ; 1680 1681 if (!Lst_IsEmpty(targs) || nullSuff == NULL) 1682 return; 1683 1684 DEBUG1(SUFF, "\tNo known suffix on %s. Using .NULL suffix\n", gn->name); 1685 1686 targ = Candidate_New(bmake_strdup(gn->name), bmake_strdup(sopref), 1687 nullSuff, NULL, gn); 1688 1689 /* 1690 * Only use the default suffix rules if we don't have commands 1691 * defined for this gnode; traditional make programs used to not 1692 * define suffix rules if the gnode had children but we don't do 1693 * this anymore. 1694 */ 1695 if (Lst_IsEmpty(&gn->commands)) 1696 CandidateList_AddCandidatesFor(srcs, targ); 1697 else { 1698 DEBUG0(SUFF, "not "); 1699 } 1700 1701 DEBUG0(SUFF, "adding suffix rules\n"); 1702 1703 Lst_Append(targs, targ); 1704 } 1705 1706 /* 1707 * Deal with finding the thing on the default search path. We always do 1708 * that, not only if the node is only a source (not on the lhs of a 1709 * dependency operator or [XXX] it has neither children or commands) as 1710 * the old pmake did. 1711 */ 1712 static void 1713 FindDepsRegularPath(GNode *gn, Candidate *targ) 1714 { 1715 if (gn->type & (OP_PHONY | OP_NOPATH)) 1716 return; 1717 1718 free(gn->path); 1719 gn->path = Dir_FindFile(gn->name, 1720 targ == NULL ? &dirSearchPath : targ->suff->searchPath); 1721 if (gn->path == NULL) 1722 return; 1723 1724 Var_Set(gn, TARGET, gn->path); 1725 1726 if (targ != NULL) { 1727 /* 1728 * Suffix known for the thing -- trim the suffix off 1729 * the path to form the proper .PREFIX variable. 1730 */ 1731 size_t savep = strlen(gn->path) - targ->suff->nameLen; 1732 char savec; 1733 1734 Suffix_Reassign(&gn->suffix, targ->suff); 1735 1736 savec = gn->path[savep]; 1737 gn->path[savep] = '\0'; 1738 1739 Var_Set(gn, PREFIX, str_basename(gn->path)); 1740 1741 gn->path[savep] = savec; 1742 } else { 1743 /* 1744 * The .PREFIX gets the full path if the target has no 1745 * known suffix. 1746 */ 1747 Suffix_Unassign(&gn->suffix); 1748 Var_Set(gn, PREFIX, str_basename(gn->path)); 1749 } 1750 } 1751 1752 /* 1753 * Locate implicit dependencies for regular targets. 1754 * 1755 * Input: 1756 * gn Node for which to find sources 1757 * 1758 * Side Effects: 1759 * Same as Suff_FindDeps 1760 */ 1761 static void 1762 FindDepsRegular(GNode *gn, CandidateSearcher *cs) 1763 { 1764 /* List of sources at which to look */ 1765 CandidateList srcs = LST_INIT; 1766 /* 1767 * List of targets to which things can be transformed. 1768 * They all have the same file, but different suff and prefix fields. 1769 */ 1770 CandidateList targs = LST_INIT; 1771 Candidate *bottom; /* Start of found transformation path */ 1772 Candidate *src; 1773 Candidate *targ; 1774 1775 const char *name = gn->name; 1776 size_t nameLen = strlen(name); 1777 1778 #ifdef DEBUG_SRC 1779 DEBUG1(SUFF, "FindDepsRegular \"%s\"\n", gn->name); 1780 #endif 1781 1782 /* 1783 * We're caught in a catch-22 here. On the one hand, we want to use 1784 * any transformation implied by the target's sources, but we can't 1785 * examine the sources until we've expanded any variables/wildcards 1786 * they may hold, and we can't do that until we've set up the 1787 * target's local variables and we can't do that until we know what 1788 * the proper suffix for the target is (in case there are two 1789 * suffixes one of which is a suffix of the other) and we can't know 1790 * that until we've found its implied source, which we may not want 1791 * to use if there's an existing source that implies a different 1792 * transformation. 1793 * 1794 * In an attempt to get around this, which may not work all the time, 1795 * but should work most of the time, we look for implied sources 1796 * first, checking transformations to all possible suffixes of the 1797 * target, use what we find to set the target's local variables, 1798 * expand the children, then look for any overriding transformations 1799 * they imply. Should we find one, we discard the one we found before. 1800 */ 1801 bottom = NULL; 1802 targ = NULL; 1803 1804 if (!(gn->type & OP_PHONY)) { 1805 1806 FindDepsRegularKnown(name, nameLen, gn, &srcs, &targs); 1807 1808 /* Handle target of unknown suffix... */ 1809 FindDepsRegularUnknown(gn, name, &srcs, &targs); 1810 1811 /* 1812 * Using the list of possible sources built up from the target 1813 * suffix(es), try and find an existing file/target that 1814 * matches. 1815 */ 1816 bottom = FindThem(&srcs, cs); 1817 1818 if (bottom == NULL) { 1819 /* 1820 * No known transformations -- use the first suffix 1821 * found for setting the local variables. 1822 */ 1823 if (targs.first != NULL) 1824 targ = targs.first->datum; 1825 else 1826 targ = NULL; 1827 } else { 1828 /* 1829 * Work up the transformation path to find the suffix 1830 * of the target to which the transformation was made. 1831 */ 1832 for (targ = bottom; 1833 targ->parent != NULL; targ = targ->parent) 1834 continue; 1835 } 1836 } 1837 1838 Var_Set(gn, TARGET, GNode_Path(gn)); 1839 Var_Set(gn, PREFIX, targ != NULL ? targ->prefix : gn->name); 1840 1841 /* 1842 * Now we've got the important local variables set, expand any sources 1843 * that still contain variables or wildcards in their names. 1844 */ 1845 { 1846 GNodeListNode *ln, *nln; 1847 for (ln = gn->children.first; ln != NULL; ln = nln) { 1848 nln = ln->next; 1849 ExpandChildren(ln, gn); 1850 } 1851 } 1852 1853 if (targ == NULL) { 1854 DEBUG1(SUFF, "\tNo valid suffix on %s\n", gn->name); 1855 1856 sfnd_abort: 1857 FindDepsRegularPath(gn, targ); 1858 goto sfnd_return; 1859 } 1860 1861 /* 1862 * If the suffix indicates that the target is a library, mark that in 1863 * the node's type field. 1864 */ 1865 if (targ->suff->library) 1866 gn->type |= OP_LIB; 1867 1868 /* 1869 * Check for overriding transformation rule implied by sources 1870 */ 1871 if (!Lst_IsEmpty(&gn->children)) { 1872 src = FindCmds(targ, cs); 1873 1874 if (src != NULL) { 1875 /* 1876 * Free up all the candidates in the transformation 1877 * path, up to but not including the parent node. 1878 */ 1879 while (bottom != NULL && bottom->parent != NULL) { 1880 CandidateSearcher_AddIfNew(cs, bottom); 1881 bottom = bottom->parent; 1882 } 1883 bottom = src; 1884 } 1885 } 1886 1887 if (bottom == NULL) { 1888 /* No idea from where it can come -- return now. */ 1889 goto sfnd_abort; 1890 } 1891 1892 /* 1893 * We now have a list of candidates headed by 'bottom' and linked via 1894 * their 'parent' pointers. What we do next is create links between 1895 * source and target nodes (which may or may not have been created) 1896 * and set the necessary local variables in each target. 1897 * 1898 * The commands for each target are set from the commands of the 1899 * transformation rule used to get from the src suffix to the targ 1900 * suffix. Note that this causes the commands list of the original 1901 * node, gn, to be replaced with the commands of the final 1902 * transformation rule. 1903 */ 1904 if (bottom->node == NULL) 1905 bottom->node = Targ_GetNode(bottom->file); 1906 1907 for (src = bottom; src->parent != NULL; src = src->parent) { 1908 targ = src->parent; 1909 1910 Suffix_Reassign(&src->node->suffix, src->suff); 1911 1912 if (targ->node == NULL) 1913 targ->node = Targ_GetNode(targ->file); 1914 1915 ApplyTransform(targ->node, src->node, targ->suff, src->suff); 1916 1917 if (targ->node != gn) { 1918 /* 1919 * Finish off the dependency-search process for any 1920 * nodes between bottom and gn (no point in questing 1921 * around the filesystem for their implicit source 1922 * when it's already known). Note that the node 1923 * can't have any sources that need expanding, since 1924 * SuffFindThem will stop on an existing node, so all 1925 * we need to do is set the standard variables. 1926 */ 1927 targ->node->type |= OP_DEPS_FOUND; 1928 Var_Set(targ->node, PREFIX, targ->prefix); 1929 Var_Set(targ->node, TARGET, targ->node->name); 1930 } 1931 } 1932 1933 Suffix_Reassign(&gn->suffix, src->suff); 1934 1935 /* 1936 * Nuke the transformation path and the candidates left over in the 1937 * two lists. 1938 */ 1939 sfnd_return: 1940 if (bottom != NULL) 1941 CandidateSearcher_AddIfNew(cs, bottom); 1942 1943 while (RemoveCandidate(&srcs) || RemoveCandidate(&targs)) 1944 continue; 1945 1946 CandidateSearcher_MoveAll(cs, &srcs); 1947 CandidateSearcher_MoveAll(cs, &targs); 1948 } 1949 1950 static void 1951 CandidateSearcher_CleanUp(CandidateSearcher *cs) 1952 { 1953 while (RemoveCandidate(&cs->list)) 1954 continue; 1955 assert(Lst_IsEmpty(&cs->list)); 1956 } 1957 1958 1959 /* 1960 * Find implicit sources for the target. 1961 * 1962 * Nodes are added to the graph as children of the passed-in node. The nodes 1963 * are marked to have their IMPSRC variable filled in. The PREFIX variable 1964 * is set for the given node and all its implied children. 1965 * 1966 * The path found by this target is the shortest path in the transformation 1967 * graph, which may pass through nonexistent targets, to an existing target. 1968 * The search continues on all paths from the root suffix until a file is 1969 * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the 1970 * .l,v file exists but the .c and .l files don't, the search will branch out 1971 * in all directions from .o and again from all the nodes on the next level 1972 * until the .l,v node is encountered. 1973 */ 1974 void 1975 Suff_FindDeps(GNode *gn) 1976 { 1977 CandidateSearcher cs; 1978 1979 CandidateSearcher_Init(&cs); 1980 1981 FindDeps(gn, &cs); 1982 1983 CandidateSearcher_CleanUp(&cs); 1984 CandidateSearcher_Done(&cs); 1985 } 1986 1987 static void 1988 FindDeps(GNode *gn, CandidateSearcher *cs) 1989 { 1990 if (gn->type & OP_DEPS_FOUND) 1991 return; 1992 gn->type |= OP_DEPS_FOUND; 1993 1994 /* Make sure we have these set, may get revised below. */ 1995 Var_Set(gn, TARGET, GNode_Path(gn)); 1996 Var_Set(gn, PREFIX, gn->name); 1997 1998 DEBUG1(SUFF, "SuffFindDeps \"%s\"\n", gn->name); 1999 2000 if (gn->type & OP_ARCHV) 2001 FindDepsArchive(gn, cs); 2002 else if (gn->type & OP_LIB) 2003 FindDepsLib(gn); 2004 else 2005 FindDepsRegular(gn, cs); 2006 } 2007 2008 /* 2009 * Define which suffix is the null suffix. 2010 * 2011 * Need to handle the changing of the null suffix gracefully so the old 2012 * transformation rules don't just go away. 2013 */ 2014 void 2015 Suff_SetNull(const char *name) 2016 { 2017 Suffix *suff = FindSuffixByName(name); 2018 if (suff == NULL) { 2019 Parse_Error(PARSE_WARNING, 2020 "Desired null suffix %s not defined", 2021 name); 2022 return; 2023 } 2024 2025 if (nullSuff != NULL) 2026 nullSuff->isNull = false; 2027 suff->isNull = true; 2028 /* XXX: Here's where the transformation mangling would take place. */ 2029 nullSuff = suff; 2030 } 2031 2032 /* Initialize the suffixes module. */ 2033 void 2034 Suff_Init(void) 2035 { 2036 /* 2037 * Create null suffix for single-suffix rules (POSIX). The thing 2038 * doesn't actually go on the suffix list or everyone will think 2039 * that's its suffix. 2040 */ 2041 Suff_ClearSuffixes(); 2042 } 2043 2044 #ifdef CLEANUP 2045 /* Clean up the suffixes module. */ 2046 void 2047 Suff_End(void) 2048 { 2049 SuffixListNode *ln; 2050 2051 for (ln = sufflist.first; ln != NULL; ln = ln->next) 2052 Suffix_Free(ln->datum); 2053 Lst_Done(&sufflist); 2054 for (ln = suffClean.first; ln != NULL; ln = ln->next) 2055 Suffix_Free(ln->datum); 2056 Lst_Done(&suffClean); 2057 if (nullSuff != NULL) 2058 Suffix_Free(nullSuff); 2059 Lst_Done(&transforms); 2060 } 2061 #endif 2062 2063 2064 static void 2065 PrintSuffNames(const char *prefix, const SuffixList *suffs) 2066 { 2067 SuffixListNode *ln; 2068 2069 debug_printf("#\t%s: ", prefix); 2070 for (ln = suffs->first; ln != NULL; ln = ln->next) { 2071 const Suffix *suff = ln->datum; 2072 debug_printf("%s ", suff->name); 2073 } 2074 debug_printf("\n"); 2075 } 2076 2077 static void 2078 Suffix_Print(const Suffix *suff) 2079 { 2080 Buffer buf; 2081 2082 Buf_Init(&buf); 2083 Buf_AddFlag(&buf, suff->include, "SUFF_INCLUDE"); 2084 Buf_AddFlag(&buf, suff->library, "SUFF_LIBRARY"); 2085 Buf_AddFlag(&buf, suff->isNull, "SUFF_NULL"); 2086 2087 debug_printf("# \"%s\" (num %d, ref %d)", 2088 suff->name, suff->sNum, suff->refCount); 2089 if (buf.len > 0) 2090 debug_printf(" (%s)", buf.data); 2091 debug_printf("\n"); 2092 2093 Buf_Done(&buf); 2094 2095 PrintSuffNames("To", &suff->parents); 2096 PrintSuffNames("From", &suff->children); 2097 2098 debug_printf("#\tSearch Path: "); 2099 SearchPath_Print(suff->searchPath); 2100 debug_printf("\n"); 2101 } 2102 2103 static void 2104 PrintTransformation(GNode *t) 2105 { 2106 debug_printf("%-16s:", t->name); 2107 Targ_PrintType(t->type); 2108 debug_printf("\n"); 2109 Targ_PrintCmds(t); 2110 debug_printf("\n"); 2111 } 2112 2113 void 2114 Suff_PrintAll(void) 2115 { 2116 debug_printf("#*** Suffixes:\n"); 2117 { 2118 SuffixListNode *ln; 2119 for (ln = sufflist.first; ln != NULL; ln = ln->next) 2120 Suffix_Print(ln->datum); 2121 } 2122 2123 debug_printf("#*** Transformations:\n"); 2124 { 2125 GNodeListNode *ln; 2126 for (ln = transforms.first; ln != NULL; ln = ln->next) 2127 PrintTransformation(ln->datum); 2128 } 2129 } 2130 2131 char * 2132 Suff_NamesStr(void) 2133 { 2134 Buffer buf; 2135 SuffixListNode *ln; 2136 Suffix *suff; 2137 2138 Buf_Init(&buf); 2139 for (ln = sufflist.first; ln != NULL; ln = ln->next) { 2140 suff = ln->datum; 2141 if (ln != sufflist.first) 2142 Buf_AddByte(&buf, ' '); 2143 Buf_AddStr(&buf, suff->name); 2144 } 2145 return Buf_DoneData(&buf); 2146 } 2147