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

Last change on this file since 2740 was 2740, checked in by bird, 11 years ago

rehash_file: fixed problem with multi target files, forgot to update the head pointer of the old file.

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