Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining elements of a list in GNU Make

Tags:

makefile

In my makefile I have a variable with a list of directories, like this:

DIRS = /usr /usr/share/ /lib 

Now, I need to create PATH variable from it, which is basically the same, but uses semicolon as a separator:

PATH = /usr:/usr/share/:/lib 

How do I do that? I mean, how do I join elements of DIRS list with semicolons, instead of spaces?

like image 691
Maxim Sloyko Avatar asked Oct 09 '09 04:10

Maxim Sloyko


People also ask

What is $@ in makefile?

The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.

What does Addprefix do in makefile?

What is add prefix in makefile? $(addprefix prefix , names …) The argument names is regarded as a series of names, separated by whitespace; prefix is used as a unit. The value of prefix is prepended to the front of each individual name and the resulting larger names are concatenated with single spaces between them.

What is Patsubst in makefile?

$(patsubst pattern , replacement , text ) Finds whitespace-separated words in text that match pattern and replaces them with replacement . Here pattern may contain a ' % ' which acts as a wildcard, matching any number of any characters within a word.

What is Notdir in makefile?

$(notdir names …) Extracts all but the directory-part of each file name in names . If the file name contains no slash, it is left unchanged. Otherwise, everything through the last slash is removed from it. A file name that ends with a slash becomes an empty string.


2 Answers

You can use the $(subst) command, combined with a little trick to get a variable that has a value of a single space:

p = /usr /usr/share /lib noop= space = $(noop) $(noop)  all:         @echo $(subst $(space),:,$(p)) 
like image 96
Eric Melski Avatar answered Sep 23 '22 21:09

Eric Melski


Cleanest Form (that I can find):

classpathify = $(subst $(eval) ,:,$(wildcard $1)) cp = a b c d/*.jar  target:     echo $(call classpathify,$(cp)) # prints a:b:c:d/1.jar:d/2.jar 

Notes:

  • Turning it into a pseudo-function makes the intention clearer than doing a bunch of arcane string manipulation inline.
  • I included the $(wildcard) function because you almost always use these two together when specifying a classpath
  • Make sure not to put any extra spaces in after the commas or you will get something like "::a:b:c:d:e".
like image 40
Dave Dopson Avatar answered Sep 21 '22 21:09

Dave Dopson