Using ifeq and ifndef in GNU Make

I wrote a fairly simple Makefile test where I define two goals, all clean and clean. I have two different conditional statements. One checks for the existence of the special variable $(MAKECMDGOALS) , and the other determines whether any of the command line targets match those listed in the variable ( NODEPS ). The problem I am facing is that none of the branches inside my conditions are satisfied. Ultimately, I want to use a conditional expression to decide whether the target should deliver some files with auto-generated dependencies, but at the moment I'm struggling to get either an expression to evaluate. I am running GNU make version 3.81, and I have tried it under Ubuntu and Mac OS X to no avail.

  NODEPS := clean INCLUDE = $(filter $(NODEPS),$(MAKECMDGOALS)) .PHONY : all clean ifndef $(MAKECMDGOALS) @echo "$$(MAKECMDGOALS) is not defined" else @echo "$(MAKECMDGOALS) is defined" endif ifneq (0, $(words $(INCLUDE))) @echo "INCLUDE = $(INCLUDE) != 0" else @echo "INCLUDE = $(INCLUDE) == 0" endif all : @echo "all : $(MAKECMDGOALS)" clean : @echo "clean : $(MAKECMDGOALS)" 
+4
source share
2 answers

In the end, I managed to figure out what happened. @eriktous was right in pointing out that I should use $(info) , not @echo . More subtly, although part of the problem was that I separated @echo tab. Tabs seem to be mandatory for the rules, but not allowed in conditional expressions. Another mistake was that I expanded the variable $(MAKECMDGOALS) in a test state, when it should have been written as simply ifndef MAKECMDGOALS .

+13
source

The make file is not a shell script. You cannot “arbitrarily” place executable instructions anywhere and expect them to be executed.

There are various ways to communicate with the outside world from a make file: $(info ...) , $(warning ...) , $(error ...) and $(shell @echo ...) (some or all of them may be extensions of GNU make).

Ps: you made a mistake PHONY .

+11
source

Source: https://habr.com/ru/post/1380455/


All Articles