From 56e3510e893820c6185b9d610b40574c8434828f Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Fri, 3 Aug 2018 10:19:56 +0200
Subject: [PATCH] detect deleted source files and recompile binaries

---
 makefile-binary  | 33 ++++++++++++++++++++++++++--
 makefile-library | 57 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 84 insertions(+), 6 deletions(-)

diff --git a/makefile-binary b/makefile-binary
index faece3163f..879cce9113 100644
--- a/makefile-binary
+++ b/makefile-binary
@@ -29,9 +29,38 @@ RELEASE_LINK_PATHS:=$(addsuffix /lib-release, $(addprefix ../, $(LINK_LIBRARIES)
 
 LDFLAGS_RELEASE:=-Wl,--no-as-needed $(addprefix -L, $(RELEASE_LINK_PATHS)) -rdynamic $(addprefix -l, $(LINK_LIBRARIES) $(SYSTEM_LIBRARIES)) $(LTO_PARAM) -Wl,-rpath,.
 
-OBJECTS_DEBUG:=$(patsubst src/%.cpp,obj-debug/%.o, $(shell find src/ -name *cpp))
+OBJECTS_DEBUG:=$(patsubst src/%.cpp,obj-debug/%.o, $(sort $(shell find src/ -name *cpp)))
 
-OBJECTS_RELEASE:=$(patsubst src/%.cpp,obj-release/%.o, $(shell find src/ -name *cpp))
+OBJECTS_RELEASE:=$(patsubst src/%.cpp,obj-release/%.o, $(sort $(shell find src/ -name *cpp)))
+
+# ---------------------------------------------------------------------------------------------------------------------------------------------------------
+# to handle deleted source files
+
+EXISTENT_OBJECTS_DEBUG:=$(sort $(shell find obj-debug/ -name *o))
+
+EXISTENT_OBJECTS_RELEASE:=$(sort $(shell find obj-release/ -name *o))
+
+EXTRA_OBJECTS_DEBUG:=$(filter-out $(OBJECTS_DEBUG), $(EXISTENT_OBJECTS_DEBUG))
+
+EXTRA_OBJECTS_RELEASE:=$(filter-out $(OBJECTS_RELEASE), $(EXISTENT_OBJECTS_RELEASE))
+
+ifneq ($(strip $(EXTRA_OBJECTS_DEBUG)),)
+ifneq ($(wildcard lib-debug/$(FULL_LIBRARY)),)
+ifneq ($(firstword $(EXISTENT_OBJECTS_DEBUG)),)
+$(shell touch $(firstword $(EXISTENT_OBJECTS_DEBUG)))
+endif
+endif
+endif
+
+ifneq ($(strip $(EXTRA_OBJECTS_RELEASE)),)
+ifneq ($(wildcard lib-release/$(FULL_LIBRARY)),)
+ifneq ($(firstword $(EXISTENT_OBJECTS_RELEASE)),)
+$(shell touch $(firstword $(EXISTENT_OBJECTS_RELEASE)))
+endif
+endif
+endif
+
+# ---------------------------------------------------------------------------------------------------------------------------------------------------------
 
 .PHONY: all build-debug clean-debug doc
 
diff --git a/makefile-library b/makefile-library
index 2628aae668..7bc241dbe8 100644
--- a/makefile-library
+++ b/makefile-library
@@ -44,11 +44,60 @@ LDFLAGS_RELEASE:=-rdynamic -shared $(LTO_PARAM) $(addprefix -L, $(RELEASE_LINK_P
 TEST_LDFLAGS_RELEASE:=-Wl,--no-as-needed $(addprefix -L, $(RELEASE_TEST_LINK_PATHS)) -rdynamic $(addprefix -l, $(TEST_LINK_LIBRARIES) $(TEST_SYSTEM_LIBRARIES)) -lcppunit $(LTO_PARAM) -Wl,-rpath,.
 
 
-OBJECTS_DEBUG:=$(patsubst src/%.cpp,obj-debug/%.o, $(shell find src/ -name *cpp))
-TEST_OBJECTS_DEBUG:=$(patsubst test-src/%.cpp,test-obj-debug/%.o, $(shell find test-src/ -name *cpp))
+OBJECTS_DEBUG:=$(patsubst src/%.cpp,obj-debug/%.o, $(sort $(shell find src/ -name *cpp)))
+TEST_OBJECTS_DEBUG:=$(patsubst test-src/%.cpp,test-obj-debug/%.o, $(sort $(shell find test-src/ -name *cpp)))
 
-OBJECTS_RELEASE:=$(patsubst src/%.cpp,obj-release/%.o, $(filter-out $(wildcard src/debug/*), $(shell find src/ -name *cpp)))
-TEST_OBJECTS_RELEASE:=$(patsubst test-src/%.cpp,test-obj-release/%.o, $(shell find test-src/ -name *cpp))
+OBJECTS_RELEASE:=$(patsubst src/%.cpp,obj-release/%.o, $(filter-out $(wildcard src/debug/*), $(sort $(shell find src/ -name *cpp))))
+TEST_OBJECTS_RELEASE:=$(patsubst test-src/%.cpp,test-obj-release/%.o, $(sort $(shell find test-src/ -name *cpp)))
+
+# ---------------------------------------------------------------------------------------------------------------------------------------------------------
+# to handle deleted source files
+
+EXISTENT_OBJECTS_DEBUG:=$(sort $(shell find obj-debug/ -name *o))
+EXISTENT_TEST_OBJECTS_DEBUG:=$(sort $(shell find test-obj-debug/ -name *o))
+
+EXISTENT_OBJECTS_RELEASE:=$(sort $(shell find obj-release/ -name *o))
+EXISTENT_TEST_OBJECTS_RELEASE:=$(sort $(shell find test-obj-release/ -name *o))
+
+EXTRA_OBJECTS_DEBUG:=$(filter-out $(OBJECTS_DEBUG), $(EXISTENT_OBJECTS_DEBUG))
+EXTRA_TEST_OBJECTS_DEBUG:=$(filter-out $(TEST_OBJECTS_DEBUG), $(EXISTENT_TEST_OBJECTS_DEBUG))
+
+EXTRA_OBJECTS_RELEASE:=$(filter-out $(OBJECTS_RELEASE), $(EXISTENT_OBJECTS_RELEASE))
+EXTRA_TEST_OBJECTS_RELEASE:=$(filter-out $(TEST_OBJECTS_RELEASE), $(EXISTENT_TEST_OBJECTS_RELEASE))
+
+ifneq ($(strip $(EXTRA_OBJECTS_DEBUG)),)
+ifneq ($(wildcard lib-debug/$(FULL_LIBRARY)),)
+ifneq ($(firstword $(EXISTENT_OBJECTS_DEBUG)),)
+$(shell touch $(firstword $(EXISTENT_OBJECTS_DEBUG)))
+endif
+endif
+endif
+
+ifneq ($(strip $(EXTRA_TEST_OBJECTS_DEBUG)),)
+ifneq ($(wildcard test-bin-debug/$(TESTBIN)),)
+ifneq ($(firstword $(EXISTENT_TEST_OBJECTS_DEBUG)),)
+$(shell touch $(firstword $(EXISTENT_TEST_OBJECTS_DEBUG)))
+endif
+endif
+endif
+
+ifneq ($(strip $(EXTRA_OBJECTS_RELEASE)),)
+ifneq ($(wildcard lib-release/$(FULL_LIBRARY)),)
+ifneq ($(firstword $(EXISTENT_OBJECTS_RELEASE)),)
+$(shell touch $(firstword $(EXISTENT_OBJECTS_RELEASE)))
+endif
+endif
+endif
+
+ifneq ($(strip $(EXTRA_TEST_OBJECTS_RELEASE)),)
+ifneq ($(wildcard test-bin-release/$(TESTBIN)),)
+ifneq ($(firstword $(EXISTENT_TEST_OBJECTS_RELEASE)),)
+$(shell touch $(firstword $(EXISTENT_TEST_OBJECTS_RELEASE)))
+endif
+endif
+endif
+
+# ---------------------------------------------------------------------------------------------------------------------------------------------------------
 
 .PHONY: all debug release clean build-debug build-release clean-debug clean-release doc
 
-- 
GitLab