]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - Makefile
Compare Makefile with itself instead of using `--help`
[qmk_firmware.git] / Makefile
index c8122e5765ac04038b413f876eee35ea951455fc..636eae3e5b4e3177fdcf7c70a0af949588705bd1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,10 @@ ifndef VERBOSE
 .SILENT:
 endif
 
+# Never run this makefile in parallel, as it could screw things up
+# It won't affect the submakes, so you still get the speedup from specifying -jx
+.NOTPARALLEL:
+
 # Allow the silent with lower caps to work the same way as upper caps
 ifdef silent
     SILENT = $(silent)
@@ -28,6 +32,9 @@ ABS_ROOT_MAKEFILE := $(abspath $(ROOT_MAKEFILE))
 ABS_STARTING_DIR := $(dir $(ABS_STARTING_MAKEFILE))
 ABS_ROOT_DIR := $(dir $(ABS_ROOT_MAKEFILE))
 STARTING_DIR := $(subst $(ABS_ROOT_DIR),,$(ABS_STARTING_DIR))
+BUILD_DIR := $(ROOT_DIR)/.build
+TEST_DIR := $(BUILD_DIR)/test
+ERROR_FILE := $(BUILD_DIR)/error_occured
 
 MAKEFILE_INCLUDED=yes
 
@@ -224,6 +231,8 @@ define PARSE_RULE
     # PARSE_ALL_KEYBOARDS
     ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,allkb),true)
         $$(eval $$(call PARSE_ALL_KEYBOARDS))
+    else ifeq ($$(call COMPARE_AND_REMOVE_FROM_RULE,test),true)
+        $$(eval $$(call PARSE_TEST))
     # If the rule starts with the name of a known keyboard, then continue
     # the parsing from PARSE_KEYBOARD
     else ifeq ($$(call TRY_TO_MATCH_RULE_FROM_LIST,$$(KEYBOARDS)),true)
@@ -262,7 +271,7 @@ define PARSE_KEYBOARD
        # If there's no matching subproject, we assume it's the default
        # This will allow you to leave the subproject part of the target out
     else 
-        $$(eval $$(call PARSE_SUBPROJECT,defaultsp))
+        $$(eval $$(call PARSE_SUBPROJECT,))
     endif
 endef
 
@@ -278,12 +287,14 @@ endef
 define PARSE_SUBPROJECT
     # If we want to compile the default subproject, then we need to 
     # include the correct makefile to determine the actual name of it
-    ifeq ($1,defaultsp)
+    CURRENT_SP := $1
+    ifeq ($$(CURRENT_SP),)
+        CURRENT_SP := defaultsp
+    endif
+    ifeq ($$(CURRENT_SP),defaultsp)
         SUBPROJECT_DEFAULT=
         $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/Makefile)
         CURRENT_SP := $$(SUBPROJECT_DEFAULT)
-    else
-        CURRENT_SP := $1
     endif
     # If current subproject is empty (the default was not defined), and we have a list of subproject
     # then make all of them
@@ -314,16 +325,16 @@ define PARSE_SUBPROJECT
         # Otherwise try to match the keymap from the current folder, or arguments to the make command
         else ifneq ($$(KEYMAP),)
             $$(eval $$(call PARSE_KEYMAP,$$(KEYMAP)))
+        # No matching keymap found, so we assume that the rest of the rule is the target
+        # If we haven't been able to parse out a subproject, then make all of them
+        # This is consistent with running make without any arguments from the keyboard
+        # folder
+        else ifeq ($1,)
+            $$(eval $$(call PARSE_ALL_SUBPROJECTS))
+        # Otherwise, make all keymaps, again this is consistent with how it works without
+        # any arguments
         else
-            # Otherwise something is wrong with the target
-            # Try to give as much information as possible of what it it was trying to do
-            ifeq ($$(CURRENT_SP),)
-                $$(info make: *** No rule to make target '$$(CURRENT_KB)-$$(RULE)'. Stop.)
-            else
-                $$(info make: *** No rule to make target '$$(CURRENT_KB)-$$(CURRENT_SP)-$$(RULE)'. Stop.)
-            endif
-            # Notice the tab instead of spaces below!
-                       exit 1
+            $$(eval $$(call PARSE_ALL_KEYMAPS))
         endif
     else
         # As earlier mentione,d when allsb is specified, we call our self recursively
