[prev in list] [next in list] [prev in thread] [next in thread] 

List:       linux-sparse
Subject:    Re: [PATCH]: Makefile automatic header dependency
From:       "Christopher Li" <sparse () chrisli ! org>
Date:       2008-12-26 0:14:59
Message-ID: 70318cbf0812251614y1c00fbe3ye85e3501803ef737 () mail ! gmail ! com
[Download RAW message or body]

On Thu, Dec 25, 2008 at 1:30 AM, Sam Ravnborg <sam@ravnborg.org> wrote:
> Hi Chris.
> 
> I tried to apply your patch here.
> It was word wrapped but that was easy to fix.
> 
> A few comments below.

Thanks, that is exactly what I am looking for.

> > +CFLAGS += -Wp,-MD,$(@D)/.$(@F).d
> 
> This works well with gcc - but if we want to build sparse with a wider
> set of tool chains then this may fail.

Good point. I add a test run of the flags before we apply them.

> > 's|@version@|$(VERSION)|g;s|@prefix@|$(PREFIX)|g;s|@libdir@|$(LIBDIR)|g;s|@includedir@|$(INCLUDEDIR)|g'
> >  sparse.pc.in > sparse.pc
> 
> When you are patching the Makefile then I suggest breaking this line up somehow.
> Should be a follow-up patch.

Will do.

> Such a catch-all rule may likely give problems
> in the future. But for now it looks good.

I am trying to stay away from those scary double $$.
I just convert them using macros. Have to use a few
double $$, but it does not look too bad.

> I tried with:
> $(PROGRAMS): %.o: $(LIBS)
> 
> but that failed due to the special treatment of compile
> in the rule above.

You mean:

$(PROGRAMS): % : %o  $(LIBS)

The only exception is "compile" depend on compile.o and compile-i386.o.
So I use a macro to solve that.

> This gives a warning when there is no .*.o.d files.
> You need to do something like:
> $(if $(wildcard .*.o.d), include $(wildcard .*.o.d))

It is fixed.

Thank for the feed back. See the updated patch

Chris


["makefile.patch.txt" (text/plain)]

Makefile automatic header dependency

It use the gcc generated dependency file to track
header file changes.

Use pattern rules to build programes.
Makefile is much shorter now. Easier to add
new objs or new programs.

Signed-Off-By: Christopher Li<sparse@chrisli.org>

Index: sparse.chrisl/Makefile
===================================================================
--- sparse.chrisl.orig/Makefile
+++ sparse.chrisl/Makefile
@@ -15,10 +15,16 @@ AR = ar
 #CFLAGS += -O0 -DDEBUG -g3 -gdwarf-2
 
 HAVE_LIBXML=$(shell pkg-config --exists libxml-2.0 && echo 'yes')
-
+HAVE_GCC_DEP=$(shell touch .gcc-test.c && 				\
+		$(CC) -c -Wp,-MD,.gcc-test.d .gcc-test.c 2>/dev/null && \
+		echo 'yes'; rm -f .gcc-test.d .gcc-test.o .gcc-test.c)
 
 CFLAGS += -DGCC_BASE=\"$(shell $(CC) --print-file-name=)\"
 
+ifeq ($(HAVE_GCC_DEP),yes)
+CFLAGS += -Wp,-MD,$(@D)/.$(@F).d
+endif
+
 DESTDIR=
 PREFIX=$(HOME)
 BINDIR=$(PREFIX)/bin
@@ -28,16 +34,15 @@ MAN1DIR=$(MANDIR)/man1
 INCLUDEDIR=$(PREFIX)/include
 PKGCONFIGDIR=$(LIBDIR)/pkgconfig
 
-PROGRAMS=test-lexing test-parsing obfuscate compile graph sparse test-linearize \
                example \
-	 test-unssa test-dissect ctags
-
-
+PROGRAMS=test-lexing test-parsing obfuscate compile graph sparse \
+	 test-linearize example test-unssa test-dissect ctags
 INST_PROGRAMS=sparse cgcc
 INST_MAN1=sparse.1 cgcc.1
 
 ifeq ($(HAVE_LIBXML),yes)
 PROGRAMS+=c2xml
 INST_PROGRAMS+=c2xml
+c2xml_EXTRA_OBJS = `pkg-config --libs libxml-2.0`
 endif
 
 LIB_H=    token.h parse.h lib.h symbol.h scope.h expression.h target.h \
