From 032a56fdb6f0f31bf86407aa18be4308e279d8b2 Mon Sep 17 00:00:00 2001 From: Seebs Date: Sat, 26 Jan 2019 12:24:42 -0600 Subject: [PATCH] Automatically include build.mk after rules.mk. In some cases, you might want to have rules which are conditional on settings, but to allow later files to override the settings. For instance: FOO_ENABLE = no ifeq $($(strip $(FOO_ENABLE)),yes) SRC += foo.c endif if you override FOO_ENABLE elsewhere, nothing happens. So we split this into a rules.mk containing the setting, and a build.mk containing the build rules, then include every build.mk file *after* all the rules.mk files have been included. I made this change to ergodox_ez as an example. A call to include with no filename in an eval works harmlessly, so we don't have to do existence checks. Signed-off-by: seebs --- Makefile | 23 ++++++++++++++++------- build_keyboard.mk | 13 +++++++------ common.mk | 5 +++++ keyboards/ergodox_ez/build.mk | 3 +++ keyboards/ergodox_ez/rules.mk | 5 ----- 5 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 keyboards/ergodox_ez/build.mk diff --git a/Makefile b/Makefile index 45e47c003710..bd34cdaff195 100644 --- a/Makefile +++ b/Makefile @@ -282,6 +282,13 @@ define PARSE_RULE endif endef +# different from the common.mk version because this needs extra $ because +# it's used in PARSE_KEYBOARD which is already really magic +define USE_RULES + $$(if $$(filter $(1),$$(RULES_MK_LIST)),,$$(eval RULES_MK_LIST += $(1)))\ + $$(eval include $(1)) +endef + # $1 = Keyboard # Parses a rule in the format : # the keyboard is already known when entering this function @@ -295,9 +302,9 @@ define PARSE_KEYBOARD DEFAULT_FOLDER := $$(CURRENT_KB) # We assume that every rules.mk will contain the full default value - $$(eval include $(ROOT_DIR)/keyboards/$$(CURRENT_KB)/rules.mk) + $(call USE_RULES,$(ROOT_DIR)/keyboards/$$(CURRENT_KB)/rules.mk) ifneq ($$(DEFAULT_FOLDER),$$(CURRENT_KB)) - $$(eval include $(ROOT_DIR)/keyboards/$$(DEFAULT_FOLDER)/rules.mk) + $(call USE_RULES,$(ROOT_DIR)/keyboards/$$(DEFAULT_FOLDER)/rules.mk) endif CURRENT_KB := $$(DEFAULT_FOLDER) @@ -322,27 +329,27 @@ define PARSE_KEYBOARD KEYBOARD_LAYOUTS := ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_5)/rules.mk)","") LAYOUTS := - $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_5)/rules.mk) + $(call USE_RULES,$(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_5)/rules.mk) KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS)) endif ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_4)/rules.mk)","") LAYOUTS := - $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_4)/rules.mk) + $(call USE_RULES,$(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_4)/rules.mk) KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS)) endif ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_3)/rules.mk)","") LAYOUTS := - $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_3)/rules.mk) + $(call USE_RULES,$(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_3)/rules.mk) KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS)) endif ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_2)/rules.mk)","") LAYOUTS := - $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_2)/rules.mk) + $(call USE_RULES,$(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_2)/rules.mk) KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS)) endif ifneq ("$$(wildcard $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_1)/rules.mk)","") LAYOUTS := - $$(eval include $(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_1)/rules.mk) + $(call USE_RULES,$(ROOT_DIR)/keyboards/$$(KEYBOARD_FOLDER_PATH_1)/rules.mk) KEYBOARD_LAYOUTS := $$(sort $$(LAYOUTS) $$(KEYBOARD_LAYOUTS)) endif @@ -351,6 +358,8 @@ define PARSE_KEYBOARD KEYMAPS := $$(sort $$(KEYMAPS) $$(LAYOUT_KEYMAPS)) + $$(eval include $$(foreach RULES_MK,$$(RULES_MK_LIST),$$(wildcard $$(subst rules.mk,build.mk,$$(RULES_MK))))) + # if the rule after removing the start of it is empty (we haven't specified a kemap or target) # compile all the keymaps ifeq ($$(RULE),) diff --git a/build_keyboard.mk b/build_keyboard.mk index 1d60f3ad7984..30ec77543b17 100644 --- a/build_keyboard.mk +++ b/build_keyboard.mk @@ -76,19 +76,19 @@ endif # Pull in rules.mk files from all our subfolders ifneq ("$(wildcard $(KEYBOARD_PATH_5)/rules.mk)","") - include $(KEYBOARD_PATH_5)/rules.mk + $(call USE_RULES,$(KEYBOARD_PATH_5)/rules.mk) endif ifneq ("$(wildcard $(KEYBOARD_PATH_4)/rules.mk)","") - include $(KEYBOARD_PATH_4)/rules.mk + $(call USE_RULES,$(KEYBOARD_PATH_4)/rules.mk) endif ifneq ("$(wildcard $(KEYBOARD_PATH_3)/rules.mk)","") - include $(KEYBOARD_PATH_3)/rules.mk + $(call USE_RULES,$(KEYBOARD_PATH_3)/rules.mk) endif ifneq ("$(wildcard $(KEYBOARD_PATH_2)/rules.mk)","") - include $(KEYBOARD_PATH_2)/rules.mk + $(call USE_RULES,$(KEYBOARD_PATH_2)/rules.mk) endif ifneq ("$(wildcard $(KEYBOARD_PATH_1)/rules.mk)","") - include $(KEYBOARD_PATH_1)/rules.mk + $(call USE_RULES,$(KEYBOARD_PATH_1)/rules.mk) endif @@ -287,7 +287,8 @@ ifeq ("$(USER_NAME)","") endif USER_PATH := users/$(USER_NAME) --include $(USER_PATH)/rules.mk +$(call USE_RULES,$(USER_PATH)/rules.mk,-) +$(eval include $(foreach RULES_MK,$(RULES_MK_LIST),$(wildcard $(subst rules.mk,build.mk,$(RULES_MK))))) ifneq ("$(wildcard $(USER_PATH)/config.h)","") CONFIG_H += $(USER_PATH)/config.h endif diff --git a/common.mk b/common.mk index aea29a7a20e2..5bce3121a0d2 100644 --- a/common.mk +++ b/common.mk @@ -22,3 +22,8 @@ COMMON_VPATH += $(QUANTUM_PATH)/audio COMMON_VPATH += $(QUANTUM_PATH)/process_keycode COMMON_VPATH += $(QUANTUM_PATH)/api COMMON_VPATH += $(DRIVER_PATH) + +define USE_RULES + $(if $(filter $(1),$(RULES_MK_LIST)),,$(eval RULES_MK_LIST += $(1)))\ + $(eval $(2)include $(1)) +endef diff --git a/keyboards/ergodox_ez/build.mk b/keyboards/ergodox_ez/build.mk new file mode 100644 index 000000000000..b40fe310f784 --- /dev/null +++ b/keyboards/ergodox_ez/build.mk @@ -0,0 +1,3 @@ +ifeq ($(strip $(RGB_MATRIX_ENABLE)), no) + SRC += i2c_master.c +endif diff --git a/keyboards/ergodox_ez/rules.mk b/keyboards/ergodox_ez/rules.mk index 446ba3e21a52..ed1476ede9ad 100644 --- a/keyboards/ergodox_ez/rules.mk +++ b/keyboards/ergodox_ez/rules.mk @@ -84,9 +84,4 @@ API_SYSEX_ENABLE = no RGBLIGHT_ENABLE = yes RGB_MATRIX_ENABLE = no # enable later -ifeq ($(strip $(RGB_MATRIX_ENABLE)), no) - SRC += i2c_master.c -endif - - LAYOUTS = ergodox