@@ -354,7 +365,6 @@ define PARSE_KEYMAP
     MAKE_TARGET := $$(patsubst -%,%,$$(RULE))
     # We need to generate an unique indentifer to append to the COMMANDS list
     COMMAND := COMMAND_KEYBOARD_$$(CURRENT_KB)_SUBPROJECT_$(CURRENT_SP)_KEYMAP_$$(CURRENT_KM)
-    COMMANDS += $$(COMMAND)
     # If we are compiling a keyboard without a subproject, we want to display just the name
     # of the keyboard, otherwise keyboard/subproject
     ifeq ($$(CURRENT_SP),)
@@ -366,13 +376,18 @@ define PARSE_KEYMAP
     KB_SP := $(BOLD)$$(KB_SP)$(NO_COLOR)
     # Specify the variables that we are passing forward to submake
     MAKE_VARS := KEYBOARD=$$(CURRENT_KB) SUBPROJECT=$$(CURRENT_SP) KEYMAP=$$(CURRENT_KM)
-    MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
     # And the first part of the make command
     MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_keyboard.mk $$(MAKE_TARGET)
     # The message to display
     MAKE_MSG := $$(MSG_MAKE_KB)
     # We run the command differently, depending on if we want more output or not
     # The true version for silent output and the false version otherwise
+    $$(eval $$(call BUILD))
+endef
+
+define BUILD
+    MAKE_VARS += VERBOSE=$(VERBOSE) COLOR=$(COLOR)
+    COMMANDS += $$(COMMAND)
     COMMAND_true_$$(COMMAND) := \
         printf "$$(MAKE_MSG)" | \
         $$(MAKE_MSG_FORMAT); \
@@ -386,7 +401,10 @@ define PARSE_KEYMAP
         fi;
     COMMAND_false_$$(COMMAND) := \
         printf "$$(MAKE_MSG)\n\n"; \
-        $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false;
+        $$(MAKE_CMD) $$(MAKE_VARS) SILENT=false; \
+        if [ $$$$? -gt 0 ]; \
+            then error_occured=1; \
+        fi;
 endef
 
 # Just parse all the keymaps for a specifc keyboard
@@ -394,6 +412,41 @@ define PARSE_ALL_KEYMAPS
     $$(eval $$(call PARSE_ALL_IN_LIST,PARSE_KEYMAP,$$(KEYMAPS)))
 endef
 
+define BUILD_TEST
+    TEST_NAME := $1
+    MAKE_TARGET := $2
+    COMMAND := $1
+    MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_test.mk $$(MAKE_TARGET)
+    MAKE_VARS := TEST=$$(TEST_NAME)
+    MAKE_MSG := $$(MSG_MAKE_TEST)
+    $$(eval $$(call BUILD))
+    ifneq ($$(MAKE_TARGET),clean)
+        TEST_EXECUTABLE := $$(TEST_DIR)/$$(TEST_NAME).elf
+        TESTS += $$(TEST_NAME)
+        TEST_MSG := $$(MSG_TEST)
+        $$(TEST_NAME)_COMMAND := \
+            printf "$$(TEST_MSG)\n"; \
+            $$(TEST_EXECUTABLE); \
+            if [ $$$$? -gt 0 ]; \
+                then error_occured=1; \
+            fi; \
+            printf "\n";
+    endif
+endef
+
+define PARSE_TEST
+    TESTS :=
+    TEST_NAME := $$(firstword $$(subst -, ,$$(RULE)))
+    TEST_TARGET := $$(subst $$(TEST_NAME),,$$(subst $$(TEST_NAME)-,,$$(RULE)))
+    ifeq ($$(TEST_NAME),all)
+        MATCHED_TESTS := $$(TEST_LIST)
+    else
+        MATCHED_TESTS := $$(foreach TEST,$$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME),$$(TEST)),$$(TEST),))
+    endif
+    $$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
+endef
+
+
 # Set the silent mode depending on if we are trying to compile multiple keyboards or not
 # By default it's on in that case, but it can be overriden by specifying silent=false 
 # from the command line
