1 /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993
2 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* Hash table of files the makefile knows how to make. */
29 #define FILE_BUCKETS 1007
31 static struct file
*files
[FILE_BUCKETS
];
33 /* Number of files with the `intermediate' flag set. */
35 unsigned int num_intermediates
= 0;
38 /* Access the hash table of all file records.
39 lookup_file given a name, return the struct file * for that name,
40 or nil if there is none.
41 enter_file similar, but create one if there is none. */
47 register struct file
*f
;
49 register unsigned int hashval
;
54 /* This is also done in parse_file_seq, so this is redundant
55 for names read from makefiles. It is here for names passed
56 on the command line. */
57 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
61 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
66 /* It was all slashes after a dot. */
70 for (n
= name
; *n
!= '\0'; ++n
)
72 hashval
%= FILE_BUCKETS
;
74 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
75 if (streq (f
->name
, name
))
84 register struct file
*f
, *new;
86 register unsigned int hashval
;
91 /* This is also done in parse_file_seq, so this is redundant
92 for names read from makefiles. It is here for names passed
93 on the command line. */
94 while (name
[0] == '.' && name
[1] == '/' && name
[2] != '\0')
98 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
104 /* It was all slashes! Move back to the dot and truncate
105 it after the first slash, so it becomes just "./". */
108 while (name
[0] != '.');
113 for (n
= name
; *n
!= '\0'; ++n
)
115 hashval
%= FILE_BUCKETS
;
117 for (f
= files
[hashval
]; f
!= 0; f
= f
->next
)
118 if (streq (f
->name
, name
))
121 if (f
!= 0 && !f
->double_colon
)
124 new = (struct file
*) xmalloc (sizeof (struct file
));
125 bzero ((char *) new, sizeof (struct file
));
127 new->update_status
= -1;
131 /* This is a completely new file. */
132 new->next
= files
[hashval
];
133 files
[hashval
] = new;
137 /* There is already a double-colon entry for this file. */
146 /* Rename FILE to NAME. This is not as simple as resetting
147 the `name' member, since it must be put in a new hash bucket,
148 and possibly merged with an existing file called NAME. */
151 rename_file (file
, name
)
152 register struct file
*file
;
155 char *oldname
= file
->name
;
156 register unsigned int oldhash
;
159 while (file
->renamed
!= 0)
160 file
= file
->renamed
;
162 /* Find the hash values of the old and new names. */
165 for (n
= oldname
; *n
!= '\0'; ++n
)
168 file_hash_enter (file
, name
, oldhash
, file
->name
);
172 file_hash_enter (file
, name
, oldhash
, oldname
)
173 register struct file
*file
;
175 unsigned int oldhash
;
178 unsigned int oldbucket
= oldhash
% FILE_BUCKETS
;
179 register unsigned int newhash
, newbucket
;
180 struct file
*oldfile
;
182 register struct file
*f
;
185 for (n
= name
; *n
!= '\0'; ++n
)
187 newbucket
= newhash
% FILE_BUCKETS
;
189 /* Look for an existing file under the new name. */
191 for (oldfile
= files
[newbucket
]; oldfile
!= 0; oldfile
= oldfile
->next
)
192 if (streq (oldfile
->name
, name
))
195 if (oldhash
!= 0 && (newbucket
!= oldbucket
|| oldfile
!= 0))
197 /* Remove FILE from its hash bucket. */
199 struct file
*lastf
= 0;
201 for (f
= files
[oldbucket
]; f
!= file
; f
= f
->next
)
205 files
[oldbucket
] = f
->next
;
207 lastf
->next
= f
->next
;
210 /* Give FILE its new name. */
212 for (f
= file
; f
!= 0; f
= f
->prev
)
217 /* There is no existing file with the new name. */
219 if (newbucket
!= oldbucket
)
221 /* Put FILE in its new hash bucket. */
222 file
->next
= files
[newbucket
];
223 files
[newbucket
] = file
;
228 /* There is an existing file with the new name.
229 We must merge FILE into the existing file. */
231 register struct dep
*d
;
235 if (oldfile
->cmds
== 0)
236 oldfile
->cmds
= file
->cmds
;
237 else if (file
->cmds
!= oldfile
->cmds
)
239 /* We have two sets of commands. We will go with the
240 one given in the rule explicitly mentioning this name,
241 but give a message to let the user know what's going on. */
242 if (oldfile
->cmds
->filename
!= 0)
243 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
244 "Commands were specified for \
245 file `%s' at %s:%u,",
246 oldname
, oldfile
->cmds
->filename
,
247 oldfile
->cmds
->lineno
);
249 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
250 "Commands for file `%s' were found by \
251 implicit rule search,",
253 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
254 "but `%s' is now considered the same file \
257 makefile_error (file
->cmds
->filename
, file
->cmds
->lineno
,
258 "Commands for `%s' will be ignored \
259 in favor of those for `%s'.",
264 /* Merge the dependencies of the two files. */
268 oldfile
->deps
= file
->deps
;
273 d
->next
= file
->deps
;
276 merge_variable_set_lists (&oldfile
->variables
, file
->variables
);
278 if (oldfile
->double_colon
&& !file
->double_colon
)
279 fatal ("can't rename single-colon `%s' to double-colon `%s'",
281 if (!oldfile
->double_colon
&& file
->double_colon
)
282 fatal ("can't rename double-colon `%s' to single-colon `%s'",
285 if (file
->last_mtime
> oldfile
->last_mtime
)
286 /* %%% Kludge so -W wins on a file that gets vpathized. */
287 oldfile
->last_mtime
= file
->last_mtime
;
289 #define MERGE(field) oldfile->field |= file->field
291 MERGE (tried_implicit
);
299 file
->renamed
= oldfile
;
303 /* Remove all nonprecious intermediate files.
304 If SIG is nonzero, this was caused by a fatal signal,
305 meaning that a different message will be printed, and
306 the message will go to stderr rather than stdout. */
309 remove_intermediates (sig
)
313 register struct file
*f
;
316 if (!sig
&& just_print_flag
)
320 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
321 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
322 if (f
->intermediate
&& (f
->dontcare
|| !f
->precious
))
329 status
= unlink (f
->name
);
330 if (status
< 0 && errno
== ENOENT
)
336 error ("*** Deleting file `%s'", f
->name
);
337 else if (!silent_flag
)
341 fputs ("rm ", stdout
);
345 fputs (f
->name
, stdout
);
349 perror_with_name ("unlink: ", f
->name
);
360 /* For each dependency of each file, make the `struct dep' point
361 at the appropriate `struct file' (which may have to be created).
363 Also mark the files depended on by .PRECIOUS and .PHONY. */
368 register struct file
*f
, *f2
;
369 register struct dep
*d
;
372 /* Enter each dependency name as a file. */
373 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
374 for (f
= files
[i
]; f
!= 0; f
= f
->next
)
375 for (f2
= f
; f2
!= 0; f2
= f2
->prev
)
376 for (d
= f2
->deps
; d
!= 0; d
= d
->next
)
379 d
->file
= lookup_file (d
->name
);
381 d
->file
= enter_file (d
->name
);
387 for (f
= lookup_file (".PRECIOUS"); f
!= 0; f
= f
->prev
)
388 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
389 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
392 for (f
= lookup_file (".PHONY"); f
!= 0; f
= f
->prev
)
393 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
394 for (f2
= d
->file
; f2
!= 0; f2
= f2
->prev
)
396 /* Mark this file as phony and nonexistent. */
398 f2
->last_mtime
= (time_t) -1;
401 f
= lookup_file (".EXPORT_ALL_VARIABLES");
402 if (f
!= 0 && f
->is_target
)
403 export_all_variables
= 1;
406 /* Print the data base of files. */
409 print_file_data_base ()
411 register unsigned int i
, nfiles
, per_bucket
;
412 register struct file
*file
;
413 register struct dep
*d
;
417 per_bucket
= nfiles
= 0;
418 for (i
= 0; i
< FILE_BUCKETS
; ++i
)
420 register unsigned int this_bucket
= 0;
422 for (file
= files
[i
]; file
!= 0; file
= file
->next
)
424 register struct file
*f
;
428 for (f
= file
; f
!= 0; f
= f
->prev
)
432 puts ("# Not a target:");
433 printf ("%s:%s", f
->name
, f
->double_colon
? ":" : "");
435 for (d
= f
->deps
; d
!= 0; d
= d
->next
)
436 printf (" %s", dep_name (d
));
440 puts ("# Precious file (dependency of .PRECIOUS).");
442 puts ("# Phony target (dependency of .PHONY).");
444 puts ("# Command-line target.");
446 puts ("# A default or MAKEFILES makefile.");
447 printf ("# Implicit rule search has%s been done.\n",
448 f
->tried_implicit
? "" : " not");
450 printf ("# Implicit/static pattern stem: `%s'\n", f
->stem
);
452 puts ("# File is an intermediate dependency.");
453 if (f
->also_make
!= 0)
455 fputs ("# Also makes:", stdout
);
456 for (d
= f
->also_make
; d
!= 0; d
= d
->next
)
457 printf (" %s", dep_name (d
));
460 if (f
->last_mtime
== (time_t) 0)
461 puts ("# Modification time never checked.");
462 else if (f
->last_mtime
== (time_t) -1)
463 puts ("# File does not exist.");
465 printf ("# Last modified %.24s (%ld)\n",
466 ctime (&f
->last_mtime
), (long int) f
->last_mtime
);
467 printf ("# File has%s been updated.\n",
468 f
->updated
? "" : " not");
469 switch (f
->command_state
)
472 puts ("# Commands currently running (THIS IS A BUG).");
474 case cs_deps_running
:
475 puts ("# Dependencies commands running (THIS IS A BUG).");
479 switch (f
->update_status
)
484 puts ("# Successfully updated.");
487 puts ("# Failed to be updated.");
490 puts ("# Invalid value in `update_status' member!");
497 puts ("# Invalid value in `command_state' member!");
503 if (f
->variables
!= 0)
504 print_file_variables (file
);
507 print_commands (f
->cmds
);
511 nfiles
+= this_bucket
;
512 if (this_bucket
> per_bucket
)
513 per_bucket
= this_bucket
;
517 puts ("\n# No files.");
520 printf ("\n# %u files in %u hash buckets.\n", nfiles
, FILE_BUCKETS
);
522 printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
523 ((double) FILE_BUCKETS
) / ((double) nfiles
) * 100.0, per_bucket
);