VirtualBox

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

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

kmk: includedep dependencies does not require unescaping or any other parsing in expand_deps. This saves more than 10% in a typical kBuild testcase.

  • Property svn:eol-style set to native
File size: 34.8 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 if (d->includedep)
519 {
520 new = alloc_dep();
521 new->name = d->name;
522 }
523 else
524 {
525#endif
526
527 /* Create the dependency list.
528 If we're not doing 2nd expansion, then it's just the name. We will
529 still need to massage it though. */
530 if (! d->need_2nd_expansion)
531 {
532 p = variable_expand ("");
533 buffer_offset = p - variable_buffer;
534#ifndef CONFIG_WITH_VALUE_LENGTH
535 variable_buffer_output (p, d->name, strlen (d->name) + 1);
536#else
537 len = strcache_get_len (d->name);
538 variable_buffer_output (p, d->name, len + 1);
539#endif
540 p = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. (observed it) */
541 }
542 else
543 {
544 /* If it's from a static pattern rule, convert the patterns into
545 "$*" so they'll expand properly. */
546 if (d->staticpattern)
547 {
548 char *o;
549 char *buffer = variable_expand ("");
550 buffer_offset = buffer - variable_buffer; /* bird */
551
552 o = subst_expand (buffer, d->name, "%", "$*", 1, 2, 0);
553 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
554
555 d->name = strcache_add_len (buffer, o - buffer);
556 d->staticpattern = 0; /* Clear staticpattern so that we don't
557 re-expand %s below. */
558 }
559
560 /* We are going to do second expansion so initialize file variables
561 for the file. Since the stem for static pattern rules comes from
562 individual dep lines, we will temporarily set f->stem to d->stem.
563 */
564 if (!initialized)
565 {
566 initialize_file_variables (f, 0);
567 initialized = 1;
568 }
569
570 if (d->stem != 0)
571 f->stem = d->stem;
572
573 set_file_variables (f);
574
575#ifndef CONFIG_WITH_VALUE_LENGTH
576 p = variable_expand_for_file (d->name, f);
577#else
578 len = strcache_get_len (d->name);
579 p = variable_expand_for_file_2 (NULL, d->name, len, f, &len);
580#endif
581
582 if (d->stem != 0)
583 f->stem = file_stem;
584 }
585
586 /* Parse the prerequisites. */
587 new = parse_prereqs (p);
588
589 /* If this dep list was from a static pattern rule, expand the %s. We
590 use patsubst_expand to translate the prerequisites' patterns into
591 plain prerequisite names. */
592 if (new && d->staticpattern)
593 {
594 const char *pattern = "%";
595 char *buffer = variable_expand ("");
596 const size_t buffer_offset = buffer - variable_buffer; /* bird */
597 struct dep *dp = new, *dl = 0;
598
599 while (dp != 0)
600 {
601 char *percent;
602#ifndef KMK
603 int nl = strlen (dp->name) + 1;
604 char *nm = alloca (nl);
605 memcpy (nm, dp->name, nl);
606 percent = find_percent (nm);
607#else /* KMK - don't make a stack copy unless it's actually required! */
608 unsigned int nl = strcache_get_len (dp->name);
609 char *nm;
610 percent = memchr (nm, '%', nl);
611 if (percent)
612 {
613 nm = alloca (nl + 1);
614 memcpy (nm, dp->name, nl + 1);
615 percent = find_percent (nm);
616 }
617#endif /* KMK */
618 if (percent)
619 {
620 char *o;
621
622 /* We have to handle empty stems specially, because that
623 would be equivalent to $(patsubst %,dp->name,) which
624 will always be empty. */
625 if (d->stem[0] == '\0')
626 {
627 memmove (percent, percent+1, strlen (percent));
628 o = variable_buffer_output (buffer, nm, strlen (nm) + 1);
629 }
630 else
631 o = patsubst_expand_pat (buffer, d->stem, pattern, nm,
632 pattern+1, percent+1);
633 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
634
635
636 /* If the name expanded to the empty string, ignore it. */
637 if (buffer[0] == '\0')
638 {
639 struct dep *df = dp;
640 if (dp == new)
641 dp = new = new->next;
642 else
643 dp = dl->next = dp->next;
644 free_dep (df);
645 continue;
646 }
647
648 /* Save the name. */
649 dp->name = strcache_add_len (buffer, o - buffer);
650 }
651 dl = dp;
652 dp = dp->next;
653 }
654 }
655
656#ifdef CONFIG_WITH_INCLUDEDEP
657 }
658#endif
659
660 /* Enter them as files. */
661 for (d1 = new; d1 != 0; d1 = d1->next)
662 {
663 d1->file = lookup_file (d1->name);
664 if (d1->file == 0)
665 d1->file = enter_file (d1->name);
666 d1->name = 0;
667 d1->staticpattern = 0;
668 d1->need_2nd_expansion = 0;
669 }
670
671 /* Add newly parsed deps to f->deps. If this is the last dependency
672 line and this target has commands then put it in front so the
673 last dependency line (the one with commands) ends up being the
674 first. This is important because people expect $< to hold first
675 prerequisite from the rule with commands. If it is not the last
676 dependency line or the rule does not have commands then link it
677 at the end so it appears in makefile order. */
678
679 if (new != 0)
680 {
681 if (d->next == 0 && last_dep_has_cmds)
682 {
683 struct dep **d_ptr;
684 for (d_ptr = &new; *d_ptr; d_ptr = &(*d_ptr)->next)
685 ;
686
687 *d_ptr = f->deps;
688 f->deps = new;
689 }
690 else
691 {
692 struct dep **d_ptr;
693 for (d_ptr = &f->deps; *d_ptr; d_ptr = &(*d_ptr)->next)
694 ;
695
696 *d_ptr = new;
697 }
698 }
699 }
700
701 free_dep_chain (old);
702}
703
704/* For each dependency of each file, make the `struct dep' point
705 at the appropriate `struct file' (which may have to be created).
706
707 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
708 and various other special targets. */
709
710void
711snap_deps (void)
712{
713 struct file *f;
714 struct file *f2;
715 struct dep *d;
716 struct file **file_slot_0;
717 struct file **file_slot;
718 struct file **file_end;
719
720 /* Perform second expansion and enter each dependency name as a file. */
721
722 /* Expand .SUFFIXES first; it's dependencies are used for $$* calculation. */
723 for (f = lookup_file (".SUFFIXES"); f != 0; f = f->prev)
724 expand_deps (f);
725
726#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
727 /* Perform 2nd target expansion on files which requires this. This will
728 be re-inserting (delete+insert) hash table entries so we have to use
729 hash_dump(). */
730 if (second_target_expansion)
731 {
732# ifdef KMK /* turn on warnings here. */
733 int save = warn_undefined_variables_flag;
734 warn_undefined_variables_flag = 1;
735# endif
736
737 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
738 file_end = file_slot_0 + files.ht_fill;
739 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
740 for (f = *file_slot; f != 0; f = f->prev)
741 if (f->need_2nd_target_expansion)
742 do_2nd_target_expansion (f);
743 free (file_slot_0);
744
745# ifdef KMK
746 warn_undefined_variables_flag = save;
747# endif
748
749 /* Disable second target expansion now since we won't expand files
750 entered after this point. (Saves CPU cycles in enter_file()). */
751 second_target_expansion = 0;
752 }
753#endif /* CONFIG_WITH_2ND_TARGET_EXPANSION */
754
755#ifdef CONFIG_WITH_INCLUDEDEP
756 /* Process any queued includedep files. Since includedep is supposed
757 to be *simple* stuff, we can do this after the second target expansion
758 and thereby save a little time. */
759 incdep_flush_and_term ();
760#endif
761
762 /* For every target that's not .SUFFIXES, expand its dependencies.
763 We must use hash_dump (), because within this loop we might add new files
764 to the table, possibly causing an in-situ table expansion. */
765 file_slot_0 = (struct file **) hash_dump (&files, 0, 0);
766 file_end = file_slot_0 + files.ht_fill;
767 for (file_slot = file_slot_0; file_slot < file_end; file_slot++)
768 for (f = *file_slot; f != 0; f = f->prev)
769 if (strcmp (f->name, ".SUFFIXES") != 0)
770 expand_deps (f);
771 free (file_slot_0);
772
773 /* Now manage all the special targets. */
774
775 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
776 for (d = f->deps; d != 0; d = d->next)
777 for (f2 = d->file; f2 != 0; f2 = f2->prev)
778 f2->precious = 1;
779
780 for (f = lookup_file (".LOW_RESOLUTION_TIME"); f != 0; f = f->prev)
781 for (d = f->deps; d != 0; d = d->next)
782 for (f2 = d->file; f2 != 0; f2 = f2->prev)
783 f2->low_resolution_time = 1;
784
785 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
786 for (d = f->deps; d != 0; d = d->next)
787 for (f2 = d->file; f2 != 0; f2 = f2->prev)
788 {
789 /* Mark this file as phony nonexistent target. */
790 f2->phony = 1;
791 f2->is_target = 1;
792 f2->last_mtime = NONEXISTENT_MTIME;
793 f2->mtime_before_update = NONEXISTENT_MTIME;
794 }
795
796 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
797 /* Mark .INTERMEDIATE deps as intermediate files. */
798 for (d = f->deps; d != 0; d = d->next)
799 for (f2 = d->file; f2 != 0; f2 = f2->prev)
800 f2->intermediate = 1;
801 /* .INTERMEDIATE with no deps does nothing.
802 Marking all files as intermediates is useless since the goal targets
803 would be deleted after they are built. */
804
805 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
806 /* Mark .SECONDARY deps as both intermediate and secondary. */
807 if (f->deps)
808 for (d = f->deps; d != 0; d = d->next)
809 for (f2 = d->file; f2 != 0; f2 = f2->prev)
810 f2->intermediate = f2->secondary = 1;
811 /* .SECONDARY with no deps listed marks *all* files that way. */
812 else
813 {
814 all_secondary = 1;
815 hash_map (&files, set_intermediate);
816 }
817
818 f = lookup_file (".EXPORT_ALL_VARIABLES");
819 if (f != 0 && f->is_target)
820 export_all_variables = 1;
821
822 f = lookup_file (".IGNORE");
823 if (f != 0 && f->is_target)
824 {
825 if (f->deps == 0)
826 ignore_errors_flag = 1;
827 else
828 for (d = f->deps; d != 0; d = d->next)
829 for (f2 = d->file; f2 != 0; f2 = f2->prev)
830 f2->command_flags |= COMMANDS_NOERROR;
831 }
832
833 f = lookup_file (".SILENT");
834 if (f != 0 && f->is_target)
835 {
836 if (f->deps == 0)
837 silent_flag = 1;
838 else
839 for (d = f->deps; d != 0; d = d->next)
840 for (f2 = d->file; f2 != 0; f2 = f2->prev)
841 f2->command_flags |= COMMANDS_SILENT;
842 }
843
844 f = lookup_file (".NOTPARALLEL");
845 if (f != 0 && f->is_target)
846#ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
847 not_parallel = 1;
848#else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
849 {
850 if (f->deps == 0)
851 {
852 DB (DB_KMK, (_("not_parallel -1\n")));
853 not_parallel = -1;
854 }
855 else
856 for (d = f->deps; d != 0; d = d->next)
857 for (f2 = d->file; f2 != 0; f2 = f2->prev)
858 f2->command_flags |= COMMANDS_NOTPARALLEL;
859 }
860#endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
861
862#ifndef NO_MINUS_C_MINUS_O
863 /* If .POSIX was defined, remove OUTPUT_OPTION to comply. */
864 /* This needs more work: what if the user sets this in the makefile?
865 if (posix_pedantic)
866 define_variable (STRING_SIZE_TUPLE("OUTPUT_OPTION"), "", o_default, 1);
867 */
868#endif
869
870 /* Remember that we've done this. */
871 snapped_deps = 1;
872}
873
874
875/* Set the `command_state' member of FILE and all its `also_make's. */
876
877void
878set_command_state (struct file *file, enum cmd_state state)
879{
880 struct dep *d;
881
882 file->command_state = state;
883
884 for (d = file->also_make; d != 0; d = d->next)
885 d->file->command_state = state;
886
887#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
888 if (file->multi_head)
889 for (file = file->multi_head; file != 0; file = file->multi_next)
890 file->command_state = state;
891#endif
892}
893
894
895/* Convert an external file timestamp to internal form. */
896
897FILE_TIMESTAMP
898file_timestamp_cons (const char *fname, time_t s, int ns)
899{
900 int offset = ORDINARY_MTIME_MIN + (FILE_TIMESTAMP_HI_RES ? ns : 0);
901 FILE_TIMESTAMP product = (FILE_TIMESTAMP) s << FILE_TIMESTAMP_LO_BITS;
902 FILE_TIMESTAMP ts = product + offset;
903
904 if (! (s <= FILE_TIMESTAMP_S (ORDINARY_MTIME_MAX)
905 && product <= ts && ts <= ORDINARY_MTIME_MAX))
906 {
907 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
908 ts = s <= OLD_MTIME ? ORDINARY_MTIME_MIN : ORDINARY_MTIME_MAX;
909 file_timestamp_sprintf (buf, ts);
910 error (NILF, _("%s: Timestamp out of range; substituting %s"),
911 fname ? fname : _("Current time"), buf);
912 }
913
914 return ts;
915}
916
917
918/* Return the current time as a file timestamp, setting *RESOLUTION to
919 its resolution. */
920FILE_TIMESTAMP
921file_timestamp_now (int *resolution)
922{
923 int r;
924 time_t s;
925 int ns;
926
927 /* Don't bother with high-resolution clocks if file timestamps have
928 only one-second resolution. The code below should work, but it's
929 not worth the hassle of debugging it on hosts where it fails. */
930#if FILE_TIMESTAMP_HI_RES
931# if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
932 {
933 struct timespec timespec;
934 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
935 {
936 r = 1;
937 s = timespec.tv_sec;
938 ns = timespec.tv_nsec;
939 goto got_time;
940 }
941 }
942# endif
943# if HAVE_GETTIMEOFDAY
944 {
945 struct timeval timeval;
946 if (gettimeofday (&timeval, 0) == 0)
947 {
948 r = 1000;
949 s = timeval.tv_sec;
950 ns = timeval.tv_usec * 1000;
951 goto got_time;
952 }
953 }
954# endif
955#endif
956
957 r = 1000000000;
958 s = time ((time_t *) 0);
959 ns = 0;
960
961#if FILE_TIMESTAMP_HI_RES
962 got_time:
963#endif
964 *resolution = r;
965 return file_timestamp_cons (0, s, ns);
966}
967
968/* Place into the buffer P a printable representation of the file
969 timestamp TS. */
970void
971file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
972{
973 time_t t = FILE_TIMESTAMP_S (ts);
974 struct tm *tm = localtime (&t);
975
976 if (tm)
977 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
978 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
979 tm->tm_hour, tm->tm_min, tm->tm_sec);
980 else if (t < 0)
981 sprintf (p, "%ld", (long) t);
982 else
983 sprintf (p, "%lu", (unsigned long) t);
984 p += strlen (p);
985
986 /* Append nanoseconds as a fraction, but remove trailing zeros. We don't
987 know the actual timestamp resolution, since clock_getres applies only to
988 local times, whereas this timestamp might come from a remote filesystem.
989 So removing trailing zeros is the best guess that we can do. */
990 sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
991 p += strlen (p) - 1;
992 while (*p == '0')
993 p--;
994 p += *p != '.';
995
996 *p = '\0';
997}
998
999
1000/* Print the data base of files. */
1001
1002static void
1003print_file (const void *item)
1004{
1005 const struct file *f = item;
1006 struct dep *d;
1007 struct dep *ood = 0;
1008
1009 putchar ('\n');
1010 if (!f->is_target)
1011 puts (_("# Not a target:"));
1012#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1013 if (f->multi_head)
1014 {
1015 const struct file *f2;
1016 if (f->multi_head == f)
1017 {
1018 int multi_maybe = -1;
1019 assert (!f->multi_maybe);
1020 assert (!f->double_colon);
1021
1022 printf ("%s", f->name);
1023 for (f2 = f->multi_next; f2 != 0; f2 = f2->multi_next)
1024 {
1025 printf (" %s%s", f2->multi_maybe != multi_maybe
1026 ? f2->multi_maybe ? "+| " : "+ " : "",
1027 f2->name);
1028 multi_maybe = f2->multi_maybe;
1029 }
1030 putchar (':');
1031 }
1032 else
1033 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1034 }
1035 else
1036#endif
1037 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
1038
1039 /* Print all normal dependencies; note any order-only deps. */
1040 for (d = f->deps; d != 0; d = d->next)
1041 if (! d->ignore_mtime)
1042 printf (" %s", dep_name (d));
1043 else if (! ood)
1044 ood = d;
1045
1046 /* Print order-only deps, if we have any. */
1047 if (ood)
1048 {
1049 printf (" | %s", dep_name (ood));
1050 for (d = ood->next; d != 0; d = d->next)
1051 if (d->ignore_mtime)
1052 printf (" %s", dep_name (d));
1053 }
1054
1055 putchar ('\n');
1056
1057#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
1058 if (f->multi_head && f->multi_head != f)
1059 {
1060 const struct file *f2;
1061 fputs (_("# In multi target list:"), stdout);
1062 for (f2 = f->multi_head; f2 != 0; f2 = f2->multi_next)
1063 printf (" %s%s", f2->name, f == f2 ? "(*)" : "");
1064 putchar ('\n');
1065 if (f->multi_maybe)
1066 puts (_("# File is an optional multi target member."));
1067 }
1068#endif
1069
1070 if (f->precious)
1071 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
1072 if (f->phony)
1073 puts (_("# Phony target (prerequisite of .PHONY)."));
1074 if (f->cmd_target)
1075 puts (_("# Command-line target."));
1076 if (f->dontcare)
1077 puts (_("# A default, MAKEFILES, or -include/sinclude makefile."));
1078 puts (f->tried_implicit
1079 ? _("# Implicit rule search has been done.")
1080 : _("# Implicit rule search has not been done."));
1081 if (f->stem != 0)
1082 printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
1083 if (f->intermediate)
1084 puts (_("# File is an intermediate prerequisite."));
1085 if (f->also_make != 0)
1086 {
1087 fputs (_("# Also makes:"), stdout);
1088 for (d = f->also_make; d != 0; d = d->next)
1089 printf (" %s", dep_name (d));
1090 putchar ('\n');
1091 }
1092 if (f->last_mtime == UNKNOWN_MTIME)
1093 puts (_("# Modification time never checked."));
1094 else if (f->last_mtime == NONEXISTENT_MTIME)
1095 puts (_("# File does not exist."));
1096 else if (f->last_mtime == OLD_MTIME)
1097 puts (_("# File is very old."));
1098 else
1099 {
1100 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
1101 file_timestamp_sprintf (buf, f->last_mtime);
1102 printf (_("# Last modified %s\n"), buf);
1103 }
1104 puts (f->updated
1105 ? _("# File has been updated.") : _("# File has not been updated."));
1106 switch (f->command_state)
1107 {
1108 case cs_running:
1109 puts (_("# Commands currently running (THIS IS A BUG)."));
1110 break;
1111 case cs_deps_running:
1112 puts (_("# Dependencies commands running (THIS IS A BUG)."));
1113 break;
1114 case cs_not_started:
1115 case cs_finished:
1116 switch (f->update_status)
1117 {
1118 case -1:
1119 break;
1120 case 0:
1121 puts (_("# Successfully updated."));
1122 break;
1123 case 1:
1124 assert (question_flag);
1125 puts (_("# Needs to be updated (-q is set)."));
1126 break;
1127 case 2:
1128 puts (_("# Failed to be updated."));
1129 break;
1130 default:
1131 puts (_("# Invalid value in `update_status' member!"));
1132 fflush (stdout);
1133 fflush (stderr);
1134 abort ();
1135 }
1136 break;
1137 default:
1138 puts (_("# Invalid value in `command_state' member!"));
1139 fflush (stdout);
1140 fflush (stderr);
1141 abort ();
1142 }
1143
1144 if (f->variables != 0)
1145 print_file_variables (f);
1146
1147 if (f->cmds != 0)
1148 print_commands (f->cmds);
1149
1150 if (f->prev)
1151 print_file ((const void *) f->prev);
1152}
1153
1154void
1155print_file_data_base (void)
1156{
1157 puts (_("\n# Files"));
1158
1159 hash_map (&files, print_file);
1160
1161 fputs (_("\n# files hash-table stats:\n# "), stdout);
1162 hash_print_stats (&files, stdout);
1163}
1164
1165
1166/* Verify the integrity of the data base of files. */
1167
1168#define VERIFY_CACHED(_p,_n) \
1169 do{\
1170 if (_p->_n && _p->_n[0] && !strcache_iscached (_p->_n)) \
1171 printf ("%s: Field %s not cached: %s\n", _p->name, # _n, _p->_n); \
1172 }while(0)
1173
1174static void
1175verify_file (const void *item)
1176{
1177 const struct file *f = item;
1178 const struct dep *d;
1179
1180 VERIFY_CACHED (f, name);
1181 VERIFY_CACHED (f, hname);
1182 VERIFY_CACHED (f, vpath);
1183 VERIFY_CACHED (f, stem);
1184
1185 /* Check the deps. */
1186 for (d = f->deps; d != 0; d = d->next)
1187 {
1188 VERIFY_CACHED (d, name);
1189 VERIFY_CACHED (d, stem);
1190 }
1191}
1192
1193void
1194verify_file_data_base (void)
1195{
1196 hash_map (&files, verify_file);
1197}
1198
1199#define EXPANSION_INCREMENT(_l) ((((_l) / 500) + 1) * 500)
1200
1201char *
1202build_target_list (char *value)
1203{
1204 static unsigned long last_targ_count = 0;
1205
1206 if (files.ht_fill != last_targ_count)
1207 {
1208 unsigned long max = EXPANSION_INCREMENT (strlen (value));
1209 unsigned long len;
1210 char *p;
1211 struct file **fp = (struct file **) files.ht_vec;
1212 struct file **end = &fp[files.ht_size];
1213
1214 /* Make sure we have at least MAX bytes in the allocated buffer. */
1215 value = xrealloc (value, max);
1216
1217 p = value;
1218 len = 0;
1219 for (; fp < end; ++fp)
1220 if (!HASH_VACANT (*fp) && (*fp)->is_target)
1221 {
1222 struct file *f = *fp;
1223 int l = strlen (f->name);
1224
1225 len += l + 1;
1226 if (len > max)
1227 {
1228 unsigned long off = p - value;
1229
1230 max += EXPANSION_INCREMENT (l + 1);
1231 value = xrealloc (value, max);
1232 p = &value[off];
1233 }
1234
1235 memcpy (p, f->name, l);
1236 p += l;
1237 *(p++) = ' ';
1238 }
1239 *(p-1) = '\0';
1240
1241 last_targ_count = files.ht_fill;
1242 }
1243
1244 return value;
1245}
1246
1247void
1248init_hash_files (void)
1249{
1250#ifdef KMK
1251 hash_init (&files, /*65535*/ 32755, file_hash_1, file_hash_2, file_hash_cmp);
1252#else
1253 hash_init (&files, 1000, file_hash_1, file_hash_2, file_hash_cmp);
1254#endif
1255}
1256
1257/* 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