@@ -409,8 +462,21 @@ endef
 
 include $(ROOT_DIR)/message.mk
 
-RUN_COMMAND = \
-$(COMMAND_$(SILENT_MODE)_$(COMMAND))
+# The empty line is important here, as it will force a new shell to be created for each command
+# Otherwise the command line will become too long with a lot of keyboards and keymaps
+define RUN_COMMAND
++error_occured=0;\
+$(COMMAND_$(SILENT_MODE)_$(COMMAND))\
+if [ $$error_occured -gt 0 ]; then echo $$error_occured > $(ERROR_FILE); fi;
+
+
+endef
+define RUN_TEST
++error_occured=0;\
+$($(TEST)_COMMAND))\
+if [ $$error_occured -gt 0 ]; then echo $$error_occured > $(ERROR_FILE); fi;
+
+endef
 
 # Allow specifying just the subproject, in the keyboard directory, which will compile all keymaps
 SUBPROJECTS := $(notdir $(patsubst %/Makefile,%,$(wildcard ./*/Makefile)))
@@ -421,7 +487,7 @@ $(SUBPROJECTS): %: %-allkm
 .PHONY: %
 %: 
        # Check if we have the CMP tool installed
-       cmp --version >/dev/null 2>&1; if [ $$? -gt 0 ]; then printf "$(MSG_NO_CMP)"; exit 1; fi;
+       cmp $(ROOT_DIR)/Makefile $(ROOT_DIR)/Makefile >/dev/null 2>&1; if [ $$? -gt 0 ]; then printf "$(MSG_NO_CMP)"; exit 1; fi;
        # Check if the submodules are dirty, and display a warning if they are
        git submodule status --recursive 2>/dev/null | \
        while IFS= read -r x; do \
@@ -430,20 +496,21 @@ $(SUBPROJECTS): %: %-allkm
                        *) printf "$(MSG_SUBMODULE_DIRTY)";break;; \
                esac \
        done
+       rm -f $(ERROR_FILE) > /dev/null 2>&1
        $(eval $(call PARSE_RULE,$@))
        $(eval $(call SET_SILENT_MODE))
        # Run all the commands in the same shell, notice the + at the first line
        # it has to be there to allow parallel execution of the submake
        # This always tries to compile everything, even if error occurs in the middle
        # But we return the error code at the end, to trigger travis failures
-       +error_occured=0; \
-       $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND)) \
-       if [ $$error_occured -gt 0 ]; then printf "$(MSG_ERRORS)" & exit $$error_occured; fi
-       
+       $(foreach COMMAND,$(COMMANDS),$(RUN_COMMAND))
+       if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
+       $(foreach TEST,$(TESTS),$(RUN_TEST)) 
+       if [ -f $(ERROR_FILE) ]; then printf "$(MSG_ERRORS)" & exit 1; fi;
 
 # All should compile everything
 .PHONY: all
-all: all-keyboards 
+all: all-keyboards test-all
 
 # Define some shortcuts, mostly for compability with the old syntax
 .PHONY: all-keyboards
@@ -452,9 +519,16 @@ all-keyboards: allkb-allsp-allkm
 .PHONY: all-keyboards-defaults
 all-keyboards-defaults: allkb-allsp-default
 
+.PHONY: test
+test: test-all
+
+.PHONY: test-clean
+test-clean: test-all-clean
 
 # Generate the version.h file
 GIT_VERSION := $(shell git describe --abbrev=6 --dirty --always --tags 2>/dev/null || date +"%Y-%m-%d-%H:%M:%S")
 BUILD_DATE := $(shell date +"%Y-%m-%d-%H:%M:%S")
 $(shell echo '#define QMK_VERSION "$(GIT_VERSION)"' > $(ROOT_DIR)/quantum/version.h)
-$(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
\ No newline at end of file
+$(shell echo '#define QMK_BUILDDATE "$(BUILD_DATE)"' >> $(ROOT_DIR)/quantum/version.h)
+
+include $(ROOT_DIR)/testlist.mk