git » libjio » commit 5898467

libjio: Add dependency tracking and independant build dir. to the Makefile

author Alberto Bertogli
2009-09-11 01:38:31 UTC
committer Alberto Bertogli
2009-09-11 01:38:31 UTC
parent 8f543bc9fa3c0b3c201b58278feb23ce6301fa01

libjio: Add dependency tracking and independant build dir. to the Makefile

This patch incorporates two Makefile improvements:

 - Put the build output in a different directory ("build/" by default)
 - Use dependency tracking, getting the dependencies while compiling (so
   they're almost free)

It could probably be split in different commits, but since they were all
done at once and it's not even source code, I don't think it's worth the
effort.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

.gitignore +2 -0
libjio/Makefile +62 -41

diff --git a/.gitignore b/.gitignore
index 81e88cf..3bab0cd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,8 @@
 *.so.*
 *.gcda
 *.gcno
+*.gcov
+libjio/build/
 jiofsck
 libjio.pc
 samples/full
diff --git a/libjio/Makefile b/libjio/Makefile
index 2e25ca3..b3dc0da 100644
--- a/libjio/Makefile
+++ b/libjio/Makefile
@@ -1,4 +1,7 @@
 
+# output directory, will be created if it doesn't exist
+O = build
+
 CFLAGS = -std=c99 -pedantic -Wall -O3
 
 MANDATORY_CFLAGS := \
@@ -10,11 +13,16 @@ MANDATORY_LDFLAGS := $(shell getconf LFS_LIBS 2>/dev/null)
 ALL_CFLAGS += $(CFLAGS) $(MANDATORY_CFLAGS) -fPIC
 ALL_LDFLAGS += $(LDFLAGS) $(MANDATORY_LDFLAGS) -fPIC
 
+
 # some platforms do not have librt, we only use it if available
 NEED_LIBRT := $(shell ld -o rtcheck.so -shared -lrt 2>/dev/null && echo -lrt; \
 	rm -f rtcheck.so)
+
 LIBS = -lpthread $(NEED_LIBRT)
 
+
+# shorthands for common build variants
+
 ifdef DEBUG
 ALL_CFLAGS += -g
 ALL_LDFLAGS += -g
@@ -30,6 +38,7 @@ ALL_CFLAGS += -DFIU_ENABLE=1
 LIBS += -lfiu
 endif
 
+
 # prefix for installing the binaries
 PREFIX = /usr/local
 
@@ -39,87 +48,99 @@ DESTDIR=$(PREFIX)
 # install utility, we assume it's GNU/BSD compatible
 INSTALL=install
 
+
+# nicer output
 ifneq ($(V), 1)
-        NICE_CC = @echo "  CC  $@"; $(CC)
-        NICE_AR = @echo "  AR  $@"; $(AR)
+        N_CC = @echo "  CC  $(subst $O/,,$@)"; $(CC)
+        N_AR = @echo "  AR  $(subst $O/,,$@)"; $(AR)
 else
-        NICE_CC = $(CC)
-        NICE_AR = $(AR)
+        N_CC = $(CC)
+        N_AR = $(AR)
 endif
 
 
+# library version, used for soname and generated documentation
 LIB_VER=0.90
 LIB_SO_VER=0
 
 
-# objects to build
-OBJS = autosync.o checksum.o common.o compat.o trans.o check.o journal.o \
-       unix.o ansi.o
+OBJS = $(addprefix $O/,autosync.o checksum.o common.o compat.o trans.o \
+               check.o journal.o unix.o ansi.o)
+
+
+# targets
 
-# rules
 default: all
 
-all: libjio.so libjio.a libjio.pc jiofsck
+all: $O/libjio.so $O/libjio.a $O/libjio.pc $O/jiofsck
 
-libjio.so: build-flags $(OBJS)
-	$(NICE_CC) -shared $(ALL_LDFLAGS) \
+# used to rebuild everything when the build flags have changed
+BF = $(CC) ~ $(ALL_CFLAGS) ~ $(PREFIX)
+$O/build-flags: .force-build-flags
+	@mkdir -p $O
+	@if [ x"$(BF)" != x"`cat $O/build-flags 2>/dev/null`" ]; then \
+		if [ -f $O/build-flags ]; then \
+			echo "build flags changed, rebuilding"; \
+		fi; \
+		echo "$(BF)" > $O/build-flags; \
+	fi
+
+$(OBJS): $O/build-flags
+
+$O/%.o: %.c
+	@mkdir -p $O
+	$(N_CC) $(ALL_CFLAGS) -MMD -MF $@.mak -MP -MT $@ -c $< -o $@
+
+sinclude $(OBJS:.o=.o.mak)
+
+$O/libjio.so: $O/build-flags $(OBJS)
+	$(N_CC) -shared $(ALL_LDFLAGS) \
 		-Wl,-soname,libjio.so.$(LIB_SO_VER) \
-		$(LIBS) $(OBJS) -o libjio.so.$(LIB_VER)
-	ln -fs libjio.so.$(LIB_VER) libjio.so
+		$(LIBS) $(OBJS) -o $O/libjio.so.$(LIB_VER)
+	@echo "  LN  libjio.so.$(LIB_VER)"
+	@ln -fs libjio.so.$(LIB_VER) $O/libjio.so
 
-libjio.a: build-flags $(OBJS)
-	$(NICE_AR) cr libjio.a $(OBJS)
+$O/libjio.a: $O/build-flags $(OBJS)
+	$(N_AR) cr $@ $(OBJS)
 
-libjio.pc: build-flags libjio.pc.in
-	@echo "generating libjio.pc"
+$O/libjio.pc: $O/build-flags libjio.pc.in
+	@echo "  GEN libjio.pc"
 	@cat libjio.pc.in | \
 		sed 's@++PREFIX++@$(DESTDIR)@g' | \
 		sed 's@++VERSION++@$(LIB_VER)@g' | \
 		sed 's@++CFLAGS++@$(MANDATORY_CFLAGS)@g' \
-		> libjio.pc
+		> $O/libjio.pc
 
-jiofsck: build-flags jiofsck.o libjio.a
-	$(NICE_CC) $(ALL_LDFLAGS) jiofsck.o libjio.a $(LIBS) -o jiofsck
+$O/jiofsck: $O/build-flags $O/jiofsck.o $O/libjio.a
+	$(N_CC) $(ALL_LDFLAGS) $O/jiofsck.o $O/libjio.a $(LIBS) -o $@
 
 install: all
 	$(INSTALL) -d $(PREFIX)/lib
-	$(INSTALL) -m 0755 libjio.so.$(LIB_VER) $(PREFIX)/lib
+	$(INSTALL) -m 0755 $O/libjio.so.$(LIB_VER) $(PREFIX)/lib
 	ln -fs libjio.so.$(LIB_VER) $(PREFIX)/lib/libjio.so
 	ln -fs libjio.so.$(LIB_VER) $(PREFIX)/lib/libjio.so.$(LIB_SO_VER)
-	$(INSTALL) -m 0644 libjio.a $(PREFIX)/lib
+	$(INSTALL) -m 0644 $O/libjio.a $(PREFIX)/lib
 	$(INSTALL) -d $(PREFIX)/include
 	$(INSTALL) -m 0644 libjio.h $(PREFIX)/include
 	$(INSTALL) -d $(PREFIX)/lib/pkgconfig
-	$(INSTALL) -m 644 libjio.pc $(PREFIX)/lib/pkgconfig
+	$(INSTALL) -m 644 $O/libjio.pc $(PREFIX)/lib/pkgconfig
 	$(INSTALL) -d $(PREFIX)/bin
-	$(INSTALL) -m 0775 jiofsck $(PREFIX)/bin
+	$(INSTALL) -m 0775 $O/jiofsck $(PREFIX)/bin
 	$(INSTALL) -d $(PREFIX)/share/man/man3
 	$(INSTALL) -m 0644 libjio.3 $(PREFIX)/share/man/man3/
 	@echo
 	@echo "Please run ldconfig to update your library cache"
 	@echo
 
-BF = $(ALL_CFLAGS) ~ $(PREFIX)
-build-flags: .force-build-flags
-	@if [ x"$(BF)" != x"`cat build-flags 2>/dev/null`" ]; then \
-		if [ -f build-flags ]; then \
-			echo "build flags changed, rebuilding"; \
-		fi; \
-		echo "$(BF)" > build-flags; \
-	fi
-
-$(OBJS): build-flags
-
-.c.o:
-	$(NICE_CC) $(ALL_CFLAGS) -c $< -o $@
-
 doxygen:
 	$(MAKE) LIB_VER=$(LIB_VER) -C doxygen
 
 clean:
-	rm -f libjio.a libjio.so libjio.so.$(LIB_VER) libjio.pc
-	rm -f $(OBJS) jiofsck.o jiofsck
-	rm -f *.bb *.bbg *.da *.gcov *.gcno *.gcda gmon.out build-flags
+	rm -f $O/libjio.a $O/libjio.so $O/libjio.so.$(LIB_VER) $O/libjio.pc
+	rm -f $(OBJS) $O/jiofsck.o $O/jiofsck
+	rm -f $O/*.bb $O/*.bbg $O/*.da $O/*.gcov $O/*.gcno $O/*.gcda $O/gmon.out
+	rm -f $O/build-flags $O/*.o.mak
+
 	$(MAKE) -C doxygen $@