From 915746cbfdc6a3eb027a2c0dbcd4f4a8906b55c4 Mon Sep 17 00:00:00 2001
From: Assaf Gordon <assafgordon@gmail.com>
Date: Thu, 23 Feb 2012 18:27:07 -0500
Subject: [PATCH] Makefile: extract the sub-dir make into targets.

Allows parallel make with "-j N" .
Recommended in "Managing Projects with GNU Make", chapter 6, page 109.
---
 Makefile | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/Makefile b/Makefile
index 5b0b8624..5df6dc0b 100644
--- a/Makefile
+++ b/Makefile
@@ -78,25 +78,7 @@ UTIL_SUBDIRS =	$(SRC_DIR)/utils/bedFile \
 BUILT_OBJECTS = $(OBJ_DIR)/*.o
 
 
-all: gitversion
-	[ -d $(OBJ_DIR) ] || mkdir -p $(OBJ_DIR)
-	[ -d $(BIN_DIR) ] || mkdir -p $(BIN_DIR)
-	
-	@echo "Building BEDTools:"
-	@echo "========================================================="
-	
-	@for dir in $(UTIL_SUBDIRS); do \
-		echo "- Building in $$dir"; \
-		$(MAKE) --no-print-directory -C $$dir; \
-		echo ""; \
-	done
-
-	@for dir in $(SUBDIRS); do \
-		echo "- Building in $$dir"; \
-		$(MAKE) --no-print-directory -C $$dir; \
-		echo ""; \
-	done
-
+all: print_banner $(OBJ_DIR) $(BIN_DIR) gitversion $(UTIL_SUBDIRS) $(SUBDIRS)
 	@echo "- Building main bedtools binary."
 	@$(CXX) $(CXXFLAGS) -c src/bedtools.cpp -o obj/bedtools.o -I$(UTIL_DIR)/version/
 	@$(CXX) $(LDFLAGS) $(CXXFLAGS) -o $(BIN_DIR)/bedtools $(BUILT_OBJECTS) -L$(UTIL_DIR)/BamTools/lib/ -lbamtools $(LIBS)
@@ -110,6 +92,22 @@ all: gitversion
 
 .PHONY: all
 
+print_banner:
+	@echo "Building BEDTools:"
+	@echo "========================================================="
+.PHONY: print_banner
+
+# make the "obj/" and "bin/" directories, if they don't exist
+$(OBJ_DIR) $(BIN_DIR):
+	@mkdir -p $@
+
+# even though these are real directories, treat them as phony targets, forcing to always go in them are re-make.
+# a future improvement would be the check for the compiled object, and rebuild only if the source code is newer.
+.PHONY: $(UTIL_SUBDIRS) $(SUBDIRS)
+$(UTIL_SUBDIRS) $(SUBDIRS): $(OBJ_DIR) $(BIN_DIR)
+	@echo "- Building in $@"
+	@$(MAKE) --no-print-directory --directory=$@
+
 clean:
 	@echo "Cleaning up."	
 	@rm -f $(OBJ_DIR)/* $(BIN_DIR)/*
-- 
GitLab