I have a several Makefiles in app specific directories like this:
/project1/apps/app_typeA/Makefile
/project1/apps/app_typeB/Makefile
/project1/apps/app_typeC/Makefile
Each Makefile includes a .inc file in this path one level up:
/project1/apps/app_rules.inc
Inside app_rules.inc I'm setting the destination of where I want the binaries to be placed when built. I want all binaries to be in their respective app_type
path:
/project1/bin/app_typeA/
I tried using $(CURDIR)
, like this:
OUTPUT_PATH = /project1/bin/$(CURDIR)
but instead I got the binaries buried in the entire path name like this: (notice the redundancy)
/project1/bin/projects/users/bob/project1/apps/app_typeA
What can I do to get the "current directory" of execution so that I can know just the app_typeX
in order to put the binaries in their respective types folder?
The shell function.
You can use
shell
function:current_dir = $(shell pwd)
. Orshell
in combination withnotdir
, if you need not absolute path:current_dir = $(notdir $(shell pwd))
.Update.
Given solution only works when you are running
make
from the Makefile's current directory.As @Flimm noted:
Code below will work for any for Makefiles invoked from any directory:
If you are using GNU make, $(CURDIR) is actually a built-in variable. It is the
location where the Makefile residesthe current working directory, which is probably where the Makefile is, but not always.See Appendix A Quick Reference in http://www.gnu.org/software/make/manual/make.html
Example for your reference, as below:
The folder structure might be as:
Where there are two Makefiles, each as below;
Now, let us see the content of the Makefiles.
Now, execute the sample/Makefile, as;
Explanation, would be that the parent/home directory can be stored in the environment-flag, and can be exported, so that it can be used in all the sub-directory makefiles.
Here is one-liner to get absolute path to your
Makefile
file using shell syntax:And here is version without shell based on @0xff answer:
Test it by printing it, like:
I like the chosen answer, but I think it would be more helpful to actually show it working than explain it.
/tmp/makefile_path_test.sh
Output:
NOTE: The function
$(patsubst %/,%,[path/goes/here/])
is used to strip the trailing slash.