help-make
[Top][All Lists]
Advanced

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

[RFC] New feature: $(xargs ... )


From: Dave Korn
Subject: [RFC] New feature: $(xargs ... )
Date: Fri, 9 Mar 2007 17:00:06 -0000

  So, I found myself running out of command line length the other day.

TOTALDEPFILE:=makefile.dep
TOTALDEPS:=$(OBJS:%.o=%.d)
$(TOTALDEPFILE): $(TOTALDEPS)
        @echo "# Auto-generated dependencies - do not edit" > $@
        @cat $^ >> $@

  That's why: I want to cat all my .d files into one big one for the entire
project, but now we've got a lot of .d files, and the list overflowed
MAX_ARGS.

  OK, it's a bit superfluous to cat them all into one file, but when you've
got hundreds and hundreds of them it's quicker during a build to just read in
one hefty file instead of hundreds of tiny ones.

  And for the same reason, I was reluctant to just use a foreach and cat them
into the output file one at a time.  Inefficient.

  What I really wanted is to use xargs.  But of course, if "cat <list>" is too
long, then "echo <list> | xargs cat" isn't going to be any shorter.  In fact,
I was completely stumped: I couldn't figure any way to manipulate that list of
names, because it was too long to get into a shell commandline any way at all.


  So no 'cut/sed/grep' tricks could help, I couldn't echo it to a temporary
file, I couldn't see I had any choice between processing all the entries
one-by-one or doing the whole lot in one go (and failing).

  The solution I hit on was to add a new function: $(xargs ...).  It behaves
very much like foreach, but instead of copying single words one at a time from
the list to the iteration variable and expanding the text once per word in the
list, this one accumulates as many words from the list as you tell it will fit
in the iteration variable at one time and expands the repeated text as few
times as needed until the entire input list is exhausted.  Here's the doco:





8.6 The `xargs' Function
========================

The `xargs' function is very similar to the `foreach' function.  It
differs in that it allows more than one substitution to be performed
into each of the repeated uses of the text.

   The syntax of the `xargs' function is:

     $(xargs FLAGS,VAR,LIST,TEXT)

The first three arguments, FLAGS, VAR and LIST, are expanded before
anything else is done; note that the last argument, TEXT, is *not*
expanded at the same time.  Then, one or more words at a time from the
expanded value of LIST are concatenated into the variable named by the
expanded value of VAR, and TEXT is expanded.  Presumably TEXT contains
references to that variable, so its expansion will be different each
time.

   The result is that TEXT is expanded many times, each time with one
or more whitespace-separated words from LIST, until all the words in
LIST have been exhausted.  The multiple expansions of TEXT are
concatenated, with spaces between them, to make the result of `xargs'.

   The FLAGS passed to the `xargs' function control the way in which it
assembles words from LIST into VAR each time it expands TEXT, and are
similar to the option switches available to the `xargs' command-line
utility.  Each switch must be concatenated to its argument to form a
single whitespace-separated word in FLAGS.  The recognized options are:

     `-nMAX-ARGS'  - set maximum number of words to assemble
                          in VAR per expansion of TEXT.

     `-sMAX-CHARS' - set maximum number of characters to assemble
                          in VAR per expansion of TEXT.

     `-l'          - append a newline between each of the multiple
                          expansions of TEXT, instead of a single space.

     `-L'          - append a newline and a TAB between each of the multiple
                          expansions of TEXT, instead of a single space.





  Any interest in this new feature?  

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....





reply via email to

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