From 92c324b2d85c326f270a0e9531b78d63ec8a95f8 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Fri, 13 Jun 2014 18:36:13 +0200
Subject: [PATCH] new faster makefile

---
 alib2/makefile     | 54 +++++++++++++++++++++++++---------------------
 alib2/objMakefile  | 33 ++++++++++++++++++++++++++++
 alib2/testMakefile | 33 ++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 25 deletions(-)
 create mode 100644 alib2/objMakefile
 create mode 100644 alib2/testMakefile

diff --git a/alib2/makefile b/alib2/makefile
index 96101b6a7b..f7b812d12c 100644
--- a/alib2/makefile
+++ b/alib2/makefile
@@ -1,52 +1,56 @@
 SHELL:=/bin/bash
 LIBRARY:=libalib2.so 
 TESTBIN:=alib2test
-CXXFLAGS:= -std=c++11 -O2 -g -c -Wall -pedantic -Wextra -fPIC -I/usr/include/libxml2/ 
+
 LDFLAGS:= -shared -lxml2
-TEST_CXXFLAGS:= -std=c++11 -O2 -g -c -Wall -pedantic -Wextra -I../alib2/src -I/usr/include/libxml2/ 
 TEST_LDFLAGS:= -L../alib2/lib -lxml2 -lalib2 -lcppunit -Wl,-rpath,.
 
-SOURCES:=$(shell find src/ -name *cpp)
-OBJECTS:=$(patsubst src/%.cpp, obj/%.o, $(SOURCES))
-DEPENDENCIES:=$(patsubst src/%.cpp, obj/%.d, $(SOURCES))
-
-TEST_SOURCES:=$(shell find test-src/ -name *cpp)
-TEST_OBJECTS:=$(patsubst test-src/%.cpp, test-obj/%.o, $(TEST_SOURCES))
-TEST_DEPENDENCIES:=$(patsubst test-src/%.cpp, test-obj/%.d, $(TEST_SOURCES))
+OBJECTS:=$(patsubst src/%.cpp, obj/%.o, $(shell find src/ -name *cpp))
+TEST_OBJECTS:=$(patsubst test-src/%.cpp, test-obj/%.o, $(shell find test-src/ -name *cpp))
 
 .PHONY: all build test clean
-.PRECIOUS: $(DEPENDENCIES) $(TEST_DEPENDENCIES)
 
 all: build test
 
-lib/$(LIBRARY): $(OBJECTS)
+
+
+lib/$(LIBRARY): obj/
 	mkdir -p lib;
-	$(CXX) $(OBJECTS) -o $@ $(LDFLAGS);
+	$(CXX) $(OBJECTS) -o $@ $(LDFLAGS)
 
-obj/%.d:
+obj/makefile:
 	mkdir -p $(dir $@)
-	echo "$(shell sha1sum <<< "$@" | sed "s/  -//g") = \$$(shell \$$(CXX) -MM \$$(CXXFLAGS) $(patsubst obj/%.d,src/%.cpp, $@) | sed \"s/.*://g;s/\\\\\\\\//g\")" >> $@
-	echo "$(patsubst %.d,%.o, $@): \$$($(shell sha1sum <<< "$@" | sed "s/  -//g"))" >> $@
-	echo "	\$$(CXX) \$$(CXXFLAGS) \$$< -o $(patsubst %.d,%.o, $@)" >> $@
+	cp objMakefile $@
+
+obj/: FORCE | obj/makefile
+	$(MAKE) -C $@
+
+
 
 build: lib/$(LIBRARY)
 
-test-bin/$(TESTBIN): $(TEST_OBJECTS) lib/$(LIBRARY)
-	mkdir -p test-bin; \
-	$(CXX) $(TEST_OBJECTS) -o $@ $(TEST_LDFLAGS); \
 
-test-obj/%.d:
+
+test-bin/$(TESTBIN): test-obj/ lib/$(LIBRARY)
+	mkdir -p test-bin;
+	$(CXX) $(TEST_OBJECTS) -o $@ $(TEST_LDFLAGS)
+
+test-obj/makefile:
 	mkdir -p $(dir $@)
-	echo "$(shell sha1sum <<< "$@" | sed "s/  -//g") = \$$(shell \$$(CXX) -MM \$$(TEST_CXXFLAGS) $(patsubst test-obj/%.d,test-src/%.cpp, $@) | sed \"s/.*://g;s/\\\\\\\\//g\")" >> $@
-	echo "$(patsubst %.d,%.o, $@): \$$($(shell sha1sum <<< "$@" | sed "s/  -//g"))" >> $@
-	echo "	\$$(CXX) \$$(TEST_CXXFLAGS) \$$< -o $(patsubst %.d,%.o, $@)" >> $@
+	cp testMakefile $@
+
+test-obj/: FORCE | test-obj/makefile
+	$(MAKE) -C $@
+
+
 
 test: test-bin/$(TESTBIN)
 	LD_LIBRARY_PATH=lib test-bin/$(TESTBIN)
 
+
+
 clean:
 	$(RM) -r *.o *.d lib obj test-bin test-obj
 
--include $(DEPENDENCIES)
--include $(TEST_DEPENDENCIES)
+FORCE:
 
