| 1 | /* Implicit rule searching for GNU Make. | 
|---|
| 2 | Copyright (C) 1988,89,90,91,92,93,94,97,2000 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of GNU Make. | 
|---|
| 4 |  | 
|---|
| 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. | 
|---|
| 9 |  | 
|---|
| 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. | 
|---|
| 14 |  | 
|---|
| 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.  */ | 
|---|
| 19 |  | 
|---|
| 20 | #include "make.h" | 
|---|
| 21 | #include "filedef.h" | 
|---|
| 22 | #include "rule.h" | 
|---|
| 23 | #include "dep.h" | 
|---|
| 24 | #include "debug.h" | 
|---|
| 25 |  | 
|---|
| 26 | static int pattern_search PARAMS ((struct file *file, int archive, unsigned int depth, | 
|---|
| 27 | unsigned int recursions)); | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | /* For a FILE which has no commands specified, try to figure out some | 
|---|
| 31 | from the implicit pattern rules. | 
|---|
| 32 | Returns 1 if a suitable implicit rule was found, | 
|---|
| 33 | after modifying FILE to contain the appropriate commands and deps, | 
|---|
| 34 | or returns 0 if no implicit rule was found.  */ | 
|---|
| 35 |  | 
|---|
| 36 | int | 
|---|
| 37 | try_implicit_rule (struct file *file, unsigned int depth) | 
|---|
| 38 | { | 
|---|
| 39 | DBF (DB_IMPLICIT, _("Looking for an implicit rule for `%s'.\n")); | 
|---|
| 40 |  | 
|---|
| 41 | /* The order of these searches was previously reversed.  My logic now is | 
|---|
| 42 | that since the non-archive search uses more information in the target | 
|---|
| 43 | (the archive search omits the archive name), it is more specific and | 
|---|
| 44 | should come first.  */ | 
|---|
| 45 |  | 
|---|
| 46 | if (pattern_search (file, 0, depth, 0)) | 
|---|
| 47 | return 1; | 
|---|
| 48 |  | 
|---|
| 49 | #ifndef NO_ARCHIVES | 
|---|
| 50 | /* If this is an archive member reference, use just the | 
|---|
| 51 | archive member name to search for implicit rules.  */ | 
|---|
| 52 | if (ar_name (file->name)) | 
|---|
| 53 | { | 
|---|
| 54 | DBF (DB_IMPLICIT, | 
|---|
| 55 | _("Looking for archive-member implicit rule for `%s'.\n")); | 
|---|
| 56 | if (pattern_search (file, 1, depth, 0)) | 
|---|
| 57 | return 1; | 
|---|
| 58 | } | 
|---|
| 59 | #endif | 
|---|
| 60 |  | 
|---|
| 61 | return 0; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 | /* Search the pattern rules for a rule with an existing dependency to make | 
|---|
| 67 | FILE.  If a rule is found, the appropriate commands and deps are put in FILE | 
|---|
| 68 | and 1 is returned.  If not, 0 is returned. | 
|---|
| 69 |  | 
|---|
| 70 | If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)".  A rule for | 
|---|
| 71 | "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into | 
|---|
| 72 | directory and filename parts. | 
|---|
| 73 |  | 
|---|
| 74 | If an intermediate file is found by pattern search, the intermediate file | 
|---|
| 75 | is set up as a target by the recursive call and is also made a dependency | 
|---|
| 76 | of FILE. | 
|---|
| 77 |  | 
|---|
| 78 | DEPTH is used for debugging messages.  */ | 
|---|
| 79 |  | 
|---|
| 80 | static int | 
|---|
| 81 | pattern_search (struct file *file, int archive, | 
|---|
| 82 | unsigned int depth, unsigned int recursions) | 
|---|
| 83 | { | 
|---|
| 84 | /* Filename we are searching for a rule for.  */ | 
|---|
| 85 | char *filename = archive ? strchr (file->name, '(') : file->name; | 
|---|
| 86 |  | 
|---|
| 87 | /* Length of FILENAME.  */ | 
|---|
| 88 | unsigned int namelen = strlen (filename); | 
|---|
| 89 |  | 
|---|
| 90 | /* The last slash in FILENAME (or nil if there is none).  */ | 
|---|
| 91 | char *lastslash; | 
|---|
| 92 |  | 
|---|
| 93 | /* This is a file-object used as an argument in | 
|---|
| 94 | recursive calls.  It never contains any data | 
|---|
| 95 | except during a recursive call.  */ | 
|---|
| 96 | struct file *intermediate_file = 0; | 
|---|
| 97 |  | 
|---|
| 98 | /* List of dependencies found recursively.  */ | 
|---|
| 99 | struct file **intermediate_files | 
|---|
| 100 | = (struct file **) xmalloc (max_pattern_deps * sizeof (struct file *)); | 
|---|
| 101 |  | 
|---|
| 102 | /* List of the patterns used to find intermediate files.  */ | 
|---|
| 103 | char **intermediate_patterns | 
|---|
| 104 | = (char **) alloca (max_pattern_deps * sizeof (char *)); | 
|---|
| 105 |  | 
|---|
| 106 | /* This buffer records all the dependencies actually found for a rule.  */ | 
|---|
| 107 | char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *)); | 
|---|
| 108 | /* Number of dep names now in FOUND_FILES.  */ | 
|---|
| 109 | unsigned int deps_found = 0; | 
|---|
| 110 |  | 
|---|
| 111 | /* Names of possible dependencies are constructed in this buffer.  */ | 
|---|
| 112 | register char *depname = (char *) alloca (namelen + max_pattern_dep_length); | 
|---|
| 113 |  | 
|---|
| 114 | /* The start and length of the stem of FILENAME for the current rule.  */ | 
|---|
| 115 | register char *stem = 0; | 
|---|
| 116 | register unsigned int stemlen = 0; | 
|---|
| 117 | register unsigned int fullstemlen = 0; | 
|---|
| 118 |  | 
|---|
| 119 | /* Buffer in which we store all the rules that are possibly applicable.  */ | 
|---|
| 120 | struct rule **tryrules | 
|---|
| 121 | = (struct rule **) xmalloc (num_pattern_rules * max_pattern_targets | 
|---|
| 122 | * sizeof (struct rule *)); | 
|---|
| 123 |  | 
|---|
| 124 | /* Number of valid elements in TRYRULES.  */ | 
|---|
| 125 | unsigned int nrules; | 
|---|
| 126 |  | 
|---|
| 127 | /* The numbers of the rule targets of each rule | 
|---|
| 128 | in TRYRULES that matched the target file.  */ | 
|---|
| 129 | unsigned int *matches | 
|---|
| 130 | = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int)); | 
|---|
| 131 |  | 
|---|
| 132 | /* Each element is nonzero if LASTSLASH was used in | 
|---|
| 133 | matching the corresponding element of TRYRULES.  */ | 
|---|
| 134 | char *checked_lastslash | 
|---|
| 135 | = (char *) alloca (num_pattern_rules * sizeof (char)); | 
|---|
| 136 |  | 
|---|
| 137 | /* The index in TRYRULES of the rule we found.  */ | 
|---|
| 138 | unsigned int foundrule; | 
|---|
| 139 |  | 
|---|
| 140 | /* Nonzero if should consider intermediate files as dependencies.  */ | 
|---|
| 141 | int intermed_ok; | 
|---|
| 142 |  | 
|---|
| 143 | /* Nonzero if we have matched a pattern-rule target | 
|---|
| 144 | that is not just `%'.  */ | 
|---|
| 145 | int specific_rule_matched = 0; | 
|---|
| 146 |  | 
|---|
| 147 | register unsigned int i = 0;  /* uninit checks OK */ | 
|---|
| 148 | register struct rule *rule; | 
|---|
| 149 | register struct dep *dep; | 
|---|
| 150 |  | 
|---|
| 151 | char *p, *vp; | 
|---|
| 152 |  | 
|---|
| 153 | #ifndef NO_ARCHIVES | 
|---|
| 154 | if (archive || ar_name (filename)) | 
|---|
| 155 | lastslash = 0; | 
|---|
| 156 | else | 
|---|
| 157 | #endif | 
|---|
| 158 | { | 
|---|
| 159 | /* Set LASTSLASH to point at the last slash in FILENAME | 
|---|
| 160 | but not counting any slash at the end.  (foo/bar/ counts as | 
|---|
| 161 | bar/ in directory foo/, not empty in directory foo/bar/.)  */ | 
|---|
| 162 | #ifdef VMS | 
|---|
| 163 | lastslash = strrchr (filename, ']'); | 
|---|
| 164 | if (lastslash == 0) | 
|---|
| 165 | lastslash = strrchr (filename, ':'); | 
|---|
| 166 | #else | 
|---|
| 167 | lastslash = strrchr (filename, '/'); | 
|---|
| 168 | #ifdef HAVE_DOS_PATHS | 
|---|
| 169 | /* Handle backslashes (possibly mixed with forward slashes) | 
|---|
| 170 | and the case of "d:file".  */ | 
|---|
| 171 | { | 
|---|
| 172 | char *bslash = strrchr (filename, '\\'); | 
|---|
| 173 | if (lastslash == 0 || bslash > lastslash) | 
|---|
| 174 | lastslash = bslash; | 
|---|
| 175 | if (lastslash == 0 && filename[0] && filename[1] == ':') | 
|---|
| 176 | lastslash = filename + 1; | 
|---|
| 177 | } | 
|---|
| 178 | #endif | 
|---|
| 179 | #endif | 
|---|
| 180 | if (lastslash != 0 && lastslash[1] == '\0') | 
|---|
| 181 | lastslash = 0; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | /* First see which pattern rules match this target | 
|---|
| 185 | and may be considered.  Put them in TRYRULES.  */ | 
|---|
| 186 |  | 
|---|
| 187 | nrules = 0; | 
|---|
| 188 | for (rule = pattern_rules; rule != 0; rule = rule->next) | 
|---|
| 189 | { | 
|---|
| 190 | /* If the pattern rule has deps but no commands, ignore it. | 
|---|
| 191 | Users cancel built-in rules by redefining them without commands.  */ | 
|---|
| 192 | if (rule->deps != 0 && rule->cmds == 0) | 
|---|
| 193 | continue; | 
|---|
| 194 |  | 
|---|
| 195 | /* If this rule is in use by a parent pattern_search, | 
|---|
| 196 | don't use it here.  */ | 
|---|
| 197 | if (rule->in_use) | 
|---|
| 198 | { | 
|---|
| 199 | DBS (DB_IMPLICIT, (_("Avoiding implicit rule recursion.\n"))); | 
|---|
| 200 | continue; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | for (i = 0; rule->targets[i] != 0; ++i) | 
|---|
| 204 | { | 
|---|
| 205 | char *target = rule->targets[i]; | 
|---|
| 206 | char *suffix = rule->suffixes[i]; | 
|---|
| 207 | int check_lastslash; | 
|---|
| 208 |  | 
|---|
| 209 | /* Rules that can match any filename and are not terminal | 
|---|
| 210 | are ignored if we're recursing, so that they cannot be | 
|---|
| 211 | intermediate files.  */ | 
|---|
| 212 | if (recursions > 0 && target[1] == '\0' && !rule->terminal) | 
|---|
| 213 | continue; | 
|---|
| 214 |  | 
|---|
| 215 | if (rule->lens[i] > namelen) | 
|---|
| 216 | /* It can't possibly match.  */ | 
|---|
| 217 | continue; | 
|---|
| 218 |  | 
|---|
| 219 | /* From the lengths of the filename and the pattern parts, | 
|---|
| 220 | find the stem: the part of the filename that matches the %.  */ | 
|---|
| 221 | stem = filename + (suffix - target - 1); | 
|---|
| 222 | stemlen = namelen - rule->lens[i] + 1; | 
|---|
| 223 |  | 
|---|
| 224 | /* Set CHECK_LASTSLASH if FILENAME contains a directory | 
|---|
| 225 | prefix and the target pattern does not contain a slash.  */ | 
|---|
| 226 |  | 
|---|
| 227 | #ifdef VMS | 
|---|
| 228 | check_lastslash = lastslash != 0 | 
|---|
| 229 | && ((strchr (target, ']') == 0) | 
|---|
| 230 | && (strchr (target, ':') == 0)); | 
|---|
| 231 | #else | 
|---|
| 232 | check_lastslash = lastslash != 0 && strchr (target, '/') == 0; | 
|---|
| 233 | #endif | 
|---|
| 234 | if (check_lastslash) | 
|---|
| 235 | { | 
|---|
| 236 | /* In that case, don't include the | 
|---|
| 237 | directory prefix in STEM here.  */ | 
|---|
| 238 | unsigned int difference = lastslash - filename + 1; | 
|---|
| 239 | if (difference > stemlen) | 
|---|
| 240 | continue; | 
|---|
| 241 | stemlen -= difference; | 
|---|
| 242 | stem += difference; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | /* Check that the rule pattern matches the text before the stem.  */ | 
|---|
| 246 | if (check_lastslash) | 
|---|
| 247 | { | 
|---|
| 248 | if (stem > (lastslash + 1) | 
|---|
| 249 | && !strneq (target, lastslash + 1, stem - lastslash - 1)) | 
|---|
| 250 | continue; | 
|---|
| 251 | } | 
|---|
| 252 | else if (stem > filename | 
|---|
| 253 | && !strneq (target, filename, stem - filename)) | 
|---|
| 254 | continue; | 
|---|
| 255 |  | 
|---|
| 256 | /* Check that the rule pattern matches the text after the stem. | 
|---|
| 257 | We could test simply use streq, but this way we compare the | 
|---|
| 258 | first two characters immediately.  This saves time in the very | 
|---|
| 259 | common case where the first character matches because it is a | 
|---|
| 260 | period.  */ | 
|---|
| 261 | if (*suffix != stem[stemlen] | 
|---|
| 262 | || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1]))) | 
|---|
| 263 | continue; | 
|---|
| 264 |  | 
|---|
| 265 | /* Record if we match a rule that not all filenames will match.  */ | 
|---|
| 266 | if (target[1] != '\0') | 
|---|
| 267 | specific_rule_matched = 1; | 
|---|
| 268 |  | 
|---|
| 269 | /* A rule with no dependencies and no commands exists solely to set | 
|---|
| 270 | specific_rule_matched when it matches.  Don't try to use it.  */ | 
|---|
| 271 | if (rule->deps == 0 && rule->cmds == 0) | 
|---|
| 272 | continue; | 
|---|
| 273 |  | 
|---|
| 274 | /* Record this rule in TRYRULES and the index of the matching | 
|---|
| 275 | target in MATCHES.  If several targets of the same rule match, | 
|---|
| 276 | that rule will be in TRYRULES more than once.  */ | 
|---|
| 277 | tryrules[nrules] = rule; | 
|---|
| 278 | matches[nrules] = i; | 
|---|
| 279 | checked_lastslash[nrules] = check_lastslash; | 
|---|
| 280 | ++nrules; | 
|---|
| 281 | } | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | /* If we have found a matching rule that won't match all filenames, | 
|---|
| 285 | retroactively reject any non-"terminal" rules that do always match.  */ | 
|---|
| 286 | if (specific_rule_matched) | 
|---|
| 287 | for (i = 0; i < nrules; ++i) | 
|---|
| 288 | if (!tryrules[i]->terminal) | 
|---|
| 289 | { | 
|---|
| 290 | register unsigned int j; | 
|---|
| 291 | for (j = 0; tryrules[i]->targets[j] != 0; ++j) | 
|---|
| 292 | if (tryrules[i]->targets[j][1] == '\0') | 
|---|
| 293 | break; | 
|---|
| 294 | if (tryrules[i]->targets[j] != 0) | 
|---|
| 295 | tryrules[i] = 0; | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | /* Try each rule once without intermediate files, then once with them.  */ | 
|---|
| 299 | for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok) | 
|---|
| 300 | { | 
|---|
| 301 | /* Try each pattern rule till we find one that applies. | 
|---|
| 302 | If it does, copy the names of its dependencies (as substituted) | 
|---|
| 303 | and store them in FOUND_FILES.  DEPS_FOUND is the number of them.  */ | 
|---|
| 304 |  | 
|---|
| 305 | for (i = 0; i < nrules; i++) | 
|---|
| 306 | { | 
|---|
| 307 | int check_lastslash; | 
|---|
| 308 |  | 
|---|
| 309 | rule = tryrules[i]; | 
|---|
| 310 |  | 
|---|
| 311 | /* RULE is nil when we discover that a rule, | 
|---|
| 312 | already placed in TRYRULES, should not be applied.  */ | 
|---|
| 313 | if (rule == 0) | 
|---|
| 314 | continue; | 
|---|
| 315 |  | 
|---|
| 316 | /* Reject any terminal rules if we're | 
|---|
| 317 | looking to make intermediate files.  */ | 
|---|
| 318 | if (intermed_ok && rule->terminal) | 
|---|
| 319 | continue; | 
|---|
| 320 |  | 
|---|
| 321 | /* Mark this rule as in use so a recursive | 
|---|
| 322 | pattern_search won't try to use it.  */ | 
|---|
| 323 | rule->in_use = 1; | 
|---|
| 324 |  | 
|---|
| 325 | /* From the lengths of the filename and the matching pattern parts, | 
|---|
| 326 | find the stem: the part of the filename that matches the %.  */ | 
|---|
| 327 | stem = filename | 
|---|
| 328 | + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1; | 
|---|
| 329 | stemlen = namelen - rule->lens[matches[i]] + 1; | 
|---|
| 330 | check_lastslash = checked_lastslash[i]; | 
|---|
| 331 | if (check_lastslash) | 
|---|
| 332 | { | 
|---|
| 333 | stem += lastslash - filename + 1; | 
|---|
| 334 | stemlen -= (lastslash - filename) + 1; | 
|---|
| 335 | } | 
|---|
| 336 |  | 
|---|
| 337 | DBS (DB_IMPLICIT, (_("Trying pattern rule with stem `%.*s'.\n"), | 
|---|
| 338 | (int) stemlen, stem)); | 
|---|
| 339 |  | 
|---|
| 340 | /* Try each dependency; see if it "exists".  */ | 
|---|
| 341 |  | 
|---|
| 342 | deps_found = 0; | 
|---|
| 343 | for (dep = rule->deps; dep != 0; dep = dep->next) | 
|---|
| 344 | { | 
|---|
| 345 | /* If the dependency name has a %, substitute the stem.  */ | 
|---|
| 346 | p = strchr (dep_name (dep), '%'); | 
|---|
| 347 | if (p != 0) | 
|---|
| 348 | { | 
|---|
| 349 | register unsigned int i; | 
|---|
| 350 | if (check_lastslash) | 
|---|
| 351 | { | 
|---|
| 352 | /* Copy directory name from the original FILENAME.  */ | 
|---|
| 353 | i = lastslash - filename + 1; | 
|---|
| 354 | bcopy (filename, depname, i); | 
|---|
| 355 | } | 
|---|
| 356 | else | 
|---|
| 357 | i = 0; | 
|---|
| 358 | bcopy (dep_name (dep), depname + i, p - dep_name (dep)); | 
|---|
| 359 | i += p - dep_name (dep); | 
|---|
| 360 | bcopy (stem, depname + i, stemlen); | 
|---|
| 361 | i += stemlen; | 
|---|
| 362 | strcpy (depname + i, p + 1); | 
|---|
| 363 | p = depname; | 
|---|
| 364 | } | 
|---|
| 365 | else | 
|---|
| 366 | p = dep_name (dep); | 
|---|
| 367 |  | 
|---|
| 368 | /* P is now the actual dependency name as substituted.  */ | 
|---|
| 369 |  | 
|---|
| 370 | if (file_impossible_p (p)) | 
|---|
| 371 | { | 
|---|
| 372 | /* If this dependency has already been ruled | 
|---|
| 373 | "impossible", then the rule fails and don't | 
|---|
| 374 | bother trying it on the second pass either | 
|---|
| 375 | since we know that will fail too.  */ | 
|---|
| 376 | DBS (DB_IMPLICIT, | 
|---|
| 377 | (p == depname | 
|---|
| 378 | ? _("Rejecting impossible implicit prerequisite `%s'.\n") | 
|---|
| 379 | : _("Rejecting impossible rule prerequisite `%s'.\n"), | 
|---|
| 380 | p)); | 
|---|
| 381 | tryrules[i] = 0; | 
|---|
| 382 | break; | 
|---|
| 383 | } | 
|---|
| 384 |  | 
|---|
| 385 | intermediate_files[deps_found] = 0; | 
|---|
| 386 |  | 
|---|
| 387 | DBS (DB_IMPLICIT, | 
|---|
| 388 | (p == depname | 
|---|
| 389 | ? _("Trying implicit prerequisite `%s'.\n") | 
|---|
| 390 | : _("Trying rule prerequisite `%s'.\n"), p)); | 
|---|
| 391 |  | 
|---|
| 392 | /* The DEP->changed flag says that this dependency resides in a | 
|---|
| 393 | nonexistent directory.  So we normally can skip looking for | 
|---|
| 394 | the file.  However, if CHECK_LASTSLASH is set, then the | 
|---|
| 395 | dependency file we are actually looking for is in a different | 
|---|
| 396 | directory (the one gotten by prepending FILENAME's directory), | 
|---|
| 397 | so it might actually exist.  */ | 
|---|
| 398 |  | 
|---|
| 399 | if (lookup_file (p) != 0 | 
|---|
| 400 | || ((!dep->changed || check_lastslash) && file_exists_p (p))) | 
|---|
| 401 | { | 
|---|
| 402 | found_files[deps_found++] = xstrdup (p); | 
|---|
| 403 | continue; | 
|---|
| 404 | } | 
|---|
| 405 | /* This code, given FILENAME = "lib/foo.o", dependency name | 
|---|
| 406 | "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c".  */ | 
|---|
| 407 | vp = p; | 
|---|
| 408 | if (vpath_search (&vp, (FILE_TIMESTAMP *) 0)) | 
|---|
| 409 | { | 
|---|
| 410 | DBS (DB_IMPLICIT, | 
|---|
| 411 | (_("Found prerequisite `%s' as VPATH `%s'\n"), p, vp)); | 
|---|
| 412 | strcpy (vp, p); | 
|---|
| 413 | found_files[deps_found++] = vp; | 
|---|
| 414 | continue; | 
|---|
| 415 | } | 
|---|
| 416 |  | 
|---|
| 417 | /* We could not find the file in any place we should look. | 
|---|
| 418 | Try to make this dependency as an intermediate file, | 
|---|
| 419 | but only on the second pass.  */ | 
|---|
| 420 |  | 
|---|
| 421 | if (intermed_ok) | 
|---|
| 422 | { | 
|---|
| 423 | if (intermediate_file == 0) | 
|---|
| 424 | intermediate_file | 
|---|
| 425 | = (struct file *) alloca (sizeof (struct file)); | 
|---|
| 426 |  | 
|---|
| 427 | DBS (DB_IMPLICIT, | 
|---|
| 428 | (_("Looking for a rule with intermediate file `%s'.\n"), | 
|---|
| 429 | p)); | 
|---|
| 430 |  | 
|---|
| 431 | bzero ((char *) intermediate_file, sizeof (struct file)); | 
|---|
| 432 | intermediate_file->name = p; | 
|---|
| 433 | if (pattern_search (intermediate_file, 0, depth + 1, | 
|---|
| 434 | recursions + 1)) | 
|---|
| 435 | { | 
|---|
| 436 | p = xstrdup (p); | 
|---|
| 437 | intermediate_patterns[deps_found] | 
|---|
| 438 | = intermediate_file->name; | 
|---|
| 439 | intermediate_file->name = p; | 
|---|
| 440 | intermediate_files[deps_found] = intermediate_file; | 
|---|
| 441 | intermediate_file = 0; | 
|---|
| 442 | /* Allocate an extra copy to go in FOUND_FILES, | 
|---|
| 443 | because every elt of FOUND_FILES is consumed | 
|---|
| 444 | or freed later.  */ | 
|---|
| 445 | found_files[deps_found] = xstrdup (p); | 
|---|
| 446 | ++deps_found; | 
|---|
| 447 | continue; | 
|---|
| 448 | } | 
|---|
| 449 |  | 
|---|
| 450 | /* If we have tried to find P as an intermediate | 
|---|
| 451 | file and failed, mark that name as impossible | 
|---|
| 452 | so we won't go through the search again later.  */ | 
|---|
| 453 | file_impossible (p); | 
|---|
| 454 | } | 
|---|
| 455 |  | 
|---|
| 456 | /* A dependency of this rule does not exist. | 
|---|
| 457 | Therefore, this rule fails.  */ | 
|---|
| 458 | break; | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 | /* This rule is no longer `in use' for recursive searches.  */ | 
|---|
| 462 | rule->in_use = 0; | 
|---|
| 463 |  | 
|---|
| 464 | if (dep != 0) | 
|---|
| 465 | { | 
|---|
| 466 | /* This pattern rule does not apply. | 
|---|
| 467 | If some of its dependencies succeeded, | 
|---|
| 468 | free the data structure describing them.  */ | 
|---|
| 469 | while (deps_found-- > 0) | 
|---|
| 470 | { | 
|---|
| 471 | register struct file *f = intermediate_files[deps_found]; | 
|---|
| 472 | free (found_files[deps_found]); | 
|---|
| 473 | if (f != 0 | 
|---|
| 474 | && (f->stem < f->name | 
|---|
| 475 | || f->stem > f->name + strlen (f->name))) | 
|---|
| 476 | free (f->stem); | 
|---|
| 477 | } | 
|---|
| 478 | } | 
|---|
| 479 | else | 
|---|
| 480 | /* This pattern rule does apply.  Stop looking for one.  */ | 
|---|
| 481 | break; | 
|---|
| 482 | } | 
|---|
| 483 |  | 
|---|
| 484 | /* If we found an applicable rule without | 
|---|
| 485 | intermediate files, don't try with them.  */ | 
|---|
| 486 | if (i < nrules) | 
|---|
| 487 | break; | 
|---|
| 488 |  | 
|---|
| 489 | rule = 0; | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | /* RULE is nil if the loop went all the way | 
|---|
| 493 | through the list and everything failed.  */ | 
|---|
| 494 | if (rule == 0) | 
|---|
| 495 | goto done; | 
|---|
| 496 |  | 
|---|
| 497 | foundrule = i; | 
|---|
| 498 |  | 
|---|
| 499 | /* If we are recursing, store the pattern that matched | 
|---|
| 500 | FILENAME in FILE->name for use in upper levels.  */ | 
|---|
| 501 |  | 
|---|
| 502 | if (recursions > 0) | 
|---|
| 503 | /* Kludge-o-matic */ | 
|---|
| 504 | file->name = rule->targets[matches[foundrule]]; | 
|---|
| 505 |  | 
|---|
| 506 | /* FOUND_FILES lists the dependencies for the rule we found. | 
|---|
| 507 | This includes the intermediate files, if any. | 
|---|
| 508 | Convert them into entries on the deps-chain of FILE.  */ | 
|---|
| 509 |  | 
|---|
| 510 | while (deps_found-- > 0) | 
|---|
| 511 | { | 
|---|
| 512 | register char *s; | 
|---|
| 513 |  | 
|---|
| 514 | if (intermediate_files[deps_found] != 0) | 
|---|
| 515 | { | 
|---|
| 516 | /* If we need to use an intermediate file, | 
|---|
| 517 | make sure it is entered as a target, with the info that was | 
|---|
| 518 | found for it in the recursive pattern_search call. | 
|---|
| 519 | We know that the intermediate file did not already exist as | 
|---|
| 520 | a target; therefore we can assume that the deps and cmds | 
|---|
| 521 | of F below are null before we change them.  */ | 
|---|
| 522 |  | 
|---|
| 523 | struct file *imf = intermediate_files[deps_found]; | 
|---|
| 524 | register struct file *f = enter_file (imf->name); | 
|---|
| 525 | f->deps = imf->deps; | 
|---|
| 526 | f->cmds = imf->cmds; | 
|---|
| 527 | f->stem = imf->stem; | 
|---|
| 528 | f->also_make = imf->also_make; | 
|---|
| 529 | imf = lookup_file (intermediate_patterns[deps_found]); | 
|---|
| 530 | if (imf != 0 && imf->precious) | 
|---|
| 531 | f->precious = 1; | 
|---|
| 532 | f->intermediate = 1; | 
|---|
| 533 | f->tried_implicit = 1; | 
|---|
| 534 | for (dep = f->deps; dep != 0; dep = dep->next) | 
|---|
| 535 | { | 
|---|
| 536 | dep->file = enter_file (dep->name); | 
|---|
| 537 | /* enter_file uses dep->name _if_ we created a new file.  */ | 
|---|
| 538 | if (dep->name != dep->file->name) | 
|---|
| 539 | free (dep->name); | 
|---|
| 540 | dep->name = 0; | 
|---|
| 541 | dep->file->tried_implicit |= dep->changed; | 
|---|
| 542 | } | 
|---|
| 543 | } | 
|---|
| 544 |  | 
|---|
| 545 | dep = (struct dep *) xmalloc (sizeof (struct dep)); | 
|---|
| 546 | dep->ignore_mtime = 0; | 
|---|
| 547 | s = found_files[deps_found]; | 
|---|
| 548 | if (recursions == 0) | 
|---|
| 549 | { | 
|---|
| 550 | dep->name = 0; | 
|---|
| 551 | dep->file = lookup_file (s); | 
|---|
| 552 | if (dep->file == 0) | 
|---|
| 553 | /* enter_file consumes S's storage.  */ | 
|---|
| 554 | dep->file = enter_file (s); | 
|---|
| 555 | else | 
|---|
| 556 | /* A copy of S is already allocated in DEP->file->name. | 
|---|
| 557 | So we can free S.  */ | 
|---|
| 558 | free (s); | 
|---|
| 559 | } | 
|---|
| 560 | else | 
|---|
| 561 | { | 
|---|
| 562 | dep->name = s; | 
|---|
| 563 | dep->file = 0; | 
|---|
| 564 | dep->changed = 0; | 
|---|
| 565 | } | 
|---|
| 566 | if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal) | 
|---|
| 567 | { | 
|---|
| 568 | /* If the file actually existed (was not an intermediate file), | 
|---|
| 569 | and the rule that found it was a terminal one, then we want | 
|---|
| 570 | to mark the found file so that it will not have implicit rule | 
|---|
| 571 | search done for it.  If we are not entering a `struct file' for | 
|---|
| 572 | it now, we indicate this with the `changed' flag.  */ | 
|---|
| 573 | if (dep->file == 0) | 
|---|
| 574 | dep->changed = 1; | 
|---|
| 575 | else | 
|---|
| 576 | dep->file->tried_implicit = 1; | 
|---|
| 577 | } | 
|---|
| 578 | dep->next = file->deps; | 
|---|
| 579 | file->deps = dep; | 
|---|
| 580 | } | 
|---|
| 581 |  | 
|---|
| 582 | if (!checked_lastslash[foundrule]) | 
|---|
| 583 | { | 
|---|
| 584 | /* Always allocate new storage, since STEM might be | 
|---|
| 585 | on the stack for an intermediate file.  */ | 
|---|
| 586 | file->stem = savestring (stem, stemlen); | 
|---|
| 587 | fullstemlen = stemlen; | 
|---|
| 588 | } | 
|---|
| 589 | else | 
|---|
| 590 | { | 
|---|
| 591 | int dirlen = (lastslash + 1) - filename; | 
|---|
| 592 |  | 
|---|
| 593 | /* We want to prepend the directory from | 
|---|
| 594 | the original FILENAME onto the stem.  */ | 
|---|
| 595 | fullstemlen = dirlen + stemlen; | 
|---|
| 596 | file->stem = (char *) xmalloc (fullstemlen + 1); | 
|---|
| 597 | bcopy (filename, file->stem, dirlen); | 
|---|
| 598 | bcopy (stem, file->stem + dirlen, stemlen); | 
|---|
| 599 | file->stem[fullstemlen] = '\0'; | 
|---|
| 600 | } | 
|---|
| 601 |  | 
|---|
| 602 | file->cmds = rule->cmds; | 
|---|
| 603 |  | 
|---|
| 604 | /* If this rule builds other targets, too, put the others into FILE's | 
|---|
| 605 | `also_make' member.  */ | 
|---|
| 606 |  | 
|---|
| 607 | if (rule->targets[1] != 0) | 
|---|
| 608 | for (i = 0; rule->targets[i] != 0; ++i) | 
|---|
| 609 | if (i != matches[foundrule]) | 
|---|
| 610 | { | 
|---|
| 611 | struct dep *new = (struct dep *) xmalloc (sizeof (struct dep)); | 
|---|
| 612 | /* GKM FIMXE: handle '|' here too */ | 
|---|
| 613 | new->ignore_mtime = 0; | 
|---|
| 614 | new->name = p = (char *) xmalloc (rule->lens[i] + fullstemlen + 1); | 
|---|
| 615 | bcopy (rule->targets[i], p, | 
|---|
| 616 | rule->suffixes[i] - rule->targets[i] - 1); | 
|---|
| 617 | p += rule->suffixes[i] - rule->targets[i] - 1; | 
|---|
| 618 | bcopy (file->stem, p, fullstemlen); | 
|---|
| 619 | p += fullstemlen; | 
|---|
| 620 | bcopy (rule->suffixes[i], p, | 
|---|
| 621 | rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1); | 
|---|
| 622 | new->file = enter_file (new->name); | 
|---|
| 623 | new->next = file->also_make; | 
|---|
| 624 | file->also_make = new; | 
|---|
| 625 | } | 
|---|
| 626 |  | 
|---|
| 627 | done: | 
|---|
| 628 | free (intermediate_files); | 
|---|
| 629 | free (tryrules); | 
|---|
| 630 |  | 
|---|
| 631 | return rule != 0; | 
|---|
| 632 | } | 
|---|