@@ -98,44 +103,17 @@ install: $(INST_PROGRAMS) $(LIBS) $(LIB_
 sparse.pc: sparse.pc.in
 	$(QUIET_GEN)sed 's|@version@|$(VERSION)|g;s|@prefix@|$(PREFIX)|g;s|@libdir@|$(LIBDIR)|g;s|@includedir@|$(INCLUDEDIR)|g' \
sparse.pc.in > sparse.pc  
-test-lexing: test-lexing.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
-
-test-parsing: test-parsing.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
-
-test-linearize: test-linearize.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
-
-test-sort: test-sort.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
-
-compile: compile.o compile-i386.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< compile-i386.o $(LIBS)
-
-obfuscate: obfuscate.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
-
-sparse: sparse.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
-
-graph: graph.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
-
-example: example.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
 
-test-unssa: test-unssa.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
+compile_EXTRA_DEPS = compile-i386.o
 
-test-dissect: test-dissect.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
+PROG_LINK_CMD = $(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $^ $($@_EXTRA_OBJS) 
 
-ctags: ctags.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
+define BUILD_PROGRAM
+$(prog): $(prog).o $$($(prog)_EXTRA_DEPS) $$(LIBS)
+	$$(PROG_LINK_CMD)
+endef
 
-c2xml: c2xml.o $(LIBS)
-	$(QUIET_LINK)$(CC) $(LDFLAGS)  -o $@ $< $(LIBS) `pkg-config --libs libxml-2.0`
+$(foreach prog,$(PROGRAMS),$(eval $(BUILD_PROGRAM)))
 
 $(LIB_FILE): $(LIB_OBJS)
 	$(QUIET_AR)$(AR) rcs $@ $(LIB_OBJS)
@@ -143,47 +121,13 @@ $(LIB_FILE): $(LIB_OBJS)
 $(SLIB_FILE): $(LIB_OBJS)
 	$(QUIET_LINK)$(CC) $(LDFLAGS) -Wl,-soname,$@ -shared -o $@ $(LIB_OBJS)
 
-evaluate.o: $(LIB_H)
-expression.o: $(LIB_H)
-lib.o: $(LIB_H)
-allocate.o: $(LIB_H)
-ptrlist.o: $(LIB_H)
-parse.o: $(LIB_H)
-pre-process.o: $(LIB_H)
-scope.o: $(LIB_H)
-show-parse.o: $(LIB_H)
-symbol.o: $(LIB_H)
-expand.o: $(LIB_H)
-linearize.o: $(LIB_H)
-flow.o: $(LIB_H)
-cse.o: $(LIB_H)
-simplify.o: $(LIB_H)
-memops.o: $(LIB_H)
-liveness.o: $(LIB_H)
-sort.o: $(LIB_H)
-inline.o: $(LIB_H)
-target.o: $(LIB_H)
-test-lexing.o: $(LIB_H)
-test-parsing.o: $(LIB_H)
-test-linearize.o: $(LIB_H)
-test-dissect.o: $(LIB_H)
-test-unssa.o: $(LIB_H)
-ctags.o: $(LIB_H)
-compile.o: $(LIB_H) compile.h
-compile-i386.o: $(LIB_H) compile.h
-tokenize.o: $(LIB_H)
-sparse.o: $(LIB_H)
-obfuscate.o: $(LIB_H)
-example.o: $(LIB_H)
-storage.o: $(LIB_H)
-dissect.o: $(LIB_H)
-graph.o: $(LIB_H)
+DEP_FILES := $(wildcard .*.o.d)
+$(if $(DEP_FILES),$(eval include $(DEP_FILES)))
 
 c2xml.o: c2xml.c $(LIB_H)
 	$(QUIET_CC)$(CC) `pkg-config --cflags libxml-2.0` -o $@ -c $(CFLAGS) $<
 
-compat-linux.o: compat/strtold.c compat/mmap-blob.c \
-	$(LIB_H)
+compat-linux.o: compat/strtold.c compat/mmap-blob.c $(LIB_H)
 compat-solaris.o: compat/mmap-blob.c $(LIB_H)
 compat-mingw.o: $(LIB_H)
 compat-cygwin.o: $(LIB_H)
@@ -192,7 +136,7 @@ compat-cygwin.o: $(LIB_H)
 	$(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $<
 
 clean: clean-check
-	rm -f *.[oa] *.so $(PROGRAMS) $(SLIB_FILE) pre-process.h sparse.pc
+	rm -f *.[oa] .*.d *.so $(PROGRAMS) $(SLIB_FILE) pre-process.h sparse.pc
 
 dist:
 	@if test "`git describe`" != "$(VERSION)" ; then \


--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic