VirtualBox

source: kBuild/trunk/src/kmk/file.c@ 2074

Last change on this file since 2074 was 2024, checked in by bird, 16 years ago

kmk: Created a custom hook into the update_file process for checking additional file dependencies like changes in the commands and such. It's call .MUST_MAKE.

  • Property svn:eol-style set to native
File size: 39.1 KB
Line 
1/* Target file management for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include "dep.h"
24#include "filedef.h"
25#include "job.h"
26#include "commands.h"
27#include "variable.h"
28#include "debug.h"
29#include "hash.h"
30#ifdef CONFIG_WITH_STRCACHE2
31# include <stddef.h>
32#endif
33
34
35/* Remember whether snap_deps has been invoked: we need this to be sure we
36 don't add new rules (via $(eval ...)) afterwards. In the future it would
37 be nice to support this, but it means we'd need to re-run snap_deps() or
38 at least its functionality... it might mean changing snap_deps() to be run
39 per-file, so we can invoke it after the eval... or remembering which files
40 in the hash have been snapped (a new boolean flag?) and having snap_deps()
41 only work on files which have not yet been snapped. */
42int snapped_deps = 0;
43
44/* Hash table of files the makefile knows how to make. */
45
46#ifndef CONFIG_WITH_STRCACHE2
47static unsigned long
48file_hash_1 (const void *key)
49{
50 return_ISTRING_HASH_1 (((struct file const *) key)->hname);
51}
52
53static unsigned long
54file_hash_2 (const void *key)
55{
56 return_ISTRING_HASH_2 (((struct file const *) key)->hname);
57}
58#endif /* !CONFIG_WITH_STRCACHE2 */
59
60static int
61file_hash_cmp (const void *x, const void *y)
62{
63#ifndef CONFIG_WITH_STRCACHE2
64 return_ISTRING_COMPARE (((struct file const *) x)->hname,
65 ((struct file const *) y)->hname);
66#else /* CONFIG_WITH_STRCACHE2 */
67 return ((struct file const *) x)->hname
68 == ((struct file const *) y)->hname ? 0 : -1;
69#endif /* CONFIG_WITH_STRCACHE2 */
70}
71
72#ifndef FILE_BUCKETS
73#define FILE_BUCKETS 1007
74#endif
75static struct hash_table files;
76
77/* Whether or not .SECONDARY with no prerequisites was given. */
78static int all_secondary = 0;
79
80/* Access the hash table of all file records.
81 lookup_file given a name, return the struct file * for that name,
82 or nil if there is none.
83*/
84
85#ifndef CONFIG_WITH_STRCACHE2
86struct file *
87lookup_file (const char *name)
88#else /* CONFIG_WITH_STRCACHE2 */
89MY_INLINE struct file *
90lookup_file_common (const char *name, int cached)
91#endif /* CONFIG_WITH_STRCACHE2 */
92{
93 struct file *f;
94 struct file file_key;
95#if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
96 char *lname;
97#endif
98
99 assert (*name != '\0');
100
101 /* This is also done in parse_file_seq, so this is redundant
102 for names read from makefiles. It is here for names passed
103 on the command line. */
104#ifdef VMS
105# ifndef WANT_CASE_SENSITIVE_TARGETS
106 if (*name != '.')
107 {
108 const char *n;
109 char *ln;
110 lname = xstrdup (name);
111 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
112 *ln = isupper ((unsigned char)*n) ? tolower ((unsigned char)*n) : *n;
113 *ln = '\0';
114 name = lname;
115 }
116# endif
117
118 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
119 name += 2;
120#endif
121 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
122 {
123 name += 2;
124 while (*name == '/')
125 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
126 ++name;
127 }
128
129 if (*name == '\0')
130 /* It was all slashes after a dot. */
131#if defined(VMS)
132 name = "[]";
133#elif defined(_AMIGA)
134 name = "";
135#else
136 name = "./";
137#endif
138
139#ifndef CONFIG_WITH_STRCACHE2
140 file_key.hname = name;
141 f = hash_find_item (&files, &file_key);
142#else /* CONFIG_WITH_STRCACHE2 */
143 if (!cached)
144 {
145 file_key.hname = strcache2_lookup_file (&file_strcache, name, strlen (name));
146 if (file_key.hname)
147 f = hash_find_item_strcached (&files, &file_key);
148 else
149 f = NULL;
150 }
151 else
152 {
153 file_key.hname = name;
154 f = hash_find_item_strcached (&files, &file_key);
155 }
156
157#endif /* CONFIG_WITH_STRCACHE2 */
158#if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
159 if (*name != '.')
160 free (lname);
161#endif
162
163 return f;
164}
165
166#ifdef CONFIG_WITH_STRCACHE2
167/* Given a name, return the struct file * for that name,
168 or nil if there is none. */
169
170struct file *
171lookup_file (const char *name)
172{
173 return lookup_file_common (name, 0 /* cached */);
174}
175
176/* Given a name in the strcache, return the struct file * for that name,
177 or nil if there is none. */
178struct file *
179lookup_file_cached (const char *name)
180{
181 assert (strcache_iscached (name));
182 return lookup_file_common (name, 1 /* cached */);
183}
184#endif /* CONFIG_WITH_STRCACHE2 */
185
186
187/* Look up a file record for file NAME and return it.
188 Create a new record if one doesn't exist. NAME will be stored in the
189 new record so it should be constant or in the strcache etc.
190 */
191
192struct file *
193enter_file (const char *name)
194{
195 struct file *f;
196 struct file *new;
197 struct file **file_slot;
198 struct file file_key;
199
200 assert (*name != '\0');
201 assert (strcache_iscached (name));
202
203#if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
204 if (*name != '.')
205 {
206 const char *n;
207 char *lname, *ln;
208 lname = xstrdup (name);
209 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
210 if (isupper ((unsigned char)*n))
211 *ln = tolower ((unsigned char)*n);
212 else
213 *ln = *n;
214
215 *ln = '\0';
216 name = strcache_add (lname);
217 free (lname);
218 }
219#endif
220
221 file_key.hname = name;
222#ifndef CONFIG_WITH_STRCACHE2
223 file_slot = (struct file **) hash_find_slot (&files, &file_key);
224#else /* CONFIG_WITH_STRCACHE2 */
225 file_slot = (struct file **) hash_find_slot_strcached (&files, &file_key);
226#endif /* CONFIG_WITH_STRCACHE2 */
227 f = *file_slot;
228 if (! HASH_VACANT (f) && !f->double_colon)
229 return f;
230
231#ifndef CONFIG_WITH_ALLOC_CACHES
232 new = xmalloc (sizeof (struct file));
233 memset (new, '\0', sizeof (struct file));
234#else
235 new = alloccache_calloc (&file_cache);
236#endif
237 new->name = new->hname = name;
238 new->update_status = -1;
239
240 if (HASH_VACANT (f))
241 {
242 new->last = new;
243 hash_insert_at (&files, new, file_slot);
244 }
245 else
246 {
247 /* There is already a double-colon entry for this file. */
248 new->double_colon = f;
249 f->last->prev = new;
250 f->last = new;
251 }
252
253#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
254 /* Check if the name needs 2nd expansion or not. */
255 if (second_target_expansion && strchr (name, '$') != NULL)
256 new->need_2nd_target_expansion = 1;
257#endif
258
259 return new;
260}
261
262
263/* Rehash FILE to NAME. This is not as simple as resetting
264 the `hname' member, since it must be put in a new hash bucket,
265 and possibly merged with an existing file called NAME. */
266
267void
268rehash_file (struct file *from_file, const char *to_hname)
269{
270 struct file file_key;
271 struct file **file_slot;
272 struct file *to_file;
273 struct file *deleted_file;
274 struct file *f;
275
276#ifdef CONFIG_WITH_STRCACHE2
277 assert (strcache_iscached (to_hname));
278 assert (strcache_iscached (from_file->hname));
279#endif
280
281 /* If it's already that name, we're done. */
282 file_key.hname = to_hname;
283 if (! file_hash_cmp (from_file, &file_key))
284 return;
285
286 /* Find the end of the renamed list for the "from" file. */
287 file_key.hname = from_file->hname;
288 while (from_file->renamed != 0)
289 from_file = from_file->renamed;
290 if (file_hash_cmp (from_file, &file_key))
291 /* hname changed unexpectedly!! */
292 abort ();
293
294 /* Remove the "from" file from the hash. */
295#ifndef CONFIG_WITH_STRCACHE2
296 deleted_file = hash_delete (&files, from_file);
297#else
298 deleted_file = hash_delete_strcached (&files, from_file);
299#endif
300 if (deleted_file != from_file)
301 /* from_file isn't the one stored in files */
302 abort ();
303
304 /* Find where the newly renamed file will go in the hash. */
305 file_key.hname = to_hname;
306#ifndef CONFIG_WITH_STRCACHE2
307 file_slot = (struct file **) hash_find_slot (&files, &file_key);
308#else /* CONFIG_WITH_STRCACHE2 */
309 file_slot = (struct file **) hash_find_slot_strcached (&files, &file_key);
310#endif /* CONFIG_WITH_STRCACHE2 */
311 to_file = *file_slot;
312
313 /* Change the hash name for this file. */
314 from_file->hname = to_hname;
315 for (f = from_file->double_colon; f != 0; f = f->prev)
316 f->hname = to_hname;
317
318 /* If the new name doesn't exist yet just set it to the renamed file. */
319 if (HASH_VACANT (to_file))
320 {
321 hash_insert_at (&files, from_file, file_slot);
322 return;
323 }
324
325 /* TO_FILE already exists under TO_HNAME.
326 We must retain TO_FILE and merge FROM_FILE into it. */
327
328 if (from_file->cmds != 0)
329 {
330 if (to_file->cmds == 0)
331 to_file->cmds = from_file->cmds;
332 else if (from_file->cmds != to_file->cmds)
333 {
334 /* We have two sets of commands. We will go with the
335 one given in the rule explicitly mentioning this name,
336 but give a message to let the user know what's going on. */
337 if (to_file->cmds->fileinfo.filenm != 0)
338 error (&from_file->cmds->fileinfo,
339 _("Recipe was specified for file `%s' at %s:%lu,"),
340 from_file->name, to_file->cmds->fileinfo.filenm,
341 to_file->cmds->fileinfo.lineno);
342 else
343 error (&from_file->cmds->fileinfo,
344 _("Recipe for file `%s' was found by implicit rule search,"),
345 from_file->name);
346 error (&from_file->cmds->fileinfo,
347 _("but `%s' is now considered the same file as `%s'."),
348 from_file->name, to_hname);
349 error (&from_file->cmds->fileinfo,
350 _("Recipe for `%s' will be ignored in favor of the one for `%s'."),
351 to_hname, from_file->name);
352 }
353 }
354
355 /* Merge the dependencies of the two files. */
356
357 if (to_file->deps == 0)
358 to_file->deps = from_file->deps;
359 else
360 {
361 struct dep *deps = to_file->deps;
362 while (deps->next != 0)
363 deps = deps->next;
364 deps->next = from_file->deps;
365 }
366
367 merge_variable_set_lists (&to_file->variables, from_file->variables);
368
369 if (to_file->double_colon && from_file->is_target && !from_file->double_colon)
370 fatal (NILF, _("can't rename single-colon `%s' to double-colon `%s'"),
371 from_file->name, to_hname);
372 if (!to_file->double_colon && from_file->double_colon)
373 {
374 if (to_file->is_target)
375 fatal (NILF, _("can't rename double-colon `%s' to single-colon `%s'"),
376 from_file->name, to_hname);
377 else
378 to_file->double_colon = from_file->double_colon;
379 }
380
381 if (from_file->last_mtime > to_file->last_mtime)
382 /* %%% Kludge so -W wins on a file that gets vpathized. */
383 to_file->last_mtime = from_file->last_mtime;
384
385 to_file->mtime_before_update = from_file->mtime_before_update;
386
387#define MERGE(field) to_file->field |= from_file->field
388 MERGE (precious);
389 MERGE (tried_implicit);
390 MERGE (updating);
391 MERGE (updated);
392 MERGE (is_target);
393 MERGE (cmd_target);
394 MERGE (phony);
395 MERGE (ignore_vpath);
396#undef MERGE
397
398 from_file->renamed = to_file;
399}
400
401/* Rename FILE to NAME. This is not as simple as resetting
402 the `name' member, since it must be put in a new hash bucket,
403 and possibly merged with an existing file called NAME. */
404
405void
406rename_file (struct file *from_file, const char *to_hname)
407{
408 rehash_file (from_file, to_hname);
409 while (from_file)
410 {
411 from_file->name = from_file->hname;
412 from_file = from_file->prev;
413 }
414}
415
416
417#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
418/* Performs secondary target name expansion and then renames
419 the file using rename_file. */
420static void
421do_2nd_target_expansion (struct file *f)
422{
423 unsigned int len;
424 char *tmp_name = allocated_variable_expand_2 (
425 f->name, strcache2_get_len (&file_strcache, f->name), &len);
426 const char *name = strcache_add_len (tmp_name, len);
427 free (tmp_name);
428 rename_file (f, name);
429}
430#endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
431
432
433/* Remove all nonprecious intermediate files.
434 If SIG is nonzero, this was caused by a fatal signal,
435 meaning that a different message will be printed, and
436 the message will go to stderr rather than stdout. */
437
438void
439remove_intermediates (int sig)
440{
441 struct file **file_slot;
442 struct file **file_end;
443 int doneany = 0;
444
445 /* If there's no way we will ever remove anything anyway, punt early. */
446 if (question_flag || touch_flag || all_secondary)
447 return;
448
449 if (sig && just_print_flag)
450 return;
451
452 file_slot = (struct file **) files.ht_vec;
453 file_end = file_slot + files.ht_size;
454 for ( ; file_slot < file_end; file_slot++)
455 if (! HASH_VACANT (*file_slot))
456 {
457 struct file *f = *file_slot;
458 /* Is this file eligible for automatic deletion?
459 Yes, IFF: it's marked intermediate, it's not secondary, it wasn't
460 given on the command line, and it's either a -include makefile or
461 it's not precious. */
462 if (f->intermediate && (f->dontcare || !f->precious)
463 && !f->secondary && !f->cmd_target)
464 {
465 int status;
466 if (f->update_status == -1)
467 /* If nothing would have created this file yet,
468 don't print an "rm" command for it. */
469 continue;
470 if (just_print_flag)
471 status = 0;
472 else
473 {
474 status = unlink (f->name);
475 if (status < 0 && errno == ENOENT)
476 continue;
477 }
478 if (!f->dontcare)
479 {
480 if (sig)
481 error (NILF, _("*** Deleting intermediate file `%s'"), f->name);
482 else
483 {
484 if (! doneany)
485 DB (DB_BASIC, (_("Removing intermediate files...\n")));
486 if (!silent_flag)
487 {
488 if (! doneany)
489 {
490 fputs ("rm ", stdout);
491 doneany = 1;
492 }
493 else
494 putchar (' ');
495 fputs (f->name, stdout);
496 fflush (stdout);
497 }
498 }
499 if (status < 0)
500 perror_with_name ("unlink: ", f->name);
501 }
502 }
503 }
504
505 if (doneany && !sig)
506 {
507 putchar ('\n');
508 fflush (stdout);
509 }
510}
511
512
513struct dep *
514parse_prereqs (char *p)
515{
516#ifndef CONFIG_WITH_ALLOC_CACHES
517 struct dep *new = (struct dep *)
518 multi_glob (parse_file_seq (&p, '|', sizeof (struct dep), 1),
519 sizeof (struct dep));
520#else
521 struct dep *new = (struct dep *)
522 multi_glob (parse_file_seq (&p, '|', &dep_cache, 1), &dep_cache);
523#endif
524
525 if (*p)
526 {
527 /* Files that follow '|' are "order-only" prerequisites that satisfy the
528 dependency by existing: their modification times are irrelevant. */
529 struct dep *ood;
530
531 ++p;
532#ifndef CONFIG_WITH_ALLOC_CACHES
533 ood = (struct dep *)
534 multi_glob (parse_file_seq (&p, '\0', sizeof (struct dep), 1),
535 sizeof (struct dep));
536#else
537 ood = (struct dep *)
538 multi_glob (parse_file_seq (&p, '\0', &dep_cache, 1), &dep_cache);
539#endif
540
541 if (! new)
542 new = ood;
543 else
544 {
545 struct dep *dp;
546 for (dp = new; dp->next != NULL; dp = dp->next)
547 ;
548 dp->next = ood;
549 }
550
551 for (; ood != NULL; ood = ood->next)
552 ood->ignore_mtime = 1;
553 }
554
555 return new;
556}
557
558/* Set the intermediate flag. */
559
560static void
561set_intermediate (const void *item)
562{
563 struct file *f = (struct file *) item;
564 f->intermediate = 1;
565}
566
567/* Expand and parse each dependency line. */
568static void
569expand_deps (struct file *f)
570{
571 struct dep *d;
572 struct dep *old = f->deps;
573 const char *file_stem = f->stem;
574 unsigned int last_dep_has_cmds = f->updating;
575 int initialized = 0;
576
577 f->updating = 0;
578 f->deps = 0;
579
580 for (d = old; d != 0; d = d->next)
581 {
582 struct dep *new, *d1;
583 char *p;
584#ifdef CONFIG_WITH_STRCACHE2
585 unsigned int len;
586#endif
587
588 if (! d->name)
589 continue;
590
591#ifdef CONFIG_WITH_INCLUDEDEP
592 /* Dependencies loaded by includedep are ready for use and we skip
593 the expensive parsing and globbing for them. To avoid wasting
594 lots of time walking the f->deps chain, we will advance D and
595 process all subsequent includedep records. */
596
597 if (d->includedep)
598 {
599 new = d1 = alloc_dep();
600 d1->staticpattern = 0;
601 d1->need_2nd_expansion = 0;
602 d1->includedep = 1;
603 d1->file = lookup_file (d->name);
604 if (d1->file == 0)
605 d1->file = enter_file (d->name);
606
607 while (d->next && d->next->includedep)
608 {
609 d = d->next;
610 d1 = d1->next = alloc_dep();
611 d1->staticpattern = 0;
612 d1->need_2nd_expansion = 0;
613 d1->includedep = 1;
614 d1->file = lookup_file (d->name);
615 if (d1->file == 0)
616 d1->file = enter_file (d->name);
617 }
618 }
619 else
620 {
621#endif
622
623 /* Create the dependency list.
624 If we're not doing 2nd expansion, then it's just the name. We will
625 still need to massage it though. */
626 if (! d->need_2nd_expansion)
627 {
628 p = variable_expand ("");
629#ifndef CONFIG_WITH_STRCACHE2
630 variable_buffer_output (p, d->name, strlen (d->name) + 1);
631#else
632 len = strcache2_get_len (&file_strcache, d->name);
633 variable_buffer_output (p, d->name, len + 1);
634#endif
635 p = variable_buffer;
636 }
637 else
638 {
639 /* If it's from a static pattern rule, convert the patterns into
640 "$*" so they'll expand properly. */
641 if (d->staticpattern)
642 {
643 char *o;
644 char *buffer = variable_expand ("");
645
646 o = subst_expand (buffer, d->name, "%", "$*", 1, 2, 0);
647 buffer = variable_buffer; /* bird - variable_buffer may have been reallocated. */
648
649 d->name = strcache_add_len (variable_buffer,
650 o - variable_buffer);
651 d->staticpattern = 0; /* Clear staticpattern so that we don't
652 re-expand %s below. */
653 }
654
655 /* We are going to do second expansion so initialize file variables
656 for the file. Since the stem for static pattern rules comes from
657 individual dep lines, we will temporarily set f->stem to d->stem.
658 */
659 if (!initialized)
660 {
661 initialize_file_variables (f, 0);
662 initialized = 1;
663 }
664
665 if (d->stem != 0)
666 f->stem = d->stem;
667
668#if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
669 set_file_variables (f, 0 /* real call, f->deps == 0 so we're ok. */);
670#else
671 set_file_variables (f);
672#endif
673
674#if !defined (CONFIG_WITH_VALUE_LENGTH) || !defined (CONFIG_WITH_STRCACHE2)
675 p = variable_expand_for_file (d->name, f);
676#else
677 len = strcache2_get_len (&file_strcache, d->name);
678 p = variable_expand_for_file_2 (NULL, d->name, len, f, &len);
679#endif
680
681 if (d->stem != 0)
682 f->stem = file_stem;
683 }
684
685 /* Parse the prerequisites. */
686 new = parse_prereqs (p);
687
688 /* If this dep list was from a static pattern rule, expand the %s. We
689 use patsubst_expand to translate the prerequisites' patterns into
690 plain prerequisite names. */
691 if (new && d->staticpattern)
692 {
693 const char *pattern = "%";
694 char *buffer = variable_expand ("");
695 struct dep *dp = new, *dl = 0;
696
697 while (dp != 0)
698 {
699 char *percent;
700#ifndef KMK
701 int nl = strlen (dp->name) + 1;
702 char *nm = alloca (nl);
703 memcpy (nm, dp->name, nl);
704 percent = find_percent (nm);
705#else /* KMK - don't make a stack copy unless it's actually required! */
706 unsigned int nl = strcache2_get_len (&file_strcache, dp->name);
707 char *nm;
708 percent = memchr (dp->name, '%', nl);
709 if (percent)
710 {
711 nm = alloca (nl + 1);
712 memcpy (nm, dp->name, nl + 1);
713 percent = find_percent (nm);
714 }
715#endif /* KMK */
716 if (percent)
717 {
718 char *o;
719
720 /* We have to handle empty stems specially, because that
721 would be equivalent to $(patsubst %,dp->name,) which
722 will always be empty. */
723 if (d->stem[0] == '\0')
724 {
725 memmove (percent, percent+1, strlen (percent));
726 o = variable_buffer_output (buffer, nm, strlen (nm) + 1);
727 }
728 else
729 {
730 o = patsubst_expand_pat (buffer, d->stem, pattern, nm,
731 pattern+1, percent+1);
732 o = variable_buffer_output (o, "", 1); /* bird fix - patsubst_expand_pat doesn't terminate,
733 the if case does and strcache would appreciate it. */
734 }
735 buffer = variable_buffer; /* bird fix - variable_buffer may have been reallocated. */
736
737
738 /* If the name expanded to the empty string, ignore it. */
739 if (buffer[0] == '\0')
740 {
741 struct dep *df = dp;
742 if (dp == new)
743 dp = new = new->next;
744 else
745 dp = dl->next = dp->next;
746 free_dep (df);
747 continue;
748 }
749
750 /* Save the name. */
751 dp->name = strcache_add_len (buffer, o - buffer - 1); /* bird fix - don't include the terminator. */
752 }
753 dl = dp;
754 dp = dp->next;
755 }
756 }
757
758 /* Enter them as files. */
759 for (d1 = new; d1 != 0; d1 = d1->next)
760 {
761 d1->file = lookup_file (d1->name);
762 if (d1->file == 0)
763 d1->file = enter_file (d1->name);
764 d1->name = 0;
765 d1->staticpattern = 0;
766 d1->need_2nd_expansion = 0;
767 }
768
769#ifdef CONFIG_WITH_INCLUDEDEP
770 }
771#endif
772
773 /* Add newly parsed deps to f->deps. If this is the last dependency
774 line and this target has commands then put it in front so the
775 last dependency line (the one with commands) ends up being the
776 first. This is important because people expect $< to hold first
777 prerequisite from the rule with commands. If it is not the last
778 dependency line or the rule does not have commands then link it
779 at the end so it appears in makefile order. */
780
781 if (new != 0)
782 {
783 if (d->next == 0 && last_dep_has_cmds)
784 {
785 struct dep **d_ptr;
786 for (d_ptr = &new; *d_ptr; d_ptr = &(*d_ptr)->next)
787 ;
788
789 *d_ptr = f->deps;
790 f->deps = new;
791 }
792 else
793 {
794 struct dep **d_ptr;
795 for (d_ptr = &f->deps; *d_ptr; d_ptr = &(*d_ptr)->next)
796 ;
797
798 *d_ptr = new;
799 }
800 }
801 }
802
803 free_dep_chain (old);
804}
805
806/* For each dependency of each file, make the `struct dep' point
807 at the appropriate `struct file' (which may have to be created).
808
809 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
810 and various other special targets. */
811
812void
813snap_deps (void)
814{
815 struct file *f;
816 struct file *f2;
817 struct dep *d;
818 struct file **file_slot_0;
819 struct file **file_slot;
820 struct file **file_end;
821
822 /* Perform second expansion and enter each dependency name as a file. */
823
824 /* Expand .SUFFIXES first; it's dependencies are used for $$* calculation. */
825 for (f = lookup_file (".SUFFIXES"); f != 0; f = f->prev)
826 expand_deps (f);
827
828#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
829 /* Perform 2nd target expansion on files which requires this. This will
830 be re-inserting (delete+insert) hash table entries so we have to use
831 hash_dump(). */
832 if (second_target_expansion)
833 {
834# ifdef KMK /* turn on warnings here. */
835 int save = warn_undefined_variables_flag;
836 warn_undefined_variables_flag = 1;
837# endif
838
839 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
840 file_end = file_slot_0 + files.ht_fill;
841 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
842 for (f = *file_slot; f != 0; f = f->prev)
843 if (f->need_2nd_target_expansion)
844 do_2nd_target_expansion (f);
845 free (file_slot_0);
846
847# ifdef KMK
848 warn_undefined_variables_flag = save;
849# endif
850
851 /* Disable second target expansion now since we won't expand files
852 entered after this point. (Saves CPU cycles in enter_file()). */
853 second_target_expansion = 0;
854 }
855#endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
856
857#ifdef CONFIG_WITH_INCLUDEDEP
858 /* Process any queued includedep files. Since includedep is supposed
859 to be *simple* stuff, we can do this after the second target expansion
860 and thereby save a little time. */
861 incdep_flush_and_term ();
862#endif /* CONFIG_WITH_INCLUDEDEP */
863
864 /* For every target that's not .SUFFIXES, expand its dependencies.
865 We must use hash_dump (), because within this loop we might add new files
866 to the table, possibly causing an in-situ table expansion. */
867 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
868 file_end = file_slot_0 + files.ht_fill;
869 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
870 for (f = *file_slot; f != 0; f = f->prev)
871#ifndef CONFIG_WITH_STRCACHE2
872 if (strcmp (f->name, ".SUFFIXES") != 0)
873#else
874 if (f->name != suffixes_strcached)
875#endif
876 expand_deps (f);
877#ifdef KMK
878 /* This is a HACK to work around the still broken test #9 in
879 features/double_colon. It produces the wrong result if the build is
880 parallel because of changed evaluation order. Just make these
881 problematic rules execute in single field till a proper fix is
882 forthcomming... */
883
884 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
885 if ( (f = *file_slot) != 0
886 && f->double_colon
887 && ( f->double_colon != f
888 || f->last != f))
889 for (f2 = f->double_colon; f2 != 0; f2 = f2->prev)
890 f2->command_flags |= COMMANDS_NOTPARALLEL;
891#endif /* KMK */
892 free (file_slot_0);
893
894 /* Now manage all the special targets. */
895
896 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
897 for (d = f->deps; d != 0; d = d->next)
898 for (f2 = d->file; f2 != 0; f2 = f2->prev)
899 f2->precious = 1;
900
901 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
902 for (d = f->deps; d != 0; d = d->next)
903 for (f2 = d->file; f2 != 0; f2 = f2->prev)
904 f2->low_resolution_time = 1;
905
906 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
907 for (d = f->deps; d != 0; d = d->next)
908 for (f2 = d->file; f2 != 0; f2 = f2->prev)
909 {
910 /* Mark this file as phony nonexistent target. */
911 f2->phony = 1;
912 f2->is_target = 1;
913 f2->last_mtime = NONEXISTENT_MTIME;
914 f2->mtime_before_update = NONEXISTENT_MTIME;
915 }
916
917 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
918 /* Mark .INTERMEDIATE deps as intermediate files. */
919 for (d = f->deps; d != 0; d = d->next)
920 for (f2 = d->file; f2 != 0; f2 = f2->prev)
921 f2->intermediate = 1;
922 /* .INTERMEDIATE with no deps does nothing.
923 Marking all files as intermediates is useless since the goal targets
924 would be deleted after they are built. */
925
926 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
927 /* Mark .SECONDARY deps as both intermediate and secondary. */
928 if (f->deps)
929 for (d = f->deps; d != 0; d = d->next)
930 for (f2 = d->file; f2 != 0; f2 = f2->prev)
931 f2->intermediate = f2->secondary = 1;
932 /* .SECONDARY with no deps listed marks *all* files that way. */
933 else
934 {
935 all_secondary = 1;
936 hash_map (&files, set_intermediate);
937 }
938
939 f = lookup_file (".EXPORT_ALL_VARIABLES");
940 if (f != 0 && f->is_target)
941 export_all_variables = 1;
942
943 f = lookup_file (".IGNORE");
944 if (f != 0 && f->is_target)
945 {
946 if (f->deps == 0)
947 ignore_errors_flag = 1;
948 else
949 for (d = f->deps; d != 0; d = d->next)
950 for (f2 = d->file; f2 != 0; f2 = f2->prev)
951 f2->command_flags |= COMMANDS_NOERROR;
952 }
953
954 f = lookup_file (".SILENT");
955 if (f != 0 && f->is_target)
956 {
957 if (f->deps == 0)
958 silent_flag = 1;
959 else
960 for (d = f->deps; d != 0; d = d->next)
961 for (f2 = d->file; f2 != 0; f2 = f2->prev)
962 f2->command_flags |= COMMANDS_SILENT;
963 }
964
965 f = lookup_file (".NOTPARALLEL");
966 if (f != 0 && f->is_target)
967#ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
968 not_parallel = 1;
969#else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
970 {
971 if (f->deps == 0)
972 {
973 DB (DB_KMK, (_("not_parallel -1\n")));
974 not_parallel = -1;
975 }
976 else
977 for (d = f->deps; d != 0; d = d->next)
978 for (f2 = d->file; f2 != 0; f2 = f2->prev)
979 f2->command_flags |= COMMANDS_NOTPARALLEL;
980 }
981#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
982
983#ifndef NO_MINUS_C_MINUS_O
984 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
985 /* This needs more work: what if the user sets this in the makefile?
986 if (posix_pedantic)
987 define_variable (STRING_SIZE_TUPLE("OUTPUT_OPTION"), "", o_default, 1);
988 */
989#endif
990
991 /* Remember that we've done this. */
992 snapped_deps = 1;
993}
994
995
996/* Set the `command_state' member of FILE and all its `also_make's. */
997
998void
999set_command_state (struct file *file, enum cmd_state state)
1000{
1001 struct dep *d;
1002
1003 file->command_state = state;
1004
1005 for (d = file->also_make; d != 0; d = d->next)
1006 d->file->command_state = state;
1007
1008#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1009 if (file->multi_head)
1010 for (file = file->multi_head; file != 0; file = file->multi_next)
1011 file->command_state = state;
1012#endif
1013}
1014
1015
1016/* Convert an external file timestamp to internal form. */
1017
1018FILE_TIMESTAMP
1019file_timestamp_cons (const char *fname, time_t s, int ns)
1020{
1021 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
1022 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
1023 FILE_TIMESTAMP ts = product + offset;
1024
1025 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
1026 && product <= ts && ts <= ORDINARY_MTIME_MAX))
1027 {
1028 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1029 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
1030 file_timestamp_sprintf (buf, ts);
1031 error (NILF, _("%s: Timestamp out of range; substituting %s"),
1032 fname ? fname : _("Current time"), buf);
1033 }
1034
1035 return ts;
1036}
1037
1038
1039/* Return the current time as a file timestamp, setting *RESOLUTION to
1040 its resolution. */
1041FILE_TIMESTAMP
1042file_timestamp_now (int *resolution)
1043{
1044 int r;
1045 time_t s;
1046 int ns;
1047
1048 /* Don't bother with high-resolution clocks if file timestamps have
1049 only one-second resolution. The code below should work, but it's
1050 not worth the hassle of debugging it on hosts where it fails. */
1051#if FILE_TIMESTAMP_HI_RES
1052# if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
1053 {
1054 struct timespec timespec;
1055 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
1056 {
1057 r = 1;
1058 s = timespec.tv_sec;
1059 ns = timespec.tv_nsec;
1060 goto got_time;
1061 }
1062 }
1063# endif
1064# if HAVE_GETTIMEOFDAY
1065 {
1066 struct timeval timeval;
1067 if (gettimeofday (&timeval, 0) == 0)
1068 {
1069 r = 1000;
1070 s = timeval.tv_sec;
1071 ns = timeval.tv_usec * 1000;
1072 goto got_time;
1073 }
1074 }
1075# endif
1076#endif
1077
1078 r = 1000000000;
1079 s = time ((time_t *) 0);
1080 ns = 0;
1081
1082#if FILE_TIMESTAMP_HI_RES
1083 got_time:
1084#endif
1085 *resolution = r;
1086 return file_timestamp_cons (0, s, ns);
1087}
1088
1089/* Place into the buffer P a printable representation of the file
1090 timestamp TS. */
1091void
1092file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
1093{
1094 time_t t = FILE_TIMESTAMP_S (ts);
1095 struct tm *tm = localtime (&t);
1096
1097 if (tm)
1098 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
1099 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1100 tm->tm_hour, tm->tm_min, tm->tm_sec);
1101 else if (t < 0)
1102 sprintf (p, "%ld", (long) t);
1103 else
1104 sprintf (p, "%lu", (unsigned long) t);
1105 p += strlen (p);
1106
1107 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
1108 know the actual timestamp resolution, since clock_getres applies only to
1109 local times, whereas this timestamp might come from a remote filesystem.
1110 So removing trailing zeros is the best guess that we can do. */
1111 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
1112 p += strlen (p) - 1;
1113 while (*p == '0')
1114 p--;
1115 p += *p != '.';
1116
1117 *p = '\0';
1118}
1119
1120
1121/* Print the data base of files. */
1122
1123static void
1124print_file (const void *item)
1125{
1126 const struct file *f = item;
1127 struct dep *d;
1128 struct dep *ood = 0;
1129
1130 putchar ('\n');
1131 if (!f->is_target)
1132 puts (_("# Not a target:"));
1133#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1134 if (f->multi_head)
1135 {
1136 const struct file *f2;
1137 if (f->multi_head == f)
1138 {
1139 int multi_maybe = -1;
1140 assert (!f->multi_maybe);
1141 assert (!f->double_colon);
1142
1143 printf ("%s", f->name);
1144 for (f2 = f->multi_next; f2 != 0; f2 = f2->multi_next)
1145 {
1146 printf (" %s%s", f2->multi_maybe != multi_maybe
1147 ? f2->multi_maybe ? "+| " : "+ " : "",
1148 f2->name);
1149 multi_maybe = f2->multi_maybe;
1150 }
1151 putchar (':');
1152 }
1153 else
1154 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1155 }
1156 else
1157#endif
1158 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1159
1160 /* Print all normal dependencies; note any order-only deps. */
1161 for (d = f->deps; d != 0; d = d->next)
1162 if (! d->ignore_mtime)
1163 printf (" %s", dep_name (d));
1164 else if (! ood)
1165 ood = d;
1166
1167 /* Print order-only deps, if we have any. */
1168 if (ood)
1169 {
1170 printf (" | %s", dep_name (ood));
1171 for (d = ood->next; d != 0; d = d->next)
1172 if (d->ignore_mtime)
1173 printf (" %s", dep_name (d));
1174 }
1175
1176 putchar ('\n');
1177
1178#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1179 if (f->multi_head && f->multi_head != f)
1180 {
1181 const struct file *f2;
1182 fputs (_("# In multi target list:"), stdout);
1183 for (f2 = f->multi_head; f2 != 0; f2 = f2->multi_next)
1184 printf (" %s%s", f2->name, f == f2 ? "(*)" : "");
1185 putchar ('\n');
1186 if (f->multi_maybe)
1187 puts (_("# File is an optional multi target member."));
1188 }
1189#endif
1190
1191 if (f->precious)
1192 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
1193 if (f->phony)
1194 puts (_("# Phony target (prerequisite of .PHONY)."));
1195 if (f->cmd_target)
1196 puts (_("# Command line target."));
1197 if (f->dontcare)
1198 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
1199 puts (f->tried_implicit
1200 ? _("# Implicit rule search has been done.")
1201 : _("# Implicit rule search has not been done."));
1202 if (f->stem != 0)
1203 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
1204 if (f->intermediate)
1205 puts (_("# File is an intermediate prerequisite."));
1206 if (f->also_make != 0)
1207 {
1208 fputs (_("# Also makes:"), stdout);
1209 for (d = f->also_make; d != 0; d = d->next)
1210 printf (" %s", dep_name (d));
1211 putchar ('\n');
1212 }
1213 if (f->last_mtime == UNKNOWN_MTIME)
1214 puts (_("# Modification time never checked."));
1215 else if (f->last_mtime == NONEXISTENT_MTIME)
1216 puts (_("# File does not exist."));
1217 else if (f->last_mtime == OLD_MTIME)
1218 puts (_("# File is very old."));
1219 else
1220 {
1221 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1222 file_timestamp_sprintf (buf, f->last_mtime);
1223 printf (_("# Last modified %s\n"), buf);
1224 }
1225 puts (f->updated
1226 ? _("# File has been updated.") : _("# File has not been updated."));
1227 switch (f->command_state)
1228 {
1229 case cs_running:
1230 puts (_("# Recipe currently running (THIS IS A BUG)."));
1231 break;
1232 case cs_deps_running:
1233 puts (_("# Dependencies recipe running (THIS IS A BUG)."));
1234 break;
1235 case cs_not_started:
1236 case cs_finished:
1237 switch (f->update_status)
1238 {
1239 case -1:
1240 break;
1241 case 0:
1242 puts (_("# Successfully updated."));
1243 break;
1244 case 1:
1245 assert (question_flag);
1246 puts (_("# Needs to be updated (-q is set)."));
1247 break;
1248 case 2:
1249 puts (_("# Failed to be updated."));
1250 break;
1251 default:
1252 puts (_("# Invalid value in `update_status' member!"));
1253 fflush (stdout);
1254 fflush (stderr);
1255 abort ();
1256 }
1257 break;
1258 default:
1259 puts (_("# Invalid value in `command_state' member!"));
1260 fflush (stdout);
1261 fflush (stderr);
1262 abort ();
1263 }
1264
1265 if (f->variables != 0)
1266 print_file_variables (f);
1267
1268 if (f->cmds != 0)
1269 print_commands (f->cmds);
1270
1271 if (f->prev)
1272 print_file ((const void *) f->prev);
1273}
1274
1275void
1276print_file_data_base (void)
1277{
1278 puts (_("\n# Files"));
1279
1280 hash_map (&files, print_file);
1281
1282 fputs (_("\n# files hash-table stats:\n# "), stdout);
1283 hash_print_stats (&files, stdout);
1284}
1285
1286#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
1287void
1288print_file_stats (void)
1289{
1290 fputs (_("\n# files hash-table stats:\n# "), stdout);
1291 hash_print_stats (&files, stdout);
1292 fputs ("\n", stdout);
1293}
1294#endif
1295
1296
1297/* Verify the integrity of the data base of files. */
1298
1299#define VERIFY_CACHED(_p,_n) \
1300 do{\
1301 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1302 printf ("%s: Field %s not cached: %s\n", _p->name, # _n, _p->_n); \
1303 }while(0)
1304
1305static void
1306verify_file (const void *item)
1307{
1308 const struct file *f = item;
1309 const struct dep *d;
1310
1311 VERIFY_CACHED (f, name);
1312 VERIFY_CACHED (f, hname);
1313 VERIFY_CACHED (f, vpath);
1314 VERIFY_CACHED (f, stem);
1315
1316 /* Check the deps. */
1317 for (d = f->deps; d != 0; d = d->next)
1318 {
1319 VERIFY_CACHED (d, name);
1320 VERIFY_CACHED (d, stem);
1321 }
1322}
1323
1324void
1325verify_file_data_base (void)
1326{
1327 hash_map (&files, verify_file);
1328}
1329
1330#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1331
1332char *
1333build_target_list (char *value)
1334{
1335 static unsigned long last_targ_count = 0;
1336
1337 if (files.ht_fill != last_targ_count)
1338 {
1339 unsigned long max = EXPANSION_INCREMENT (strlen (value));
1340 unsigned long len;
1341 char *p;
1342 struct file **fp = (struct file **) files.ht_vec;
1343 struct file **end = &fp[files.ht_size];
1344
1345 /* Make sure we have at least MAX bytes in the allocated buffer. */
1346 value = xrealloc (value, max);
1347
1348 p = value;
1349 len = 0;
1350 for (; fp < end; ++fp)
1351 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1352 {
1353 struct file *f = *fp;
1354 int l = strlen (f->name);
1355
1356 len += l + 1;
1357 if (len > max)
1358 {
1359 unsigned long off = p - value;
1360
1361 max += EXPANSION_INCREMENT (l + 1);
1362 value = xrealloc (value, max);
1363 p = &value[off];
1364 }
1365
1366 memcpy (p, f->name, l);
1367 p += l;
1368 *(p++) = ' ';
1369 }
1370 *(p-1) = '\0';
1371
1372 last_targ_count = files.ht_fill;
1373 }
1374
1375 return value;
1376}
1377
1378void
1379init_hash_files (void)
1380{
1381#ifndef CONFIG_WITH_STRCACHE2
1382# ifdef KMK
1383 hash_init (&files, /*65535*/ 32755, file_hash_1, file_hash_2, file_hash_cmp);
1384# else
1385 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1386# endif
1387#else /* CONFIG_WITH_STRCACHE2 */
1388# ifdef KMK
1389 hash_init_strcached (&files, 32755, &file_strcache,
1390 offsetof (struct file, hname));
1391# else
1392 hash_init_strcached (&files, 1000, &file_strcache,
1393 offsetof (struct file, hname));
1394# endif
1395#endif /* CONFIG_WITH_STRCACHE2 */
1396}
1397
1398/* EOF */
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