1 # Determine what keyboard we are building and setup the build environment.
3 # We support folders up to 5 levels deep below `keyboards/`. This file is
4 # responsible for determining which folder is being used and doing the
5 # corresponding environment setup.
15 # Set the filename for the final firmware binary
16 KEYBOARD_FILESAFE := $(subst /,_,$(KEYBOARD))
17 TARGET ?= $(KEYBOARD_FILESAFE)_$(KEYMAP)
18 KEYBOARD_OUTPUT := $(BUILD_DIR)/obj_$(KEYBOARD_FILESAFE)
19 STM32_PATH := quantum/stm32
24 # For split boards we need to set a master half.
30 ifeq ($(MASTER),right)
31 OPT_DEFS += -DMASTER_IS_ON_RIGHT
33 ifneq ($(MASTER),left)
34 $(error MASTER does not have a valid value(left/right))
39 OPT_DEFS += -DSKIP_VERSION
42 # Determine which subfolders exist.
43 KEYBOARD_FOLDER_PATH_1 := $(KEYBOARD)
44 KEYBOARD_FOLDER_PATH_2 := $(patsubst %/,%,$(dir $(KEYBOARD_FOLDER_PATH_1)))
45 KEYBOARD_FOLDER_PATH_3 := $(patsubst %/,%,$(dir $(KEYBOARD_FOLDER_PATH_2)))
46 KEYBOARD_FOLDER_PATH_4 := $(patsubst %/,%,$(dir $(KEYBOARD_FOLDER_PATH_3)))
47 KEYBOARD_FOLDER_PATH_5 := $(patsubst %/,%,$(dir $(KEYBOARD_FOLDER_PATH_4)))
48 KEYBOARD_FOLDER_1 := $(notdir $(KEYBOARD_FOLDER_PATH_1))
49 KEYBOARD_FOLDER_2 := $(notdir $(KEYBOARD_FOLDER_PATH_2))
50 KEYBOARD_FOLDER_3 := $(notdir $(KEYBOARD_FOLDER_PATH_3))
51 KEYBOARD_FOLDER_4 := $(notdir $(KEYBOARD_FOLDER_PATH_4))
52 KEYBOARD_FOLDER_5 := $(notdir $(KEYBOARD_FOLDER_PATH_5))
54 KEYBOARD_PATH_1 := keyboards/$(KEYBOARD_FOLDER_PATH_1)
55 KEYBOARD_PATH_2 := keyboards/$(KEYBOARD_FOLDER_PATH_2)
56 KEYBOARD_PATH_3 := keyboards/$(KEYBOARD_FOLDER_PATH_3)
57 KEYBOARD_PATH_4 := keyboards/$(KEYBOARD_FOLDER_PATH_4)
58 KEYBOARD_PATH_5 := keyboards/$(KEYBOARD_FOLDER_PATH_5)
60 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/)","")
61 KEYBOARD_PATHS += $(KEYBOARD_PATH_5)
63 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/)","")
64 KEYBOARD_PATHS += $(KEYBOARD_PATH_4)
66 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/)","")
67 KEYBOARD_PATHS += $(KEYBOARD_PATH_3)
69 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/)","")
70 KEYBOARD_PATHS += $(KEYBOARD_PATH_2)
72 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/)","")
73 KEYBOARD_PATHS += $(KEYBOARD_PATH_1)
77 # Pull in rules.mk files from all our subfolders
78 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/rules.mk)","")
79 include $(KEYBOARD_PATH_5)/rules.mk
81 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/rules.mk)","")
82 include $(KEYBOARD_PATH_4)/rules.mk
84 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/rules.mk)","")
85 include $(KEYBOARD_PATH_3)/rules.mk
87 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/rules.mk)","")
88 include $(KEYBOARD_PATH_2)/rules.mk
90 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/rules.mk)","")
91 include $(KEYBOARD_PATH_1)/rules.mk
95 MAIN_KEYMAP_PATH_1 := $(KEYBOARD_PATH_1)/keymaps/$(KEYMAP)
96 MAIN_KEYMAP_PATH_2 := $(KEYBOARD_PATH_2)/keymaps/$(KEYMAP)
97 MAIN_KEYMAP_PATH_3 := $(KEYBOARD_PATH_3)/keymaps/$(KEYMAP)
98 MAIN_KEYMAP_PATH_4 := $(KEYBOARD_PATH_4)/keymaps/$(KEYMAP)
99 MAIN_KEYMAP_PATH_5 := $(KEYBOARD_PATH_5)/keymaps/$(KEYMAP)
101 # Check for keymap.json first, so we can regenerate keymap.c
102 include build_json.mk
104 ifeq ("$(wildcard $(KEYMAP_PATH))", "")
105 # Look through the possible keymap folders until we find a matching keymap.c
106 ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_5)/keymap.c)","")
107 -include $(MAIN_KEYMAP_PATH_5)/rules.mk
108 KEYMAP_C := $(MAIN_KEYMAP_PATH_5)/keymap.c
109 KEYMAP_PATH := $(MAIN_KEYMAP_PATH_5)
110 else ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_4)/keymap.c)","")
111 -include $(MAIN_KEYMAP_PATH_4)/rules.mk
112 KEYMAP_C := $(MAIN_KEYMAP_PATH_4)/keymap.c
113 KEYMAP_PATH := $(MAIN_KEYMAP_PATH_4)
114 else ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_3)/keymap.c)","")
115 -include $(MAIN_KEYMAP_PATH_3)/rules.mk
116 KEYMAP_C := $(MAIN_KEYMAP_PATH_3)/keymap.c
117 KEYMAP_PATH := $(MAIN_KEYMAP_PATH_3)
118 else ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_2)/keymap.c)","")
119 -include $(MAIN_KEYMAP_PATH_2)/rules.mk
120 KEYMAP_C := $(MAIN_KEYMAP_PATH_2)/keymap.c
121 KEYMAP_PATH := $(MAIN_KEYMAP_PATH_2)
122 else ifneq ("$(wildcard $(MAIN_KEYMAP_PATH_1)/keymap.c)","")
123 -include $(MAIN_KEYMAP_PATH_1)/rules.mk
124 KEYMAP_C := $(MAIN_KEYMAP_PATH_1)/keymap.c
125 KEYMAP_PATH := $(MAIN_KEYMAP_PATH_1)
126 else ifneq ($(LAYOUTS),)
127 # If we haven't found a keymap yet fall back to community layouts
128 include build_layout.mk
130 $(error Could not find keymap)
131 # this state should never be reached
135 ifeq ($(strip $(CTPC)), yes)
136 CONVERT_TO_PROTON_C=yes
139 ifeq ($(strip $(CONVERT_TO_PROTON_C)), yes)
140 TARGET := $(TARGET)_proton_c
141 include $(STM32_PATH)/proton_c.mk
142 OPT_DEFS += -DCONVERT_TO_PROTON_C
145 ifneq ($(FORCE_LAYOUT),)
146 TARGET := $(TARGET)_$(FORCE_LAYOUT)
149 include quantum/mcu_selection.mk
152 OPT_DEFS += -DQMK_STM32
153 KEYBOARD_PATHS += $(STM32_PATH)
157 # Find all the C source files to be compiled in subfolders.
160 KEYBOARD_C_1 := $(KEYBOARD_PATH_1)/$(KEYBOARD_FOLDER_1).c
161 KEYBOARD_C_2 := $(KEYBOARD_PATH_2)/$(KEYBOARD_FOLDER_2).c
162 KEYBOARD_C_3 := $(KEYBOARD_PATH_3)/$(KEYBOARD_FOLDER_3).c
163 KEYBOARD_C_4 := $(KEYBOARD_PATH_4)/$(KEYBOARD_FOLDER_4).c
164 KEYBOARD_C_5 := $(KEYBOARD_PATH_5)/$(KEYBOARD_FOLDER_5).c
166 ifneq ("$(wildcard $(KEYBOARD_C_5))","")
167 KEYBOARD_SRC += $(KEYBOARD_C_5)
169 ifneq ("$(wildcard $(KEYBOARD_C_4))","")
170 KEYBOARD_SRC += $(KEYBOARD_C_4)
172 ifneq ("$(wildcard $(KEYBOARD_C_3))","")
173 KEYBOARD_SRC += $(KEYBOARD_C_3)
175 ifneq ("$(wildcard $(KEYBOARD_C_2))","")
176 KEYBOARD_SRC += $(KEYBOARD_C_2)
178 ifneq ("$(wildcard $(KEYBOARD_C_1))","")
179 KEYBOARD_SRC += $(KEYBOARD_C_1)
182 # Generate KEYBOARD_name_subname for all levels of the keyboard folder
183 KEYBOARD_FILESAFE_1 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_1)))
184 KEYBOARD_FILESAFE_2 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_2)))
185 KEYBOARD_FILESAFE_3 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_3)))
186 KEYBOARD_FILESAFE_4 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_4)))
187 KEYBOARD_FILESAFE_5 := $(subst .,,$(subst /,_,$(KEYBOARD_FOLDER_PATH_5)))
189 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/)","")
190 OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_5)
192 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/)","")
193 OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_4)
195 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/)","")
196 OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_3)
198 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/)","")
199 OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_2)
201 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/)","")
202 OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_1)
205 # Setup the define for QMK_KEYBOARD_H. This is used inside of keymaps so
206 # that the same keymap may be used on multiple keyboards.
208 # We grab the most top-level include file that we can. That file should
209 # use #ifdef statements to include all the neccesary subfolder includes,
212 # https://docs.qmk.fm/#/feature_layouts?id=tips-for-making-layouts-keyboard-agnostic
214 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/$(KEYBOARD_FOLDER_1).h)","")
215 QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_1).h
217 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/$(KEYBOARD_FOLDER_2).h)","")
218 QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_2).h
220 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/$(KEYBOARD_FOLDER_3).h)","")
221 QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_3).h
223 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/$(KEYBOARD_FOLDER_4).h)","")
224 QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_4).h
226 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/$(KEYBOARD_FOLDER_5).h)","")
227 QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_5).h
230 # Determine and set parameters based on the keyboard's processor family.
231 # We can assume a ChibiOS target When MCU_FAMILY is defined since it's
244 ifeq ($(PLATFORM),CHIBIOS)
245 include $(TMK_PATH)/chibios.mk
247 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/bootloader_defs.h)","")
248 OPT_DEFS += -include $(KEYBOARD_PATH_5)/bootloader_defs.h
249 else ifneq ("$(wildcard $(KEYBOARD_PATH_5)/boards/$(BOARD)/bootloader_defs.h)","")
250 OPT_DEFS += -include $(KEYBOARD_PATH_5)/boards/$(BOARD)/bootloader_defs.h
251 else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/bootloader_defs.h)","")
252 OPT_DEFS += -include $(KEYBOARD_PATH_4)/bootloader_defs.h
253 else ifneq ("$(wildcard $(KEYBOARD_PATH_4)/boards/$(BOARD)/bootloader_defs.h)","")
254 OPT_DEFS += -include $(KEYBOARD_PATH_4)/boards/$(BOARD)/bootloader_defs.h
255 else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/bootloader_defs.h)","")
256 OPT_DEFS += -include $(KEYBOARD_PATH_3)/bootloader_defs.h
257 else ifneq ("$(wildcard $(KEYBOARD_PATH_3)/boards/$(BOARD)/bootloader_defs.h)","")
258 OPT_DEFS += -include $(KEYBOARD_PATH_3)/boards/$(BOARD)/bootloader_defs.h
259 else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/bootloader_defs.h)","")
260 OPT_DEFS += -include $(KEYBOARD_PATH_2)/bootloader_defs.h
261 else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/boards/$(BOARD)/bootloader_defs.h)","")
262 OPT_DEFS += -include $(KEYBOARD_PATH_2)/boards/$(BOARD)/bootloader_defs.h
263 else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/bootloader_defs.h)","")
264 OPT_DEFS += -include $(KEYBOARD_PATH_1)/bootloader_defs.h
265 else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/boards/$(BOARD)/bootloader_defs.h)","")
266 OPT_DEFS += -include $(KEYBOARD_PATH_1)/boards/$(BOARD)/bootloader_defs.h
267 else ifneq ("$(wildcard $(TOP_DIR)/drivers/boards/$(BOARD)/bootloader_defs.h)","")
268 OPT_DEFS += -include $(TOP_DIR)/drivers/boards/$(BOARD)/bootloader_defs.h
272 # Find all of the config.h files and add them to our CONFIG_H define.
274 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/config.h)","")
275 CONFIG_H += $(KEYBOARD_PATH_5)/config.h
277 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/config.h)","")
278 CONFIG_H += $(KEYBOARD_PATH_4)/config.h
280 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/config.h)","")
281 CONFIG_H += $(KEYBOARD_PATH_3)/config.h
283 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/config.h)","")
284 CONFIG_H += $(KEYBOARD_PATH_2)/config.h
286 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/config.h)","")
287 CONFIG_H += $(KEYBOARD_PATH_1)/config.h
291 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/post_config.h)","")
292 POST_CONFIG_H += $(KEYBOARD_PATH_1)/post_config.h
294 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/post_config.h)","")
295 POST_CONFIG_H += $(KEYBOARD_PATH_2)/post_config.h
297 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/post_config.h)","")
298 POST_CONFIG_H += $(KEYBOARD_PATH_3)/post_config.h
300 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/post_config.h)","")
301 POST_CONFIG_H += $(KEYBOARD_PATH_4)/post_config.h
303 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/post_config.h)","")
304 POST_CONFIG_H += $(KEYBOARD_PATH_5)/post_config.h
307 # Save the defines and includes here, so we don't include any keymap specific ones
308 PROJECT_DEFS := $(OPT_DEFS)
309 PROJECT_INC := $(VPATH) $(EXTRAINCDIRS) $(KEYBOARD_PATHS)
310 PROJECT_CONFIG := $(CONFIG_H)
312 # Userspace setup and definitions
313 ifeq ("$(USER_NAME)","")
314 USER_NAME := $(KEYMAP)
316 USER_PATH := users/$(USER_NAME)
318 -include $(USER_PATH)/rules.mk
319 ifneq ("$(wildcard $(USER_PATH)/config.h)","")
320 CONFIG_H += $(USER_PATH)/config.h
323 # Object files directory
324 # To put object files in current directory, use a dot (.), do NOT make
325 # this an empty or blank macro!
326 KEYMAP_OUTPUT := $(BUILD_DIR)/obj_$(TARGET)
328 ifneq ("$(wildcard $(KEYMAP_PATH)/config.h)","")
329 CONFIG_H += $(KEYMAP_PATH)/config.h
332 # project specific files
333 SRC += $(KEYBOARD_SRC) \
337 # Optimize size but this may cause error "relocation truncated to fit"
338 #EXTRALDFLAGS = -Wl,--relax
341 VPATH += $(KEYMAP_PATH)
342 VPATH += $(USER_PATH)
343 VPATH += $(KEYBOARD_PATHS)
344 VPATH += $(COMMON_VPATH)
346 include common_features.mk
347 include $(TMK_PATH)/protocol.mk
348 include $(TMK_PATH)/common.mk
349 include bootloader.mk
351 SRC += $(patsubst %.c,%.clib,$(LIB_SRC))
352 SRC += $(patsubst %.c,%.clib,$(QUANTUM_LIB_SRC))
353 SRC += $(TMK_COMMON_SRC)
354 OPT_DEFS += $(TMK_COMMON_DEFS)
355 EXTRALDFLAGS += $(TMK_COMMON_LDFLAGS)
357 ifeq ($(PLATFORM),AVR)
358 ifeq ($(strip $(PROTOCOL)), VUSB)
359 include $(TMK_PATH)/protocol/vusb.mk
361 include $(TMK_PATH)/protocol/lufa.mk
363 include $(TMK_PATH)/avr.mk
366 ifeq ($(PLATFORM),ARM_ATSAM)
367 include $(TMK_PATH)/arm_atsam.mk
368 include $(TMK_PATH)/protocol/arm_atsam.mk
371 ifeq ($(PLATFORM),CHIBIOS)
372 include $(TMK_PATH)/protocol/chibios.mk
375 ifeq ($(strip $(VISUALIZER_ENABLE)), yes)
376 VISUALIZER_DIR = $(QUANTUM_DIR)/visualizer
377 VISUALIZER_PATH = $(QUANTUM_PATH)/visualizer
378 include $(VISUALIZER_PATH)/visualizer.mk
381 CONFIG_H += $(POST_CONFIG_H)
382 ALL_CONFIGS := $(PROJECT_CONFIG) $(CONFIG_H)
384 OUTPUTS := $(KEYMAP_OUTPUT) $(KEYBOARD_OUTPUT)
385 $(KEYMAP_OUTPUT)_SRC := $(SRC)
386 $(KEYMAP_OUTPUT)_DEFS := $(OPT_DEFS) $(GFXDEFS) \
387 -DQMK_KEYBOARD=\"$(KEYBOARD)\" -DQMK_KEYBOARD_H=\"$(QMK_KEYBOARD_H)\" -DQMK_KEYBOARD_CONFIG_H=\"$(KEYBOARD_PATH_1)/config.h\" \
388 -DQMK_KEYMAP=\"$(KEYMAP)\" -DQMK_KEYMAP_H=\"$(KEYMAP).h\" -DQMK_KEYMAP_CONFIG_H=\"$(KEYMAP_PATH)/config.h\" \
389 -DQMK_SUBPROJECT -DQMK_SUBPROJECT_H -DQMK_SUBPROJECT_CONFIG_H
390 $(KEYMAP_OUTPUT)_INC := $(VPATH) $(EXTRAINCDIRS)
391 $(KEYMAP_OUTPUT)_CONFIG := $(CONFIG_H)
392 $(KEYBOARD_OUTPUT)_SRC := $(CHIBISRC) $(GFXSRC)
393 $(KEYBOARD_OUTPUT)_DEFS := $(PROJECT_DEFS) $(GFXDEFS)
394 $(KEYBOARD_OUTPUT)_INC := $(PROJECT_INC) $(GFXINC)
395 $(KEYBOARD_OUTPUT)_CONFIG := $(PROJECT_CONFIG)
398 all: build check-size
399 build: elf cpfirmware
403 include show_options.mk
404 include $(TMK_PATH)/rules.mk