VirtualBox

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

Last change on this file since 3387 was 3309, checked in by bird, 4 years ago

kmk/file.c: Disabled pointless hack for making the double_colon test (#9) run smoothly with -j != 1. Total performance buzzkill.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use