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