4

If I understand the make documentation correctly I can only use character % once in my target-pattern!

I am facing a condition which my target is look like:

$(Home)/ecc_%_1st_%.gff: %.fpp

And whenever I am trying to match /home/ecc_map_1st_map.gff: map.fpp Make complains that can't find a pattern.

I know it is because of using % twice ($(Home)/ecc_map_1st_%.gff: %.fpp runes perfectly)

Is there anyway which I can use two % in one target!?

Thanks

khikho
  • 319
  • 3
  • 11
  • 2
    what is the need to have word `map` twice in the file name? – igagis Aug 25 '16 at 18:57
  • Possible duplicate of [How to match double stem in target like %/% or other way?](https://stackoverflow.com/questions/58404646/how-to-match-double-stem-in-target-like-or-other-way) – Y00 Oct 16 '19 at 05:19

1 Answers1

2

You can't have two patterns. The best way would be not having map twice in the target name.

If you must, and you're using GNU make, secondary expansion might be useful:

.SECONDEXPANSION:
$(Home)/ecc_%.gff: $$(word 1,$$(subst _, ,$$*)).fpp

Normally you can't use automatic variables such as $* in prerequisites, but secondary expansion allows you to do just that. Note that I used $$ in place of $ so that the real expansion happens in the second pass and not in the first one.

Say you are making $(Home)/ecc_map_1st_map.gff. The $* variable is going to be the stem, i.e. map_1st_map. Then $(subst _, ,...) replaces underscores with spaces, to obtain map 1st map. Now $(word N,...) extracts the N-th space-separated word in there. First word would be the first map, second would be 1st, third would be the second map. I'm picking the first one.

Of course this is pretty lax and doesn't guarantee you'll match exactly the targets you wanted ($(Home)/ecc_map_2nd_notmap__foo.gff would match and give map.fpp). You could do further checks (1st in the middle, other two must match) and bail out:

extract-name = $(call extract-name-helper,$1,$(word 1,$(subst _, ,$1)))
extract-name-helper = $(if $(filter $2_1st_$2,$1),$2,$(error Invalid .gff target))

.SECONDEXPANSION:
ecc_%.gff: $$(call extract-name,$$*).fpp

But that doesn't look like a sensible solution.

Wrapping up, if don't have similarly named files, then the first solution would work well. Also, if you know the target names beforehand, you could make it safer with an explicit pattern rule (or even generate them on the fly).

Community
  • 1
  • 1
Andrea Biondo
  • 1,626
  • 14
  • 13