Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove item from a Makefile variable?

I have a makefile, which includes several other makefiles, which in turn all add to a variable like this:

VAR := Something SomethingElse VAR += SomeOtherThing  (...) 

Now I wish to remove SomethingElse from the VAR variable. What do I put in place of (...) to do this?

I am using GNU Make, and a GNU Make specific solution will be fine.

like image 932
Bjarke Freund-Hansen Avatar asked Sep 13 '11 12:09

Bjarke Freund-Hansen


People also ask

What is $$ in Makefile?

Double dollar sign If you want a string to have a dollar sign, you can use $$ . This is how to use a shell variable in bash or sh . Note the differences between Makefile variables and Shell variables in this next example.

What does += mean in Makefile?

6.6 Appending More Text to Variables Often it is useful to add more text to the value of a variable already defined. You do this with a line containing ' += ', like this: objects += another.o.

What is strip in Makefile?

$(strip string ) Removes leading and trailing whitespace from string and replaces each internal sequence of one or more whitespace characters with a single space. Thus, ' $(strip a b c ) ' results in ' a b c '. The function strip can be very useful when used in conjunction with conditionals.

How do I print a variable in Makefile?

How do I print a variable in a file? To use it, just set the list of variables to print on the command line, and include the debug target: $ make V="USERNAME SHELL" debug makefile:2: USERNAME = Owner makefile:2: SHELL = /bin/sh.exe make: debug is up to date.


2 Answers

You could use the filter-out text function if you're using GNU Make.

OTHERVAR := $(filter-out SomethingElse,$(VAR)) 
like image 111
Mat Avatar answered Sep 29 '22 04:09

Mat


On top of the correct answer above:

VAR = bla1 bla2 bla3 bla4 bla5  TMPVAR := $(VAR) VAR = $(filter-out bla3, $(TMPVAR))  all:     @echo "VAR is: $(VAR)" 

Output:
VAR is: bla1 bla2 bla4 bla5

Note that this breaks all "recursivity" when filter-out is executed, but that might not matter in your case.

like image 36
Andreas Mikael Bank Avatar answered Sep 29 '22 05:09

Andreas Mikael Bank