VirtualBox

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

Last change on this file since 2741 was 2741, checked in by bird, 10 years ago

file.c: Forgot to update to_file->multi_head when the file being rehashed was at the head of a multi target link. This caused the renamed target to be used during updating and lots of trouble.

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