Tagging 0.1.5-p2 (again)
[kbuild-mirror.git] / src / kmk / file.c
blobf43ead40fda8ac68c6342c485cb666db74b83c5a
1 /* Target file management for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "make.h"
21 #include <assert.h>
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 #ifdef CONFIG_WITH_STRCACHE2
31 # include <stddef.h>
32 #endif
35 /* Remember whether snap_deps has been invoked: we need this to be sure we
36 don't add new rules (via $(eval ...)) afterwards. In the future it would
37 be nice to support this, but it means we'd need to re-run snap_deps() or
38 at least its functionality... it might mean changing snap_deps() to be run
39 per-file, so we can invoke it after the eval... or remembering which files
40 in the hash have been snapped (a new boolean flag?) and having snap_deps()
41 only work on files which have not yet been snapped. */
42 int snapped_deps = 0;
44 /* Hash table of files the makefile knows how to make. */
46 #ifndef CONFIG_WITH_STRCACHE2
47 static unsigned long
48 file_hash_1 (const void *key)
50 return_ISTRING_HASH_1 (((struct file const *) key)->hname);
53 static unsigned long
54 file_hash_2 (const void *key)
56 return_ISTRING_HASH_2 (((struct file const *) key)->hname);
58 #endif /* !CONFIG_WITH_STRCACHE2 */
60 static int
61 file_hash_cmp (const void *x, const void *y)
63 #ifndef CONFIG_WITH_STRCACHE2
64 return_ISTRING_COMPARE (((struct file const *) x)->hname,
65 ((struct file const *) y)->hname);
66 #else /* CONFIG_WITH_STRCACHE2 */
67 return ((struct file const *) x)->hname
68 == ((struct file const *) y)->hname ? 0 : -1;
69 #endif /* CONFIG_WITH_STRCACHE2 */
72 #ifndef FILE_BUCKETS
73 #define FILE_BUCKETS 1007
74 #endif
75 static struct hash_table files;
77 /* Whether or not .SECONDARY with no prerequisites was given. */
78 static int all_secondary = 0;
80 /* Access the hash table of all file records.
81 lookup_file given a name, return the struct file * for that name,
82 or nil if there is none.
85 #ifndef CONFIG_WITH_STRCACHE2
86 struct file *
87 lookup_file (const char *name)
88 #else /* CONFIG_WITH_STRCACHE2 */
89 MY_INLINE struct file *
90 lookup_file_common (const char *name, int cached)
91 #endif /* CONFIG_WITH_STRCACHE2 */
93 struct file *f;
94 struct file file_key;
95 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
96 char *lname;
97 #endif
99 assert (*name != '\0');
101 /* This is also done in parse_file_seq, so this is redundant
102 for names read from makefiles. It is here for names passed
103 on the command line. */
104 #ifdef VMS
105 # ifndef WANT_CASE_SENSITIVE_TARGETS
106 if (*name != '.')
108 const char *n;
109 char *ln;
110 lname = xstrdup (name);
111 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
112 *ln = isupper ((unsigned char)*n) ? tolower ((unsigned char)*n) : *n;
113 *ln = '\0';
114 name = lname;
116 # endif
118 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
119 name += 2;
120 #endif
121 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
123 name += 2;
124 while (*name == '/')
125 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
126 ++name;
129 if (*name == '\0')
130 /* It was all slashes after a dot. */
131 #if defined(VMS)
132 name = "[]";
133 #elif defined(_AMIGA)
134 name = "";
135 #else
136 name = "./";
137 #endif
139 #ifndef CONFIG_WITH_STRCACHE2
140 file_key.hname = name;
141 f = hash_find_item (&files, &file_key);
142 #else /* CONFIG_WITH_STRCACHE2 */
143 if (!cached)
145 file_key.hname = strcache2_lookup_file (&file_strcache, name, strlen (name));
146 if (file_key.hname)
147 f = hash_find_item_strcached (&files, &file_key);
148 else
149 f = NULL;
151 else
153 file_key.hname = name;
154 f = hash_find_item_strcached (&files, &file_key);
157 #endif /* CONFIG_WITH_STRCACHE2 */
158 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
159 if (*name != '.')
160 free (lname);
161 #endif
163 return f;
166 #ifdef CONFIG_WITH_STRCACHE2
167 /* Given a name, return the struct file * for that name,
168 or nil if there is none. */
170 struct file *
171 lookup_file (const char *name)
173 return lookup_file_common (name, 0 /* cached */);
176 /* Given a name in the strcache, return the struct file * for that name,
177 or nil if there is none. */
178 struct file *
179 lookup_file_cached (const char *name)
181 assert (strcache_iscached (name));
182 return lookup_file_common (name, 1 /* cached */);
184 #endif /* CONFIG_WITH_STRCACHE2 */
187 /* Look up a file record for file NAME and return it.
188 Create a new record if one doesn't exist. NAME will be stored in the
189 new record so it should be constant or in the strcache etc.
192 struct file *
193 enter_file (const char *name)
195 struct file *f;
196 struct file *new;
197 struct file **file_slot;
198 struct file file_key;
200 assert (*name != '\0');
201 assert (strcache_iscached (name));
203 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
204 if (*name != '.')
206 const char *n;
207 char *lname, *ln;
208 lname = xstrdup (name);
209 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
210 if (isupper ((unsigned char)*n))
211 *ln = tolower ((unsigned char)*n);
212 else
213 *ln = *n;
215 *ln = '\0';
216 name = strcache_add (lname);
217 free (lname);
219 #endif
221 file_key.hname = name;
222 #ifndef CONFIG_WITH_STRCACHE2
223 file_slot = (struct file **) hash_find_slot (&files, &file_key);
224 #else /* CONFIG_WITH_STRCACHE2 */
225 file_slot = (struct file **) hash_find_slot_strcached (&files, &file_key);
226 #endif /* CONFIG_WITH_STRCACHE2 */
227 f = *file_slot;
228 if (! HASH_VACANT (f) && !f->double_colon)
229 return f;
231 #ifndef CONFIG_WITH_ALLOC_CACHES
232 new = xmalloc (sizeof (struct file));
233 memset (new, '\0', sizeof (struct file));
234 #else
235 new = alloccache_calloc (&file_cache);
236 #endif
237 new->name = new->hname = name;
238 new->update_status = -1;
240 if (HASH_VACANT (f))
242 new->last = new;
243 hash_insert_at (&files, new, file_slot);
245 else
247 /* There is already a double-colon entry for this file. */
248 new->double_colon = f;
249 f->last->prev = new;
250 f->last = new;
253 #ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
254 /* Check if the name needs 2nd expansion or not. */
255 if (second_target_expansion && strchr (name, '$') != NULL)
256 new->need_2nd_target_expansion = 1;
257 #endif
259 return new;
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. */
266 void
267 rehash_file (struct file *from_file, const char *to_hname)
269 struct file file_key;
270 struct file **file_slot;
271 struct file *to_file;
272 struct file *deleted_file;
273 struct file *f;
275 #ifdef CONFIG_WITH_STRCACHE2
276 assert (strcache_iscached (to_hname));
277 assert (strcache_iscached (from_file->hname));
278 #endif
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;
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 ();
293 /* Remove the "from" file from the hash. */
294 #ifndef CONFIG_WITH_STRCACHE2
295 deleted_file = hash_delete (&files, from_file);
296 #else
297 deleted_file = hash_delete_strcached (&files, from_file);
298 #endif
299 if (deleted_file != from_file)
300 /* from_file isn't the one stored in files */
301 abort ();
303 /* Find where the newly renamed file will go in the hash. */
304 file_key.hname = to_hname;
305 #ifndef CONFIG_WITH_STRCACHE2
306 file_slot = (struct file **) hash_find_slot (&files, &file_key);
307 #else /* CONFIG_WITH_STRCACHE2 */
308 file_slot = (struct file **) hash_find_slot_strcached (&files, &file_key);
309 #endif /* CONFIG_WITH_STRCACHE2 */
310 to_file = *file_slot;
312 /* Change the hash name for this file. */
313 from_file->hname = to_hname;
314 for (f = from_file->double_colon; f != 0; f = f->prev)
315 f->hname = to_hname;
317 /* If the new name doesn't exist yet just set it to the renamed file. */
318 if (HASH_VACANT (to_file))
320 hash_insert_at (&files, from_file, file_slot);
321 return;
324 /* TO_FILE already exists under TO_HNAME.
325 We must retain TO_FILE and merge FROM_FILE into it. */
327 if (from_file->cmds != 0)
329 if (to_file->cmds == 0)
330 to_file->cmds = from_file->cmds;
331 else if (from_file->cmds != to_file->cmds)
333 /* We have two sets of commands. We will go with the
334 one given in the rule explicitly mentioning this name,
335 but give a message to let the user know what's going on. */
336 if (to_file->cmds->fileinfo.filenm != 0)
337 error (&from_file->cmds->fileinfo,
338 _("Recipe was specified for file `%s' at %s:%lu,"),
339 from_file->name, to_file->cmds->fileinfo.filenm,
340 to_file->cmds->fileinfo.lineno);
341 else
342 error (&from_file->cmds->fileinfo,
343 _("Recipe for file `%s' was found by implicit rule search,"),
344 from_file->name);
345 error (&from_file->cmds->fileinfo,
346 _("but `%s' is now considered the same file as `%s'."),
347 from_file->name, to_hname);
348 error (&from_file->cmds->fileinfo,
349 _("Recipe for `%s' will be ignored in favor of the one for `%s'."),
350 to_hname, from_file->name);
354 /* Merge the dependencies of the two files. */
356 if (to_file->deps == 0)
357 to_file->deps = from_file->deps;
358 else
360 struct dep *deps = to_file->deps;
361 while (deps->next != 0)
362 deps = deps->next;
363 deps->next = from_file->deps;
366 merge_variable_set_lists (&to_file->variables, from_file->variables);
368 if (to_file->double_colon && from_file->is_target && !from_file->double_colon)
369 fatal (NILF, _("can't rename single-colon `%s' to double-colon `%s'"),
370 from_file->name, to_hname);
371 if (!to_file->double_colon && from_file->double_colon)
373 if (to_file->is_target)
374 fatal (NILF, _("can't rename double-colon `%s' to single-colon `%s'"),
375 from_file->name, to_hname);
376 else
377 to_file->double_colon = from_file->double_colon;
380 if (from_file->last_mtime > to_file->last_mtime)
381 /* %%% Kludge so -W wins on a file that gets vpathized. */
382 to_file->last_mtime = from_file->last_mtime;
384 to_file->mtime_before_update = from_file->mtime_before_update;
386 #define MERGE(field) to_file->field |= from_file->field
387 MERGE (precious);
388 MERGE (tried_implicit);
389 MERGE (updating);
390 MERGE (updated);
391 MERGE (is_target);
392 MERGE (cmd_target);
393 MERGE (phony);
394 MERGE (ignore_vpath);
395 #undef MERGE
397 from_file->renamed = to_file;
400 /* Rename FILE to NAME. This is not as simple as resetting
401 the `name' member, since it must be put in a new hash bucket,
402 and possibly merged with an existing file called NAME. */
404 void
405 rename_file (struct file *from_file, const char *to_hname)
407 rehash_file (from_file, to_hname);
408 while (from_file)
410 from_file->name = from_file->hname;
411 from_file = from_file->prev;
415 #ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
416 /* Performs secondary target name expansion and then renames
417 the file using rename_file. */
418 static void
419 do_2nd_target_expansion (struct file *f)
421 unsigned int len;
422 char *tmp_name = allocated_variable_expand_2 (
423 f->name, strcache2_get_len (&file_strcache, f->name), &len);
424 const char *name = strcache_add_len (tmp_name, len);
425 free (tmp_name);
426 rename_file (f, name);
428 #endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
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. */
435 void
436 remove_intermediates (int sig)
438 struct file **file_slot;
439 struct file **file_end;
440 int doneany = 0;
442 /* If there's no way we will ever remove anything anyway, punt early. */
443 if (question_flag || touch_flag || all_secondary)
444 return;
446 if (sig && just_print_flag)
447 return;
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))
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)
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
471 status = unlink (f->name);
472 if (status < 0 && errno == ENOENT)
473 continue;
475 if (!f->dontcare)
477 if (sig)
478 error (NILF, _("*** Deleting intermediate file `%s'"), f->name);
479 else
481 if (! doneany)
482 DB (DB_BASIC, (_("Removing intermediate files...\n")));
483 if (!silent_flag)
485 if (! doneany)
487 fputs ("rm ", stdout);
488 doneany = 1;
490 else
491 putchar (' ');
492 fputs (f->name, stdout);
493 fflush (stdout);
496 if (status < 0)
497 perror_with_name ("unlink: ", f->name);
502 if (doneany && !sig)
504 putchar ('\n');
505 fflush (stdout);
509 struct dep *
510 parse_prereqs (char *p)
512 #ifndef CONFIG_WITH_ALLOC_CACHES
513 struct dep *new = (struct dep *)
514 multi_glob (parse_file_seq (&p, '|', sizeof (struct dep), 1),
515 sizeof (struct dep));
516 #else
517 struct dep *new = (struct dep *)
518 multi_glob (parse_file_seq (&p, '|', &dep_cache, 1), &dep_cache);
519 #endif
521 if (*p)
523 /* Files that follow '|' are "order-only" prerequisites that satisfy the
524 dependency by existing: their modification times are irrelevant. */
525 struct dep *ood;
527 ++p;
528 #ifndef CONFIG_WITH_ALLOC_CACHES
529 ood = (struct dep *)
530 multi_glob (parse_file_seq (&p, '\0', sizeof (struct dep), 1),
531 sizeof (struct dep));
532 #else
533 ood = (struct dep *)
534 multi_glob (parse_file_seq (&p, '\0', &dep_cache, 1), &dep_cache);
535 #endif
537 if (! new)
538 new = ood;
539 else
541 struct dep *dp;
542 for (dp = new; dp->next != NULL; dp = dp->next)
544 dp->next = ood;
547 for (; ood != NULL; ood = ood->next)
548 ood->ignore_mtime = 1;
551 return new;
554 /* Set the intermediate flag. */
556 static void
557 set_intermediate (const void *item)
559 struct file *f = (struct file *) item;
560 f->intermediate = 1;
563 /* Expand and parse each dependency line. */
564 static void
565 expand_deps (struct file *f)
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;
573 f->updating = 0;
574 f->deps = 0;
576 for (d = old; d != 0; d = d->next)
578 struct dep *new, *d1;
579 char *p;
580 #ifdef CONFIG_WITH_STRCACHE2
581 unsigned int len;
582 #endif
584 if (! d->name)
585 continue;
587 #ifdef CONFIG_WITH_INCLUDEDEP
588 /* Dependencies loaded by includedep are ready for use and we skip
589 the expensive parsing and globbing for them. To avoid wasting
590 lots of time walking the f->deps chain, we will advance D and
591 process all subsequent includedep records. */
593 if (d->includedep)
595 new = d1 = alloc_dep();
596 d1->staticpattern = 0;
597 d1->need_2nd_expansion = 0;
598 d1->includedep = 1;
599 d1->file = lookup_file (d->name);
600 if (d1->file == 0)
601 d1->file = enter_file (d->name);
603 while (d->next && d->next->includedep)
605 d = d->next;
606 d1 = d1->next = alloc_dep();
607 d1->staticpattern = 0;
608 d1->need_2nd_expansion = 0;
609 d1->includedep = 1;
610 d1->file = lookup_file (d->name);
611 if (d1->file == 0)
612 d1->file = enter_file (d->name);
615 else
617 #endif
619 /* Create the dependency list.
620 If we're not doing 2nd expansion, then it's just the name. We will
621 still need to massage it though. */
622 if (! d->need_2nd_expansion)
624 p = variable_expand ("");
625 #ifndef CONFIG_WITH_STRCACHE2
626 variable_buffer_output (p, d->name, strlen (d->name) + 1);
627 #else
628 len = strcache2_get_len (&file_strcache, d->name);
629 variable_buffer_output (p, d->name, len + 1);
630 #endif
631 p = variable_buffer;
633 else
635 /* If it's from a static pattern rule, convert the patterns into
636 "$*" so they'll expand properly. */
637 if (d->staticpattern)
639 char *o;
640 char *buffer = variable_expand ("");
642 o = subst_expand (buffer, d->name, "%", "$*", 1, 2, 0);
643 buffer = variable_buffer; /* bird - variable_buffer may have been reallocated. */
645 d->name = strcache_add_len (variable_buffer,
646 o - variable_buffer);
647 d->staticpattern = 0; /* Clear staticpattern so that we don't
648 re-expand %s below. */
651 /* We are going to do second expansion so initialize file variables
652 for the file. Since the stem for static pattern rules comes from
653 individual dep lines, we will temporarily set f->stem to d->stem.
655 if (!initialized)
657 initialize_file_variables (f, 0);
658 initialized = 1;
661 if (d->stem != 0)
662 f->stem = d->stem;
664 #if defined(CONFIG_WITH_COMMANDS_FUNC) || defined (CONFIG_WITH_DOT_MUST_MAKE)
665 set_file_variables (f, 0 /* real call, f->deps == 0 so we're ok. */);
666 #else
667 set_file_variables (f);
668 #endif
670 #if !defined (CONFIG_WITH_VALUE_LENGTH) || !defined (CONFIG_WITH_STRCACHE2)
671 p = variable_expand_for_file (d->name, f);
672 #else
673 len = strcache2_get_len (&file_strcache, d->name);
674 p = variable_expand_for_file_2 (NULL, d->name, len, f, &len);
675 #endif
677 if (d->stem != 0)
678 f->stem = file_stem;
681 /* Parse the prerequisites. */
682 new = parse_prereqs (p);
684 /* If this dep list was from a static pattern rule, expand the %s. We
685 use patsubst_expand to translate the prerequisites' patterns into
686 plain prerequisite names. */
687 if (new && d->staticpattern)
689 const char *pattern = "%";
690 char *buffer = variable_expand ("");
691 struct dep *dp = new, *dl = 0;
693 while (dp != 0)
695 char *percent;
696 #ifndef KMK
697 int nl = strlen (dp->name) + 1;
698 char *nm = alloca (nl);
699 memcpy (nm, dp->name, nl);
700 percent = find_percent (nm);
701 #else /* KMK - don't make a stack copy unless it's actually required! */
702 unsigned int nl = strcache2_get_len (&file_strcache, dp->name);
703 char *nm;
704 percent = memchr (dp->name, '%', nl);
705 if (percent)
707 nm = alloca (nl + 1);
708 memcpy (nm, dp->name, nl + 1);
709 percent = find_percent (nm);
711 #endif /* KMK */
712 if (percent)
714 char *o;
716 /* We have to handle empty stems specially, because that
717 would be equivalent to $(patsubst %,dp->name,) which
718 will always be empty. */
719 if (d->stem[0] == '\0')
721 memmove (percent, percent+1, strlen (percent));
722 o = variable_buffer_output (buffer, nm, strlen (nm) + 1);
724 else
726 o = patsubst_expand_pat (buffer, d->stem, pattern, nm,
727 pattern+1, percent+1);
728 o = variable_buffer_output (o, "", 1); /* bird fix - patsubst_expand_pat doesn't terminate,
729 the if case does and strcache would appreciate it. */
731 buffer = variable_buffer; /* bird fix - variable_buffer may have been reallocated. */
734 /* If the name expanded to the empty string, ignore it. */
735 if (buffer[0] == '\0')
737 struct dep *df = dp;
738 if (dp == new)
739 dp = new = new->next;
740 else
741 dp = dl->next = dp->next;
742 free_dep (df);
743 continue;
746 /* Save the name. */
747 dp->name = strcache_add_len (buffer, o - buffer - 1); /* bird fix - don't include the terminator. */
749 dl = dp;
750 dp = dp->next;
754 /* Enter them as files. */
755 for (d1 = new; d1 != 0; d1 = d1->next)
757 d1->file = lookup_file (d1->name);
758 if (d1->file == 0)
759 d1->file = enter_file (d1->name);
760 d1->name = 0;
761 d1->staticpattern = 0;
762 d1->need_2nd_expansion = 0;
765 #ifdef CONFIG_WITH_INCLUDEDEP
767 #endif
769 /* Add newly parsed deps to f->deps. If this is the last dependency
770 line and this target has commands then put it in front so the
771 last dependency line (the one with commands) ends up being the
772 first. This is important because people expect $< to hold first
773 prerequisite from the rule with commands. If it is not the last
774 dependency line or the rule does not have commands then link it
775 at the end so it appears in makefile order. */
777 if (new != 0)
779 if (d->next == 0 && last_dep_has_cmds)
781 struct dep **d_ptr;
782 for (d_ptr = &new; *d_ptr; d_ptr = &(*d_ptr)->next)
785 *d_ptr = f->deps;
786 f->deps = new;
788 else
790 struct dep **d_ptr;
791 for (d_ptr = &f->deps; *d_ptr; d_ptr = &(*d_ptr)->next)
794 *d_ptr = new;
799 free_dep_chain (old);
802 /* For each dependency of each file, make the `struct dep' point
803 at the appropriate `struct file' (which may have to be created).
805 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
806 and various other special targets. */
808 void
809 snap_deps (void)
811 struct file *f;
812 struct file *f2;
813 struct dep *d;
814 struct file **file_slot_0;
815 struct file **file_slot;
816 struct file **file_end;
818 /* Perform second expansion and enter each dependency name as a file. */
820 /* Expand .SUFFIXES first; it's dependencies are used for $$* calculation. */
821 for (f = lookup_file (".SUFFIXES"); f != 0; f = f->prev)
822 expand_deps (f);
824 #ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
825 /* Perform 2nd target expansion on files which requires this. This will
826 be re-inserting (delete+insert) hash table entries so we have to use
827 hash_dump(). */
828 if (second_target_expansion)
830 # ifdef KMK /* turn on warnings here. */
831 int save = warn_undefined_variables_flag;
832 warn_undefined_variables_flag = 1;
833 # endif
835 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
836 file_end = file_slot_0 + files.ht_fill;
837 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
838 for (f = *file_slot; f != 0; f = f->prev)
839 if (f->need_2nd_target_expansion)
840 do_2nd_target_expansion (f);
841 free (file_slot_0);
843 # ifdef KMK
844 warn_undefined_variables_flag = save;
845 # endif
847 /* Disable second target expansion now since we won't expand files
848 entered after this point. (Saves CPU cycles in enter_file()). */
849 second_target_expansion = 0;
851 #endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
853 #ifdef CONFIG_WITH_INCLUDEDEP
854 /* Process any queued includedep files. Since includedep is supposed
855 to be *simple* stuff, we can do this after the second target expansion
856 and thereby save a little time. */
857 incdep_flush_and_term ();
858 #endif /* CONFIG_WITH_INCLUDEDEP */
860 /* For every target that's not .SUFFIXES, expand its dependencies.
861 We must use hash_dump (), because within this loop we might add new files
862 to the table, possibly causing an in-situ table expansion. */
863 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
864 file_end = file_slot_0 + files.ht_fill;
865 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
866 for (f = *file_slot; f != 0; f = f->prev)
867 #ifndef CONFIG_WITH_STRCACHE2
868 if (strcmp (f->name, ".SUFFIXES") != 0)
869 #else
870 if (f->name != suffixes_strcached)
871 #endif
872 expand_deps (f);
873 #ifdef KMK
874 /* This is a HACK to work around the still broken test #9 in
875 features/double_colon. It produces the wrong result if the build is
876 parallel because of changed evaluation order. Just make these
877 problematic rules execute in single field till a proper fix is
878 forthcomming... */
880 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
881 if ( (f = *file_slot) != 0
882 && f->double_colon
883 && ( f->double_colon != f
884 || f->last != f))
885 for (f2 = f->double_colon; f2 != 0; f2 = f2->prev)
886 f2->command_flags |= COMMANDS_NOTPARALLEL;
887 #endif /* KMK */
888 free (file_slot_0);
890 /* Now manage all the special targets. */
892 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
893 for (d = f->deps; d != 0; d = d->next)
894 for (f2 = d->file; f2 != 0; f2 = f2->prev)
895 f2->precious = 1;
897 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
898 for (d = f->deps; d != 0; d = d->next)
899 for (f2 = d->file; f2 != 0; f2 = f2->prev)
900 f2->low_resolution_time = 1;
902 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
903 for (d = f->deps; d != 0; d = d->next)
904 for (f2 = d->file; f2 != 0; f2 = f2->prev)
906 /* Mark this file as phony nonexistent target. */
907 f2->phony = 1;
908 f2->is_target = 1;
909 f2->last_mtime = NONEXISTENT_MTIME;
910 f2->mtime_before_update = NONEXISTENT_MTIME;
913 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
914 /* Mark .INTERMEDIATE deps as intermediate files. */
915 for (d = f->deps; d != 0; d = d->next)
916 for (f2 = d->file; f2 != 0; f2 = f2->prev)
917 f2->intermediate = 1;
918 /* .INTERMEDIATE with no deps does nothing.
919 Marking all files as intermediates is useless since the goal targets
920 would be deleted after they are built. */
922 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
923 /* Mark .SECONDARY deps as both intermediate and secondary. */
924 if (f->deps)
925 for (d = f->deps; d != 0; d = d->next)
926 for (f2 = d->file; f2 != 0; f2 = f2->prev)
927 f2->intermediate = f2->secondary = 1;
928 /* .SECONDARY with no deps listed marks *all* files that way. */
929 else
931 all_secondary = 1;
932 hash_map (&files, set_intermediate);
935 f = lookup_file (".EXPORT_ALL_VARIABLES");
936 if (f != 0 && f->is_target)
937 export_all_variables = 1;
939 f = lookup_file (".IGNORE");
940 if (f != 0 && f->is_target)
942 if (f->deps == 0)
943 ignore_errors_flag = 1;
944 else
945 for (d = f->deps; d != 0; d = d->next)
946 for (f2 = d->file; f2 != 0; f2 = f2->prev)
947 f2->command_flags |= COMMANDS_NOERROR;
950 f = lookup_file (".SILENT");
951 if (f != 0 && f->is_target)
953 if (f->deps == 0)
954 silent_flag = 1;
955 else
956 for (d = f->deps; d != 0; d = d->next)
957 for (f2 = d->file; f2 != 0; f2 = f2->prev)
958 f2->command_flags |= COMMANDS_SILENT;
961 f = lookup_file (".NOTPARALLEL");
962 if (f != 0 && f->is_target)
963 #ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
964 not_parallel = 1;
965 #else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
967 if (f->deps == 0)
969 DB (DB_KMK, (_("not_parallel -1\n")));
970 not_parallel = -1;
972 else
973 for (d = f->deps; d != 0; d = d->next)
974 for (f2 = d->file; f2 != 0; f2 = f2->prev)
975 f2->command_flags |= COMMANDS_NOTPARALLEL;
977 #endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
979 #ifndef NO_MINUS_C_MINUS_O
980 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
981 /* This needs more work: what if the user sets this in the makefile?
982 if (posix_pedantic)
983 define_variable (STRING_SIZE_TUPLE("OUTPUT_OPTION"), "", o_default, 1);
985 #endif
987 /* Remember that we've done this. */
988 snapped_deps = 1;
991 /* Set the `command_state' member of FILE and all its `also_make's. */
993 void
994 set_command_state (struct file *file, enum cmd_state state)
996 struct dep *d;
998 file->command_state = state;
1000 for (d = file->also_make; d != 0; d = d->next)
1001 d->file->command_state = state;
1003 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1004 if (file->multi_head)
1005 for (file = file->multi_head; file != 0; file = file->multi_next)
1006 file->command_state = state;
1007 #endif
1010 /* Convert an external file timestamp to internal form. */
1012 FILE_TIMESTAMP
1013 file_timestamp_cons (const char *fname, time_t s, int ns)
1015 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
1016 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
1017 FILE_TIMESTAMP ts = product + offset;
1019 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
1020 && product <= ts && ts <= ORDINARY_MTIME_MAX))
1022 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1023 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
1024 file_timestamp_sprintf (buf, ts);
1025 error (NILF, _("%s: Timestamp out of range; substituting %s"),
1026 fname ? fname : _("Current time"), buf);
1029 return ts;
1032 /* Return the current time as a file timestamp, setting *RESOLUTION to
1033 its resolution. */
1034 FILE_TIMESTAMP
1035 file_timestamp_now (int *resolution)
1037 int r;
1038 time_t s;
1039 int ns;
1041 /* Don't bother with high-resolution clocks if file timestamps have
1042 only one-second resolution. The code below should work, but it's
1043 not worth the hassle of debugging it on hosts where it fails. */
1044 #if FILE_TIMESTAMP_HI_RES
1045 # if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
1047 struct timespec timespec;
1048 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
1050 r = 1;
1051 s = timespec.tv_sec;
1052 ns = timespec.tv_nsec;
1053 goto got_time;
1056 # endif
1057 # if HAVE_GETTIMEOFDAY
1059 struct timeval timeval;
1060 if (gettimeofday (&timeval, 0) == 0)
1062 r = 1000;
1063 s = timeval.tv_sec;
1064 ns = timeval.tv_usec * 1000;
1065 goto got_time;
1068 # endif
1069 #endif
1071 r = 1000000000;
1072 s = time ((time_t *) 0);
1073 ns = 0;
1075 #if FILE_TIMESTAMP_HI_RES
1076 got_time:
1077 #endif
1078 *resolution = r;
1079 return file_timestamp_cons (0, s, ns);
1082 /* Place into the buffer P a printable representation of the file
1083 timestamp TS. */
1084 void
1085 file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
1087 time_t t = FILE_TIMESTAMP_S (ts);
1088 struct tm *tm = localtime (&t);
1090 if (tm)
1091 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
1092 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1093 tm->tm_hour, tm->tm_min, tm->tm_sec);
1094 else if (t < 0)
1095 sprintf (p, "%ld", (long) t);
1096 else
1097 sprintf (p, "%lu", (unsigned long) t);
1098 p += strlen (p);
1100 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
1101 know the actual timestamp resolution, since clock_getres applies only to
1102 local times, whereas this timestamp might come from a remote filesystem.
1103 So removing trailing zeros is the best guess that we can do. */
1104 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
1105 p += strlen (p) - 1;
1106 while (*p == '0')
1107 p--;
1108 p += *p != '.';
1110 *p = '\0';
1113 /* Print the data base of files. */
1115 static void
1116 print_file (const void *item)
1118 const struct file *f = item;
1119 struct dep *d;
1120 struct dep *ood = 0;
1122 putchar ('\n');
1123 if (!f->is_target)
1124 puts (_("# Not a target:"));
1125 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1126 if (f->multi_head)
1128 const struct file *f2;
1129 if (f->multi_head == f)
1131 int multi_maybe = -1;
1132 assert (!f->multi_maybe);
1133 assert (!f->double_colon);
1135 printf ("%s", f->name);
1136 for (f2 = f->multi_next; f2 != 0; f2 = f2->multi_next)
1138 printf (" %s%s", f2->multi_maybe != multi_maybe
1139 ? f2->multi_maybe ? "+| " : "+ " : "",
1140 f2->name);
1141 multi_maybe = f2->multi_maybe;
1143 putchar (':');
1145 else
1146 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1148 else
1149 #endif
1150 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1152 /* Print all normal dependencies; note any order-only deps. */
1153 for (d = f->deps; d != 0; d = d->next)
1154 if (! d->ignore_mtime)
1155 printf (" %s", dep_name (d));
1156 else if (! ood)
1157 ood = d;
1159 /* Print order-only deps, if we have any. */
1160 if (ood)
1162 printf (" | %s", dep_name (ood));
1163 for (d = ood->next; d != 0; d = d->next)
1164 if (d->ignore_mtime)
1165 printf (" %s", dep_name (d));
1168 putchar ('\n');
1170 #ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1171 if (f->multi_head && f->multi_head != f)
1173 const struct file *f2;
1174 fputs (_("# In multi target list:"), stdout);
1175 for (f2 = f->multi_head; f2 != 0; f2 = f2->multi_next)
1176 printf (" %s%s", f2->name, f == f2 ? "(*)" : "");
1177 putchar ('\n');
1178 if (f->multi_maybe)
1179 puts (_("# File is an optional multi target member."));
1181 #endif
1183 if (f->precious)
1184 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
1185 if (f->phony)
1186 puts (_("# Phony target (prerequisite of .PHONY)."));
1187 if (f->cmd_target)
1188 puts (_("# Command line target."));
1189 if (f->dontcare)
1190 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
1191 puts (f->tried_implicit
1192 ? _("# Implicit rule search has been done.")
1193 : _("# Implicit rule search has not been done."));
1194 if (f->stem != 0)
1195 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
1196 if (f->intermediate)
1197 puts (_("# File is an intermediate prerequisite."));
1198 if (f->also_make != 0)
1200 fputs (_("# Also makes:"), stdout);
1201 for (d = f->also_make; d != 0; d = d->next)
1202 printf (" %s", dep_name (d));
1203 putchar ('\n');
1205 if (f->last_mtime == UNKNOWN_MTIME)
1206 puts (_("# Modification time never checked."));
1207 else if (f->last_mtime == NONEXISTENT_MTIME)
1208 puts (_("# File does not exist."));
1209 else if (f->last_mtime == OLD_MTIME)
1210 puts (_("# File is very old."));
1211 else
1213 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1214 file_timestamp_sprintf (buf, f->last_mtime);
1215 printf (_("# Last modified %s\n"), buf);
1217 puts (f->updated
1218 ? _("# File has been updated.") : _("# File has not been updated."));
1219 switch (f->command_state)
1221 case cs_running:
1222 puts (_("# Recipe currently running (THIS IS A BUG)."));
1223 break;
1224 case cs_deps_running:
1225 puts (_("# Dependencies recipe running (THIS IS A BUG)."));
1226 break;
1227 case cs_not_started:
1228 case cs_finished:
1229 switch (f->update_status)
1231 case -1:
1232 break;
1233 case 0:
1234 puts (_("# Successfully updated."));
1235 break;
1236 case 1:
1237 assert (question_flag);
1238 puts (_("# Needs to be updated (-q is set)."));
1239 break;
1240 case 2:
1241 puts (_("# Failed to be updated."));
1242 break;
1243 default:
1244 puts (_("# Invalid value in `update_status' member!"));
1245 fflush (stdout);
1246 fflush (stderr);
1247 abort ();
1249 break;
1250 default:
1251 puts (_("# Invalid value in `command_state' member!"));
1252 fflush (stdout);
1253 fflush (stderr);
1254 abort ();
1257 if (f->variables != 0)
1258 print_file_variables (f);
1260 if (f->cmds != 0)
1261 print_commands (f->cmds);
1263 if (f->prev)
1264 print_file ((const void *) f->prev);
1267 void
1268 print_file_data_base (void)
1270 puts (_("\n# Files"));
1272 hash_map (&files, print_file);
1274 fputs (_("\n# files hash-table stats:\n# "), stdout);
1275 hash_print_stats (&files, stdout);
1278 #ifdef CONFIG_WITH_PRINT_STATS_SWITCH
1279 void
1280 print_file_stats (void)
1282 fputs (_("\n# files hash-table stats:\n# "), stdout);
1283 hash_print_stats (&files, stdout);
1284 fputs ("\n", stdout);
1286 #endif
1288 /* Verify the integrity of the data base of files. */
1290 #define VERIFY_CACHED(_p,_n) \
1291 do{\
1292 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1293 printf ("%s: Field %s not cached: %s\n", _p->name, # _n, _p->_n); \
1294 }while(0)
1296 static void
1297 verify_file (const void *item)
1299 const struct file *f = item;
1300 const struct dep *d;
1302 VERIFY_CACHED (f, name);
1303 VERIFY_CACHED (f, hname);
1304 VERIFY_CACHED (f, vpath);
1305 VERIFY_CACHED (f, stem);
1307 /* Check the deps. */
1308 for (d = f->deps; d != 0; d = d->next)
1310 VERIFY_CACHED (d, name);
1311 VERIFY_CACHED (d, stem);
1315 void
1316 verify_file_data_base (void)
1318 hash_map (&files, verify_file);
1321 #define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1323 char *
1324 build_target_list (char *value)
1326 static unsigned long last_targ_count = 0;
1328 if (files.ht_fill != last_targ_count)
1330 unsigned long max = EXPANSION_INCREMENT (strlen (value));
1331 unsigned long len;
1332 char *p;
1333 struct file **fp = (struct file **) files.ht_vec;
1334 struct file **end = &fp[files.ht_size];
1336 /* Make sure we have at least MAX bytes in the allocated buffer. */
1337 value = xrealloc (value, max);
1339 p = value;
1340 len = 0;
1341 for (; fp < end; ++fp)
1342 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1344 struct file *f = *fp;
1345 int l = strlen (f->name);
1347 len += l + 1;
1348 if (len > max)
1350 unsigned long off = p - value;
1352 max += EXPANSION_INCREMENT (l + 1);
1353 value = xrealloc (value, max);
1354 p = &value[off];
1357 memcpy (p, f->name, l);
1358 p += l;
1359 *(p++) = ' ';
1361 *(p-1) = '\0';
1363 last_targ_count = files.ht_fill;
1366 return value;
1369 void
1370 init_hash_files (void)
1372 #ifndef CONFIG_WITH_STRCACHE2
1373 # ifdef KMK
1374 hash_init (&files, /*65535*/ 32755, file_hash_1, file_hash_2, file_hash_cmp);
1375 # else
1376 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1377 # endif
1378 #else /* CONFIG_WITH_STRCACHE2 */
1379 # ifdef KMK
1380 hash_init_strcached (&files, 32755, &file_strcache,
1381 offsetof (struct file, hname));
1382 # else
1383 hash_init_strcached (&files, 1000, &file_strcache,
1384 offsetof (struct file, hname));
1385 # endif
1386 #endif /* CONFIG_WITH_STRCACHE2 */
1389 /* EOF */