LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Makefile and subdirectories (https://www.linuxquestions.org/questions/programming-9/makefile-and-subdirectories-354140/)

pycoucou 08-17-2005 04:35 AM

Makefile and subdirectories
 
Hi there,

I would like to write down a simple Makefile at the root of my source directory in order to go in each directory to do a make all there.

I'm developping some javacode and a my own library called py. In py, I've got different subfoler in order to keep my source more or less organised:
./py/util (general purpose class, like extra math functions)
./py/images (class which open images, process them)
./py/gui (class of the gui to my program)
./py/io

etc...

In each folder, I've got some .java files and a Makefile. Usually, I go in each directory and perform a make all there and it works. Now, I would like to be smart. I stay in ./ and I type make all. I would like it goes in each subdirectory and perform a make all.

I tried so far and unsuccessfully:

-------------------------------------------------
SHELL = /bin/sh
CC = javac
SUBDIRS = images gui io util
#---------------------------------------------------------------------
# Rules
#---------------------------------------------------------------------

all:
for dir in ${SUBDIRS} ; do ( cd py/$dir ; ${MAKE} all ) ; done

-------------------------------------------------------

It says everything is up to date :(

How can I do?

Thanks for your help,
PY

cdhgee 08-17-2005 07:39 AM

Try this in your Makefile:

Code:

DIRS = src doc man etc

all:
        set -e; for d in $(DIRS); do $(MAKE) -C $$d ; done


pycoucou 08-17-2005 08:22 AM

Great! It works very well !

The only point is to give the rule to follow otherwise it takes the first by default !
Code:

all:
        set -e; for dir in ${SUBDIRS} ; do  ${MAKE} -C py/$$dir all; done

And a quick question: What the purpose of `set -e'?

cdhgee 08-17-2005 08:40 AM

It tells it not to exit if the result code is non-zero. This means in practice that it will continue trying to make all the subdirectories even if it encounters errors doing so.

So if you've got

Code:

SUBDIRS = images gui io util
and if make in the images directory fails, it will continue trying to make gui, io, and util.

bigearsbilly 08-18-2005 04:43 AM

don't forget to read this:

http://www.canb.auug.org.au/~millerp...cons-harm.html

recursive make considered harmful ;)


All times are GMT -5. The time now is 10:19 PM.