* Fix up and document $(apply ...) function.
[make.git] / file.c
blob2a9a79129be8d4893b4aeeaac9f56b3f746c274c
1 /* Target file hash table management for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <assert.h>
22 #include "make.h"
23 #include "dep.h"
24 #include "filedef.h"
25 #include "job.h"
26 #include "commands.h"
27 #include "variable.h"
30 /* Hash table of files the makefile knows how to make. */
32 #ifndef FILE_BUCKETS
33 #define FILE_BUCKETS 1007
34 #endif
35 static struct file *files[FILE_BUCKETS];
37 /* Number of files with the `intermediate' flag set. */
39 unsigned int num_intermediates = 0;
42 /* Access the hash table of all file records.
43 lookup_file given a name, return the struct file * for that name,
44 or nil if there is none.
45 enter_file similar, but create one if there is none. */
47 struct file *
48 lookup_file (name)
49 char *name;
51 register struct file *f;
52 register char *n;
53 register unsigned int hashval;
54 #ifdef VMS
55 register char *lname, *ln;
56 #endif
58 if (*name == '\0')
59 abort ();
61 /* This is also done in parse_file_seq, so this is redundant
62 for names read from makefiles. It is here for names passed
63 on the command line. */
64 #ifdef VMS
65 lname = (char *)malloc(strlen(name) + 1);
66 for (n=name, ln=lname; *n != '\0'; ++n, ++ln)
67 *ln = isupper(*n) ? tolower(*n) : *n;
68 *ln = '\0';
69 name = lname;
71 while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
72 name += 2;
73 #endif
74 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
76 name += 2;
77 while (*name == '/')
78 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
79 ++name;
82 if (*name == '\0')
83 /* It was all slashes after a dot. */
84 #ifdef VMS
85 name = "[]";
86 #else
87 #ifdef _AMIGA
88 name = "";
89 #else
90 name = "./";
91 #endif /* AMIGA */
92 #endif /* VMS */
94 hashval = 0;
95 for (n = name; *n != '\0'; ++n)
96 HASHI (hashval, *n);
97 hashval %= FILE_BUCKETS;
99 for (f = files[hashval]; f != 0; f = f->next)
101 if (strieq (f->hname, name))
103 #ifdef VMS
104 free (lname);
105 #endif
106 return f;
109 #ifdef VMS
110 free (lname);
111 #endif
112 return 0;
115 struct file *
116 enter_file (name)
117 char *name;
119 register struct file *f, *new;
120 register char *n;
121 register unsigned int hashval;
122 #ifdef VMS
123 char *lname, *ln;
124 #endif
126 if (*name == '\0')
127 abort ();
129 #ifdef VMS
130 lname = (char *)malloc (strlen (name) + 1);
131 for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
133 if (isupper(*n))
134 *ln = tolower(*n);
135 else
136 *ln = *n;
138 *ln = 0;
139 name = lname;
140 #endif
142 hashval = 0;
143 for (n = name; *n != '\0'; ++n)
144 HASHI (hashval, *n);
145 hashval %= FILE_BUCKETS;
147 for (f = files[hashval]; f != 0; f = f->next)
148 if (strieq (f->hname, name))
149 break;
151 if (f != 0 && !f->double_colon)
153 #ifdef VMS
154 free(lname);
155 #endif
156 return f;
159 new = (struct file *) xmalloc (sizeof (struct file));
160 bzero ((char *) new, sizeof (struct file));
161 new->name = new->hname = name;
162 new->update_status = -1;
164 if (f == 0)
166 /* This is a completely new file. */
167 new->next = files[hashval];
168 files[hashval] = new;
170 else
172 /* There is already a double-colon entry for this file. */
173 new->double_colon = f;
174 while (f->prev != 0)
175 f = f->prev;
176 f->prev = new;
179 return new;
182 /* Rehash FILE to NAME. This is not as simple as resetting
183 the `hname' member, since it must be put in a new hash bucket,
184 and possibly merged with an existing file called NAME. */
186 void
187 rehash_file (file, name)
188 register struct file *file;
189 char *name;
191 char *oldname = file->hname;
192 register unsigned int oldhash;
193 register char *n;
195 while (file->renamed != 0)
196 file = file->renamed;
198 /* Find the hash values of the old and new names. */
200 oldhash = 0;
201 for (n = oldname; *n != '\0'; ++n)
202 HASHI (oldhash, *n);
204 file_hash_enter (file, name, oldhash, file->name);
207 /* Rename FILE to NAME. This is not as simple as resetting
208 the `name' member, since it must be put in a new hash bucket,
209 and possibly merged with an existing file called NAME. */
211 void
212 rename_file (file, name)
213 register struct file *file;
214 char *name;
216 rehash_file(file, name);
217 while (file)
219 file->name = file->hname;
220 file = file->prev;
224 void
225 file_hash_enter (file, name, oldhash, oldname)
226 register struct file *file;
227 char *name;
228 unsigned int oldhash;
229 char *oldname;
231 unsigned int oldbucket = oldhash % FILE_BUCKETS;
232 register unsigned int newhash, newbucket;
233 struct file *oldfile;
234 register char *n;
235 register struct file *f;
237 newhash = 0;
238 for (n = name; *n != '\0'; ++n)
239 HASHI (newhash, *n);
240 newbucket = newhash % FILE_BUCKETS;
242 /* Look for an existing file under the new name. */
244 for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
245 if (strieq (oldfile->hname, name))
246 break;
248 /* If the old file is the same as the new file, something's wrong. */
249 assert (oldfile != file);
251 if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
253 /* Remove FILE from its hash bucket. */
255 struct file *lastf = 0;
257 for (f = files[oldbucket]; f != file; f = f->next)
258 lastf = f;
260 if (lastf == 0)
261 files[oldbucket] = f->next;
262 else
263 lastf->next = f->next;
266 /* Give FILE its new name. */
268 file->hname = name;
269 for (f = file->double_colon; f != 0; f = f->prev)
270 f->hname = name;
272 if (oldfile == 0)
274 /* There is no existing file with the new name. */
276 if (newbucket != oldbucket)
278 /* Put FILE in its new hash bucket. */
279 file->next = files[newbucket];
280 files[newbucket] = file;
283 else
285 /* There is an existing file with the new name.
286 We must merge FILE into the existing file. */
288 register struct dep *d;
290 if (file->cmds != 0)
292 if (oldfile->cmds == 0)
293 oldfile->cmds = file->cmds;
294 else if (file->cmds != oldfile->cmds)
296 /* We have two sets of commands. We will go with the
297 one given in the rule explicitly mentioning this name,
298 but give a message to let the user know what's going on. */
299 if (oldfile->cmds->fileinfo.filenm != 0)
300 error (&file->cmds->fileinfo,
301 "Commands were specified for \
302 file `%s' at %s:%lu,",
303 oldname, oldfile->cmds->fileinfo.filenm,
304 oldfile->cmds->fileinfo.lineno);
305 else
306 error (&file->cmds->fileinfo,
307 "Commands for file `%s' were found by \
308 implicit rule search,",
309 oldname);
310 error (&file->cmds->fileinfo,
311 "but `%s' is now considered the same file \
312 as `%s'.",
313 oldname, name);
314 error (&file->cmds->fileinfo,
315 "Commands for `%s' will be ignored \
316 in favor of those for `%s'.",
317 name, oldname);
321 /* Merge the dependencies of the two files. */
323 d = oldfile->deps;
324 if (d == 0)
325 oldfile->deps = file->deps;
326 else
328 while (d->next != 0)
329 d = d->next;
330 d->next = file->deps;
333 merge_variable_set_lists (&oldfile->variables, file->variables);
335 if (oldfile->double_colon && file->is_target && !file->double_colon)
336 fatal (NILF, "can't rename single-colon `%s' to double-colon `%s'",
337 oldname, name);
338 if (!oldfile->double_colon && file->double_colon)
340 if (oldfile->is_target)
341 fatal (NILF, "can't rename double-colon `%s' to single-colon `%s'",
342 oldname, name);
343 else
344 oldfile->double_colon = file->double_colon;
347 if (file->last_mtime > oldfile->last_mtime)
348 /* %%% Kludge so -W wins on a file that gets vpathized. */
349 oldfile->last_mtime = file->last_mtime;
351 oldfile->mtime_before_update = file->mtime_before_update;
353 #define MERGE(field) oldfile->field |= file->field
354 MERGE (precious);
355 MERGE (tried_implicit);
356 MERGE (updating);
357 MERGE (updated);
358 MERGE (is_target);
359 MERGE (cmd_target);
360 MERGE (phony);
361 MERGE (ignore_vpath);
362 #undef MERGE
364 file->renamed = oldfile;
368 /* Remove all nonprecious intermediate files.
369 If SIG is nonzero, this was caused by a fatal signal,
370 meaning that a different message will be printed, and
371 the message will go to stderr rather than stdout. */
373 void
374 remove_intermediates (sig)
375 int sig;
377 register int i;
378 register struct file *f;
379 char doneany;
381 if (question_flag || touch_flag)
382 return;
383 if (sig && just_print_flag)
384 return;
386 doneany = 0;
387 for (i = 0; i < FILE_BUCKETS; ++i)
388 for (f = files[i]; f != 0; f = f->next)
389 if (f->intermediate && (f->dontcare || !f->precious)
390 && !f->secondary)
392 int status;
393 if (f->update_status == -1)
394 /* If nothing would have created this file yet,
395 don't print an "rm" command for it. */
396 continue;
397 else if (just_print_flag)
398 status = 0;
399 else
401 status = unlink (f->name);
402 if (status < 0 && errno == ENOENT)
403 continue;
405 if (!f->dontcare)
407 if (sig)
408 error (NILF, "*** Deleting intermediate file `%s'", f->name);
409 else if (!silent_flag)
411 if (! doneany)
413 fputs ("rm ", stdout);
414 doneany = 1;
416 else
417 putchar (' ');
418 fputs (f->name, stdout);
419 fflush (stdout);
421 if (status < 0)
422 perror_with_name ("unlink: ", f->name);
426 if (doneany && !sig)
428 putchar ('\n');
429 fflush (stdout);
433 /* For each dependency of each file, make the `struct dep' point
434 at the appropriate `struct file' (which may have to be created).
436 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
437 and various other special targets. */
439 void
440 snap_deps ()
442 register struct file *f, *f2;
443 register struct dep *d;
444 register int i;
446 /* Enter each dependency name as a file. */
447 for (i = 0; i < FILE_BUCKETS; ++i)
448 for (f = files[i]; f != 0; f = f->next)
449 for (f2 = f; f2 != 0; f2 = f2->prev)
450 for (d = f2->deps; d != 0; d = d->next)
451 if (d->name != 0)
453 d->file = lookup_file (d->name);
454 if (d->file == 0)
455 d->file = enter_file (d->name);
456 else
457 free (d->name);
458 d->name = 0;
461 for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
462 for (d = f->deps; d != 0; d = d->next)
463 for (f2 = d->file; f2 != 0; f2 = f2->prev)
464 f2->precious = 1;
466 for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
467 for (d = f->deps; d != 0; d = d->next)
468 for (f2 = d->file; f2 != 0; f2 = f2->prev)
470 /* Mark this file as phony and nonexistent. */
471 f2->phony = 1;
472 f2->last_mtime = (FILE_TIMESTAMP) -1;
473 f2->mtime_before_update = (FILE_TIMESTAMP) -1;
476 for (f = lookup_file (".INTERMEDIATE"); f != 0; f = f->prev)
478 /* .INTERMEDIATE with deps listed
479 marks those deps as intermediate files. */
480 for (d = f->deps; d != 0; d = d->next)
481 for (f2 = d->file; f2 != 0; f2 = f2->prev)
482 f2->intermediate = 1;
483 /* .INTERMEDIATE with no deps does nothing.
484 Marking all files as intermediates is useless
485 since the goal targets would be deleted after they are built. */
488 for (f = lookup_file (".SECONDARY"); f != 0; f = f->prev)
490 /* .SECONDARY with deps listed
491 marks those deps as intermediate files
492 in that they don't get rebuilt if not actually needed;
493 but unlike real intermediate files,
494 these are not deleted after make finishes. */
495 if (f->deps)
497 for (d = f->deps; d != 0; d = d->next)
498 for (f2 = d->file; f2 != 0; f2 = f2->prev)
499 f2->intermediate = f2->secondary = 1;
501 /* .SECONDARY with no deps listed marks *all* files that way. */
502 else
504 int i;
505 for (i = 0; i < FILE_BUCKETS; i++)
506 for (f2 = files[i]; f2; f2= f2->next)
507 f2->intermediate = f2->secondary = 1;
511 f = lookup_file (".EXPORT_ALL_VARIABLES");
512 if (f != 0 && f->is_target)
513 export_all_variables = 1;
515 f = lookup_file (".IGNORE");
516 if (f != 0 && f->is_target)
518 if (f->deps == 0)
519 ignore_errors_flag = 1;
520 else
521 for (d = f->deps; d != 0; d = d->next)
522 for (f2 = d->file; f2 != 0; f2 = f2->prev)
523 f2->command_flags |= COMMANDS_NOERROR;
526 f = lookup_file (".SILENT");
527 if (f != 0 && f->is_target)
529 if (f->deps == 0)
530 silent_flag = 1;
531 else
532 for (d = f->deps; d != 0; d = d->next)
533 for (f2 = d->file; f2 != 0; f2 = f2->prev)
534 f2->command_flags |= COMMANDS_SILENT;
537 f = lookup_file (".POSIX");
538 if (f != 0 && f->is_target)
539 posix_pedantic = 1;
542 /* Set the `command_state' member of FILE and all its `also_make's. */
544 void
545 set_command_state (file, state)
546 struct file *file;
547 int state;
549 struct dep *d;
551 file->command_state = state;
553 for (d = file->also_make; d != 0; d = d->next)
554 d->file->command_state = state;
557 /* Get and print file timestamps. */
559 FILE_TIMESTAMP
560 file_timestamp_now ()
562 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
563 struct timespec timespec;
564 if (clock_gettime (CLOCK_REALTIME, &timespec) == 0)
565 return FILE_TIMESTAMP_FROM_S_AND_NS (timespec.tv_sec, timespec.tv_nsec);
566 #endif
567 return FILE_TIMESTAMP_FROM_S_AND_NS (time ((time_t *) 0), 0);
570 void
571 file_timestamp_sprintf (p, ts)
572 char *p;
573 FILE_TIMESTAMP ts;
575 time_t t = FILE_TIMESTAMP_S (ts);
576 struct tm *tm = localtime (&t);
578 if (tm)
579 sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
580 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
581 tm->tm_hour, tm->tm_min, tm->tm_sec);
582 else if (t < 0)
583 sprintf (p, "%ld", (long) t);
584 else
585 sprintf (p, "%lu", (unsigned long) t);
586 p += strlen (p);
588 /* Append nanoseconds as a fraction, but remove trailing zeros.
589 We don't know the actual timestamp resolution, since clock_getres
590 applies only to local times, whereas this timestamp might come
591 from a remote filesystem. So removing trailing zeros is the
592 best guess that we can do. */
593 sprintf (p, ".%09ld", (long) FILE_TIMESTAMP_NS (ts));
594 p += strlen (p) - 1;
595 while (*p == '0')
596 p--;
597 p += *p != '.';
599 *p = '\0';
602 /* Print the data base of files. */
604 static void
605 print_file (f)
606 struct file *f;
608 register struct dep *d;
610 putchar ('\n');
611 if (!f->is_target)
612 puts ("# Not a target:");
613 printf ("%s:%s", f->name, f->double_colon ? ":" : "");
615 for (d = f->deps; d != 0; d = d->next)
616 printf (" %s", dep_name (d));
617 putchar ('\n');
619 if (f->precious)
620 puts ("# Precious file (dependency of .PRECIOUS).");
621 if (f->phony)
622 puts ("# Phony target (dependency of .PHONY).");
623 if (f->cmd_target)
624 puts ("# Command-line target.");
625 if (f->dontcare)
626 puts ("# A default or MAKEFILES makefile.");
627 printf ("# Implicit rule search has%s been done.\n",
628 f->tried_implicit ? "" : " not");
629 if (f->stem != 0)
630 printf ("# Implicit/static pattern stem: `%s'\n", f->stem);
631 if (f->intermediate)
632 puts ("# File is an intermediate dependency.");
633 if (f->also_make != 0)
635 fputs ("# Also makes:", stdout);
636 for (d = f->also_make; d != 0; d = d->next)
637 printf (" %s", dep_name (d));
638 putchar ('\n');
640 if (f->last_mtime == 0)
641 puts ("# Modification time never checked.");
642 else if (f->last_mtime == (FILE_TIMESTAMP) -1)
643 puts ("# File does not exist.");
644 else
646 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
647 file_timestamp_sprintf (buf, f->last_mtime);
648 printf ("# Last modified %s\n", buf);
650 printf ("# File has%s been updated.\n",
651 f->updated ? "" : " not");
652 switch (f->command_state)
654 case cs_running:
655 puts ("# Commands currently running (THIS IS A BUG).");
656 break;
657 case cs_deps_running:
658 puts ("# Dependencies commands running (THIS IS A BUG).");
659 break;
660 case cs_not_started:
661 case cs_finished:
662 switch (f->update_status)
664 case -1:
665 break;
666 case 0:
667 puts ("# Successfully updated.");
668 break;
669 case 1:
670 assert (question_flag);
671 puts ("# Needs to be updated (-q is set).");
672 break;
673 case 2:
674 puts ("# Failed to be updated.");
675 break;
676 default:
677 puts ("# Invalid value in `update_status' member!");
678 fflush (stdout);
679 fflush (stderr);
680 abort ();
682 break;
683 default:
684 puts ("# Invalid value in `command_state' member!");
685 fflush (stdout);
686 fflush (stderr);
687 abort ();
690 if (f->variables != 0)
691 print_file_variables (f);
693 if (f->cmds != 0)
694 print_commands (f->cmds);
697 void
698 print_file_data_base ()
700 register unsigned int i, nfiles, per_bucket;
701 register struct file *file;
703 puts ("\n# Files");
705 per_bucket = nfiles = 0;
706 for (i = 0; i < FILE_BUCKETS; ++i)
708 register unsigned int this_bucket = 0;
710 for (file = files[i]; file != 0; file = file->next)
712 register struct file *f;
714 ++this_bucket;
716 for (f = file; f != 0; f = f->prev)
717 print_file (f);
720 nfiles += this_bucket;
721 if (this_bucket > per_bucket)
722 per_bucket = this_bucket;
725 if (nfiles == 0)
726 puts ("\n# No files.");
727 else
729 printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
730 #ifndef NO_FLOAT
731 printf ("# average %.3f files per bucket, max %u files in one bucket.\n",
732 ((double) nfiles) / ((double) FILE_BUCKETS), per_bucket);
733 #endif
737 /* EOF */