Tech Support > Operating Systems > Linux / Variants > Question about variables in a makefile
Question about variables in a makefile
Posted by Madhusudan Singh on March 4th, 2004


Hi

I have used simple makefiles in the past but this is the first time I am
writing a (relatively) fancy makefile.

I am using a shell command (grep -i -l -e "pattern") to extract a list of
files from the current directory. I want to assign the entire such list to
a makefile variable. How do I do this ?

I want the solution to work both on SunOS and Linux. From the manpages,
there are some differences between the way make is implemented.

Thanks,

MS

Posted by Paul D. Smith on March 4th, 2004


%% Madhusudan Singh <spammers-go-here@nowhere.now> writes:

ms> I am using a shell command (grep -i -l -e "pattern") to extract a
ms> list of files from the current directory. I want to assign the
ms> entire such list to a makefile variable. How do I do this ?

ms> I want the solution to work both on SunOS and Linux. From the
ms> manpages, there are some differences between the way make is
ms> implemented.

The only portable way to do this is via recursion:

all:
$(MAKE) recurse FILES=`grep -i -l -e "pattern"`

# All the real stuff happens here
recurse:


This is a bad solution in a lot of ways, but it's the only thing you can
do from within make itself that will work on all versions of make.

Other alternatives include invoking a shell script that does the
computation, then runs make for you with the right variable setting on
the command line (as above).

--
-------------------------------------------------------------------------------
Paul D. Smith <psmith@gnu.org> Find some GNU make tips at:
http://www.gnu.org http://make.paulandlesley.org
"Please remain calm...I may be mad, but I am a professional." --Mad Scientist

Posted by Madhusudan Singh on March 4th, 2004


On Thu, 04 Mar 2004 18:34:22 -0500, Paul D. Smith wrote:

Well, I decided to type in the files directly. I have another question.

Let us say that the variable SRCFILES contains a space separated list of
source files. Now how do I create the list $(OBJDIR)/$(SRCFILES:.f90=.o) ?

I just want to prepend a string to the name of each resulting object file
in the macro string substitution.

Posted by Jeff Schwab on March 5th, 2004


Madhusudan Singh wrote:

On Solaris9:

CMD=egrep -il pattern *
MATCHES=$(CMD:sh)
main: $(MATCHES)
echo $(MATCHES)


On Linux:

MATCHES=$(shell grep -ile pattern *)
main: $(MATCHES)
echo $(MATCHES)

The portable solution is to call make from a shell script, exporting
environment variables that contain the output of the shell command.


Posted by Dan Espen on March 5th, 2004


Madhusudan Singh <spammers-go-here@nowhere.now> writes:

Paul's right about the portability issue EXCEPT that gnumake is portable
to SunOS and Linux.

Still not convinced, try this:

CCS:=$(wildcard *.cc)
CCSTARG:=$(subst .cc,.o,$(CCS))

Only works with gnumake.
Use it, learn to love it.

Posted by Paul D. Smith on March 5th, 2004


%% Madhusudan Singh <spammers-go-here@yahoo.com> writes:

ms> I found the following on Sun's make (I think it is dmake) :

No. Sun's make is SysV make.

ms> Pattern Replacement Macro References
ms> Pattern matching replacements can also be applied to macros,
ms> with a reference of the form:

ms> $(name: op%os= np%ns)

ms> where op is the existing (old) prefix and os is the existing
ms> (old) suffix, np and ns are the new prefix and new suffix,
ms> respectively, and the pattern matched by % (a string of zero
ms> or more characters), is carried forward from the value being

ms> Is this supported for gmake ?

Yes, this syntax is supported by GNU make.

However note that it is not supported by _every_ make.

The POSIX standard definition of make is very weak: it doesn't even
require that make support an "include" capability! While in most cases
I prefer to write to a standard and use whatever standard tools are
available on the system, in the case of make the standard is not robust
enough to implement anything but the most simplistic (and therefore
difficult to write and maintain) makefiles.

My opinion (completely unbiased!! ) is that in this situation it's
better to rely on a portable _make_ (and one of my primary concerns with
GNU make is always portability) and not bother trying to write portable
makefiles.


YMMV of course.

--
-------------------------------------------------------------------------------
Paul D. Smith <psmith@gnu.org> Find some GNU make tips at:
http://www.gnu.org http://make.paulandlesley.org
"Please remain calm...I may be mad, but I am a professional." --Mad Scientist

Posted by Gianni Mariani on March 5th, 2004


Madhusudan Singh wrote:
GNU make supports all kinds of things like this. Do an "info make" and
see for yourself.

BTW - alot of these kinds of things are handled by MakeXS - check out
http://www.makexs.com. (shameless plug)






Posted by Dan Espen on March 5th, 2004


Madhusudan Singh <spammers-go-here@yahoo.com> writes:

So, what you've got above would look like this with gnumake:

CPPS:=$(wildcard *.cpp)
CPPTARG:=$(addprefix $(PREFIX)/, $(subst .cpp,.o,$(CPPS)))

Somehow, I find the gnumake stuff lot easier to read.

You don't have to be admin to install gnumake.
I believe gnumake is on the Solaris installation CDs.
Your admin should have no problem installing stuff packaged by Sun.


Similar Posts