VirtualBox

source: kBuild/trunk/src/kmk/implicit.c@ 1878

Last change on this file since 1878 was 1864, checked in by bird, 16 years ago

kmk: use alloc caches for variables, variable sets and varaible set lists.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette