VirtualBox

source: kBuild/trunk/src/kmk/file.c@ 1849

Last change on this file since 1849 was 1849, checked in by bird, 16 years ago

kmk: a little adjustment to save 10 clock ticks or something.

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