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

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

kmk: Don't bother with secondary target expansion if it's not enabled (you can't turn it off when it's on). In the KMK case we force undefined variable warnings during scondary target expansion because bad stuff may happen if you get this wrong (like file in the root).

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