wangx1ng wangx1ng - 1 year ago 86
Bash Question

Getting parent directory's basename in makefile

I need to automate a variable alignment in my Makefile. My Makefile's full file path is:


In my Makefile, I have a variable
, a part of which should be equal to the
in the full path.

So I did this:

BRANCH_NAME= $(shell cd ../.. && basename "$PWD" && cd projectname/modulename)

Apparently I expected
to be
here, but after
I got
instead. I think it's most likely that I got a wrong
. Is there something wrong with what I did? And if yes I'd like to get some advice about how to do it correctly.


Answer Source

It's because $ has a special meaning to Make, so if you want to pass that up to shell you have to "escape" it. In case of Make, you escape the dollar sign by doubling. So you have to use $$PWD.

Also, what you are doing is not really the best way - it is always best to avoid the shell and use Make functionality if possible. In your case, the best way to do what you want is this:

BUILD_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/../../build)

You have to put the above line in the makefile in question, near the top, so that it is before you include any other makefiles.