source: trunk/src/kmk/file.c@ 1857

Last change on this file since 1857 was 1857, checked in by bird, 17 years ago

kmk: improved the hashing of file table entries by making the strcache cache their hash values along with the string length.

  • Property svn:eol-style set to native
File size: 37.4 KB
Line 
1/* Target file management for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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 struct dep *new = (struct dep *)
527 multi_glob (parse_file_seq (&p, '|', sizeof (struct dep), 1),
528 sizeof (struct dep));
529
530 if (*p)
531 {
532 /* Files that follow '|' are "order-only" prerequisites that satisfy the
533 dependency by existing: their modification times are irrelevant. */
534 struct dep *ood;
535
536 ++p;
537 ood = (struct dep *)
538 multi_glob (parse_file_seq (&p, '\0', sizeof (struct dep), 1),
539 sizeof (struct dep));
540
541 if (! new)
542 new = ood;
543 else
544 {
545 struct dep *dp;
546 for (dp = new; dp->next != NULL; dp = dp->next)
547 ;
548 dp->next = ood;
549 }
550
551 for (; ood != NULL; ood = ood->next)
552 ood->ignore_mtime = 1;
553 }
554
555 return new;
556}
557
558/* Set the intermediate flag. */
559
560static void
561set_intermediate (const void *item)
562{
563 struct file *f = (struct file *) item;
564 f->intermediate = 1;
565}
566
567/* Expand and parse each dependency line. */
568static void
569expand_deps (struct file *f)
570{
571 struct dep *d;
572 struct dep *old = f->deps;
573 const char *file_stem = f->stem;
574 unsigned int last_dep_has_cmds = f->updating;
575 int initialized = 0;
576
577 f->updating = 0;
578 f->deps = 0;
579
580 for (d = old; d != 0; d = d->next)
581 {
582 size_t buffer_offset; /* bird */
583 struct dep *new, *d1;
584 char *p;
585#ifdef CONFIG_WITH_VALUE_LENGTH
586 unsigned int len;
587#endif
588
589 if (! d->name)
590 continue;
591
592#ifdef CONFIG_WITH_INCLUDEDEP
593 /* Dependencies loaded by includedep are ready for use and we skip
594 the expensive parsing and globbing for them. To avoid wasting
595 lots of time walking the f->deps chain, we will advance D and
596 process all subsequent includedep records. */
597
598 if (d->includedep)
599 {
600 new = d1 = alloc_dep();
601 d1->staticpattern = 0;
602 d1->need_2nd_expansion = 0;
603 d1->includedep = 1;
604 d1->file = lookup_file (d->name);
605 if (d1->file == 0)
606 d1->file = enter_file (d->name);
607
608 while (d->next && d->next->includedep)
609 {
610 d = d->next;
611 d1 = d1->next = alloc_dep();
612 d1->staticpattern = 0;
613 d1->need_2nd_expansion = 0;
614 d1->includedep = 1;
615 d1->file = lookup_file (d->name);
616 if (d1->file == 0)
617 d1->file = enter_file (d->name);
618 }
619 }
620 else
621 {
622#endif
623
624 /* Create the dependency list.
625 If we're not doing 2nd expansion, then it's just the name. We will
626 still need to massage it though. */
627 if (! d->need_2nd_expansion)
628 {
629 p = variable_expand ("");
630 buffer_offset = p - variable_buffer;
631#ifndef CONFIG_WITH_VALUE_LENGTH
632 variable_buffer_output (p, d->name, strlen (d->name) + 1);
633#else
634 len = strcache_get_len (d->name);
635 variable_buffer_output (p, d->name, len + 1);
636#endif
637 p = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. (observed it) */
638 }
639 else
640 {
641 /* If it's from a static pattern rule, convert the patterns into
642 "$*" so they'll expand properly. */
643 if (d->staticpattern)
644 {
645 char *o;
646 char *buffer = variable_expand ("");
647 buffer_offset = buffer - variable_buffer; /* bird */
648
649 o = subst_expand (buffer, d->name, "%", "$*", 1, 2, 0);
650 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
651
652 d->name = strcache_add_len (buffer, o - buffer);
653 d->staticpattern = 0; /* Clear staticpattern so that we don't
654 re-expand %s below. */
655 }
656
657 /* We are going to do second expansion so initialize file variables
658 for the file. Since the stem for static pattern rules comes from
659 individual dep lines, we will temporarily set f->stem to d->stem.
660 */
661 if (!initialized)
662 {
663 initialize_file_variables (f, 0);
664 initialized = 1;
665 }
666
667 if (d->stem != 0)
668 f->stem = d->stem;
669
670 set_file_variables (f);
671
672#ifndef CONFIG_WITH_VALUE_LENGTH
673 p = variable_expand_for_file (d->name, f);
674#else
675 len = strcache_get_len (d->name);
676 p = variable_expand_for_file_2 (NULL, d->name, len, f, &len);
677#endif
678
679 if (d->stem != 0)
680 f->stem = file_stem;
681 }
682
683 /* Parse the prerequisites. */
684 new = parse_prereqs (p);
685
686 /* If this dep list was from a static pattern rule, expand the %s. We
687 use patsubst_expand to translate the prerequisites' patterns into
688 plain prerequisite names. */
689 if (new && d->staticpattern)
690 {
691 const char *pattern = "%";
692 char *buffer = variable_expand ("");
693 const size_t buffer_offset = buffer - variable_buffer; /* bird */
694 struct dep *dp = new, *dl = 0;
695
696 while (dp != 0)
697 {
698 char *percent;
699#ifndef KMK
700 int nl = strlen (dp->name) + 1;
701 char *nm = alloca (nl);
702 memcpy (nm, dp->name, nl);
703 percent = find_percent (nm);
704#else /* KMK - don't make a stack copy unless it's actually required! */
705 unsigned int nl = strcache_get_len (dp->name);
706 char *nm;
707 percent = memchr (dp->name, '%', nl);
708 if (percent)
709 {
710 nm = alloca (nl + 1);
711 memcpy (nm, dp->name, nl + 1);
712 percent = find_percent (nm);
713 }
714#endif /* KMK */
715 if (percent)
716 {
717 char *o;
718
719 /* We have to handle empty stems specially, because that
720 would be equivalent to $(patsubst %,dp->name,) which
721 will always be empty. */
722 if (d->stem[0] == '\0')
723 {
724 memmove (percent, percent+1, strlen (percent));
725 o = variable_buffer_output (buffer, nm, strlen (nm) + 1);
726 }
727 else
728 o = patsubst_expand_pat (buffer, d->stem, pattern, nm,
729 pattern+1, percent+1);
730 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
731
732
733 /* If the name expanded to the empty string, ignore it. */
734 if (buffer[0] == '\0')
735 {
736 struct dep *df = dp;
737 if (dp == new)
738 dp = new = new->next;
739 else
740 dp = dl->next = dp->next;
741 free_dep (df);
742 continue;
743 }
744
745 /* Save the name. */
746 dp->name = strcache_add_len (buffer, o - buffer);
747 }
748 dl = dp;
749 dp = dp->next;
750 }
751 }
752
753 /* Enter them as files. */
754 for (d1 = new; d1 != 0; d1 = d1->next)
755 {
756 d1->file = lookup_file (d1->name);
757 if (d1->file == 0)
758 d1->file = enter_file (d1->name);
759 d1->name = 0;
760 d1->staticpattern = 0;
761 d1->need_2nd_expansion = 0;
762 }
763
764#ifdef CONFIG_WITH_INCLUDEDEP
765 }
766#endif
767
768 /* Add newly parsed deps to f->deps. If this is the last dependency
769 line and this target has commands then put it in front so the
770 last dependency line (the one with commands) ends up being the
771 first. This is important because people expect $< to hold first
772 prerequisite from the rule with commands. If it is not the last
773 dependency line or the rule does not have commands then link it
774 at the end so it appears in makefile order. */
775
776 if (new != 0)
777 {
778 if (d->next == 0 && last_dep_has_cmds)
779 {
780 struct dep **d_ptr;
781 for (d_ptr = &new; *d_ptr; d_ptr = &(*d_ptr)->next)
782 ;
783
784 *d_ptr = f->deps;
785 f->deps = new;
786 }
787 else
788 {
789 struct dep **d_ptr;
790 for (d_ptr = &f->deps; *d_ptr; d_ptr = &(*d_ptr)->next)
791 ;
792
793 *d_ptr = new;
794 }
795 }
796 }
797
798 free_dep_chain (old);
799}
800
801/* For each dependency of each file, make the `struct dep' point
802 at the appropriate `struct file' (which may have to be created).
803
804 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
805 and various other special targets. */
806
807void
808snap_deps (void)
809{
810 struct file *f;
811 struct file *f2;
812 struct dep *d;
813 struct file **file_slot_0;
814 struct file **file_slot;
815 struct file **file_end;
816
817 /* Perform second expansion and enter each dependency name as a file. */
818
819 /* Expand .SUFFIXES first; it's dependencies are used for $$* calculation. */
820 for (f = lookup_file (".SUFFIXES"); f != 0; f = f->prev)
821 expand_deps (f);
822
823#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
824 /* Perform 2nd target expansion on files which requires this. This will
825 be re-inserting (delete+insert) hash table entries so we have to use
826 hash_dump(). */
827 if (second_target_expansion)
828 {
829# ifdef KMK /* turn on warnings here. */
830 int save = warn_undefined_variables_flag;
831 warn_undefined_variables_flag = 1;
832# endif
833
834 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
835 file_end = file_slot_0 + files.ht_fill;
836 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
837 for (f = *file_slot; f != 0; f = f->prev)
838 if (f->need_2nd_target_expansion)
839 do_2nd_target_expansion (f);
840 free (file_slot_0);
841
842# ifdef KMK
843 warn_undefined_variables_flag = save;
844# endif
845
846 /* Disable second target expansion now since we won't expand files
847 entered after this point. (Saves CPU cycles in enter_file()). */
848 second_target_expansion = 0;
849 }
850#endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
851
852#ifdef CONFIG_WITH_INCLUDEDEP
853 /* Process any queued includedep files. Since includedep is supposed
854 to be *simple* stuff, we can do this after the second target expansion
855 and thereby save a little time. */
856 incdep_flush_and_term ();
857#endif
858
859 /* For every target that's not .SUFFIXES, expand its dependencies.
860 We must use hash_dump (), because within this loop we might add new files
861 to the table, possibly causing an in-situ table expansion. */
862 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
863 file_end = file_slot_0 + files.ht_fill;
864 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
865 for (f = *file_slot; f != 0; f = f->prev)
866 if (strcmp (f->name, ".SUFFIXES") != 0)
867 expand_deps (f);
868 free (file_slot_0);
869
870 /* Now manage all the special targets. */
871
872 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
873 for (d = f->deps; d != 0; d = d->next)
874 for (f2 = d->file; f2 != 0; f2 = f2->prev)
875 f2->precious = 1;
876
877 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
878 for (d = f->deps; d != 0; d = d->next)
879 for (f2 = d->file; f2 != 0; f2 = f2->prev)
880 f2->low_resolution_time = 1;
881
882 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
883 for (d = f->deps; d != 0; d = d->next)
884 for (f2 = d->file; f2 != 0; f2 = f2->prev)
885 {
886 /* Mark this file as phony nonexistent target. */
887 f2->phony = 1;
888 f2->is_target = 1;
889 f2->last_mtime = NONEXISTENT_MTIME;
890 f2->mtime_before_update = NONEXISTENT_MTIME;
891 }
892
893 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
894 /* Mark .INTERMEDIATE deps as intermediate files. */
895 for (d = f->deps; d != 0; d = d->next)
896 for (f2 = d->file; f2 != 0; f2 = f2->prev)
897 f2->intermediate = 1;
898 /* .INTERMEDIATE with no deps does nothing.
899 Marking all files as intermediates is useless since the goal targets
900 would be deleted after they are built. */
901
902 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
903 /* Mark .SECONDARY deps as both intermediate and secondary. */
904 if (f->deps)
905 for (d = f->deps; d != 0; d = d->next)
906 for (f2 = d->file; f2 != 0; f2 = f2->prev)
907 f2->intermediate = f2->secondary = 1;
908 /* .SECONDARY with no deps listed marks *all* files that way. */
909 else
910 {
911 all_secondary = 1;
912 hash_map (&files, set_intermediate);
913 }
914
915 f = lookup_file (".EXPORT_ALL_VARIABLES");
916 if (f != 0 && f->is_target)
917 export_all_variables = 1;
918
919 f = lookup_file (".IGNORE");
920 if (f != 0 && f->is_target)
921 {
922 if (f->deps == 0)
923 ignore_errors_flag = 1;
924 else
925 for (d = f->deps; d != 0; d = d->next)
926 for (f2 = d->file; f2 != 0; f2 = f2->prev)
927 f2->command_flags |= COMMANDS_NOERROR;
928 }
929
930 f = lookup_file (".SILENT");
931 if (f != 0 && f->is_target)
932 {
933 if (f->deps == 0)
934 silent_flag = 1;
935 else
936 for (d = f->deps; d != 0; d = d->next)
937 for (f2 = d->file; f2 != 0; f2 = f2->prev)
938 f2->command_flags |= COMMANDS_SILENT;
939 }
940
941 f = lookup_file (".NOTPARALLEL");
942 if (f != 0 && f->is_target)
943#ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
944 not_parallel = 1;
945#else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
946 {
947 if (f->deps == 0)
948 {
949 DB (DB_KMK, (_("not_parallel -1\n")));
950 not_parallel = -1;
951 }
952 else
953 for (d = f->deps; d != 0; d = d->next)
954 for (f2 = d->file; f2 != 0; f2 = f2->prev)
955 f2->command_flags |= COMMANDS_NOTPARALLEL;
956 }
957#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
958
959#ifndef NO_MINUS_C_MINUS_O
960 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
961 /* This needs more work: what if the user sets this in the makefile?
962 if (posix_pedantic)
963 define_variable (STRING_SIZE_TUPLE("OUTPUT_OPTION"), "", o_default, 1);
964 */
965#endif
966
967 /* Remember that we've done this. */
968 snapped_deps = 1;
969}
970
971
972/* Set the `command_state' member of FILE and all its `also_make's. */
973
974void
975set_command_state (struct file *file, enum cmd_state state)
976{
977 struct dep *d;
978
979 file->command_state = state;
980
981 for (d = file->also_make; d != 0; d = d->next)
982 d->file->command_state = state;
983
984#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
985 if (file->multi_head)
986 for (file = file->multi_head; file != 0; file = file->multi_next)
987 file->command_state = state;
988#endif
989}
990
991
992/* Convert an external file timestamp to internal form. */
993
994FILE_TIMESTAMP
995file_timestamp_cons (const char *fname, time_t s, int ns)
996{
997 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
998 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
999 FILE_TIMESTAMP ts = product + offset;
1000
1001 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
1002 && product <= ts && ts <= ORDINARY_MTIME_MAX))
1003 {
1004 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1005 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
1006 file_timestamp_sprintf (buf, ts);
1007 error (NILF, _("%s: Timestamp out of range; substituting %s"),
1008 fname ? fname : _("Current time"), buf);
1009 }
1010
1011 return ts;
1012}
1013
1014
1015/* Return the current time as a file timestamp, setting *RESOLUTION to
1016 its resolution. */
1017FILE_TIMESTAMP
1018file_timestamp_now (int *resolution)
1019{
1020 int r;
1021 time_t s;
1022 int ns;
1023
1024 /* Don't bother with high-resolution clocks if file timestamps have
1025 only one-second resolution. The code below should work, but it's
1026 not worth the hassle of debugging it on hosts where it fails. */
1027#if FILE_TIMESTAMP_HI_RES
1028# if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
1029 {
1030 struct timespec timespec;
1031 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
1032 {
1033 r = 1;
1034 s = timespec.tv_sec;
1035 ns = timespec.tv_nsec;
1036 goto got_time;
1037 }
1038 }
1039# endif
1040# if HAVE_GETTIMEOFDAY
1041 {
1042 struct timeval timeval;
1043 if (gettimeofday (&timeval, 0) == 0)
1044 {
1045 r = 1000;
1046 s = timeval.tv_sec;
1047 ns = timeval.tv_usec * 1000;
1048 goto got_time;
1049 }
1050 }
1051# endif
1052#endif
1053
1054 r = 1000000000;
1055 s = time ((time_t *) 0);
1056 ns = 0;
1057
1058#if FILE_TIMESTAMP_HI_RES
1059 got_time:
1060#endif
1061 *resolution = r;
1062 return file_timestamp_cons (0, s, ns);
1063}
1064
1065/* Place into the buffer P a printable representation of the file
1066 timestamp TS. */
1067void
1068file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
1069{
1070 time_t t = FILE_TIMESTAMP_S (ts);
1071 struct tm *tm = localtime (&t);
1072
1073 if (tm)
1074 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
1075 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1076 tm->tm_hour, tm->tm_min, tm->tm_sec);
1077 else if (t < 0)
1078 sprintf (p, "%ld", (long) t);
1079 else
1080 sprintf (p, "%lu", (unsigned long) t);
1081 p += strlen (p);
1082
1083 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
1084 know the actual timestamp resolution, since clock_getres applies only to
1085 local times, whereas this timestamp might come from a remote filesystem.
1086 So removing trailing zeros is the best guess that we can do. */
1087 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
1088 p += strlen (p) - 1;
1089 while (*p == '0')
1090 p--;
1091 p += *p != '.';
1092
1093 *p = '\0';
1094}
1095
1096
1097/* Print the data base of files. */
1098
1099static void
1100print_file (const void *item)
1101{
1102 const struct file *f = item;
1103 struct dep *d;
1104 struct dep *ood = 0;
1105
1106 putchar ('\n');
1107 if (!f->is_target)
1108 puts (_("# Not a target:"));
1109#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1110 if (f->multi_head)
1111 {
1112 const struct file *f2;
1113 if (f->multi_head == f)
1114 {
1115 int multi_maybe = -1;
1116 assert (!f->multi_maybe);
1117 assert (!f->double_colon);
1118
1119 printf ("%s", f->name);
1120 for (f2 = f->multi_next; f2 != 0; f2 = f2->multi_next)
1121 {
1122 printf (" %s%s", f2->multi_maybe != multi_maybe
1123 ? f2->multi_maybe ? "+| " : "+ " : "",
1124 f2->name);
1125 multi_maybe = f2->multi_maybe;
1126 }
1127 putchar (':');
1128 }
1129 else
1130 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1131 }
1132 else
1133#endif
1134 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1135
1136 /* Print all normal dependencies; note any order-only deps. */
1137 for (d = f->deps; d != 0; d = d->next)
1138 if (! d->ignore_mtime)
1139 printf (" %s", dep_name (d));
1140 else if (! ood)
1141 ood = d;
1142
1143 /* Print order-only deps, if we have any. */
1144 if (ood)
1145 {
1146 printf (" | %s", dep_name (ood));
1147 for (d = ood->next; d != 0; d = d->next)
1148 if (d->ignore_mtime)
1149 printf (" %s", dep_name (d));
1150 }
1151
1152 putchar ('\n');
1153
1154#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1155 if (f->multi_head && f->multi_head != f)
1156 {
1157 const struct file *f2;
1158 fputs (_("# In multi target list:"), stdout);
1159 for (f2 = f->multi_head; f2 != 0; f2 = f2->multi_next)
1160 printf (" %s%s", f2->name, f == f2 ? "(*)" : "");
1161 putchar ('\n');
1162 if (f->multi_maybe)
1163 puts (_("# File is an optional multi target member."));
1164 }
1165#endif
1166
1167 if (f->precious)
1168 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
1169 if (f->phony)
1170 puts (_("# Phony target (prerequisite of .PHONY)."));
1171 if (f->cmd_target)
1172 puts (_("# Command-line target."));
1173 if (f->dontcare)
1174 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
1175 puts (f->tried_implicit
1176 ? _("# Implicit rule search has been done.")
1177 : _("# Implicit rule search has not been done."));
1178 if (f->stem != 0)
1179 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
1180 if (f->intermediate)
1181 puts (_("# File is an intermediate prerequisite."));
1182 if (f->also_make != 0)
1183 {
1184 fputs (_("# Also makes:"), stdout);
1185 for (d = f->also_make; d != 0; d = d->next)
1186 printf (" %s", dep_name (d));
1187 putchar ('\n');
1188 }
1189 if (f->last_mtime == UNKNOWN_MTIME)
1190 puts (_("# Modification time never checked."));
1191 else if (f->last_mtime == NONEXISTENT_MTIME)
1192 puts (_("# File does not exist."));
1193 else if (f->last_mtime == OLD_MTIME)
1194 puts (_("# File is very old."));
1195 else
1196 {
1197 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1198 file_timestamp_sprintf (buf, f->last_mtime);
1199 printf (_("# Last modified %s\n"), buf);
1200 }
1201 puts (f->updated
1202 ? _("# File has been updated.") : _("# File has not been updated."));
1203 switch (f->command_state)
1204 {
1205 case cs_running:
1206 puts (_("# Commands currently running (THIS IS A BUG)."));
1207 break;
1208 case cs_deps_running:
1209 puts (_("# Dependencies commands running (THIS IS A BUG)."));
1210 break;
1211 case cs_not_started:
1212 case cs_finished:
1213 switch (f->update_status)
1214 {
1215 case -1:
1216 break;
1217 case 0:
1218 puts (_("# Successfully updated."));
1219 break;
1220 case 1:
1221 assert (question_flag);
1222 puts (_("# Needs to be updated (-q is set)."));
1223 break;
1224 case 2:
1225 puts (_("# Failed to be updated."));
1226 break;
1227 default:
1228 puts (_("# Invalid value in `update_status' member!"));
1229 fflush (stdout);
1230 fflush (stderr);
1231 abort ();
1232 }
1233 break;
1234 default:
1235 puts (_("# Invalid value in `command_state' member!"));
1236 fflush (stdout);
1237 fflush (stderr);
1238 abort ();
1239 }
1240
1241 if (f->variables != 0)
1242 print_file_variables (f);
1243
1244 if (f->cmds != 0)
1245 print_commands (f->cmds);
1246
1247 if (f->prev)
1248 print_file ((const void *) f->prev);
1249}
1250
1251void
1252print_file_data_base (void)
1253{
1254 puts (_("\n# Files"));
1255
1256 hash_map (&files, print_file);
1257
1258 fputs (_("\n# files hash-table stats:\n# "), stdout);
1259 hash_print_stats (&files, stdout);
1260}
1261
1262
1263/* Verify the integrity of the data base of files. */
1264
1265#define VERIFY_CACHED(_p,_n) \
1266 do{\
1267 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1268 printf ("%s: Field %s not cached: %s\n", _p->name, # _n, _p->_n); \
1269 }while(0)
1270
1271static void
1272verify_file (const void *item)
1273{
1274 const struct file *f = item;
1275 const struct dep *d;
1276
1277 VERIFY_CACHED (f, name);
1278 VERIFY_CACHED (f, hname);
1279 VERIFY_CACHED (f, vpath);
1280 VERIFY_CACHED (f, stem);
1281
1282 /* Check the deps. */
1283 for (d = f->deps; d != 0; d = d->next)
1284 {
1285 VERIFY_CACHED (d, name);
1286 VERIFY_CACHED (d, stem);
1287 }
1288}
1289
1290void
1291verify_file_data_base (void)
1292{
1293 hash_map (&files, verify_file);
1294}
1295
1296#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1297
1298char *
1299build_target_list (char *value)
1300{
1301 static unsigned long last_targ_count = 0;
1302
1303 if (files.ht_fill != last_targ_count)
1304 {
1305 unsigned long max = EXPANSION_INCREMENT (strlen (value));
1306 unsigned long len;
1307 char *p;
1308 struct file **fp = (struct file **) files.ht_vec;
1309 struct file **end = &fp[files.ht_size];
1310
1311 /* Make sure we have at least MAX bytes in the allocated buffer. */
1312 value = xrealloc (value, max);
1313
1314 p = value;
1315 len = 0;
1316 for (; fp < end; ++fp)
1317 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1318 {
1319 struct file *f = *fp;
1320 int l = strlen (f->name);
1321
1322 len += l + 1;
1323 if (len > max)
1324 {
1325 unsigned long off = p - value;
1326
1327 max += EXPANSION_INCREMENT (l + 1);
1328 value = xrealloc (value, max);
1329 p = &value[off];
1330 }
1331
1332 memcpy (p, f->name, l);
1333 p += l;
1334 *(p++) = ' ';
1335 }
1336 *(p-1) = '\0';
1337
1338 last_targ_count = files.ht_fill;
1339 }
1340
1341 return value;
1342}
1343
1344void
1345init_hash_files (void)
1346{
1347#ifdef KMK
1348 hash_init (&files, /*65535*/ 32755, file_hash_1, file_hash_2, file_hash_cmp);
1349#else
1350 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1351#endif
1352}
1353
1354/* EOF */
Note: See TracBrowser for help on using the repository browser.