VirtualBox

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

Last change on this file since 3620 was 3620, checked in by bird, 7 months ago

kmk/file.c: Don't free names that are in the string cache from expand_deps().

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