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

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

kmk: added strcache2_get_ptr_hash for more efficent hashing by string pool users; replacing hash1 with that and hash2 with hash1, thus avoiding unnecessary hash2 calculations.

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