VirtualBox

source: kBuild/vendor/gnumake/current/file.c@ 3138

Last change on this file since 3138 was 3138, checked in by bird, 7 years ago

Imported make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6) from https://git.savannah.gnu.org/git/make.git.

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