1 /* Implicit rule searching for GNU Make. 2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software 4 Foundation, Inc. 5 This file is part of GNU Make. 6 7 GNU Make is free software; you can redistribute it and/or modify it under the 8 terms of the GNU General Public License as published by the Free Software 9 Foundation; either version 2, or (at your option) any later version. 10 11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY 12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 13 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along with 16 GNU Make; see the file COPYING. If not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */ 18 19 #include "make.h" 20 #include "filedef.h" 21 #include "rule.h" 22 #include "dep.h" 23 #include "debug.h" 24 #include "variable.h" 25 #include "job.h" /* struct child, used inside commands.h */ 26 #include "commands.h" /* set_file_variables */ 27 28 static int 29 pattern_search PARAMS ((struct file *file, int archive, 30 unsigned int depth, unsigned int recursions)); 31 32 /* For a FILE which has no commands specified, try to figure out some 34 from the implicit pattern rules. 35 Returns 1 if a suitable implicit rule was found, 36 after modifying FILE to contain the appropriate commands and deps, 37 or returns 0 if no implicit rule was found. */ 38 39 int 40 try_implicit_rule (struct file *file, unsigned int depth) 41 { 42 DBF (DB_IMPLICIT, _("Looking for an implicit rule for `%s'.\n")); 43 44 /* The order of these searches was previously reversed. My logic now is 45 that since the non-archive search uses more information in the target 46 (the archive search omits the archive name), it is more specific and 47 should come first. */ 48 49 if (pattern_search (file, 0, depth, 0)) 50 return 1; 51 52 #ifndef NO_ARCHIVES 53 /* If this is an archive member reference, use just the 54 archive member name to search for implicit rules. */ 55 if (ar_name (file->name)) 56 { 57 DBF (DB_IMPLICIT, 58 _("Looking for archive-member implicit rule for `%s'.\n")); 59 if (pattern_search (file, 1, depth, 0)) 60 return 1; 61 } 62 #endif 63 64 return 0; 65 } 66 67 69 /* Struct idep captures information about implicit prerequisites 70 that come from implicit rules. */ 71 struct idep 72 { 73 struct idep *next; /* struct dep -compatible interface */ 74 char *name; /* name of the prerequisite */ 75 struct file *intermediate_file; /* intermediate file, 0 otherwise */ 76 char *intermediate_pattern; /* pattern for intermediate file */ 77 unsigned char had_stem; /* had % substituted with stem */ 78 unsigned char ignore_mtime; /* ignore_mtime flag */ 79 }; 80 81 static void 82 free_idep_chain (struct idep *p) 83 { 84 struct idep *n; 85 86 for (; p != 0; p = n) 87 { 88 n = p->next; 89 90 if (p->name) 91 { 92 struct file *f = p->intermediate_file; 93 94 if (f != 0 95 && (f->stem < f->name || f->stem > f->name + strlen (f->name))) 96 free (f->stem); 97 98 free (p->name); 99 } 100 101 free (p); 102 } 103 } 104 105 106 /* Scans the BUFFER for the next word with whitespace as a separator. 107 Returns the pointer to the beginning of the word. LENGTH hold the 108 length of the word. */ 109 110 static char * 111 get_next_word (char *buffer, unsigned int *length) 112 { 113 char *p = buffer, *beg; 114 char c; 115 116 /* Skip any leading whitespace. */ 117 while (isblank ((unsigned char)*p)) 118 ++p; 119 120 beg = p; 121 c = *(p++); 122 123 if (c == '\0') 124 return 0; 125 126 127 /* We already found the first value of "c", above. */ 128 while (1) 129 { 130 char closeparen; 131 int count; 132 133 switch (c) 134 { 135 case '\0': 136 case ' ': 137 case '\t': 138 goto done_word; 139 140 case '$': 141 c = *(p++); 142 if (c == '$') 143 break; 144 145 /* This is a variable reference, so read it to the matching 146 close paren. */ 147 148 if (c == '(') 149 closeparen = ')'; 150 else if (c == '{') 151 closeparen = '}'; 152 else 153 /* This is a single-letter variable reference. */ 154 break; 155 156 for (count = 0; *p != '\0'; ++p) 157 { 158 if (*p == c) 159 ++count; 160 else if (*p == closeparen && --count < 0) 161 { 162 ++p; 163 break; 164 } 165 } 166 break; 167 168 case '|': 169 goto done; 170 171 default: 172 break; 173 } 174 175 c = *(p++); 176 } 177 done_word: 178 --p; 179 180 done: 181 if (length) 182 *length = p - beg; 183 184 return beg; 185 } 186 187 /* Search the pattern rules for a rule with an existing dependency to make 188 FILE. If a rule is found, the appropriate commands and deps are put in FILE 189 and 1 is returned. If not, 0 is returned. 190 191 If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)". A rule for 192 "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into 193 directory and filename parts. 194 195 If an intermediate file is found by pattern search, the intermediate file 196 is set up as a target by the recursive call and is also made a dependency 197 of FILE. 198 199 DEPTH is used for debugging messages. */ 200 201 static int 202 pattern_search (struct file *file, int archive, 203 unsigned int depth, unsigned int recursions) 204 { 205 /* Filename we are searching for a rule for. */ 206 char *filename = archive ? strchr (file->name, '(') : file->name; 207 208 /* Length of FILENAME. */ 209 unsigned int namelen = strlen (filename); 210 211 /* The last slash in FILENAME (or nil if there is none). */ 212 char *lastslash; 213 214 /* This is a file-object used as an argument in 215 recursive calls. It never contains any data 216 except during a recursive call. */ 217 struct file *intermediate_file = 0; 218 219 /* This linked list records all the prerequisites actually 220 found for a rule along with some other useful information 221 (see struct idep for details). */ 222 struct idep* deps = 0; 223 224 /* 1 if we need to remove explicit prerequisites, 0 otherwise. */ 225 unsigned int remove_explicit_deps = 0; 226 227 /* Names of possible dependencies are constructed in this buffer. */ 228 register char *depname = (char *) alloca (namelen + max_pattern_dep_length); 229 230 /* The start and length of the stem of FILENAME for the current rule. */ 231 register char *stem = 0; 232 register unsigned int stemlen = 0; 233 register unsigned int fullstemlen = 0; 234 235 /* Buffer in which we store all the rules that are possibly applicable. */ 236 struct rule **tryrules 237 = (struct rule **) xmalloc (num_pattern_rules * max_pattern_targets 238 * sizeof (struct rule *)); 239 240 /* Number of valid elements in TRYRULES. */ 241 unsigned int nrules; 242 243 /* The numbers of the rule targets of each rule 244 in TRYRULES that matched the target file. */ 245 unsigned int *matches 246 = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int)); 247 248 /* Each element is nonzero if LASTSLASH was used in 249 matching the corresponding element of TRYRULES. */ 250 char *checked_lastslash 251 = (char *) alloca (num_pattern_rules * sizeof (char)); 252 253 /* The index in TRYRULES of the rule we found. */ 254 unsigned int foundrule; 255 256 /* Nonzero if should consider intermediate files as dependencies. */ 257 int intermed_ok; 258 259 /* Nonzero if we have matched a pattern-rule target 260 that is not just `%'. */ 261 int specific_rule_matched = 0; 262 263 unsigned int i = 0; /* uninit checks OK */ 264 struct rule *rule; 265 struct dep *dep, *expl_d; 266 267 char *p, *vname; 268 269 struct idep *d; 270 struct idep **id_ptr; 271 struct dep **d_ptr; 272 273 PATH_VAR (stem_str); /* @@ Need to get rid of stem, stemlen, etc. */ 274 275 #ifndef NO_ARCHIVES 276 if (archive || ar_name (filename)) 277 lastslash = 0; 278 else 279 #endif 280 { 281 /* Set LASTSLASH to point at the last slash in FILENAME 282 but not counting any slash at the end. (foo/bar/ counts as 283 bar/ in directory foo/, not empty in directory foo/bar/.) */ 284 #ifdef VMS 285 lastslash = strrchr (filename, ']'); 286 if (lastslash == 0) 287 lastslash = strrchr (filename, ':'); 288 #else 289 lastslash = strrchr (filename, '/'); 290 #ifdef HAVE_DOS_PATHS 291 /* Handle backslashes (possibly mixed with forward slashes) 292 and the case of "d:file". */ 293 { 294 char *bslash = strrchr (filename, '\\'); 295 if (lastslash == 0 || bslash > lastslash) 296 lastslash = bslash; 297 if (lastslash == 0 && filename[0] && filename[1] == ':') 298 lastslash = filename + 1; 299 } 300 #endif 301 #endif 302 if (lastslash != 0 && lastslash[1] == '\0') 303 lastslash = 0; 304 } 305 306 /* First see which pattern rules match this target 307 and may be considered. Put them in TRYRULES. */ 308 309 nrules = 0; 310 for (rule = pattern_rules; rule != 0; rule = rule->next) 311 { 312 /* If the pattern rule has deps but no commands, ignore it. 313 Users cancel built-in rules by redefining them without commands. */ 314 if (rule->deps != 0 && rule->cmds == 0) 315 continue; 316 317 /* If this rule is in use by a parent pattern_search, 318 don't use it here. */ 319 if (rule->in_use) 320 { 321 DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n"))); 322 continue; 323 } 324 325 for (i = 0; rule->targets[i] != 0; ++i) 326 { 327 char *target = rule->targets[i]; 328 char *suffix = rule->suffixes[i]; 329 int check_lastslash; 330 331 /* Rules that can match any filename and are not terminal 332 are ignored if we're recursing, so that they cannot be 333 intermediate files. */ 334 if (recursions > 0 && target[1] == '\0' && !rule->terminal) 335 continue; 336 337 if (rule->lens[i] > namelen) 338 /* It can't possibly match. */ 339 continue; 340 341 /* From the lengths of the filename and the pattern parts, 342 find the stem: the part of the filename that matches the %. */ 343 stem = filename + (suffix - target - 1); 344 stemlen = namelen - rule->lens[i] + 1; 345 346 /* Set CHECK_LASTSLASH if FILENAME contains a directory 347 prefix and the target pattern does not contain a slash. */ 348 349 check_lastslash = 0; 350 if (lastslash) 351 { 352 #ifdef VMS 353 check_lastslash = (strchr (target, ']') == 0 354 && strchr (target, ':') == 0); 355 #else 356 check_lastslash = strchr (target, '/') == 0; 357 #ifdef HAVE_DOS_PATHS 358 /* Didn't find it yet: check for DOS-type directories. */ 359 if (check_lastslash) 360 { 361 char *b = strchr (target, '\\'); 362 check_lastslash = !(b || (target[0] && target[1] == ':')); 363 } 364 #endif 365 #endif 366 } 367 if (check_lastslash) 368 { 369 /* If so, don't include the directory prefix in STEM here. */ 370 unsigned int difference = lastslash - filename + 1; 371 if (difference > stemlen) 372 continue; 373 stemlen -= difference; 374 stem += difference; 375 } 376 377 /* Check that the rule pattern matches the text before the stem. */ 378 if (check_lastslash) 379 { 380 if (stem > (lastslash + 1) 381 && !strneq (target, lastslash + 1, stem - lastslash - 1)) 382 continue; 383 } 384 else if (stem > filename 385 && !strneq (target, filename, stem - filename)) 386 continue; 387 388 /* Check that the rule pattern matches the text after the stem. 389 We could test simply use streq, but this way we compare the 390 first two characters immediately. This saves time in the very 391 common case where the first character matches because it is a 392 period. */ 393 if (*suffix != stem[stemlen] 394 || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1]))) 395 continue; 396 397 /* Record if we match a rule that not all filenames will match. */ 398 if (target[1] != '\0') 399 specific_rule_matched = 1; 400 401 /* A rule with no dependencies and no commands exists solely to set 402 specific_rule_matched when it matches. Don't try to use it. */ 403 if (rule->deps == 0 && rule->cmds == 0) 404 continue; 405 406 /* Record this rule in TRYRULES and the index of the matching 407 target in MATCHES. If several targets of the same rule match, 408 that rule will be in TRYRULES more than once. */ 409 tryrules[nrules] = rule; 410 matches[nrules] = i; 411 checked_lastslash[nrules] = check_lastslash; 412 ++nrules; 413 } 414 } 415 416 /* If we have found a matching rule that won't match all filenames, 417 retroactively reject any non-"terminal" rules that do always match. */ 418 if (specific_rule_matched) 419 for (i = 0; i < nrules; ++i) 420 if (!tryrules[i]->terminal) 421 { 422 register unsigned int j; 423 for (j = 0; tryrules[i]->targets[j] != 0; ++j) 424 if (tryrules[i]->targets[j][1] == '\0') 425 break; 426 if (tryrules[i]->targets[j] != 0) 427 tryrules[i] = 0; 428 } 429 430 /* We are going to do second expansion so initialize file variables 431 for the rule. */ 432 initialize_file_variables (file, 0); 433 434 /* Try each rule once without intermediate files, then once with them. */ 435 for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok) 436 { 437 /* Try each pattern rule till we find one that applies. 438 If it does, expand its dependencies (as substituted) 439 and chain them in DEPS. */ 440 441 for (i = 0; i < nrules; i++) 442 { 443 struct file *f; 444 unsigned int failed = 0; 445 int check_lastslash; 446 int file_variables_set = 0; 447 448 rule = tryrules[i]; 449 450 remove_explicit_deps = 0; 451 452 /* RULE is nil when we discover that a rule, 453 already placed in TRYRULES, should not be applied. */ 454 if (rule == 0) 455 continue; 456 457 /* Reject any terminal rules if we're 458 looking to make intermediate files. */ 459 if (intermed_ok && rule->terminal) 460 continue; 461 462 /* Mark this rule as in use so a recursive 463 pattern_search won't try to use it. */ 464 rule->in_use = 1; 465 466 /* From the lengths of the filename and the matching pattern parts, 467 find the stem: the part of the filename that matches the %. */ 468 stem = filename 469 + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1; 470 stemlen = namelen - rule->lens[matches[i]] + 1; 471 check_lastslash = checked_lastslash[i]; 472 if (check_lastslash) 473 { 474 stem += lastslash - filename + 1; 475 stemlen -= (lastslash - filename) + 1; 476 } 477 478 DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"), 479 (int) stemlen, stem)); 480 481 strncpy (stem_str, stem, stemlen); 482 stem_str[stemlen] = '\0'; 483 484 /* Temporary assign STEM to file->stem (needed to set file 485 variables below). */ 486 file->stem = stem_str; 487 488 /* Try each dependency; see if it "exists". */ 489 490 for (dep = rule->deps; dep != 0; dep = dep->next) 491 { 492 unsigned int len; 493 char *p2; 494 unsigned int order_only = 0; /* Set if '|' was seen. */ 495 496 /* In an ideal world we would take the dependency line, 497 substitute the stem, re-expand the whole line and chop it 498 into individual prerequisites. Unfortunately this won't work 499 because of the "check_lastslash" twist. Instead, we will 500 have to go word by word, taking $()'s into account, for each 501 word we will substitute the stem, re-expand, chop it up, and, 502 if check_lastslash != 0, add the directory part to each 503 resulting prerequisite. */ 504 505 p = get_next_word (dep->name, &len); 506 507 while (1) 508 { 509 int add_dir = 0; 510 int had_stem = 0; 511 512 if (p == 0) 513 break; /* No more words */ 514 515 /* Is there a pattern in this prerequisite? */ 516 517 for (p2 = p; p2 < p + len && *p2 != '%'; ++p2) 518 ; 519 520 if (dep->need_2nd_expansion) 521 { 522 /* If the dependency name has %, substitute the stem. 523 524 Watch out, we are going to do something tricky 525 here. If we just replace % with the stem value, 526 later, when we do the second expansion, we will 527 re-expand this stem value once again. This is not 528 good especially if you have certain characters in 529 your stem (like $). 530 531 Instead, we will replace % with $* and allow the 532 second expansion to take care of it for us. This way 533 (since $* is a simple variable) there won't be 534 additional re-expansion of the stem. */ 535 536 if (p2 < p + len) 537 { 538 register unsigned int i = p2 - p; 539 bcopy (p, depname, i); 540 bcopy ("$*", depname + i, 2); 541 bcopy (p2 + 1, depname + i + 2, len - i - 1); 542 depname[len + 2 - 1] = '\0'; 543 544 if (check_lastslash) 545 add_dir = 1; 546 547 had_stem = 1; 548 } 549 else 550 { 551 bcopy (p, depname, len); 552 depname[len] = '\0'; 553 } 554 555 /* Set file variables. Note that we cannot do it once 556 at the beginning of the function because of the stem 557 value. */ 558 if (!file_variables_set) 559 { 560 set_file_variables (file); 561 file_variables_set = 1; 562 } 563 564 p2 = variable_expand_for_file (depname, file); 565 } 566 else 567 { 568 if (p2 < p + len) 569 { 570 register unsigned int i = p2 - p; 571 bcopy (p, depname, i); 572 bcopy (stem_str, depname + i, stemlen); 573 bcopy (p2 + 1, depname + i + stemlen, len - i - 1); 574 depname[len + stemlen - 1] = '\0'; 575 576 if (check_lastslash) 577 add_dir = 1; 578 579 had_stem = 1; 580 } 581 else 582 { 583 bcopy (p, depname, len); 584 depname[len] = '\0'; 585 } 586 587 p2 = depname; 588 } 589 590 /* Parse the dependencies. */ 591 592 while (1) 593 { 594 id_ptr = &deps; 595 596 for (; *id_ptr; id_ptr = &(*id_ptr)->next) 597 ; 598 599 *id_ptr = (struct idep *) 600 multi_glob ( 601 parse_file_seq (&p2, 602 order_only ? '\0' : '|', 603 sizeof (struct idep), 604 1), sizeof (struct idep)); 605 606 /* @@ It would be nice to teach parse_file_seq or 607 multi_glob to add prefix. This would save us some 608 reallocations. */ 609 610 if (order_only || add_dir || had_stem) 611 { 612 unsigned long l = lastslash - filename + 1; 613 614 for (d = *id_ptr; d != 0; d = d->next) 615 { 616 if (order_only) 617 d->ignore_mtime = 1; 618 619 if (add_dir) 620 { 621 char *p = d->name; 622 623 d->name = xmalloc (strlen (p) + l + 1); 624 625 bcopy (filename, d->name, l); 626 bcopy (p, d->name + l, strlen (p) + 1); 627 628 free (p); 629 } 630 631 if (had_stem) 632 d->had_stem = 1; 633 } 634 } 635 636 if (!order_only && *p2) 637 { 638 ++p2; 639 order_only = 1; 640 continue; 641 } 642 643 break; 644 } 645 646 p += len; 647 p = get_next_word (p, &len); 648 } 649 } 650 651 /* Reset the stem in FILE. */ 652 653 file->stem = 0; 654 655 /* @@ This loop can be combined with the previous one. I do 656 it separately for now for transparency.*/ 657 658 for (d = deps; d != 0; d = d->next) 659 { 660 char *name = d->name; 661 662 if (file_impossible_p (name)) 663 { 664 /* If this dependency has already been ruled "impossible", 665 then the rule fails and don't bother trying it on the 666 second pass either since we know that will fail too. */ 667 DBS (DB_IMPLICIT, 668 (d->had_stem 669 ? _("Rejecting impossible implicit prerequisite `%s'.\n") 670 : _("Rejecting impossible rule prerequisite `%s'.\n"), 671 name)); 672 tryrules[i] = 0; 673 674 failed = 1; 675 break; 676 } 677 678 DBS (DB_IMPLICIT, 679 (d->had_stem 680 ? _("Trying implicit prerequisite `%s'.\n") 681 : _("Trying rule prerequisite `%s'.\n"), name)); 682 683 /* If this prerequisite also happened to be explicitly mentioned 684 for FILE skip all the test below since it it has to be built 685 anyway, no matter which implicit rule we choose. */ 686 687 for (expl_d = file->deps; expl_d != 0; expl_d = expl_d->next) 688 if (streq (dep_name (expl_d), name)) 689 break; 690 if (expl_d != 0) 691 continue; 692 693 /* The DEP->changed flag says that this dependency resides in a 694 nonexistent directory. So we normally can skip looking for 695 the file. However, if CHECK_LASTSLASH is set, then the 696 dependency file we are actually looking for is in a different 697 directory (the one gotten by prepending FILENAME's directory), 698 so it might actually exist. */ 699 700 /* @@ dep->changed check is disabled. */ 701 if (((f = lookup_file (name)) != 0 && f->is_target) 702 /*|| ((!dep->changed || check_lastslash) && */ 703 || file_exists_p (name)) 704 continue; 705 706 /* This code, given FILENAME = "lib/foo.o", dependency name 707 "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c". */ 708 vname = name; 709 if (vpath_search (&vname, (FILE_TIMESTAMP *) 0)) 710 { 711 DBS (DB_IMPLICIT, 712 (_("Found prerequisite `%s' as VPATH `%s'\n"), 713 name, 714 vname)); 715 716 free (vname); 717 continue; 718 } 719 720 721 /* We could not find the file in any place we should look. Try 722 to make this dependency as an intermediate file, but only on 723 the second pass. */ 724 725 if (intermed_ok) 726 { 727 if (intermediate_file == 0) 728 intermediate_file 729 = (struct file *) alloca (sizeof (struct file)); 730 731 DBS (DB_IMPLICIT, 732 (_("Looking for a rule with intermediate file `%s'.\n"), 733 name)); 734 735 bzero ((char *) intermediate_file, sizeof (struct file)); 736 intermediate_file->name = name; 737 if (pattern_search (intermediate_file, 738 0, 739 depth + 1, 740 recursions + 1)) 741 { 742 d->intermediate_file = intermediate_file; 743 d->intermediate_pattern = intermediate_file->name; 744 745 intermediate_file->name = xstrdup (name); 746 intermediate_file = 0; 747 748 continue; 749 } 750 751 /* If we have tried to find P as an intermediate 752 file and failed, mark that name as impossible 753 so we won't go through the search again later. */ 754 if (intermediate_file->variables) 755 free_variable_set (intermediate_file->variables); 756 file_impossible (name); 757 } 758 759 /* A dependency of this rule does not exist. Therefore, 760 this rule fails. */ 761 failed = 1; 762 break; 763 } 764 765 /* This rule is no longer `in use' for recursive searches. */ 766 rule->in_use = 0; 767 768 if (failed) 769 { 770 /* This pattern rule does not apply. If some of its 771 dependencies succeeded, free the data structure 772 describing them. */ 773 free_idep_chain (deps); 774 deps = 0; 775 } 776 else 777 /* This pattern rule does apply. Stop looking for one. */ 778 break; 779 } 780 781 /* If we found an applicable rule without 782 intermediate files, don't try with them. */ 783 if (i < nrules) 784 break; 785 786 rule = 0; 787 } 788 789 /* RULE is nil if the loop went all the way 790 through the list and everything failed. */ 791 if (rule == 0) 792 goto done; 793 794 foundrule = i; 795 796 /* If we are recursing, store the pattern that matched 797 FILENAME in FILE->name for use in upper levels. */ 798 799 if (recursions > 0) 800 /* Kludge-o-matic */ 801 file->name = rule->targets[matches[foundrule]]; 802 803 /* FOUND_FILES lists the dependencies for the rule we found. 804 This includes the intermediate files, if any. 805 Convert them into entries on the deps-chain of FILE. */ 806 807 if (remove_explicit_deps) 808 { 809 /* Remove all the dependencies that didn't come from 810 this implicit rule. */ 811 812 dep = file->deps; 813 while (dep != 0) 814 { 815 struct dep *next = dep->next; 816 free_dep (dep); 817 dep = next; 818 } 819 file->deps = 0; 820 } 821 822 expl_d = file->deps; /* We will add them at the end. */ 823 d_ptr = &file->deps; 824 825 for (d = deps; d != 0; d = d->next) 826 { 827 register char *s; 828 829 if (d->intermediate_file != 0) 830 { 831 /* If we need to use an intermediate file, 832 make sure it is entered as a target, with the info that was 833 found for it in the recursive pattern_search call. 834 We know that the intermediate file did not already exist as 835 a target; therefore we can assume that the deps and cmds 836 of F below are null before we change them. */ 837 838 struct file *imf = d->intermediate_file; 839 register struct file *f = lookup_file (imf->name); 840 841 /* We don't want to delete an intermediate file that happened 842 to be a prerequisite of some (other) target. Mark it as 843 precious. */ 844 if (f != 0) 845 f->precious = 1; 846 else 847 f = enter_file (imf->name); 848 849 f->deps = imf->deps; 850 f->cmds = imf->cmds; 851 f->stem = imf->stem; 852 f->also_make = imf->also_make; 853 f->is_target = 1; 854 855 if (!f->precious) 856 { 857 imf = lookup_file (d->intermediate_pattern); 858 if (imf != 0 && imf->precious) 859 f->precious = 1; 860 } 861 862 f->intermediate = 1; 863 f->tried_implicit = 1; 864 for (dep = f->deps; dep != 0; dep = dep->next) 865 { 866 dep->file = enter_file (dep->name); 867 /* enter_file uses dep->name _if_ we created a new file. */ 868 if (dep->name != dep->file->name) 869 free (dep->name); 870 dep->name = 0; 871 dep->file->tried_implicit |= dep->changed; 872 } 873 } 874 875 dep = alloc_dep (); 876 dep->ignore_mtime = d->ignore_mtime; 877 s = d->name; /* Hijacking the name. */ 878 d->name = 0; 879 if (recursions == 0) 880 { 881 dep->file = lookup_file (s); 882 if (dep->file == 0) 883 /* enter_file consumes S's storage. */ 884 dep->file = enter_file (s); 885 else 886 /* A copy of S is already allocated in DEP->file->name. 887 So we can free S. */ 888 free (s); 889 } 890 else 891 { 892 dep->name = s; 893 } 894 895 if (d->intermediate_file == 0 && tryrules[foundrule]->terminal) 896 { 897 /* If the file actually existed (was not an intermediate file), 898 and the rule that found it was a terminal one, then we want 899 to mark the found file so that it will not have implicit rule 900 search done for it. If we are not entering a `struct file' for 901 it now, we indicate this with the `changed' flag. */ 902 if (dep->file == 0) 903 dep->changed = 1; 904 else 905 dep->file->tried_implicit = 1; 906 } 907 908 *d_ptr = dep; 909 d_ptr = &dep->next; 910 } 911 912 *d_ptr = expl_d; 913 914 if (!checked_lastslash[foundrule]) 915 { 916 /* Always allocate new storage, since STEM might be 917 on the stack for an intermediate file. */ 918 file->stem = savestring (stem, stemlen); 919 fullstemlen = stemlen; 920 } 921 else 922 { 923 int dirlen = (lastslash + 1) - filename; 924 925 /* We want to prepend the directory from 926 the original FILENAME onto the stem. */ 927 fullstemlen = dirlen + stemlen; 928 file->stem = (char *) xmalloc (fullstemlen + 1); 929 bcopy (filename, file->stem, dirlen); 930 bcopy (stem, file->stem + dirlen, stemlen); 931 file->stem[fullstemlen] = '\0'; 932 } 933 934 file->cmds = rule->cmds; 935 file->is_target = 1; 936 937 /* Set precious flag. */ 938 { 939 struct file *f = lookup_file (rule->targets[matches[foundrule]]); 940 if (f && f->precious) 941 file->precious = 1; 942 } 943 944 /* If this rule builds other targets, too, put the others into FILE's 945 `also_make' member. */ 946 947 if (rule->targets[1] != 0) 948 for (i = 0; rule->targets[i] != 0; ++i) 949 if (i != matches[foundrule]) 950 { 951 struct file *f; 952 struct dep *new = alloc_dep (); 953 954 /* GKM FIMXE: handle '|' here too */ 955 new->name = p = (char *) xmalloc (rule->lens[i] + fullstemlen + 1); 956 bcopy (rule->targets[i], p, 957 rule->suffixes[i] - rule->targets[i] - 1); 958 p += rule->suffixes[i] - rule->targets[i] - 1; 959 bcopy (file->stem, p, fullstemlen); 960 p += fullstemlen; 961 bcopy (rule->suffixes[i], p, 962 rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1); 963 new->file = enter_file (new->name); 964 new->next = file->also_make; 965 966 /* Set precious flag. */ 967 f = lookup_file (rule->targets[i]); 968 if (f && f->precious) 969 new->file->precious = 1; 970 971 /* Set the is_target flag so that this file is not treated 972 as intermediate by the pattern rule search algorithm and 973 file_exists_p cannot pick it up yet. */ 974 new->file->is_target = 1; 975 976 file->also_make = new; 977 } 978 979 done: 980 free_idep_chain (deps); 981 free (tryrules); 982 983 return rule != 0; 984 } 985