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)
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. */
30 /* Hash table of files the makefile knows how to make. */
33 #define FILE_BUCKETS 1007
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. */
51 register struct file
*f
;
53 register unsigned int hashval
;
55 register char *lname
, *ln
;
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. */
65 lname
= (char *)malloc(strlen(name
) + 1);
66 for (n
=name
, ln
=lname
; *n
!= '\0'; ++n
, ++ln
)
67 *ln
= isupper(*n
) ? tolower(*n
) : *n
;
71 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
74 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
78 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
83 /* It was all slashes after a dot. */
95 for (n
= name
; *n
!= '\0'; ++n
)
97 hashval
%= FILE_BUCKETS
;
99 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
101 if (strieq (f
->hname
, name
))
119 register struct file
*f
, *new;
121 register unsigned int hashval
;
130 lname
= (char *)malloc (strlen (name
) + 1);
131 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
143 for (n
= name
; *n
!= '\0'; ++n
)
145 hashval
%= FILE_BUCKETS
;
147 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
148 if (strieq (f
->hname
, name
))
151 if (f
!= 0 && !f
->double_colon
)
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;
166 /* This is a completely new file. */
167 new->next
= files
[hashval
];
168 files
[hashval
] = new;
172 /* There is already a double-colon entry for this file. */
173 new->double_colon
= f
;
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. */
187 rehash_file (file
, name
)
188 register struct file
*file
;
191 char *oldname
= file
->hname
;
192 register unsigned int oldhash
;
195 while (file
->renamed
!= 0)
196 file
= file
->renamed
;
198 /* Find the hash values of the old and new names. */
201 for (n
= oldname
; *n
!= '\0'; ++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. */
212 rename_file (file
, name
)
213 register struct file
*file
;
216 rehash_file(file
, name
);
219 file
->name
= file
->hname
;
225 file_hash_enter (file
, name
, oldhash
, oldname
)
226 register struct file
*file
;
228 unsigned int oldhash
;
231 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
232 register unsigned int newhash
, newbucket
;
233 struct file
*oldfile
;
235 register struct file
*f
;
238 for (n
= name
; *n
!= '\0'; ++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
))
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
)
261 files
[oldbucket
] = f
->next
;
263 lastf
->next
= f
->next
;
266 /* Give FILE its new name. */
269 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
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
;
285 /* There is an existing file with the new name.
286 We must merge FILE into the existing file. */
288 register struct dep
*d
;
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
);
306 error (&file
->cmds
->fileinfo
,
307 "Commands for file `%s' were found by \
308 implicit rule search,",
310 error (&file
->cmds
->fileinfo
,
311 "but `%s' is now considered the same file \
314 error (&file
->cmds
->fileinfo
,
315 "Commands for `%s' will be ignored \
316 in favor of those for `%s'.",
321 /* Merge the dependencies of the two files. */
325 oldfile
->deps
= file
->deps
;
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'",
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'",
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
355 MERGE (tried_implicit
);
361 MERGE (ignore_vpath
);
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. */
374 remove_intermediates (sig
)
378 register struct file
*f
;
381 if (question_flag
|| touch_flag
)
383 if (sig
&& just_print_flag
)
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
)
393 if (f
->update_status
== -1)
394 /* If nothing would have created this file yet,
395 don't print an "rm" command for it. */
397 else if (just_print_flag
)
401 status
= unlink (f
->name
);
402 if (status
< 0 && errno
== ENOENT
)
408 error (NILF
, "*** Deleting intermediate file `%s'", f
->name
);
409 else if (!silent_flag
)
413 fputs ("rm ", stdout
);
418 fputs (f
->name
, stdout
);
422 perror_with_name ("unlink: ", f
->name
);
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. */
442 register struct file
*f
, *f2
;
443 register struct dep
*d
;
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
)
453 d
->file
= lookup_file (d
->name
);
455 d
->file
= enter_file (d
->name
);
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
)
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. */
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. */
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. */
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
)
519 ignore_errors_flag
= 1;
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
)
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
)
542 /* Set the `command_state' member of FILE and all its `also_make's. */
545 set_command_state (file
, state
)
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. */
560 file_timestamp_now ()
562 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
563 struct timespec timespec
;
564 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
565 return FILE_TIMESTAMP_FROM_S_AND_NS (timespec
.tv_sec
, timespec
.tv_nsec
);
567 return FILE_TIMESTAMP_FROM_S_AND_NS (time ((time_t *) 0), 0);
571 file_timestamp_sprintf (p
, ts
)
575 time_t t
= FILE_TIMESTAMP_S (ts
);
576 struct tm
*tm
= localtime (&t
);
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
);
583 sprintf (p
, "%ld", (long) t
);
585 sprintf (p
, "%lu", (unsigned long) t
);
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
));
602 /* Print the data base of files. */
608 register struct dep
*d
;
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
));
620 puts ("# Precious file (dependency of .PRECIOUS).");
622 puts ("# Phony target (dependency of .PHONY).");
624 puts ("# Command-line target.");
626 puts ("# A default or MAKEFILES makefile.");
627 printf ("# Implicit rule search has%s been done.\n",
628 f
->tried_implicit
? "" : " not");
630 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
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
));
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.");
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
)
655 puts ("# Commands currently running (THIS IS A BUG).");
657 case cs_deps_running
:
658 puts ("# Dependencies commands running (THIS IS A BUG).");
662 switch (f
->update_status
)
667 puts ("# Successfully updated.");
670 assert (question_flag
);
671 puts ("# Needs to be updated (-q is set).");
674 puts ("# Failed to be updated.");
677 puts ("# Invalid value in `update_status' member!");
684 puts ("# Invalid value in `command_state' member!");
690 if (f
->variables
!= 0)
691 print_file_variables (f
);
694 print_commands (f
->cmds
);
698 print_file_data_base ()
700 register unsigned int i
, nfiles
, per_bucket
;
701 register struct file
*file
;
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
;
716 for (f
= file
; f
!= 0; f
= f
->prev
)
720 nfiles
+= this_bucket
;
721 if (this_bucket
> per_bucket
)
722 per_bucket
= this_bucket
;
726 puts ("\n# No files.");
729 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
731 printf ("# average %.3f files per bucket, max %u files in one bucket.\n",
732 ((double) nfiles
) / ((double) FILE_BUCKETS
), per_bucket
);