Reputation: 1538
I somehow struggle to understand the intermediate files concept of make.
Consider this example of a make process:
Input files:
myProgram.code
myGraphic.picture
Build steps:
myGraphic.picture
In a Makefile, this would look like this:
all: myProgram.exe
myProgram.exe: myProgram.code myGraphic.picture.cut.converted
compiler -code myProgram.code -graphic myGraphic.picture.cut.converted
myGraphic.picture.cut.converted: myGraphic.picture.cut
converter -in myGraphic.picture.cut -out myGraphic.picture.cut.converted
myGraphic.picture.cut: myGraphic.picture
cutter -in myGraphic.picture -out myGraphic.picture.cut
As far as I understand, after running make
, I will have the compiled program, as well as the intermediate files .cut.converted
and .cut
.
Is there a way to delete those files automatically? And if so, is make intelligent enough to not to recreate all of them when the original picture is unchanged?
Upvotes: 1
Views: 446
Reputation: 106
Yes. You can delete files if you don't need them. For example:
results.txt : testzipf.py isles.dat abyss.dat last.dat
python $^ *.dat > $@
rm -f *.dat
.PONY : dats
dats : isles.dat abyss.dat last.dat
%.dat : books/%.txt countwords.py
python countwords.py $< $@
This file create a results.txt from dats and finally delete the dat files rm -f *.dat
If you run make
command again it will create the intermediate files again and delete them after utilising them to produce a target.
Your make script will look like
all: myProgram.exe
rm -f *.cut
myProgram.exe: myProgram.code myGraphic.picture.cut.converted
compiler -code myProgram.code -graphic myGraphic.picture.cut.converted
myGraphic.picture.cut.converted: myGraphic.picture.cut
converter -in myGraphic.picture.cut -out myGraphic.picture.cut.converted
myGraphic.picture.cut: myGraphic.picture
cutter -in myGraphic.picture -out myGraphic.picture.cut
Upvotes: 1
Reputation: 30992
If the target is specifically named in the Makefile, and not a dependency of the magic target .INTERMEDIATE
, then it will be kept.
So add an .INTERMEDIATE:
line, and/or rephrase your conversions to be pattern rules:
%.cut.converted: %.cut
converter -in $< -out $@
%.picture.cut: %.picture
cutter -in $< -out $@
This has a bonus of being easier to read, too.
You might be able to eliminate the need for at least some of the temporary files if the tools can be used as filters in a pipeline, of course.
Upvotes: 2
Reputation: 99172
Nothing easier. Just add this target:
.INTERMEDIATE: myGraphic.picture.cut myGraphic.picture.cut.converted
Upvotes: 3