Plazgoth
Plazgoth

Reputation: 1350

When using gnu make with -C option, how can I find the original calling directory?

Is it possible to determine the directory from which the user called make when they use the '-C' command line option?

UPDATE: A bit more explanation. I know in theory I should not need it. However in my case the user of the Makefiles would like to be able to control where the binaries end up, so I have allowed them to give me the path where they want them. So they can do something like:

make OUTPUT_PATH=/home/user/build/

However that breaks down when they call make from the directory they want to use for output.

cd /home/user/build
make -C /home/user/source OUTPUT_PATH=.

Hope that makes it a bit more clear. I am inclined to say this is not possible, but I wanted to query the community at large.

Thanks!

Upvotes: 0

Views: 2917

Answers (1)

MadScientist
MadScientist

Reputation: 100856

GNU make itself does not provide this ability. However, many shells will set the PWD environment variable, and since GNU make imports all environment variables as make macros, you can find out this way:

$ pwd
/home/madscientist

$ echo 'all: ; @echo $(PWD) $(CURDIR)' | make -f-
/home/madscientist /home/madscientist

$ echo 'all: ; @echo $(PWD) $(CURDIR)' | make -f- -C /tmp
/home/madscientist /tmp

Be aware, this relies on the shell's behavior! Different users' shells may behave differently.

Upvotes: 2

Related Questions