Changing the value of the Makefile variable inside the target body

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

What I'm trying to do is add extra flags to compile debugging:

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

So, if I call the test target, I would like to clear the environment, add new flags (for example, -DTEST to existing ones), compile all the code again (first sources, then test modules).

I do not want to copy / paste the code to compile with some new flags, since there is a lot of logic.

Is there an easy way to override the value of a variable so that I can reuse existing code?

+43
makefile target
Apr 26 '10 at 8:21
source share
3 answers

Change As explained by Beta in another answer , this is possible.




No. There is no way in the Makefile to do this. However, you can change the value of the variable on the make command line. If you rewrite your Makefile as follows:

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

You can then invoke make to run your tests using:

 make ERLCFLAGSADDED=-DTEST test 
-5
Apr 26 '10 at 8:32
source share

Yes, there is an easy way to do this without reusing Make. Use the value of the variable for the purpose :

 test: clean debug_compile debug_compile: ERLCFLAGS += -DTEST debug_compile: compile compile_test; 
+54
Apr 26 '10 at 14:11
source share

Another answer here: Define a make variable at runtime .

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

 .DBG: $(eval FLAG += $(DEBUG)) 
+38
Mar 22 '13 at 2:51 on
source share



All Articles