help-make
[Top][All Lists]
Advanced

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

adding a third type of prerequisite


From: Christof Warlich
Subject: adding a third type of prerequisite
Date: Sat, 22 Jan 2011 15:23:14 +0100
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101208 Thunderbird/3.1.7

Hi Paul,

please apologize my beggar-mail (i.e. asking to add a new feature to make), but thinking it over and over again during the last couple of days, I believe this could be something of general interest for everyone using GNU make, so I'd like to kindly
ask to read on.

First, here is what I'd like to ask for: A third type of prerequisites besides the existing two ("ordinary prerequisites" and "order-only prerequisites", let's call them "dependency-only prerequisites") that, like "order-only prerequisites", are _not_ included in the automatic variable $^, but that behave like ordinary prerequisites
otherwise.

I have set up an example to understand why I think this would be useful: Consider a tool that makes its targets from several files given on its command line, but that may have additional dependencies that must not be passed to that tool. In such a case neither of the automatic variables $< nor $^ match, as the first only expands
to the first prerequisite, while the latter expands to all prerequisites.

Here is the complete example that illustrates the idea:

$ cat sum_main.c
#include <stdio.h>
#include "sum.h"
int main(int argc, char *argv[]) {
    int sum = 0;
    while(argc--) sum += size(argv[argc]);
    printf("%d\n", sum);
    return 0;
}

$ cat sum_lib.c
#include <sys/stat.h>
int size(const char *file) {
    struct stat s;
    stat(file, &s);
    return s.st_size;
}

$ cat sum.h
int size(const char *file);

$ cat Makefile
SUM := ./sum
all: allCfileSizeSum.sum
%.sum: *.c *.h | ${SUM}
    ${SUM} $^ >$@
%: %_*.c
    gcc $^ -o $@
${SUM}: | sum.h

$ make
gcc sum_lib.c sum_main.c -o sum
./sum sum_lib.c sum_main.c sum.h >allCfileSizeSum.sum

Everything works fine when calling make for the first time, but you may have noticed that I had to use "order-only prerequisites" to avoid that the sum executable is counted as a source file and that sum.h appears as additional input file to gcc. Hence:

$ touch sum.h
$ make
make: Nothing to be done for `all'.

That's not the desired behaviour: As everything ultimately depends on sum.h,
everything should be rebuilt.

The suggested "dependency-only prerequisite" would gracefully solve the issue.

An alternative could be to introduce a new automatic variable that expands to only those prerequisites that have been listed in the rule that has a recipe while excluding any prerequisites from reciepe-less rules, but this solution would be
inferior as it doesn't solve the problem for the example's first rule .

What do you think: Does this make sense? By the way, the idea came up when
playing with David Boyce' "audited objects" tool, which implements the generic
dependency generation approach that you have described here:

http://mad-scientist.net/make/autodep.html#nonc

A "dependency-only prerequisite" would particularly well be suited within
this context.

Cheers,

Chris












reply via email to

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