1

Is there any GNU Make special variable for knowing which target was the one sent by the user in the command line call? To achieve something like (it is just an example):

// makefile
ifeq ($(USER_TARGET),"plain")
CXXFLAGS += -O0
else
CXXFLAGS += -O3
endif

plain:
    // do something using CXXFLAGS

all:
    // other rule using CXXFLAGS

It's important that, the USER_TARGET variable (which can be an special GNU Make variable, or a custom variable which value was set by some special GNU Make function returning the user target), contains the original target specified by the user in a call like:

$ make plain

and not the value in any step of the dependency tree. In other words, the USER_TARGET must not change during the building dependence chain, because, if I'm not wrong, the value of USER_TARGET will be evaluated when it is used by the first time, and than can happen in any point of the document, so, I want to be sure the current step of building chain don't change the value of USER_TARGET.

I don't know if I have been clear enough or if it is was overexplained.

If there is no built-in mechanism for that in GNU Make, any $(shell command) trick is welcome as well.

ABu
  • 10,423
  • 6
  • 52
  • 103
  • 1
    Perhaps this would work for you? [Can't assign a variable inside a recipe](http://stackoverflow.com/questions/6519234/cant-assign-variable-inside-recipe). – lurker Nov 04 '16 at 16:09
  • 1
    I'm not sure what you're trying to do, but I recently saw a makefile with `debug: clean` `@make CFLAGS='${DEBUG_CFLAGS}'` – Random832 Nov 04 '16 at 16:35
  • 1
    Just take care, multiple targets could be specified eg `make -f Makefile build test` could be used to call both the build and test targets – mjs Nov 04 '16 at 17:01
  • @mjs That's right, good point. I haven't thought about it. – ABu Nov 04 '16 at 17:13

1 Answers1

4

You want the MAKECMDGOALS variable, described here.

MadScientist
  • 92,819
  • 9
  • 109
  • 136