Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip makefile dependency generation for certain targets (e.g. `clean`)

I have several C and C++ projects that all follow a basic structure I've been using for a while now. My source files go in src/*.c, intermediate files in obj/*.[do], and the actual executable in the top level directory.

My makefiles follow roughly this template:

# The final executable
TARGET := something
# Source files (without src/)
INPUTS := foo.c bar.c baz.c

# OBJECTS will contain: obj/foo.o obj/bar.o obj/baz.o
OBJECTS := $(INPUTS:%.cpp=obj/%.o)
# DEPFILES will contain: obj/foo.d obj/bar.d obj/baz.d
DEPFILES := $(OBJECTS:%.o=%.d)

all: $(TARGET)

obj/%.o: src/%.cpp
    $(CC) $(CFLAGS) -c -o $@ $<

obj/%.d: src/%.cpp
    $(CC) $(CFLAGS) -M -MF $@ -MT $(@:%.d=%.o) $<

$(TARGET): $(OBJECTS)
    $(LD) $(LDFLAGS) -o $@ $(OBJECTS)

.PHONY: clean
clean:
    -rm -f $(OBJECTS) $(DEPFILES) $(RPOFILES) $(TARGET)

-include $(DEPFILES)

Now I'm at the point where I'm packaging this for a Debian system. I'm using debuild to build the Debian source package, and pbuilder to build the binary package. The debuild step only has to execute the clean target, but even this causes the dependency files to be generated and included.

In short, my question is really: Can I somehow prevent make from generating dependencies when all I want is to run the clean target?

like image 659
Stéphan Kochen Avatar asked May 05 '10 12:05

Stéphan Kochen


People also ask

What are makefile targets?

A simple makefile consists of "rules" with the following shape: target ... : dependencies ... command ... ... A target is usually the name of a file that is generated by a program; examples of targets are executable or object files.

What does clean do in makefile?

It allows you to type 'make clean' at the command line to get rid of your object and executable files. Sometimes the compiler will link or compile files incorrectly and the only way to get a fresh start is to remove all the object and executable files.

What is makefile in unix?

Makefile is a program building tool which runs on Unix, Linux, and their flavors. It aids in simplifying building program executables that may need various modules. To determine how the modules need to be compiled or recompiled together, make takes the help of user-defined makefiles.

Can makefile target be a directory?

Yes, a Makefile can have a directory as target. Your problem could be that the cd doesn't do what you want: it does cd and the git clone is carried out in the original directory (the one you cd ed from, not the one you cd ed to). This is because for every command in the Makefile an extra shell is created.


1 Answers

The solution is easy, don't -include generated files under clean:

ifneq ($(MAKECMDGOALS),clean)
    -include $(DEPFILES)
endif
like image 62
Hasturkun Avatar answered Sep 27 '22 22:09

Hasturkun