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

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

kmk: commands and file allocation caches.

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