VirtualBox

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

Last change on this file since 2613 was 2591, checked in by bird, 13 years ago

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

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

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