VirtualBox

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

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

kmk: save some file::deps list waking.

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