VirtualBox

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

Last change on this file since 1863 was 1863, checked in by bird, 16 years ago

kmk: Allocation caches for nameseq, dep and idep. next: variable.

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