Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top