user3214546
user3214546

Reputation: 6831

How can i call make file from other directory

I have directory structure like this

containers/con1
containers/con2
containers/con3

Now every folder like con1, con2 has Makefile in it with targets like build, run

I run it like make run and make build

But i have to go inside that folder.

Is it possible that i have another Makefile in containers/Makefile

and i can run like

Make con1.run Make con2.run

Upvotes: 0

Views: 555

Answers (3)

Valeri Atamaniouk
Valeri Atamaniouk

Reputation: 5163

Recursive makes are evil, but if you want that:

# con1.run is a phony target...
.PHONY: con1.run

con1.run:
    $(MAKE) -C con1

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 80921

Yes, you can do that. Something like the following should do what you want.

$ cat containers/Makefile
%.run: %
        $(MAKE) -C $@

That being said as you can see the command to do what you want is trivial enough to make such a makefile not really necessary (and a simple shell script is as useful here as a makefile).

$ cat run.sh
[ -d "$1" ] || { echo 'No such directory.' >&2; exit 1; }
#make -C "$1"
# OR
#cd "$1" && make

If you wanted to be able to build all the sub-directory projects at once then a makefile could help you with that but even that is a simple enough shell one-liner.

$ for mkfile in */Makefile; do make -C "$(dirname "$mkfile"); done
$ for mkfile in */Makefile; do (cd "$(dirname "$mkfile") && make); done

Upvotes: 1

Vor
Vor

Reputation: 35099

As far as I understand you want this:

   -C dir, --directory=dir
        Change to directory dir before reading the makefiles or doing anything else.  If multiple -C options are specified, each is interpreted relative to the previous one: -C / -C etc is equivalent to -C /etc.   This  is  typi‐
        cally used with recursive invocations of make.

Add -C option like this: make -C con1/

Upvotes: 0

Related Questions