Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove prefix with make

Is there a way to remove a prefix from a string (a pathname in my case) in make?

As an example, suppose I had the string:

FILES = a/b/c.d a/b/e.f

I want to remove the a/, and be left with b/c.d b/e.f

I have tried using various combinations of dir, notdir and basename from the GNU make manual, but none seem to provide a nice solution.

$(dir $(FILE))      # a/b a/b
$(notdir $(FILE))   # c.d e.f
$(basename $(FILE)) # a/b/c a/b/e

The only way I've found to do this so far is:

$( join $(basename $(dir $(FILE))), $(notdir $(FILE)) )

Which is really ugly and long-winded. What I really need is some kind of $(removeprefix ...) function. Assuming that I know the prefix (a/) to be removed, can this be done with some kind of string manipulation?

like image 919
Lee Netherton Avatar asked Oct 24 '13 16:10

Lee Netherton


People also ask

What is prefix in Makefile?

The prefix is want is /usr for most packages. So --prefix=/usr will do the job, but not all packages come with configure, a lot of them have just a makefile.

How do I remove a prefix from a name in Python?

There are multiple ways to remove whitespace and other characters from a string in Python. The most commonly known methods are strip() , lstrip() , and rstrip() . Since Python version 3.9, two highly anticipated methods were introduced to remove the prefix or suffix of a string: removeprefix() and removesuffix() .


2 Answers

You can strip off a leading a/ with

$(FILE:a/%=%)

See the text substitution function reference for more options & details.

like image 69
Dan Avatar answered Sep 20 '22 23:09

Dan


Since you say GNU make, why not just:

$(FILE:a/%=%)

?

like image 28
MadScientist Avatar answered Sep 22 '22 23:09

MadScientist