Bowi
Bowi

Reputation: 1538

Can i let make delete intermediate files?

I somehow struggle to understand the intermediate files concept of make.

Consider this example of a make process:

Input files:

Build steps:

  1. Cut off the borders of myGraphic.picture
  2. Convert the cut graphic to a special format
  3. Compile the code, including the converted graphic.

This is the concept: Concept drawing

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

Answers (3)

Ta_Req
Ta_Req

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

Toby Speight
Toby Speight

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

Beta
Beta

Reputation: 99172

Nothing easier. Just add this target:

.INTERMEDIATE: myGraphic.picture.cut myGraphic.picture.cut.converted

Upvotes: 3

Related Questions