Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Makefile variable value inside the target body

Is there a way to reassign Makefile variable value inside of the target body?

What I am trying to do is to add some extra flags for debug compilation:

%.erl: %.beam     $(ERLC) $(ERLFLAGS) -o ebin $<  test: clean debug_compile_flag compile compile_test  debug_compile:     $(ERLCFLAGS) += -DTEST 

So if I invoke test target I would like to clean up my environment, add some new flags (like -DTEST to the existing ones), compile the whole code once again (first sources, then test modules).

I do not want to copy/paste the code for compiling with some new flags set since there is a lot of logic put here and there.

Is there some easy way to redefine the variable value so I can reuse the existing code?

like image 556
paulgray Avatar asked Apr 26 '10 08:04

paulgray


People also ask

How do I change a variable in makefile?

There is one way that the makefile can change a variable that you have overridden. This is to use the override directive, which is a line that looks like this: ' override variable = value ' (see The override Directive).

What is $@ in makefile?

The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.

What is Makecmdgoals in makefile?

MAKECMDGOALS. The targets given to make on the command line. Setting this variable has no effect on the operation of make. See Arguments to Specify the Goals. The closest you could get to what you are trying to do here, I think, would be to re-execute make on the Makefile (or whatever) manually.

How do I call a target in makefile?

When you type make or make [target] , the Make will look through your current directory for a Makefile. This file must be called makefile or Makefile . Make will then look for the corresponding target in the makefile. If you don't provide a target, Make will just run the first target it finds.


2 Answers

Yes, there is an easy way to do it, and without rerunning Make. Use a target-specific variable value:

test: clean debug_compile  debug_compile: ERLCFLAGS += -DTEST debug_compile: compile compile_test; 
like image 196
Beta Avatar answered Sep 27 '22 02:09

Beta


Another answer is here: Define make variable at rule execution time.

For the lazy, you can have rules like the following (FLAG and DEBUG are my variables):

.DBG:     $(eval FLAG += $(DEBUG)) 
like image 32
Ian Ooi Avatar answered Sep 27 '22 02:09

Ian Ooi