help-make
[Top][All Lists]
Advanced

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

Re: Question about use of $(wildcard) and $(foreach)


From: Rick Flower
Subject: Re: Question about use of $(wildcard) and $(foreach)
Date: Mon, 16 Apr 2007 13:32:13 -0700
User-agent: Thunderbird 2.0.0.0 (Windows/20070326)

Mike Shal wrote:
On 4/16/07, Rick Flower <address@hidden> wrote:
I took a different approach and listed all of the possible paths in a
variable called "OBJDIRS" and wanted to write some makefile 'code' that
could pick out the only one that exists for the directory tree where
Make was invoked from.. Below is what I was trying but found that it
would only return a valid OBJDIR when the valid directory was at the end
of the list (IIRC) or other odd conditions:

OBJDIR =
OBJDIRS := \
           ../../${proc}/obj/${target) \
           ../../${target}/obj \
           ../../obj/${target}

First: note that you have "${target)" - you should probably use either
"${target}" or "$(target)", rather than mix and match your parens :).

Hmm.. Good catch.. I hadn't noticed that before.. I think that was one of the problems I had..

find_files  = $(if $(wildcard $(dir)),OBJDIR=${dir})
OBJDIR := $(foreach dir,$(OBJDIRS),$(find_files))

$(wildcard) already operates separately on each element in the list,
so you don't need to wrap it with a $(foreach) in this case. Also, you
wouldn't want OBJDIR= in the $(if) statement, since you aren't
actually executing statements there.

Anyway, this could probably be much simplified by just doing:

OBJDIR := $(firstword $(wildcard $(OBJDIRS)))

I tried that w/o the above fix for the typo in place and it didn't work (nor did John's suggestion either).. Once I fixed the typo above to have matching open/closing braces then it started working.. I wonder if this could be caught during the parsing phase and have an error issued.. I certainly didn't see it and it obviously had side effects..

The $(firstword) is used in case more than one directory exists - this
would just pick the first one. You probably also want to check if no
match was found and print an error, like:

ifeq (,$(OBJDIR))
 $(error No directory found)
endif

Thanks.. I'll put that catch-all statement in as well.. Thanks again to both Mike and John for quick answers!

-- Rick




reply via email to

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