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

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

kmk: Some optimizations for expand_deps.

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