Get the base name of a folder in GNU make

It seems that the basename function interpreted by GNU make does not match bash basename. The former decrypts the suffix, while the latter also removes the path. How to get the base name of a folder in a makefile?

Also, why did they change it? (It took me 20 minutes to find the source of my error)

+6
source share
4 answers

I assume that the basename(1) command has two orthogonal functions - deleting stripping, deleting sections of leading directories - and GNU authors make it possible to call each functionality separately. And of course, only one concept can get the name basename.

Of course, in a makefile it is useful to be able to convert foo / bar / baz.c to foo / bar / baz so that you can bind a new suffix at the end to build the associated file name in the same directory as the source file.

In the comments to @ire_and_curses, answer that $(notdir $(CURDIR)) not enough for your purposes, since (being a directory) CURDIR can be specified as

 CURDIR = /foo/bar/ 

and notdir removes all this due to a trailing slash. To enable this way of writing the directory path, you need to explicitly disable the trailing slash, for example. $(notdir $(CURDIR:%/=%)) .

+2
source

Yes, this is strange. You can get the desired behavior by linking notdir and basename :

 $(notdir names...) Extracts all but the directory-part of each file name in names... For example, $(notdir src/foo.c hacks) produces the result 'foo.c hacks'. ... $(basename names...) Extracts all but the suffix of each file name in names. If the file name contains a period, the basename is everything starting up to (and not including) the last period... For example, $(basename src/foo.c src-1.0/bar hacks) produces the result 'src/foo src-1.0/bar hacks'. 

So, for example, you can convert /home/ari/src/helloworld.c to helloworld.html by chaining such functions:

 SRC=/home/ari/src/helloworld.c TARGET=$(addsuffix .html, $(notdir $(basename $(SRC)))) 
+3
source

You can still use the bash version:

 SHELL := /bin/bash basename := $(shell basename /why/in/gods/name) 
+1
source

Source: https://habr.com/ru/post/925383/


All Articles