| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "makeint.h" |
| 18 |
|
| 19 |
#include <assert.h> |
| 20 |
|
| 21 |
#include "filedef.h" |
| 22 |
#include "dep.h" |
| 23 |
#include "job.h" |
| 24 |
#include "commands.h" |
| 25 |
#include "variable.h" |
| 26 |
#include "rule.h" |
| 27 |
|
| 28 |
static void freerule (struct rule *rule, struct rule *lastrule); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
struct rule *pattern_rules; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
struct rule *last_pattern_rule; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
unsigned int num_pattern_rules; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
unsigned int max_pattern_targets; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
unsigned int max_pattern_deps; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
size_t max_pattern_dep_length; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
struct file *suffix_file; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static size_t maxsuffix; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
void |
| 70 |
snap_implicit_rules (void) |
| 71 |
{ |
| 72 |
char *name = NULL; |
| 73 |
size_t namelen = 0; |
| 74 |
struct rule *rule; |
| 75 |
struct dep *dep; |
| 76 |
struct dep *prereqs = expand_extra_prereqs (lookup_variable (STRING_SIZE_TUPLE(".EXTRA_PREREQS"))); |
| 77 |
unsigned int pre_deps = 0; |
| 78 |
|
| 79 |
max_pattern_dep_length = 0; |
| 80 |
|
| 81 |
for (dep = prereqs; dep; dep = dep->next) |
| 82 |
{ |
| 83 |
size_t l = strlen (dep_name (dep)); |
| 84 |
if (l > max_pattern_dep_length) |
| 85 |
max_pattern_dep_length = l; |
| 86 |
++pre_deps; |
| 87 |
} |
| 88 |
|
| 89 |
num_pattern_rules = max_pattern_targets = max_pattern_deps = 0; |
| 90 |
|
| 91 |
for (rule = pattern_rules; rule; rule = rule->next) |
| 92 |
{ |
| 93 |
unsigned int ndeps = pre_deps; |
| 94 |
struct dep *lastdep = NULL; |
| 95 |
|
| 96 |
++num_pattern_rules; |
| 97 |
|
| 98 |
if (rule->num > max_pattern_targets) |
| 99 |
max_pattern_targets = rule->num; |
| 100 |
|
| 101 |
for (dep = rule->deps; dep != 0; dep = dep->next) |
| 102 |
{ |
| 103 |
const char *dname = dep_name (dep); |
| 104 |
size_t len = strlen (dname); |
| 105 |
|
| 106 |
#ifdef VMS |
| 107 |
const char *p = strrchr (dname, ']'); |
| 108 |
const char *p2; |
| 109 |
if (p == 0) |
| 110 |
p = strrchr (dname, ':'); |
| 111 |
p2 = p ? strchr (p, '%') : 0; |
| 112 |
#else |
| 113 |
const char *p = strrchr (dname, '/'); |
| 114 |
const char *p2 = p ? strchr (p, '%') : 0; |
| 115 |
#endif |
| 116 |
ndeps++; |
| 117 |
|
| 118 |
if (len > max_pattern_dep_length) |
| 119 |
max_pattern_dep_length = len; |
| 120 |
|
| 121 |
if (!dep->next) |
| 122 |
lastdep = dep; |
| 123 |
|
| 124 |
if (p2) |
| 125 |
{ |
| 126 |
|
| 127 |
|
| 128 |
if (p == dname) |
| 129 |
++p; |
| 130 |
if ((size_t) (p - dname) > namelen) |
| 131 |
{ |
| 132 |
namelen = p - dname; |
| 133 |
name = xrealloc (name, namelen + 1); |
| 134 |
} |
| 135 |
memcpy (name, dname, p - dname); |
| 136 |
name[p - dname] = '\0'; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
dep->changed = !dir_file_exists_p (name, ""); |
| 143 |
} |
| 144 |
else |
| 145 |
|
| 146 |
dep->changed = 0; |
| 147 |
} |
| 148 |
|
| 149 |
if (prereqs) |
| 150 |
{ |
| 151 |
if (lastdep) |
| 152 |
lastdep->next = copy_dep_chain (prereqs); |
| 153 |
else |
| 154 |
rule->deps = copy_dep_chain (prereqs); |
| 155 |
} |
| 156 |
|
| 157 |
if (ndeps > max_pattern_deps) |
| 158 |
max_pattern_deps = ndeps; |
| 159 |
} |
| 160 |
|
| 161 |
free (name); |
| 162 |
free_dep_chain (prereqs); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static void |
| 172 |
convert_suffix_rule (const char *target, const char *source, |
| 173 |
struct commands *cmds) |
| 174 |
{ |
| 175 |
const char **names, **percents; |
| 176 |
struct dep *deps; |
| 177 |
|
| 178 |
names = xmalloc (sizeof (const char *)); |
| 179 |
percents = xmalloc (sizeof (const char *)); |
| 180 |
|
| 181 |
if (target == 0) |
| 182 |
{ |
| 183 |
|
| 184 |
|
| 185 |
#ifdef VMS |
| 186 |
*names = strcache_add_len ("(%.obj)", 7); |
| 187 |
#else |
| 188 |
*names = strcache_add_len ("(%.o)", 5); |
| 189 |
#endif |
| 190 |
*percents = *names + 1; |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
|
| 195 |
size_t len = strlen (target); |
| 196 |
char *p = alloca (1 + len + 1); |
| 197 |
p[0] = '%'; |
| 198 |
memcpy (p + 1, target, len + 1); |
| 199 |
*names = strcache_add_len (p, len + 1); |
| 200 |
*percents = *names; |
| 201 |
} |
| 202 |
|
| 203 |
if (source == 0) |
| 204 |
deps = 0; |
| 205 |
else |
| 206 |
{ |
| 207 |
|
| 208 |
size_t len = strlen (source); |
| 209 |
char *p = alloca (1 + len + 1); |
| 210 |
p[0] = '%'; |
| 211 |
memcpy (p + 1, source, len + 1); |
| 212 |
deps = alloc_dep (); |
| 213 |
deps->name = strcache_add_len (p, len + 1); |
| 214 |
} |
| 215 |
|
| 216 |
create_pattern_rule (names, percents, 1, 0, deps, cmds, 0); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
void |
| 224 |
convert_to_pattern (void) |
| 225 |
{ |
| 226 |
struct dep *d, *d2; |
| 227 |
char *rulename; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
maxsuffix = 0; |
| 234 |
for (d = suffix_file->deps; d != 0; d = d->next) |
| 235 |
{ |
| 236 |
size_t l = strlen (dep_name (d)); |
| 237 |
if (l > maxsuffix) |
| 238 |
maxsuffix = l; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
rulename = alloca ((maxsuffix * 2) + 1); |
| 243 |
|
| 244 |
for (d = suffix_file->deps; d != 0; d = d->next) |
| 245 |
{ |
| 246 |
size_t slen; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
convert_suffix_rule (dep_name (d), 0, 0); |
| 251 |
|
| 252 |
if (d->file->cmds != 0) |
| 253 |
|
| 254 |
convert_suffix_rule ("", dep_name (d), d->file->cmds); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
slen = strlen (dep_name (d)); |
| 259 |
memcpy (rulename, dep_name (d), slen); |
| 260 |
|
| 261 |
for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next) |
| 262 |
{ |
| 263 |
struct file *f; |
| 264 |
size_t s2len; |
| 265 |
|
| 266 |
s2len = strlen (dep_name (d2)); |
| 267 |
|
| 268 |
|
| 269 |
if (slen == s2len && streq (dep_name (d), dep_name (d2))) |
| 270 |
continue; |
| 271 |
|
| 272 |
memcpy (rulename + slen, dep_name (d2), s2len + 1); |
| 273 |
f = lookup_file (rulename); |
| 274 |
|
| 275 |
|
| 276 |
if (f == 0 || f->cmds == 0) |
| 277 |
continue; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
if (f->deps != 0) |
| 285 |
{ |
| 286 |
if (posix_pedantic) |
| 287 |
continue; |
| 288 |
error (&f->cmds->fileinfo, 0, |
| 289 |
_("warning: ignoring prerequisites on suffix rule definition")); |
| 290 |
} |
| 291 |
|
| 292 |
if (s2len == 2 && rulename[slen] == '.' && rulename[slen + 1] == 'a') |
| 293 |
|
| 294 |
|
| 295 |
convert_suffix_rule (NULL, |
| 296 |
dep_name (d), |
| 297 |
f->cmds); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
convert_suffix_rule (dep_name (d2), dep_name (d), f->cmds); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
static int |
| 315 |
new_pattern_rule (struct rule *rule, int override) |
| 316 |
{ |
| 317 |
struct rule *r, *lastrule; |
| 318 |
unsigned int i, j; |
| 319 |
|
| 320 |
rule->in_use = 0; |
| 321 |
rule->terminal = 0; |
| 322 |
|
| 323 |
rule->next = 0; |
| 324 |
|
| 325 |
|
| 326 |
lastrule = 0; |
| 327 |
for (r = pattern_rules; r != 0; lastrule = r, r = r->next) |
| 328 |
for (i = 0; i < rule->num; ++i) |
| 329 |
{ |
| 330 |
for (j = 0; j < r->num; ++j) |
| 331 |
if (!streq (rule->targets[i], r->targets[j])) |
| 332 |
break; |
| 333 |
|
| 334 |
if (j == r->num) |
| 335 |
{ |
| 336 |
struct dep *d, *d2; |
| 337 |
for (d = rule->deps, d2 = r->deps; |
| 338 |
d != 0 && d2 != 0; d = d->next, d2 = d2->next) |
| 339 |
if (!streq (dep_name (d), dep_name (d2))) |
| 340 |
break; |
| 341 |
if (d == 0 && d2 == 0) |
| 342 |
{ |
| 343 |
|
| 344 |
if (override) |
| 345 |
{ |
| 346 |
|
| 347 |
freerule (r, lastrule); |
| 348 |
|
| 349 |
if (pattern_rules == 0) |
| 350 |
pattern_rules = rule; |
| 351 |
else |
| 352 |
last_pattern_rule->next = rule; |
| 353 |
last_pattern_rule = rule; |
| 354 |
|
| 355 |
|
| 356 |
goto matched; |
| 357 |
} |
| 358 |
else |
| 359 |
{ |
| 360 |
|
| 361 |
freerule (rule, (struct rule *) 0); |
| 362 |
return 0; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
matched:; |
| 369 |
|
| 370 |
if (r == 0) |
| 371 |
{ |
| 372 |
|
| 373 |
if (pattern_rules == 0) |
| 374 |
pattern_rules = rule; |
| 375 |
else |
| 376 |
last_pattern_rule->next = rule; |
| 377 |
last_pattern_rule = rule; |
| 378 |
} |
| 379 |
|
| 380 |
return 1; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
void |
| 390 |
install_pattern_rule (struct pspec *p, int terminal) |
| 391 |
{ |
| 392 |
struct rule *r; |
| 393 |
const char *ptr; |
| 394 |
|
| 395 |
r = xmalloc (sizeof (struct rule)); |
| 396 |
|
| 397 |
r->num = 1; |
| 398 |
r->targets = xmalloc (sizeof (const char *)); |
| 399 |
r->suffixes = xmalloc (sizeof (const char *)); |
| 400 |
r->lens = xmalloc (sizeof (unsigned int)); |
| 401 |
|
| 402 |
r->lens[0] = (unsigned int) strlen (p->target); |
| 403 |
r->targets[0] = p->target; |
| 404 |
r->suffixes[0] = find_percent_cached (&r->targets[0]); |
| 405 |
r->floc.filenm = NULL; |
| 406 |
r->floc.lineno = 0; |
| 407 |
assert (r->suffixes[0] != NULL); |
| 408 |
++r->suffixes[0]; |
| 409 |
|
| 410 |
ptr = p->dep; |
| 411 |
r->deps = PARSE_SIMPLE_SEQ ((char **)&ptr, struct dep); |
| 412 |
|
| 413 |
if (new_pattern_rule (r, 0)) |
| 414 |
{ |
| 415 |
r->terminal = terminal ? 1 : 0; |
| 416 |
r->cmds = xmalloc (sizeof (struct commands)); |
| 417 |
r->cmds->fileinfo.filenm = 0; |
| 418 |
r->cmds->fileinfo.lineno = 0; |
| 419 |
r->cmds->fileinfo.offset = 0; |
| 420 |
|
| 421 |
|
| 422 |
r->cmds->commands = xstrdup (p->commands); |
| 423 |
r->cmds->command_lines = 0; |
| 424 |
r->cmds->recipe_prefix = RECIPEPREFIX_DEFAULT; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
static void |
| 434 |
freerule (struct rule *rule, struct rule *lastrule) |
| 435 |
{ |
| 436 |
struct rule *next = rule->next; |
| 437 |
|
| 438 |
free_dep_chain (rule->deps); |
| 439 |
|
| 440 |
|
| 441 |
free ((void *)rule->targets); |
| 442 |
free ((void *)rule->suffixes); |
| 443 |
free (rule->lens); |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
free (rule); |
| 457 |
|
| 458 |
if (pattern_rules == rule) |
| 459 |
if (lastrule != 0) |
| 460 |
abort (); |
| 461 |
else |
| 462 |
pattern_rules = next; |
| 463 |
else if (lastrule != 0) |
| 464 |
lastrule->next = next; |
| 465 |
if (last_pattern_rule == rule) |
| 466 |
last_pattern_rule = lastrule; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
void |
| 480 |
create_pattern_rule (const char **targets, const char **target_percents, |
| 481 |
unsigned short n, int terminal, struct dep *deps, |
| 482 |
struct commands *commands, int override) |
| 483 |
{ |
| 484 |
unsigned int i; |
| 485 |
struct rule *r = xmalloc (sizeof (struct rule)); |
| 486 |
|
| 487 |
r->num = n; |
| 488 |
r->cmds = commands; |
| 489 |
r->deps = deps; |
| 490 |
r->targets = targets; |
| 491 |
r->suffixes = target_percents; |
| 492 |
r->lens = xmalloc (n * sizeof (unsigned int)); |
| 493 |
|
| 494 |
r->tracing = BRK_NONE; |
| 495 |
if (commands) { |
| 496 |
r->floc.filenm = commands->fileinfo.filenm; |
| 497 |
r->floc.lineno = commands->fileinfo.lineno - 1; |
| 498 |
} else { |
| 499 |
r->floc.filenm = NULL; |
| 500 |
r->floc.lineno = 0; |
| 501 |
} |
| 502 |
|
| 503 |
for (i = 0; i < n; ++i) |
| 504 |
{ |
| 505 |
r->lens[i] = (unsigned int) strlen (targets[i]); |
| 506 |
assert (r->suffixes[i] != NULL); |
| 507 |
++r->suffixes[i]; |
| 508 |
} |
| 509 |
|
| 510 |
if (new_pattern_rule (r, override)) |
| 511 |
r->terminal = terminal ? 1 : 0; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
void |
| 518 |
print_rule (rule_t *r, bool b_verbose) |
| 519 |
{ |
| 520 |
unsigned int i; |
| 521 |
|
| 522 |
for (i = 0; i < r->num; ++i) |
| 523 |
{ |
| 524 |
fputs (r->targets[i], stdout); |
| 525 |
putchar ((i + 1 == r->num) ? ':' : ' '); |
| 526 |
} |
| 527 |
if (r->terminal) |
| 528 |
putchar (':'); |
| 529 |
|
| 530 |
print_prereqs (r->deps); |
| 531 |
|
| 532 |
putchar ('\n'); |
| 533 |
if (!b_verbose) return; |
| 534 |
if (r->cmds != 0) |
| 535 |
print_commands (NULL, r->cmds, false); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
void |
| 540 |
print_rule_data_base (bool b_verbose) |
| 541 |
{ |
| 542 |
unsigned int rules, terminal; |
| 543 |
struct rule *r; |
| 544 |
|
| 545 |
puts (_("\n# Implicit Rules")); |
| 546 |
|
| 547 |
rules = terminal = 0; |
| 548 |
for (r = pattern_rules; r != 0; r = r->next) |
| 549 |
{ |
| 550 |
++rules; |
| 551 |
|
| 552 |
print_rule (r, b_verbose); |
| 553 |
|
| 554 |
if (r->terminal) |
| 555 |
++terminal; |
| 556 |
} |
| 557 |
|
| 558 |
if (!b_verbose) { |
| 559 |
printf("\n"); |
| 560 |
return; |
| 561 |
} |
| 562 |
|
| 563 |
if (rules == 0) |
| 564 |
puts (_("\n# No implicit rules.")); |
| 565 |
else |
| 566 |
{ |
| 567 |
printf (_("\n# %u implicit rules, %u (%.1f%%) terminal."), |
| 568 |
rules, terminal, (double) terminal / (double) rules * 100.0); |
| 569 |
} |
| 570 |
|
| 571 |
if (num_pattern_rules != rules) |
| 572 |
{ |
| 573 |
|
| 574 |
|
| 575 |
if (num_pattern_rules != 0) |
| 576 |
ONN (fatal, NILF, _("BUG: num_pattern_rules is wrong! %u != %u"), |
| 577 |
num_pattern_rules, rules); |
| 578 |
} |
| 579 |
} |