Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to dynamically generate makefile rules?

There is an existing question with a similar sounding title. However, it is not quite what I am asking.

I find the following to be a typical use case:

items=Excelsior Shibboleth AbraCadabra

ceremony@%:
  foo bar biz baz $*

ritual@%:
  ding ring bing bong $*

ceremonies: $(foreach item,$(items),ceremony@$(item))
rituals: $(foreach item,$(items),ritual@$(item))

all: ceremonies | rituals

However, many of the ceremonies and rituals are time consuming and may be undesirable. Also, make is most likely to be adopted and used when commands autocomplete and the file is not large.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I want to do is dynamically define all the ritual and ceremony commands for the pre-defined items:

# pseudo code -- doesn't work in GNU Make 3.82 (my lowest version compatibility req)
$(foreach item,$(items),$(item)@ritual: ritual@$(item))

So that when you trigger tab completion, the itemized commands appear. And it will keep the makefile from getting too bulky.

Is this possible?


And the dual or "Y" problem is: if I have a wildcard rule, is it possible to generate some relevant matched rule results for display during tab completion in zsh/bash?

>Solution :

You can make the code work using the eval function. Change this:

$(foreach item,$(items),$(item)@ritual: ritual@$(item))

to this:

$(foreach item,$(items),$(eval $(item)@ritual: ritual@$(item)))

Without that, the result of this foreach expansion will be:

Excelsior@ritual: ritual@Excelsior Shibboleth@ritual ritual@Shibboleth AbraCadabra@ritual: ritual@AbraCadabra

which is clearly not a legal make statement.

Just to note, there is no such release as GNU make "3.8". I don’t know what release you might have.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading