Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split up path names in gnu make?

Tags:

gnu-make

In GNU Make, I have a requirement to obtain the name of the parent directory of the directory my makefile is running in.

For example, if the makefile is /home/fnord/foo/bar/Makefile, I want to set a variable to the string "foo".

I can do this using some of the GNU Make built in functions, shown here all split up for clarity.

V1=$(CURDIR)
V2=$(dir $(V1))
V3=$(subst /, ,$(V2))
V4=$(lastword $(V3))

This seems complex for such a simple requirement, but I can't find a better way. Are there any better techniques for splitting up pathnames in GNU Make?

I need this to work in GNU Make version 3.81 and later.

like image 704
Simon Elliott Avatar asked Oct 04 '22 11:10

Simon Elliott


1 Answers

Because of the way the $(dir ...) function works in GNU make, it's not as nice as it could be, but it's better than the above (IMO):

V4 = $(notdir $(patsubst %/,%,$(dir $(CURDIR))))

The idea behind leaving the trailing slash is to allow joins (making the operation reversible), but in my experience this ends up not being very useful in practice, and having to strip the trailing slash is a PITA. However, it is what it is and there's far too much history to change it now.

ETA:

If you like you can make a function that works more like the shell's dirname and use call:

dirname = $(patsubst %/,%,$(dir $1))

V4 = $(notdir $(call dirname,$(CURDIR)))
like image 140
MadScientist Avatar answered Oct 12 '22 10:10

MadScientist