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(*n) ? tolower(*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)
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)
545
/* Set the `command_state' member of FILE and all its `also_make's. */
548
set_command_state (file, state)
554
file->command_state = state;
556
for (d = file->also_make; d != 0; d = d->next)
557
d->file->command_state = state;
560
/* Get and print file timestamps. */
563
file_timestamp_now ()
565
#if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
566
struct timespec timespec;
567
if (clock_gettime (CLOCK_REALTIME, ×pec) == 0)
568
return FILE_TIMESTAMP_FROM_S_AND_NS (timespec.tv_sec, timespec.tv_nsec);
570
return FILE_TIMESTAMP_FROM_S_AND_NS (time ((time_t *) 0), 0);
574
file_timestamp_sprintf (p, ts)
578
time_t t = FILE_TIMESTAMP_S (ts);
579
struct tm *tm = localtime (&t);
582
sprintf (p, "%04d-%02d-%02d %02d:%02d:%02d",
583
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
584
tm->tm_hour, tm->tm_min, tm->tm_sec);
586
sprintf (p, "%ld", (long) t);
588
sprintf (p, "%lu", (unsigned long) t);
591
/* Append nanoseconds as a fraction, but remove trailing zeros.
592
We don't know the actual timestamp resolution, since clock_getres
593
applies only to local times, whereas this timestamp might come
594
from a remote filesystem. So removing trailing zeros is the
595
best guess that we can do. */
596
sprintf (p, ".%09ld", (long) FILE_TIMESTAMP_NS (ts));
605
/* Print the data base of files. */
611
register struct dep *d;
615
puts (_("# Not a target:"));
616
printf ("%s:%s", f->name, f->double_colon ? ":" : "");
618
for (d = f->deps; d != 0; d = d->next)
619
printf (" %s", dep_name (d));
623
puts (_("# Precious file (prerequisite of .PRECIOUS)."));
625
puts (_("# Phony target (prerequisite of .PHONY)."));
627
puts (_("# Command-line target."));
629
puts (_("# A default or MAKEFILES makefile."));
630
printf (_("# Implicit rule search has%s been done.\n"),
631
f->tried_implicit ? "" : _(" not"));
633
printf (_("# Implicit/static pattern stem: `%s'\n"), f->stem);
635
puts (_("# File is an intermediate prerequisite."));
636
if (f->also_make != 0)
638
fputs (_("# Also makes:"), stdout);
639
for (d = f->also_make; d != 0; d = d->next)
640
printf (" %s", dep_name (d));
643
if (f->last_mtime == 0)
644
puts (_("# Modification time never checked."));
645
else if (f->last_mtime == (FILE_TIMESTAMP) -1)
646
puts (_("# File does not exist."));
649
char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
650
file_timestamp_sprintf (buf, f->last_mtime);
651
printf (_("# Last modified %s\n"), buf);
653
printf (_("# File has%s been updated.\n"),
654
f->updated ? "" : _(" not"));
655
switch (f->command_state)
658
puts (_("# Commands currently running (THIS IS A BUG)."));
660
case cs_deps_running:
661
puts (_("# Dependencies commands running (THIS IS A BUG)."));
665
switch (f->update_status)
670
puts (_("# Successfully updated."));
673
assert (question_flag);
674
puts (_("# Needs to be updated (-q is set)."));
677
puts (_("# Failed to be updated."));
680
puts (_("# Invalid value in `update_status' member!"));
687
puts (_("# Invalid value in `command_state' member!"));
693
if (f->variables != 0)
694
print_file_variables (f);
697
print_commands (f->cmds);
701
print_file_data_base ()
703
register unsigned int i, nfiles, per_bucket;
704
register struct file *file;
706
puts (_("\n# Files"));
708
per_bucket = nfiles = 0;
709
for (i = 0; i < FILE_BUCKETS; ++i)
711
register unsigned int this_bucket = 0;
713
for (file = files[i]; file != 0; file = file->next)
715
register struct file *f;
719
for (f = file; f != 0; f = f->prev)
723
nfiles += this_bucket;
724
if (this_bucket > per_bucket)
725
per_bucket = this_bucket;
729
puts (_("\n# No files."));
732
printf (_("\n# %u files in %u hash buckets.\n"), nfiles, FILE_BUCKETS);
734
printf (_("# average %.3f files per bucket, max %u files in one bucket.\n"),
735
((double) nfiles) / ((double) FILE_BUCKETS), per_bucket);