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