Skip to content

Commit

Permalink
Automatically include build.mk after rules.mk.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
seebs committed Jan 26, 2019
1 parent 4383864 commit 032a56f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
23 changes: 16 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 <keymap>:<target>
# the keyboard is already known when entering this function
Expand All @@ -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)

Expand All @@ -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

Expand All @@ -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),)
Expand Down
13 changes: 7 additions & 6 deletions build_keyboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions keyboards/ergodox_ez/build.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ifeq ($(strip $(RGB_MATRIX_ENABLE)), no)
SRC += i2c_master.c
endif
5 changes: 0 additions & 5 deletions keyboards/ergodox_ez/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 032a56f

Please sign in to comment.