VirtualBox

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

Last change on this file since 2657 was 2634, checked in by bird, 12 years ago

kmk/file.c: Fixed double free when CONFIG_WITH_STRCACHE2 is defined. Fixed 2nd expansion bug when CONFIG_WITH_INCLUDEDEP is defined. Fixed GNU make memory leak during 2nd expansion.

  • Property svn:eol-style set to native
File size: 37.4 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. */
676
677 if (d->includedep)
678 {
679 d->need_2nd_expansion = 0;
680 d->file = lookup_file (name);
681 if (d->file == 0)
682 d->file = enter_file (name);
683 d->name = 0;
684 free(name);
685
686 dp = &d->next;
687 d = d->next;
688 continue;
689 }
690#endif /* CONFIG_WITH_INCLUDEDEP */
691
692 /* If it's from a static pattern rule, convert the patterns into
693 "$*" so they'll expand properly. */
694 if (d->staticpattern)
695 {
696 char *o;
697 d->name = o = variable_expand ("");
698 o = subst_expand (o, name, "%", "$*", 1, 2, 0);
699 *o = '\0';
700#ifndef CONFIG_WITH_STRCACHE2
701 free (name);
702 d->name = name = xstrdup (variable_buffer); /* bird not d->name, can be reallocated */
703#else
704 d->name = strcache2_add (&file_strcache, variable_buffer, o - variable_buffer);
705#endif
706 d->staticpattern = 0;
707 }
708
709 /* We're going to do second expansion so initialize file variables for
710 the file. Since the stem for static pattern rules comes from
711 individual dep lines, we will temporarily set f->stem to d->stem. */
712 if (!initialized)
713 {
714 initialize_file_variables (f, 0);
715 initialized = 1;
716 }
717
718 if (d->stem != 0)
719 f->stem = d->stem;
720
721#if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
722 set_file_variables (f, 0 /* real call, f->deps == 0 so we're ok. */);
723#else
724 set_file_variables (f);
725#endif
726
727 p = variable_expand_for_file (d->name, f);
728
729 if (d->stem != 0)
730 f->stem = file_stem;
731
732 /* At this point we don't need the name anymore: free it. */
733 free (name);
734
735 /* Parse the prerequisites and enter them into the file database. */
736 new = enter_prereqs (split_prereqs (p), d->stem);
737
738 /* If there were no prereqs here (blank!) then throw this one out. */
739 if (new == 0)
740 {
741 *dp = d->next;
742 free_dep (d);
743 d = *dp;
744 continue;
745 }
746
747 /* Add newly parsed prerequisites. */
748 next = d->next;
749#ifdef KMK /* bird: memory leak */
750 assert(new != d);
751 free_dep (d);
752#endif
753 *dp = new;
754 for (dp = &new->next, d = new->next; d != 0; dp = &d->next, d = d->next)
755 ;
756 *dp = next;
757 d = *dp;
758 }
759}
760
761/* Reset the updating flag. */
762
763static void
764reset_updating (const void *item)
765{
766 struct file *f = (struct file *) item;
767 f->updating = 0;
768}
769
770/* For each dependency of each file, make the `struct dep' point
771 at the appropriate `struct file' (which may have to be created).
772
773 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
774 and various other special targets. */
775
776void
777snap_deps (void)
778{
779 struct file *f;
780 struct file *f2;
781 struct dep *d;
782
783 /* Remember that we've done this. Once we start snapping deps we can no
784 longer define new targets. */
785 snapped_deps = 1;
786
787#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
788 /* Perform 2nd target expansion on files which requires this. This will
789 be re-inserting (delete+insert) hash table entries so we have to use
790 hash_dump(). */
791 if (second_target_expansion)
792 {
793 struct file **file_slot_0, **file_end, **file_slot;
794# ifdef KMK /* turn on warnings here. */
795 int save = warn_undefined_variables_flag;
796 warn_undefined_variables_flag = 1;
797# endif
798
799 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
800 file_end = file_slot_0 + files.ht_fill;
801 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
802 for (f = *file_slot; f != 0; f = f->prev)
803 if (f->need_2nd_target_expansion)
804 do_2nd_target_expansion (f);
805 free (file_slot_0);
806
807# ifdef KMK
808 warn_undefined_variables_flag = save;
809# endif
810
811 /* Disable second target expansion now since we won't expand files
812 entered after this point. (Saves CPU cycles in enter_file()). */
813 second_target_expansion = 0;
814 }
815#endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
816
817#ifdef CONFIG_WITH_INCLUDEDEP
818 /* Process any queued includedep files. Since includedep is supposed
819 to be *simple* stuff, we can do this after the second target expansion
820 and thereby save a little time. */
821 incdep_flush_and_term ();
822#endif /* CONFIG_WITH_INCLUDEDEP */
823
824 /* Perform second expansion and enter each dependency name as a file. We
825 must use hash_dump() here because within these loops we likely add new
826 files to the table, possibly causing an in-situ table expansion.
827
828 We only need to do this if second_expansion has been defined; if it
829 hasn't then all deps were expanded as the makefile was read in. If we
830 ever change make to be able to unset .SECONDARY_EXPANSION this will have
831 to change. */
832
833 if (second_expansion)
834 {
835 struct file **file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
836 struct file **file_end = file_slot_0 + files.ht_fill;
837 struct file **file_slot;
838 const char *suffixes;
839
840 /* Expand .SUFFIXES: its prerequisites are used for $$* calc. */
841 f = lookup_file (".SUFFIXES");
842 suffixes = f ? f->name : 0;
843 for (; f != 0; f = f->prev)
844 expand_deps (f);
845
846#ifdef KMK
847 /* This is a HACK to work around the still broken test #9 in
848 features/double_colon. It produces the wrong result if the build is
849 parallel because of changed evaluation order. Just make these
850 problematic rules execute in single field till a proper fix is
851 forthcomming... */
852
853 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
854 if ( (f = *file_slot) != 0
855 && f->double_colon
856 && ( f->double_colon != f
857 || f->last != f))
858 for (f2 = f->double_colon; f2 != 0; f2 = f2->prev)
859 f2->command_flags |= COMMANDS_NOTPARALLEL;
860#endif /* KMK */
861
862 /* For every target that's not .SUFFIXES, expand its prerequisites. */
863
864 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
865 for (f = *file_slot; f != 0; f = f->prev)
866 if (f->name != suffixes)
867 expand_deps (f);
868 free (file_slot_0);
869 }
870 else
871 /* We're not doing second expansion, so reset updating. */
872 hash_map (&files, reset_updating);
873
874 /* Now manage all the special targets. */
875
876 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
877 for (d = f->deps; d != 0; d = d->next)
878 for (f2 = d->file; f2 != 0; f2 = f2->prev)
879 f2->precious = 1;
880
881 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
882 for (d = f->deps; d != 0; d = d->next)
883 for (f2 = d->file; f2 != 0; f2 = f2->prev)
884 f2->low_resolution_time = 1;
885
886 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
887 for (d = f->deps; d != 0; d = d->next)
888 for (f2 = d->file; f2 != 0; f2 = f2->prev)
889 {
890 /* Mark this file as phony nonexistent target. */
891 f2->phony = 1;
892 f2->is_target = 1;
893 f2->last_mtime = NONEXISTENT_MTIME;
894 f2->mtime_before_update = NONEXISTENT_MTIME;
895 }
896
897 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
898 /* Mark .INTERMEDIATE deps as intermediate files. */
899 for (d = f->deps; d != 0; d = d->next)
900 for (f2 = d->file; f2 != 0; f2 = f2->prev)
901 f2->intermediate = 1;
902 /* .INTERMEDIATE with no deps does nothing.
903 Marking all files as intermediates is useless since the goal targets
904 would be deleted after they are built. */
905
906 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
907 /* Mark .SECONDARY deps as both intermediate and secondary. */
908 if (f->deps)
909 for (d = f->deps; d != 0; d = d->next)
910 for (f2 = d->file; f2 != 0; f2 = f2->prev)
911 f2->intermediate = f2->secondary = 1;
912 /* .SECONDARY with no deps listed marks *all* files that way. */
913 else
914 {
915 all_secondary = 1;
916 hash_map (&files, set_intermediate);
917 }
918
919 f = lookup_file (".EXPORT_ALL_VARIABLES");
920 if (f != 0 && f->is_target)
921 export_all_variables = 1;
922
923 f = lookup_file (".IGNORE");
924 if (f != 0 && f->is_target)
925 {
926 if (f->deps == 0)
927 ignore_errors_flag = 1;
928 else
929 for (d = f->deps; d != 0; d = d->next)
930 for (f2 = d->file; f2 != 0; f2 = f2->prev)
931 f2->command_flags |= COMMANDS_NOERROR;
932 }
933
934 f = lookup_file (".SILENT");
935 if (f != 0 && f->is_target)
936 {
937 if (f->deps == 0)
938 silent_flag = 1;
939 else
940 for (d = f->deps; d != 0; d = d->next)
941 for (f2 = d->file; f2 != 0; f2 = f2->prev)
942 f2->command_flags |= COMMANDS_SILENT;
943 }
944
945 f = lookup_file (".NOTPARALLEL");
946 if (f != 0 && f->is_target)
947#ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
948 not_parallel = 1;
949#else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
950 {
951 if (f->deps == 0)
952 {
953 DB (DB_KMK, (_("not_parallel -1\n")));
954 not_parallel = -1;
955 }
956 else
957 for (d = f->deps; d != 0; d = d->next)
958 for (f2 = d->file; f2 != 0; f2 = f2->prev)
959 f2->command_flags |= COMMANDS_NOTPARALLEL;
960 }
961#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
962
963#ifndef NO_MINUS_C_MINUS_O
964 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
965 /* This needs more work: what if the user sets this in the makefile?
966 if (posix_pedantic)
967 define_variable_cname ("OUTPUT_OPTION", "", o_default, 1);
968 */
969#endif
970}
971
972
973/* Set the `command_state' member of FILE and all its `also_make's. */
974
975void
976set_command_state (struct file *file, enum cmd_state state)
977{
978 struct dep *d;
979
980 file->command_state = state;
981
982 for (d = file->also_make; d != 0; d = d->next)
983 d->file->command_state = state;
984
985#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
986 if (file->multi_head)
987 for (file = file->multi_head; file != 0; file = file->multi_next)
988 file->command_state = state;
989#endif
990}
991
992
993/* Convert an external file timestamp to internal form. */
994
995FILE_TIMESTAMP
996file_timestamp_cons (const char *fname, time_t s, int ns)
997{
998 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
999 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
1000 FILE_TIMESTAMP ts = product + offset;
1001
1002 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
1003 && product <= ts && ts <= ORDINARY_MTIME_MAX))
1004 {
1005 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1006 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
1007 file_timestamp_sprintf (buf, ts);
1008 error (NILF, _("%s: Timestamp out of range; substituting %s"),
1009 fname ? fname : _("Current time"), buf);
1010 }
1011
1012 return ts;
1013}
1014
1015
1016/* Return the current time as a file timestamp, setting *RESOLUTION to
1017 its resolution. */
1018FILE_TIMESTAMP
1019file_timestamp_now (int *resolution)
1020{
1021 int r;
1022 time_t s;
1023 int ns;
1024
1025 /* Don't bother with high-resolution clocks if file timestamps have
1026 only one-second resolution. The code below should work, but it's
1027 not worth the hassle of debugging it on hosts where it fails. */
1028#if FILE_TIMESTAMP_HI_RES
1029# if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
1030 {
1031 struct timespec timespec;
1032 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
1033 {
1034 r = 1;
1035 s = timespec.tv_sec;
1036 ns = timespec.tv_nsec;
1037 goto got_time;
1038 }
1039 }
1040# endif
1041# if HAVE_GETTIMEOFDAY
1042 {
1043 struct timeval timeval;
1044 if (gettimeofday (&timeval, 0) == 0)
1045 {
1046 r = 1000;
1047 s = timeval.tv_sec;
1048 ns = timeval.tv_usec * 1000;
1049 goto got_time;
1050 }
1051 }
1052# endif
1053#endif
1054
1055 r = 1000000000;
1056 s = time ((time_t *) 0);
1057 ns = 0;
1058
1059#if FILE_TIMESTAMP_HI_RES
1060 got_time:
1061#endif
1062 *resolution = r;
1063 return file_timestamp_cons (0, s, ns);
1064}
1065
1066/* Place into the buffer P a printable representation of the file
1067 timestamp TS. */
1068void
1069file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
1070{
1071 time_t t = FILE_TIMESTAMP_S (ts);
1072 struct tm *tm = localtime (&t);
1073
1074 if (tm)
1075 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
1076 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1077 tm->tm_hour, tm->tm_min, tm->tm_sec);
1078 else if (t < 0)
1079 sprintf (p, "%ld", (long) t);
1080 else
1081 sprintf (p, "%lu", (unsigned long) t);
1082 p += strlen (p);
1083
1084 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
1085 know the actual timestamp resolution, since clock_getres applies only to
1086 local times, whereas this timestamp might come from a remote filesystem.
1087 So removing trailing zeros is the best guess that we can do. */
1088 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
1089 p += strlen (p) - 1;
1090 while (*p == '0')
1091 p--;
1092 p += *p != '.';
1093
1094 *p = '\0';
1095}
1096
1097
1098/* Print the data base of files. */
1099
1100void
1101print_prereqs (const struct dep *deps)
1102{
1103 const struct dep *ood = 0;
1104
1105 /* Print all normal dependencies; note any order-only deps. */
1106 for (; deps != 0; deps = deps->next)
1107 if (! deps->ignore_mtime)
1108 printf (" %s", dep_name (deps));
1109 else if (! ood)
1110 ood = deps;
1111
1112 /* Print order-only deps, if we have any. */
1113 if (ood)
1114 {
1115 printf (" | %s", dep_name (ood));
1116 for (ood = ood->next; ood != 0; ood = ood->next)
1117 if (ood->ignore_mtime)
1118 printf (" %s", dep_name (ood));
1119 }
1120
1121 putchar ('\n');
1122}
1123
1124static void
1125print_file (const void *item)
1126{
1127 const struct file *f = item;
1128
1129 putchar ('\n');
1130 if (!f->is_target)
1131 puts (_("# Not a target:"));
1132#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1133 if (f->multi_head)
1134 {
1135 const struct file *f2;
1136 if (f->multi_head == f)
1137 {
1138 int multi_maybe = -1;
1139 assert (!f->multi_maybe);
1140 assert (!f->double_colon);
1141
1142 printf ("%s", f->name);
1143 for (f2 = f->multi_next; f2 != 0; f2 = f2->multi_next)
1144 {
1145 printf (" %s%s", f2->multi_maybe != multi_maybe
1146 ? f2->multi_maybe ? "+| " : "+ " : "",
1147 f2->name);
1148 multi_maybe = f2->multi_maybe;
1149 }
1150 putchar (':');
1151 }
1152 else
1153 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1154 }
1155 else
1156#endif
1157 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1158
1159 print_prereqs (f->deps);
1160
1161#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1162 if (f->multi_head && f->multi_head != f)
1163 {
1164 const struct file *f2;
1165 fputs (_("# In multi target list:"), stdout);
1166 for (f2 = f->multi_head; f2 != 0; f2 = f2->multi_next)
1167 printf (" %s%s", f2->name, f == f2 ? "(*)" : "");
1168 putchar ('\n');
1169 if (f->multi_maybe)
1170 puts (_("# File is an optional multi target member."));
1171 }
1172#endif
1173
1174 if (f->precious)
1175 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
1176 if (f->phony)
1177 puts (_("# Phony target (prerequisite of .PHONY)."));
1178 if (f->cmd_target)
1179 puts (_("# Command line target."));
1180 if (f->dontcare)
1181 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
1182 puts (f->tried_implicit
1183 ? _("# Implicit rule search has been done.")
1184 : _("# Implicit rule search has not been done."));
1185 if (f->stem != 0)
1186 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
1187 if (f->intermediate)
1188 puts (_("# File is an intermediate prerequisite."));
1189 if (f->also_make != 0)
1190 {
1191 const struct dep *d;
1192 fputs (_("# Also makes:"), stdout);
1193 for (d = f->also_make; d != 0; d = d->next)
1194 printf (" %s", dep_name (d));
1195 putchar ('\n');
1196 }
1197 if (f->last_mtime == UNKNOWN_MTIME)
1198 puts (_("# Modification time never checked."));
1199 else if (f->last_mtime == NONEXISTENT_MTIME)
1200 puts (_("# File does not exist."));
1201 else if (f->last_mtime == OLD_MTIME)
1202 puts (_("# File is very old."));
1203 else
1204 {
1205 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1206 file_timestamp_sprintf (buf, f->last_mtime);
1207 printf (_("# Last modified %s\n"), buf);
1208 }
1209 puts (f->updated
1210 ? _("# File has been updated.") : _("# File has not been updated."));
1211 switch (f->command_state)
1212 {
1213 case cs_running:
1214 puts (_("# Recipe currently running (THIS IS A BUG)."));
1215 break;
1216 case cs_deps_running:
1217 puts (_("# Dependencies recipe running (THIS IS A BUG)."));
1218 break;
1219 case cs_not_started:
1220 case cs_finished:
1221 switch (f->update_status)
1222 {
1223 case -1:
1224 break;
1225 case 0:
1226 puts (_("# Successfully updated."));
1227 break;
1228 case 1:
1229 assert (question_flag);
1230 puts (_("# Needs to be updated (-q is set)."));
1231 break;
1232 case 2:
1233 puts (_("# Failed to be updated."));
1234 break;
1235 default:
1236 puts (_("# Invalid value in `update_status' member!"));
1237 fflush (stdout);
1238 fflush (stderr);
1239 abort ();
1240 }
1241 break;
1242 default:
1243 puts (_("# Invalid value in `command_state' member!"));
1244 fflush (stdout);
1245 fflush (stderr);
1246 abort ();
1247 }
1248
1249 if (f->variables != 0)
1250 print_file_variables (f);
1251
1252 if (f->cmds != 0)
1253 print_commands (f->cmds);
1254
1255 if (f->prev)
1256 print_file ((const void *) f->prev);
1257}
1258
1259void
1260print_file_data_base (void)
1261{
1262 puts (_("\n# Files"));
1263
1264 hash_map (&files, print_file);
1265
1266 fputs (_("\n# files hash-table stats:\n# "), stdout);
1267 hash_print_stats (&files, stdout);
1268}
1269
1270#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
1271void
1272print_file_stats (void)
1273{
1274 fputs (_("\n# files hash-table stats:\n# "), stdout);
1275 hash_print_stats (&files, stdout);
1276 fputs ("\n", stdout);
1277}
1278#endif
1279
1280
1281/* Verify the integrity of the data base of files. */
1282
1283#define VERIFY_CACHED(_p,_n) \
1284 do{\
1285 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1286 error (NULL, "%s: Field '%s' not cached: %s\n", _p->name, # _n, _p->_n); \
1287 }while(0)
1288
1289static void
1290verify_file (const void *item)
1291{
1292 const struct file *f = item;
1293 const struct dep *d;
1294
1295 VERIFY_CACHED (f, name);
1296 VERIFY_CACHED (f, hname);
1297 VERIFY_CACHED (f, vpath);
1298 VERIFY_CACHED (f, stem);
1299
1300 /* Check the deps. */
1301 for (d = f->deps; d != 0; d = d->next)
1302 {
1303 if (! d->need_2nd_expansion)
1304 VERIFY_CACHED (d, name);
1305 VERIFY_CACHED (d, stem);
1306 }
1307}
1308
1309void
1310verify_file_data_base (void)
1311{
1312 hash_map (&files, verify_file);
1313}
1314
1315#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1316
1317char *
1318build_target_list (char *value)
1319{
1320 static unsigned long last_targ_count = 0;
1321
1322 if (files.ht_fill != last_targ_count)
1323 {
1324 unsigned long max = EXPANSION_INCREMENT (strlen (value));
1325 unsigned long len;
1326 char *p;
1327 struct file **fp = (struct file **) files.ht_vec;
1328 struct file **end = &fp[files.ht_size];
1329
1330 /* Make sure we have at least MAX bytes in the allocated buffer. */
1331 value = xrealloc (value, max);
1332
1333 p = value;
1334 len = 0;
1335 for (; fp < end; ++fp)
1336 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1337 {
1338 struct file *f = *fp;
1339 int l = strlen (f->name);
1340
1341 len += l + 1;
1342 if (len > max)
1343 {
1344 unsigned long off = p - value;
1345
1346 max += EXPANSION_INCREMENT (l + 1);
1347 value = xrealloc (value, max);
1348 p = &value[off];
1349 }
1350
1351 memcpy (p, f->name, l);
1352 p += l;
1353 *(p++) = ' ';
1354 }
1355 *(p-1) = '\0';
1356
1357 last_targ_count = files.ht_fill;
1358 }
1359
1360 return value;
1361}
1362
1363void
1364init_hash_files (void)
1365{
1366#ifndef CONFIG_WITH_STRCACHE2
1367# ifdef KMK
1368 hash_init (&files, /*65535*/ 32755, file_hash_1, file_hash_2, file_hash_cmp);
1369# else
1370 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1371# endif
1372#else /* CONFIG_WITH_STRCACHE2 */
1373# ifdef KMK
1374 hash_init_strcached (&files, 32755, &file_strcache,
1375 offsetof (struct file, hname));
1376# else
1377 hash_init_strcached (&files, 1000, &file_strcache,
1378 offsetof (struct file, hname));
1379# endif
1380#endif /* CONFIG_WITH_STRCACHE2 */
1381}
1382
1383/* 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