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;
41 /* Current value for pruning the scan of the goal chain (toggle 0/1). */
43 unsigned int considered
= 0;
45 /* Access the hash table of all file records.
46 lookup_file given a name, return the struct file * for that name,
47 or nil if there is none.
48 enter_file similar, but create one if there is none. */
54 register struct file
*f
;
56 register unsigned int hashval
;
58 register char *lname
, *ln
;
64 /* This is also done in parse_file_seq, so this is redundant
65 for names read from makefiles. It is here for names passed
66 on the command line. */
68 lname
= (char *)malloc(strlen(name
) + 1);
69 for (n
=name
, ln
=lname
; *n
!= '\0'; ++n
, ++ln
)
70 *ln
= isupper((unsigned char)*n
) ? tolower((unsigned char)*n
) : *n
;
74 while (name
[0] == '[' && name
[1] == ']' && name
[2] != '\0')
77 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
81 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
86 /* It was all slashes after a dot. */
98 for (n
= name
; *n
!= '\0'; ++n
)
100 hashval
%= FILE_BUCKETS
;
102 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
104 if (strieq (f
->hname
, name
))
122 register struct file
*f
, *new;
124 register unsigned int hashval
;
133 lname
= (char *)malloc (strlen (name
) + 1);
134 for (n
= name
, ln
= lname
; *n
!= '\0'; ++n
, ++ln
)
136 if (isupper((unsigned char)*n
))
137 *ln
= tolower((unsigned char)*n
);
146 for (n
= name
; *n
!= '\0'; ++n
)
148 hashval
%= FILE_BUCKETS
;
150 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
151 if (strieq (f
->hname
, name
))
154 if (f
!= 0 && !f
->double_colon
)
162 new = (struct file
*) xmalloc (sizeof (struct file
));
163 bzero ((char *) new, sizeof (struct file
));
164 new->name
= new->hname
= name
;
165 new->update_status
= -1;
169 /* This is a completely new file. */
170 new->next
= files
[hashval
];
171 files
[hashval
] = new;
175 /* There is already a double-colon entry for this file. */
176 new->double_colon
= f
;
185 /* Rehash FILE to NAME. This is not as simple as resetting
186 the `hname' member, since it must be put in a new hash bucket,
187 and possibly merged with an existing file called NAME. */
190 rehash_file (file
, name
)
191 register struct file
*file
;
194 char *oldname
= file
->hname
;
195 register unsigned int oldhash
;
198 while (file
->renamed
!= 0)
199 file
= file
->renamed
;
201 /* Find the hash values of the old and new names. */
204 for (n
= oldname
; *n
!= '\0'; ++n
)
207 file_hash_enter (file
, name
, oldhash
, file
->name
);
210 /* Rename FILE to NAME. This is not as simple as resetting
211 the `name' member, since it must be put in a new hash bucket,
212 and possibly merged with an existing file called NAME. */
215 rename_file (file
, name
)
216 register struct file
*file
;
219 rehash_file(file
, name
);
222 file
->name
= file
->hname
;
228 file_hash_enter (file
, name
, oldhash
, oldname
)
229 register struct file
*file
;
231 unsigned int oldhash
;
234 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
235 register unsigned int newhash
, newbucket
;
236 struct file
*oldfile
;
238 register struct file
*f
;
241 for (n
= name
; *n
!= '\0'; ++n
)
243 newbucket
= newhash
% FILE_BUCKETS
;
245 /* Look for an existing file under the new name. */
247 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
248 if (strieq (oldfile
->hname
, name
))
251 /* If the old file is the same as the new file, something's wrong. */
252 assert (oldfile
!= file
);
254 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
256 /* Remove FILE from its hash bucket. */
258 struct file
*lastf
= 0;
260 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
264 files
[oldbucket
] = f
->next
;
266 lastf
->next
= f
->next
;
269 /* Give FILE its new name. */
272 for (f
= file
->double_colon
; f
!= 0; f
= f
->prev
)
277 /* There is no existing file with the new name. */
279 if (newbucket
!= oldbucket
)
281 /* Put FILE in its new hash bucket. */
282 file
->next
= files
[newbucket
];
283 files
[newbucket
] = file
;
288 /* There is an existing file with the new name.
289 We must merge FILE into the existing file. */
291 register struct dep
*d
;
295 if (oldfile
->cmds
== 0)
296 oldfile
->cmds
= file
->cmds
;
297 else if (file
->cmds
!= oldfile
->cmds
)
299 /* We have two sets of commands. We will go with the
300 one given in the rule explicitly mentioning this name,
301 but give a message to let the user know what's going on. */
302 if (oldfile
->cmds
->fileinfo
.filenm
!= 0)
303 error (&file
->cmds
->fileinfo
,
304 _("Commands were specified for \
305 file `%s' at %s:%lu,"),
306 oldname
, oldfile
->cmds
->fileinfo
.filenm
,
307 oldfile
->cmds
->fileinfo
.lineno
);
309 error (&file
->cmds
->fileinfo
,
310 _("Commands for file `%s' were found by \
311 implicit rule search,"),
313 error (&file
->cmds
->fileinfo
,
314 _("but `%s' is now considered the same file \
317 error (&file
->cmds
->fileinfo
,
318 _("Commands for `%s' will be ignored \
319 in favor of those for `%s'."),
324 /* Merge the dependencies of the two files. */
328 oldfile
->deps
= file
->deps
;
333 d
->next
= file
->deps
;
336 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
338 if (oldfile
->double_colon
&& file
->is_target
&& !file
->double_colon
)
339 fatal (NILF
, _("can't rename single-colon `%s' to double-colon `%s'"),
341 if (!oldfile
->double_colon
&& file
->double_colon
)
343 if (oldfile
->is_target
)
344 fatal (NILF
, _("can't rename double-colon `%s' to single-colon `%s'"),
347 oldfile
->double_colon
= file
->double_colon
;
350 if (file
->last_mtime
> oldfile
->last_mtime
)
351 /* %%% Kludge so -W wins on a file that gets vpathized. */
352 oldfile
->last_mtime
= file
->last_mtime
;
354 oldfile
->mtime_before_update
= file
->mtime_before_update
;
356 #define MERGE(field) oldfile->field |= file->field
358 MERGE (tried_implicit
);
364 MERGE (ignore_vpath
);
367 file
->renamed
= oldfile
;
371 /* Remove all nonprecious intermediate files.
372 If SIG is nonzero, this was caused by a fatal signal,
373 meaning that a different message will be printed, and
374 the message will go to stderr rather than stdout. */
377 remove_intermediates (sig
)
381 register struct file
*f
;
384 if (question_flag
|| touch_flag
)
386 if (sig
&& just_print_flag
)
390 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
391 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
392 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
)
396 if (f
->update_status
== -1)
397 /* If nothing would have created this file yet,
398 don't print an "rm" command for it. */
400 else if (just_print_flag
)
404 status
= unlink (f
->name
);
405 if (status
< 0 && errno
== ENOENT
)
411 error (NILF
, _("*** Deleting intermediate file `%s'"), f
->name
);
412 else if (!silent_flag
)
416 fputs ("rm ", stdout
);
421 fputs (f
->name
, stdout
);
425 perror_with_name ("unlink: ", f
->name
);
436 /* For each dependency of each file, make the `struct dep' point
437 at the appropriate `struct file' (which may have to be created).
439 Also mark the files depended on by .PRECIOUS, .PHONY, .SILENT,
440 and various other special targets. */
445 register struct file
*f
, *f2
;
446 register struct dep
*d
;
449 /* Enter each dependency name as a file. */
450 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
451 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
452 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
453 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
456 d
->file
= lookup_file (d
->name
);
458 d
->file
= enter_file (d
->name
);
464 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
465 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
466 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
469 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
470 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
471 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
473 /* Mark this file as phony and nonexistent. */
475 f2
->last_mtime
= (FILE_TIMESTAMP
) -1;
476 f2
->mtime_before_update
= (FILE_TIMESTAMP
) -1;
479 for (f
= lookup_file (".INTERMEDIATE"); f
!= 0; f
= f
->prev
)
481 /* .INTERMEDIATE with deps listed
482 marks those deps as intermediate files. */
483 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
484 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
485 f2
->intermediate
= 1;
486 /* .INTERMEDIATE with no deps does nothing.
487 Marking all files as intermediates is useless
488 since the goal targets would be deleted after they are built. */
491 for (f
= lookup_file (".SECONDARY"); f
!= 0; f
= f
->prev
)
493 /* .SECONDARY with deps listed
494 marks those deps as intermediate files
495 in that they don't get rebuilt if not actually needed;
496 but unlike real intermediate files,
497 these are not deleted after make finishes. */
500 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
501 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
502 f2
->intermediate
= f2
->secondary
= 1;
504 /* .SECONDARY with no deps listed marks *all* files that way. */
508 for (i
= 0; i
< FILE_BUCKETS
; i
++)
509 for (f2
= files
[i
]; f2
; f2
= f2
->next
)
510 f2
->intermediate
= f2
->secondary
= 1;
514 f
= lookup_file (".EXPORT_ALL_VARIABLES");
515 if (f
!= 0 && f
->is_target
)
516 export_all_variables
= 1;
518 f
= lookup_file (".IGNORE");
519 if (f
!= 0 && f
->is_target
)
522 ignore_errors_flag
= 1;
524 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
525 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
526 f2
->command_flags
|= COMMANDS_NOERROR
;
529 f
= lookup_file (".SILENT");
530 if (f
!= 0 && f
->is_target
)
535 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
536 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
537 f2
->command_flags
|= COMMANDS_SILENT
;
540 f
= lookup_file (".POSIX");
541 if (f
!= 0 && f
->is_target
)
544 f
= lookup_file (".NOTPARALLEL");
545 if (f
!= 0 && f
->is_target
)
549 /* Set the `command_state' member of FILE and all its `also_make's. */
552 set_command_state (file
, state
)
558 file
->command_state
= state
;
560 for (d
= file
->also_make
; d
!= 0; d
= d
->next
)
561 d
->file
->command_state
= state
;
564 /* Get and print file timestamps. */
567 file_timestamp_now ()
569 #if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
570 struct timespec timespec
;
571 if (clock_gettime (CLOCK_REALTIME
, ×pec
) == 0)
572 return FILE_TIMESTAMP_FROM_S_AND_NS (timespec
.tv_sec
, timespec
.tv_nsec
);
574 return FILE_TIMESTAMP_FROM_S_AND_NS (time ((time_t *) 0), 0);
578 file_timestamp_sprintf (p
, ts
)
582 time_t t
= FILE_TIMESTAMP_S (ts
);
583 struct tm
*tm
= localtime (&t
);
586 sprintf (p
, "%04d-%02d-%02d %02d:%02d:%02d",
587 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
588 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
590 sprintf (p
, "%ld", (long) t
);
592 sprintf (p
, "%lu", (unsigned long) t
);
595 /* Append nanoseconds as a fraction, but remove trailing zeros.
596 We don't know the actual timestamp resolution, since clock_getres
597 applies only to local times, whereas this timestamp might come
598 from a remote filesystem. So removing trailing zeros is the
599 best guess that we can do. */
600 sprintf (p
, ".%09ld", (long) FILE_TIMESTAMP_NS (ts
));
609 /* Print the data base of files. */
615 register struct dep
*d
;
619 puts (_("# Not a target:"));
620 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
622 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
623 printf (" %s", dep_name (d
));
627 puts (_("# Precious file (prerequisite of .PRECIOUS)."));
629 puts (_("# Phony target (prerequisite of .PHONY)."));
631 puts (_("# Command-line target."));
633 puts (_("# A default or MAKEFILES makefile."));
634 printf (_("# Implicit rule search has%s been done.\n"),
635 f
->tried_implicit
? "" : _(" not"));
637 printf (_("# Implicit/static pattern stem: `%s'\n"), f
->stem
);
639 puts (_("# File is an intermediate prerequisite."));
640 if (f
->also_make
!= 0)
642 fputs (_("# Also makes:"), stdout
);
643 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
644 printf (" %s", dep_name (d
));
647 if (f
->last_mtime
== 0)
648 puts (_("# Modification time never checked."));
649 else if (f
->last_mtime
== (FILE_TIMESTAMP
) -1)
650 puts (_("# File does not exist."));
653 char buf
[FILE_TIMESTAMP_PRINT_LEN_BOUND
+ 1];
654 file_timestamp_sprintf (buf
, f
->last_mtime
);
655 printf (_("# Last modified %s\n"), buf
);
658 ? _("# File has been updated.") : _("# File has not been updated."));
659 switch (f
->command_state
)
662 puts (_("# Commands currently running (THIS IS A BUG)."));
664 case cs_deps_running
:
665 puts (_("# Dependencies commands running (THIS IS A BUG)."));
669 switch (f
->update_status
)
674 puts (_("# Successfully updated."));
677 assert (question_flag
);
678 puts (_("# Needs to be updated (-q is set)."));
681 puts (_("# Failed to be updated."));
684 puts (_("# Invalid value in `update_status' member!"));
691 puts (_("# Invalid value in `command_state' member!"));
697 if (f
->variables
!= 0)
698 print_file_variables (f
);
701 print_commands (f
->cmds
);
705 print_file_data_base ()
707 register unsigned int i
, nfiles
, per_bucket
;
708 register struct file
*file
;
710 puts (_("\n# Files"));
712 per_bucket
= nfiles
= 0;
713 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
715 register unsigned int this_bucket
= 0;
717 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
719 register struct file
*f
;
723 for (f
= file
; f
!= 0; f
= f
->prev
)
727 nfiles
+= this_bucket
;
728 if (this_bucket
> per_bucket
)
729 per_bucket
= this_bucket
;
733 puts (_("\n# No files."));
736 printf (_("\n# %u files in %u hash buckets.\n"), nfiles
, FILE_BUCKETS
);
738 printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
739 ((double) nfiles
) / ((double) FILE_BUCKETS
), per_bucket
);