Search code examples
linuxmakefilerm

Linux make command is deleting a source file


I have inherited a project file that has a Makefile in it that is doing something I have never seen before--It is injecting a rm command. I cannot find any reason for the rm command, so I am missing something very obvious or very esoteric.

Thanks

The results of running make are:

bison  --defines --xml --graph=calc.gv -o calc.c calc.y
Bison flags = 
cc    -c -o calc.o calc.c
Making BASE =  calc
cc  -o calc calc.o
Done making BASE
rm calc.c      <======== WHERE IS THIS COMING FROM?

The Makefile is:

BASE = calc
BISON = bison
XSLTPROC = xsltproc

all: $(BASE)

%.c %.h %.xml %.gv: %.y
    $(BISON) $(BISONFLAGS) --defines --xml --graph=$*.gv -o $*.c $<
    @echo "Bison flags = " $(BISONFLAGS)

$(BASE): $(BASE).o
    @echo "Making BASE = " $(BASE) 
    $(CC) $(CFLAGS) -o $@ $^
    @echo "Done making BASE"

run: $(BASE)
    @echo "Type arithmetic expressions.  Quit with ctrl-d."
    ./$<

html: $(BASE).html
%.html: %.xml
    $(XSLTPROC) $(XSLTPROCFLAGS) -o $@ $$($(BISON) --print-datadir)/xslt/xml2xhtml.xsl $<

CLEANFILES = $(BASE) *.o $(BASE).[ch] $(BASE).output $(BASE).xml $(BASE).html $(BASE).gv

clean:
    @echo "Running clean" $(CLEANFILES)
    rm -f $(CLEANFILES)

Solution

  • See https://www.gnu.org/software/make/manual/make.html#Chained-Rules:

    The second difference is that if make does create b in order to update something else, it deletes b later on after it is no longer needed. Therefore, an intermediate file which did not exist before make also does not exist after make. make reports the deletion to you by printing a rm -f command showing which file it is deleting.