VirtualBox

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

Last change on this file since 2700 was 2665, checked in by bird, 13 years ago

file.c: Fixed renaming/rehashing of multi-target files. This could result in us forgetting the multi_* properties and fail to resolve prerequisites. (The COMWrappers missing filesplitter issue.)

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