VirtualBox

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

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

kmk: fix for file_cache <-> file_strcache confusion bug introduced earlier tonight.

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