diff --git a/alib2/objMakefile b/alib2/objMakefile
new file mode 100644
index 0000000000..91ec62602e
--- /dev/null
+++ b/alib2/objMakefile
@@ -0,0 +1,33 @@
+SHELL:=/bin/bash
+SRCDIR:=
+DEPTH:=
+
+CXXFLAGS:= -std=c++11 -O2 -g -c -Wall -pedantic -Wextra -fPIC -I/usr/include/libxml2/
+
+SOURCES:= $(shell find ../$(DEPTH)src/$(SRCDIR) -maxdepth 1 -type f -name "*.cpp")
+DEPENDENCIES:= $(patsubst ../$(DEPTH)src/$(SRCDIR)%.cpp, ../$(DEPTH)obj/$(SRCDIR)%.d, $(SOURCES))
+OBJECTS:= $(patsubst %.d, %.o, $(DEPENDENCIES))
+SOURCES_DIRS:= $(shell find ../$(DEPTH)src/$(SRCDIR) -maxdepth 1 -mindepth 1 -type d)
+OBJECTS_DIRS:= $(patsubst ../$(DEPTH)src/$(SRCDIR)%, %/, $(SOURCES_DIRS))
+OBJECTS_DIRS_MAKEFILES:= $(patsubst %, %makefile, $(OBJECTS_DIRS))
+
+.PHONY: all
+.PRECIOUS: $(DEPENDECIES) $(OBJECTS_DIRS_MAKEFILES)
+
+all: $(OBJECTS_DIRS) $(OBJECTS)
+
+%.d:
+	@echo "$(shell sha1sum <<< "$@" | sed "s/  -//g") = \$$(shell \$$(CXX) -MM \$$(CXXFLAGS) $(patsubst ../$(DEPTH)obj/$(SRCDIR)%.d,../$(DEPTH)src/$(SRCDIR)%.cpp, $@) | sed \"s/.*://g;s/\\\\\\\\//g\")" >> $@
+	@echo "$(patsubst %.d,%.o, $@): \$$($(shell sha1sum <<< "$@" | sed "s/  -//g"))" >> $@
+	@echo "	\$$(CXX) \$$(CXXFLAGS) \$$< -o $(patsubst %.d,%.o, $@)" >> $@
+
+%/makefile:
+	mkdir -p $(dir $@)
+	cp makefile $@
+
+%/: FORCE | %/makefile
+	$(MAKE) -C $@ SRCDIR=$(SRCDIR)$(notdir $(patsubst %/, %, $@))/ DEPTH=$(DEPTH)../
+
+FORCE:
+
+-include $(DEPENDENCIES)
diff --git a/alib2/testMakefile b/alib2/testMakefile
new file mode 100644
index 0000000000..670c4bd889
--- /dev/null
+++ b/alib2/testMakefile
@@ -0,0 +1,33 @@
+SHELL:=/bin/bash
+SRCDIR:=
+DEPTH:=
+
+CXXFLAGS:= -std=c++11 -Og -g -c -Wall -pedantic -Wextra -I../$(DEPTH)src/ -I/usr/include/libxml2/ 
+
+SOURCES:= $(shell find ../$(DEPTH)test-src/$(SRCDIR) -maxdepth 1 -type f -name "*.cpp")
+DEPENDENCIES:= $(patsubst ../$(DEPTH)test-src/$(SRCDIR)%.cpp, ../$(DEPTH)test-obj/$(SRCDIR)%.d, $(SOURCES))
+OBJECTS:= $(patsubst %.d, %.o, $(DEPENDENCIES))
+SOURCES_DIRS:= $(shell find ../$(DEPTH)test-src/$(SRCDIR) -maxdepth 1 -mindepth 1 -type d)
+OBJECTS_DIRS:= $(patsubst ../$(DEPTH)test-src/$(SRCDIR)%, %/, $(SOURCES_DIRS))
+OBJECTS_DIRS_MAKEFILES:= $(patsubst %, %makefile, $(OBJECTS_DIRS))
+
+.PHONY: all
+.PRECIOUS: $(DEPENDECIES) $(OBJECTS_DIRS_MAKEFILES)
+
+all: $(OBJECTS_DIRS) $(OBJECTS)
+
+%.d: $(OBJECT_DIRS)
+	@echo "$(shell sha1sum <<< "$@" | sed "s/  -//g") = \$$(shell \$$(CXX) -MM \$$(CXXFLAGS) $(patsubst ../$(DEPTH)test-obj/$(SRCDIR)%.d,../$(DEPTH)test-src/$(SRCDIR)%.cpp, $@) | sed \"s/.*://g;s/\\\\\\\\//g\")" >> $@
+	@echo "$(patsubst %.d,%.o, $@): \$$($(shell sha1sum <<< "$@" | sed "s/  -//g"))" >> $@
+	@echo "	\$$(CXX) \$$(CXXFLAGS) \$$< -o $(patsubst %.d,%.o, $@)" >> $@
+
+%/makefile:
+	mkdir -p $(dir $@)
+	cp makefile $@
+
+%/: FORCE | %/makefile
+	$(MAKE) -C $@ SRCDIR=$(SRCDIR)$(notdir $(patsubst %/, %, $@))/ DEPTH=$(DEPTH)../
+
+FORCE:
+
+-include $(DEPENDENCIES)
-- 
GitLab