Each make hit's target: the rule node of that name in the makefile, else * in the files it includes, in reading order (the first rule of a file). * Read here, when linking, so an unchanged makefile is read too. */
| 3415 | * in the files it includes, in reading order (the first rule of a file). |
| 3416 | * Read here, when linking, so an unchanged makefile is read too. */ |
| 3417 | static void sh_make_resolve(sh_pass_t *p, cbm_pipeline_ctx_t *ctx, sh_hits_t *h) { |
| 3418 | CBMArena a; |
| 3419 | cbm_arena_init(&a); |
| 3420 | CBMHashTable *closures = cbm_ht_create(0); /* makefile -> mk_closure_t */ |
| 3421 | CBMHashTable *want = cbm_ht_create(0); /* every file a closure reads */ |
| 3422 | CBMHashTable *rules = cbm_ht_create(0); /* file \x1f name -> rule node */ |
| 3423 | for (int i = 0; closures && want && i < h->count; i++) { |
| 3424 | const sh_hit_t *x = &h->items[i]; |
| 3425 | if (!x->make || cbm_ht_get(closures, x->path)) { |
| 3426 | continue; |
| 3427 | } |
| 3428 | mk_closure_t *cl = cbm_arena_alloc(&a, sizeof(*cl)); |
| 3429 | if (!cl) { |
| 3430 | continue; |
| 3431 | } |
| 3432 | const char *dir = sh_dirname(&a, x->path); |
| 3433 | *cl = (mk_closure_t){.p = p, |
| 3434 | .ctx = ctx, |
| 3435 | .a = &a, |
| 3436 | .seen = cbm_ht_create(0), |
| 3437 | .vars = cbm_ht_create(0), |
| 3438 | .mkdir = dir && strcmp(dir, ".") != 0 ? dir : ""}; |
| 3439 | if (cl->seen && cl->vars) { |
| 3440 | mk_closure_file(cl, x->path); |
| 3441 | } |
| 3442 | for (int k = 0; k < cl->n; k++) { |
| 3443 | cbm_ht_set(want, cl->order[k], (void *)cl->order[k]); |
| 3444 | } |
| 3445 | cbm_ht_set(closures, x->path, cl); |
| 3446 | } |
| 3447 | const cbm_gbuf_node_t **nodes = NULL; |
| 3448 | int count = 0; |
| 3449 | if (rules && want && cbm_ht_count(want) > 0 && |
| 3450 | cbm_gbuf_find_by_label(ctx->gbuf, "Function", &nodes, &count) == 0) { |
| 3451 | for (int i = 0; i < count; i++) { |
| 3452 | const char *fp = nodes[i]->file_path; |
| 3453 | if (!fp || !nodes[i]->name || !cbm_ht_get(want, fp)) { |
| 3454 | continue; |
| 3455 | } |
| 3456 | const char *key = mk_rule_key(&a, fp, nodes[i]->name); |
| 3457 | const cbm_gbuf_node_t *old = key ? cbm_ht_get(rules, key) : NULL; |
| 3458 | if (key && (!old || nodes[i]->start_line < old->start_line)) { |
| 3459 | cbm_ht_set(rules, key, (void *)nodes[i]); |
| 3460 | } |
| 3461 | } |
| 3462 | } |
| 3463 | for (int i = 0; rules && closures && i < h->count; i++) { |
| 3464 | sh_hit_t *x = &h->items[i]; |
| 3465 | const mk_closure_t *cl = x->make ? cbm_ht_get(closures, x->path) : NULL; |
| 3466 | for (int k = 0; cl && k < cl->n && x->tgt <= 0; k++) { |
| 3467 | atomic_fetch_add_explicit(&g_sh_candidates, SKIP_ONE, memory_order_relaxed); |
| 3468 | const char *key = mk_rule_key(&a, cl->order[k], x->ext); |
| 3469 | const cbm_gbuf_node_t *rule = key ? cbm_ht_get(rules, key) : NULL; |
| 3470 | x->tgt = rule ? rule->id : 0; |
| 3471 | } |
| 3472 | } |
| 3473 | for (int i = 0; closures && i < h->count; i++) { |
| 3474 | mk_closure_t *cl = h->items[i].make ? cbm_ht_get(closures, h->items[i].path) : NULL; |
no test coverage detected