makefile head-scratcher: multiple targets in one go

Hi!

I've got a build process where scripts create multiple targets from their sources. But here I'm running into a conceptual problem of GNU make: If one has multiple targets in a dependency, make applies the rules once for every target that is out of sync - which is correct for normal compiling, but not here.

Lets say running myscript abcd.conf creates the files a.txt b.txt c.txt and d.txt in one go, and the script should be run if any of the targets are out of sync.

So I've got something like

a.txt b.txt c.txt d.txt : abcd.conf
  myscript abcd.conf

but that would run mysript abcd.conf four times when abcd.conf gets updated, with $@ pointing at each target in turn :wall:. As mysript is quite time- and resource-consuming, this is something I need to avoid. As any of the target files might get out of sync, I can't just select one to force a single run.

I've read the make documentation up and down to no avail, and found nothing useful on Google. My experiments so far have failed, and I'm fresh out of ideas.

I always considered make to be the "swiss army knife" of automated building processes, and used it for quite some odd things over the years, but now I could use some input:

Is that what I want to achieve even possible with GNU make or do I have to look for other options (perl or shell scripts are no problems)?

Is there something in the make documentation that I failed to see or understand properly?

Anyone out there with a clever makefile trick for this?

yours, Christian Treczoks

Hi.

My understanding is that you want to run a command if any of the targets are out of date, but only once, not once for each target. If so, then this seems to work:

#!/usr/bin/env bash

# @(#) s1	Demonstrate PHONY makefile targets.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Makefile:"
cat Makefile

pl " Command to execute if anything out of date:"
cat s2

pl " Initial content of files" [a-z].txt
cat [a-d].txt

pl " Results for touching conf:"
touch abcd.conf
make
cat [a-d].txt

pl " Results for touching a.txt:"
touch a.txt
make
cat [a-d].txt

producing:

% ./s1

-----
 Makefile:
# Sun Feb  6 15:37:15 CST 2011

.PHONY: all
all: a.txt b.txt c.txt d.txt abcd.conf
	./s2 abcd.conf

-----
 Command to execute if anything out of date:
#!/usr/bin/env bash

# @(#) s2	Add one to change content of files.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

for file in [abcd].txt
do
  t1=$( cat $file )
  (( t1++ ))
  pe "$t1" > $file
done

-----
 Initial content of files a.txt b.txt c.txt d.txt
0
0
0
0

-----
 Results for touching conf:
./s2 abcd.conf
1
1
1
1

-----
 Results for touching a.txt:
./s2 abcd.conf
2
2
2
2

The file abcd.conf here is a dummy file, yours presumably has something that is operated upon by your "myscript".

Best wishes ... cheers, drl

( Edit 1: minor typo )

---------- Post updated at 16:40 ---------- Previous update was at 16:00 ----------

Hi.

I see that I forgot to add a plain make without touching anything. That action will also run the ./s2 script, so this is probably not an appropriate solution -- one might as well just always run the script.

I'll continue to poke around ... cheers, drl

---------- Post updated at 18:21 ---------- Previous update was at 16:40 ----------

Hi.

Mod 2:

#!/usr/bin/env bash

# @(#) s1	Demonstrate PHONY makefile targets.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

M=m2
pl " Makefile, $M:"
cat $M

pl " Command to execute if anything out of date:"
cat s2

pl " Initial content of files" [a-z].txt
cat [a-d].txt

pl " Results for touching a.txt:"
touch a.txt
make -f $M
cat [a-d].txt

pl " Results for touching c.txt:"
sleep 1
touch c.txt
make -f $M
cat [a-d].txt

pl " Results for plain make, nothing out of date:"
sleep 1
make -f $M
cat [a-d].txt

pl " Results for touching abcd.conf, backdated 5 seconds:"
t1=$(date --date="now - 5 sec" +'%Y%m%d%H%M.%S')
touch -t "$t1" abcd.conf
make -f $M
cat [a-d].txt

producing:

% ./s1

-----
 Makefile, m2:
# Sun Feb  6 15:37:15 CST 2011

abcd.conf: a.txt b.txt c.txt d.txt 
	./s2 abcd.conf
	touch abcd.conf

-----
 Command to execute if anything out of date:
#!/usr/bin/env bash

# @(#) s2	Add one to change content of files.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

for file in [abcd].txt
do
  t1=$( cat $file )
  (( t1++ ))
  pe "$t1" > $file
done

-----
 Initial content of files a.txt b.txt c.txt d.txt
0
0
0
0

-----
 Results for touching a.txt:
./s2 abcd.conf
touch abcd.conf
1
1
1
1

-----
 Results for touching c.txt:
./s2 abcd.conf
touch abcd.conf
2
2
2
2

-----
 Results for plain make, nothing out of date:
make: `abcd.conf' is up to date.
2
2
2
2

-----
 Results for touching abcd.conf, backdated 5 seconds:
./s2 abcd.conf
touch abcd.conf
3
3
3
3

cheers, drl

1 Like

Yep, entirely correct.

Indeed, an interesting idea to touch the config file and turn the dependencies around.

BUT:
a) It will only work when updating an existing set of files, not with creating them initially ("No rule to make target 'a.txt' needed by 'abcd.conf'")
b) It will not produce correct results in my case, as the same config file is used by several different scripts to generate targets (sometimes only one, sometimes 256 in one go)

I found a solution. Not nice, but I have to generate the makefile anyway...

# "myscript conf.dat" creates/updates a.txt, b.txt c.txt, and d.txt
all : a.txt b.txt c.txt d.txt
a.txt : conf.dat
  myscript conf.dat

b.txt : conf.dat
  myscript conf.dat

c.txt : conf.dat
  myscript conf.dat

d.txt : conf.dat
  myscript conf.dat

Looks like GNU make re-evaluates all targets after each run of a rule. Lets see how it copes with all ~8500 rules of the project...:smiley:

Hi.

An analogy from the compilation scene -- normally you would have lines in your Makefile like:

program: main.o function1.o function2.o ... functionn.o
...
function1.o: function1.c
etc

you would not have the *.c as targets because you would have created them with an editor (with the exception of things like lex/yacc generated files, in which case you would have entered the requisite base files for them).

If you really need something to create the *.txt files, then I suggest that you create an initialization script that touches a.txt, b.txt, etc. -- which is exactly what I did: it made the initial content of the [abcd].txt files to be "0" so that a) they existed, b) had a value with which the demonstration equivalent of myscript could work.

If your solution performs satisfactorily, then use it; if not, then you might consider the suggestion above.

Best wishes ... cheers, drl