VirtualBox

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

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

Made do_2nd_target_expansion skip a strlen.

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