Search code examples
makefilegnu-make

Make: wildcard including rule name


I am producing artifacts foo.out, bar.out, and qux.out. I have a rule %.cooked: %.raw.

  • For all files foo-★.raw, I need foo.out to depend on foo-★.cooked
  • Likewise for bar and qux

Currently, I am not restricting the raw files that are being built, so make bar.out is wastefully cooking dozens of foo-1.raw, foo-2a.raw, foo-2b.raw, etc. which is really eating into my time.

I tried using secondary expansion:

.SECONDEXPANSION:
$(OUTFILES): %.out: $$(patsubst %.raw,%.cooked,$$(wildcard $$%-*.raw))
    build $< --output $@

but it seems that this ended up being empty and nothing was built.


Solution

  • Combining Renaud's suggestion of using $$* with a layer of indirection behind $$(call has done the trick:

    OUT_raws = $(patsubst %.raw,%.cooked,$(wildcard $(1)-*.raw))
    
    .SECONDEXPANSION:
    $(OUTFILES): %.out: $$(call OUT_raws,$$*)
        build $< --output $@