VirtualBox

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

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

kmk: Moved the strcache hash optimizations into the hash code.

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