about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorDan Liew <daniel.liew@imperial.ac.uk>2016-12-02 23:12:07 +0000
committerDan Liew <daniel.liew@imperial.ac.uk>2016-12-03 10:39:25 +0000
commitcfcd0d8658c41b95240f73a652f9f556d857e1be (patch)
treec37c7a9a6e4470fab3cb0c404aa7f16925fa522a
parent9293a3b584a8249ec8846e9200699e46e22a887c (diff)
downloadklee-cfcd0d8658c41b95240f73a652f9f556d857e1be.tar.gz
[CMake] Fix bug in the Makefile bitcode build system where the runtime
would not recompile if the LLVM C compiler flags changed. This could
happen if the user did something like

```
make -f Makefile.cmake.bitcode LLVMCC.ExtraFlags=-Wall
```
-rw-r--r--runtime/Makefile.cmake.bitcode.config.in1
-rw-r--r--runtime/Makefile.cmake.bitcode.rules20
2 files changed, 19 insertions, 2 deletions
diff --git a/runtime/Makefile.cmake.bitcode.config.in b/runtime/Makefile.cmake.bitcode.config.in
index c554265f..5efa0154 100644
--- a/runtime/Makefile.cmake.bitcode.config.in
+++ b/runtime/Makefile.cmake.bitcode.config.in
@@ -39,6 +39,7 @@ USE_MODULE_INSTEAD_OF_BCA := @USE_RUNTIME_BINARY_TYPE_HACK@
 # Commands
 MKDIR := mkdir
 RM := rm
+CMP := cmp
 
 # Compiler flags
 LLVMCC.Flags += $(LLVMCC.ExtraFlags) \
diff --git a/runtime/Makefile.cmake.bitcode.rules b/runtime/Makefile.cmake.bitcode.rules
index 27abde54..4471eb98 100644
--- a/runtime/Makefile.cmake.bitcode.rules
+++ b/runtime/Makefile.cmake.bitcode.rules
@@ -9,7 +9,7 @@
 # This files defines the rules used for the bitcode build system. They are
 # inspired by LLVM's old Makefile build system.
 #===------------------------------------------------------------------------===#
-.PHONY: all build_at_level clean debug_vars
+.PHONY: all build_at_level clean debug_vars force
 
 CURRENT_DIR:= $(shell pwd)
 
@@ -78,8 +78,14 @@ C_SRCS_NO_DIR := $(notdir $(C_SRCS))
 BC_FILES_NO_DIR := $(C_SRCS_NO_DIR:.c=.bc)
 BC_FILES := $(addprefix $(LOCAL_BUILD_DIR)/,$(BC_FILES_NO_DIR))
 
+# Path to file that stores the flags used for LLVMCC.
+# If the build flags are changed this should trigger
+# a change to the this file which will force a recompile
+# of all sources
+LLVMCC_FLAGS_FILE := $(LOCAL_BUILD_DIR)/LLVMCC_FLAGS
+
 # All bitcode files have these additional dependencies
-$(BC_FILES) : $(ADDITIONAL_BUILD_DEPS)
+$(BC_FILES) : $(ADDITIONAL_BUILD_DEPS) $(LLVMCC_FLAGS_FILE)
 
 # Include dependency information generated by previous
 # compiler invocations if they exist
@@ -91,6 +97,15 @@ $(LOCAL_BUILD_DIR)/%.bc : $(SRC_DIR)/%.c
 	$(Verb) $(MKDIR) -p $(LOCAL_BUILD_DIR)
 	$(Verb) $(LLVMCC) $(LLVMCC.Flags) $(LLVMCC.Warnings) $< -c -o $@ -MP -MMD -MF $(LOCAL_BUILD_DIR)/$*.dep
 
+# $(LLVMCC_FLAGS_FILE) depends on `force` which will force the rule to
+# rerun every build. However the rule will only update the file when the
+# compile flags change which means we will trigger a rebuild when the compile
+# flags change.
+LLVMCC_FLAGS_FOR_FILE := $(LLVMCC) $(LLVMCC.Flags) $(LLVMCC.Warnings)
+$(LLVMCC_FLAGS_FILE): force
+	$(Verb) $(MKDIR) -p "$(dir $(LLVMCC_FLAGS_FILE))"
+	$(Verb)echo "$(LLVMCC_FLAGS_FOR_FILE)" | $(CMP) -s - $@ || echo "$(LLVMCC_FLAGS_FOR_FILE)" > $@
+
 clean::
 	@echo "Removing bitcode files"
 	$(Verb) $(RM) -f $(BC_FILES)
@@ -154,6 +169,7 @@ debug_vars:
 	@echo "IS_RELEASE := $(IS_RELEASE)"
 	@echo "LOCAL_BUILD_DIR := $(LOCAL_BUILD_DIR)"
 	@echo "LLVMCC := $(LLVMCC)"
+	@echo "LLVMCC_FLAGS_FILE := $(LLVMCC_FLAGS_FILE)"
 	@echo "LLVMCC.Flags := $(LLVMCC.Flags)"
 	@echo "LLVMCC.ExtraFlags := $(LLVMCC.ExtraFlags)"
 	@echo "LLVMCC.Warnings := $(LLVMCC.Warnings)"