mercredi 14 avril 2021

Makefile default rule pattern

Just trying to write a conditional Makefile using this skeleton:

TARGET = test

ifeq ($(FOO),y)

$(TARGET):
    @echo This is test
$(TARGET)-a:
    @echo This is test-a
$(TARGET)-b:
    @echo This is test-b

else
$(info FOO is disabled)
endif

When the FOO condition is true, the set of rules based on the TARGET variable (composed of one $(TARGET) and a set of $(TARGET)-substring) work as expected:

$ make test
This is test

$ make test-a
This is test-a

When the FOO condition is false, I want to define a default rule for all my targets, just to report on the screen FOO variable is disabled. I don't know the proper way to do that. Tried some options:

Option1, using the skeleton example, the string "FOO is disabled" is always printed, but it generates an error:

$ make test-a
FOO is disabled
make: *** No rule to make target 'test-a'.  Stop.

$ make test  
FOO is disabled
make: *** No rule to make target 'test'.  Stop.

Option 2, if try to modify the false rule in this way:

else
$(TARGET)-%:
    $(info FOO is disabled)
endif

Then all $(TARGET)-substring targets work as expected:

$ make test-a
FOO is disabled
make: 'test-a' is up to date.

$ make test-b
FOO is disabled
make: 'test-b' is up to date.

But this rule fails when making $(TARGET):

$ make test
make: *** No rule to make target 'test'.  Stop.

Option 3, if try to remove the hyphen on the false rule defined in option2:

else
$(TARGET)%:
    $(info FOO is disabled)
endif

then making $(TARGET) executes a default rule for compiling a test.o object file:

$ make test
FOO is disabled
cc   test.o   -o test
cc: error: test.o: No such file or directory
cc: fatal error: no input files
compilation terminated.
make: *** [<builtin>: test] Error 1

And I am becoming a little bit crazy trying so satisfy this default rule. Please some help with this would be very useful. Tnks!

Aucun commentaire:

Enregistrer un commentaire