VirtualBox

source: kBuild/trunk/src/gmakenew/file.c@ 902

Last change on this file since 902 was 520, checked in by bird, 18 years ago

Cleaning up the modifications. Changes are now either configurable or marked, and dead stuff has been removed (dll shell).

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette