help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: can't always get make to auto create build directory


From: LMH
Subject: Re: can't always get make to auto create build directory
Date: Wed, 29 Jun 2016 20:34:17 -0400
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:43.0) Gecko/20100101 Firefox/43.0 SeaMonkey/2.40

Paul Smith wrote:
On Wed, 2016-06-29 at 14:03 -0400, LMH wrote:
#create build directory if it doesn't exist
$(BDIR):
         @mkdir -p $(BDIR)

all: $(BDIR)/SMD2_i386.exe

Here you've created a target $(BDIR), but your "all" target depends
only on the object file $(BDIR)/SMD2_i386.exe.

Since nothing depends on the actual target $(BDIR), make will never try
to build that target.

In order for this to work you'd need to define the directory as a
prerequisite of the target which needs it, which in this case is
$(BDIR)/SMD2_i386.exe, so you'd need to write:

   $(BDIR)/SMD2_i386.exe : <other prereqs> $(BDIR)

However, you should generally not have targets depend on directories,
because make treats them just like any other file when it checks
modification times; however directories do not act like normal files
when it comes to modification times.

I usually just use:

   $(shell mkdir -p $(BDIR))

but other people prefer order-only prerequisites:

   $(BDIR)/SMD2_i386.exe : <other prereqs> | $(BDIR)

(note the extra "|" there).  Check the GNU make manual for details.

Note these are all specific to GNU make and not portable to other versions of 
make.



Going back through older versions of some of my makefiles, I find that I used to have an "archdir" target defined along with the code to create the build directory.


archdir: ${BDIR}
${BDIR}:
        @mkdir -p $(BDIR)


The archdir target was included in the all target like,


all: archdir $(BDIR)/SMD2_i386.exe


Somewhere along the line, this archdir target seems to have been left out and I guess this is where I started having problems in cases where the build directory didn't already exist. The build directory likely did exist in many cases, so I probably didn't notice right away or thought it was a problem esoteric to the OS I was booted into.

If you don't mind my asking, how does the above "archdir" solution compare with what you suggested as far as defining $(BDIR) as a prerequisite for $(BDIR)/SMD2_i386.exe? Is there any difference?

Your shell solution is simpler than mine in that it doesn't need to actually check if the directory exists and just suppresses the error message if it does. I am wondering if there is any value to actually making the check, but I can't think of any off hand.

LMH







reply via email to

[Prev in Thread] Current Thread [Next in Thread]