]> git.donarmstrong.com Git - qmk_firmware.git/blob - build_keyboard.mk
Completely remove i2c_transmit_receive function (#7686)
[qmk_firmware.git] / build_keyboard.mk
1 # Determine what keyboard we are building and setup the build environment.
2 #
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.
6
7 ifndef VERBOSE
8 .SILENT:
9 endif
10
11 .DEFAULT_GOAL := all
12
13 include common.mk
14
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
20
21 # Force expansion
22 TARGET := $(TARGET)
23
24 # For split boards we need to set a master half.
25 MASTER ?= left
26 ifdef master
27     MASTER = $(master)
28 endif
29
30 ifeq ($(MASTER),right)
31     OPT_DEFS += -DMASTER_IS_ON_RIGHT
32 else
33     ifneq ($(MASTER),left)
34 $(error MASTER does not have a valid value(left/right))
35     endif
36 endif
37
38 ifdef SKIP_VERSION
39     OPT_DEFS += -DSKIP_VERSION
40 endif
41
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))
53 KEYBOARD_PATHS :=
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)
59
60 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/)","")
61     KEYBOARD_PATHS += $(KEYBOARD_PATH_5)
62 endif
63 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/)","")
64     KEYBOARD_PATHS += $(KEYBOARD_PATH_4)
65 endif
66 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/)","")
67     KEYBOARD_PATHS += $(KEYBOARD_PATH_3)
68 endif
69 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/)","")
70     KEYBOARD_PATHS += $(KEYBOARD_PATH_2)
71 endif
72 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/)","")
73     KEYBOARD_PATHS += $(KEYBOARD_PATH_1)
74 endif
75
76
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
80 endif
81 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/rules.mk)","")
82     include $(KEYBOARD_PATH_4)/rules.mk
83 endif
84 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/rules.mk)","")
85     include $(KEYBOARD_PATH_3)/rules.mk
86 endif
87 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/rules.mk)","")
88     include $(KEYBOARD_PATH_2)/rules.mk
89 endif
90 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/rules.mk)","")
91     include $(KEYBOARD_PATH_1)/rules.mk
92 endif
93
94
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)
100
101 # Check for keymap.json first, so we can regenerate keymap.c
102 include build_json.mk
103
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
129     else
130         $(error Could not find keymap)
131         # this state should never be reached
132     endif
133 endif
134
135 ifeq ($(strip $(CTPC)), yes)
136   CONVERT_TO_PROTON_C=yes
137 endif
138
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
143 endif
144
145 ifneq ($(FORCE_LAYOUT),)
146     TARGET := $(TARGET)_$(FORCE_LAYOUT)
147 endif
148
149 include quantum/mcu_selection.mk
150
151 ifdef MCU_FAMILY
152     OPT_DEFS += -DQMK_STM32
153     KEYBOARD_PATHS += $(STM32_PATH)
154 endif
155
156
157 # Find all the C source files to be compiled in subfolders.
158 KEYBOARD_SRC :=
159
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
165
166 ifneq ("$(wildcard $(KEYBOARD_C_5))","")
167     KEYBOARD_SRC += $(KEYBOARD_C_5)
168 endif
169 ifneq ("$(wildcard $(KEYBOARD_C_4))","")
170     KEYBOARD_SRC += $(KEYBOARD_C_4)
171 endif
172 ifneq ("$(wildcard $(KEYBOARD_C_3))","")
173     KEYBOARD_SRC += $(KEYBOARD_C_3)
174 endif
175 ifneq ("$(wildcard $(KEYBOARD_C_2))","")
176     KEYBOARD_SRC += $(KEYBOARD_C_2)
177 endif
178 ifneq ("$(wildcard $(KEYBOARD_C_1))","")
179     KEYBOARD_SRC += $(KEYBOARD_C_1)
180 endif
181
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)))
188
189 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/)","")
190     OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_5)
191 endif
192 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/)","")
193     OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_4)
194 endif
195 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/)","")
196     OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_3)
197 endif
198 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/)","")
199     OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_2)
200 endif
201 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/)","")
202     OPT_DEFS += -DKEYBOARD_$(KEYBOARD_FILESAFE_1)
203 endif
204
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.
207 #
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,
210 # as described here:
211 #
212 #    https://docs.qmk.fm/#/feature_layouts?id=tips-for-making-layouts-keyboard-agnostic
213 #
214 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/$(KEYBOARD_FOLDER_1).h)","")
215     QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_1).h
216 endif
217 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/$(KEYBOARD_FOLDER_2).h)","")
218     QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_2).h
219 endif
220 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/$(KEYBOARD_FOLDER_3).h)","")
221     QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_3).h
222 endif
223 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/$(KEYBOARD_FOLDER_4).h)","")
224     QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_4).h
225 endif
226 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/$(KEYBOARD_FOLDER_5).h)","")
227     QMK_KEYBOARD_H = $(KEYBOARD_FOLDER_5).h
228 endif
229
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
232 # not used for LUFA
233 ifdef MCU_FAMILY
234     FIRMWARE_FORMAT?=bin
235     PLATFORM=CHIBIOS
236 else ifdef ARM_ATSAM
237     PLATFORM=ARM_ATSAM
238     FIRMWARE_FORMAT=bin
239 else
240     PLATFORM=AVR
241     FIRMWARE_FORMAT?=hex
242 endif
243
244 ifeq ($(PLATFORM),CHIBIOS)
245     include $(TMK_PATH)/chibios.mk
246     OPT_OS = chibios
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
269     endif
270 endif
271
272 # Find all of the config.h files and add them to our CONFIG_H define.
273 CONFIG_H :=
274 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/config.h)","")
275     CONFIG_H += $(KEYBOARD_PATH_5)/config.h
276 endif
277 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/config.h)","")
278     CONFIG_H += $(KEYBOARD_PATH_4)/config.h
279 endif
280 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/config.h)","")
281     CONFIG_H += $(KEYBOARD_PATH_3)/config.h
282 endif
283 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/config.h)","")
284     CONFIG_H += $(KEYBOARD_PATH_2)/config.h
285 endif
286 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/config.h)","")
287     CONFIG_H += $(KEYBOARD_PATH_1)/config.h
288 endif
289
290 POST_CONFIG_H :=
291 ifneq ("$(wildcard $(KEYBOARD_PATH_1)/post_config.h)","")
292     POST_CONFIG_H += $(KEYBOARD_PATH_1)/post_config.h
293 endif
294 ifneq ("$(wildcard $(KEYBOARD_PATH_2)/post_config.h)","")
295     POST_CONFIG_H += $(KEYBOARD_PATH_2)/post_config.h
296 endif
297 ifneq ("$(wildcard $(KEYBOARD_PATH_3)/post_config.h)","")
298     POST_CONFIG_H += $(KEYBOARD_PATH_3)/post_config.h
299 endif
300 ifneq ("$(wildcard $(KEYBOARD_PATH_4)/post_config.h)","")
301     POST_CONFIG_H += $(KEYBOARD_PATH_4)/post_config.h
302 endif
303 ifneq ("$(wildcard $(KEYBOARD_PATH_5)/post_config.h)","")
304     POST_CONFIG_H += $(KEYBOARD_PATH_5)/post_config.h
305 endif
306
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)
311
312 # Userspace setup and definitions
313 ifeq ("$(USER_NAME)","")
314     USER_NAME := $(KEYMAP)
315 endif
316 USER_PATH := users/$(USER_NAME)
317
318 -include $(USER_PATH)/rules.mk
319 ifneq ("$(wildcard $(USER_PATH)/config.h)","")
320     CONFIG_H += $(USER_PATH)/config.h
321 endif
322
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)
327
328 ifneq ("$(wildcard $(KEYMAP_PATH)/config.h)","")
329     CONFIG_H += $(KEYMAP_PATH)/config.h
330 endif
331
332 # project specific files
333 SRC += $(KEYBOARD_SRC) \
334     $(KEYMAP_C) \
335     $(QUANTUM_SRC)
336
337 # Optimize size but this may cause error "relocation truncated to fit"
338 #EXTRALDFLAGS = -Wl,--relax
339
340 # Search Path
341 VPATH += $(KEYMAP_PATH)
342 VPATH += $(USER_PATH)
343 VPATH += $(KEYBOARD_PATHS)
344 VPATH += $(COMMON_VPATH)
345
346 include common_features.mk
347 include $(TMK_PATH)/protocol.mk
348 include $(TMK_PATH)/common.mk
349 include bootloader.mk
350
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)
356
357 ifeq ($(PLATFORM),AVR)
358 ifeq ($(strip $(PROTOCOL)), VUSB)
359     include $(TMK_PATH)/protocol/vusb.mk
360 else
361     include $(TMK_PATH)/protocol/lufa.mk
362 endif
363     include $(TMK_PATH)/avr.mk
364 endif
365
366 ifeq ($(PLATFORM),ARM_ATSAM)
367     include $(TMK_PATH)/arm_atsam.mk
368     include $(TMK_PATH)/protocol/arm_atsam.mk
369 endif
370
371 ifeq ($(PLATFORM),CHIBIOS)
372     include $(TMK_PATH)/protocol/chibios.mk
373 endif
374
375 ifeq ($(strip $(VISUALIZER_ENABLE)), yes)
376     VISUALIZER_DIR = $(QUANTUM_DIR)/visualizer
377     VISUALIZER_PATH = $(QUANTUM_PATH)/visualizer
378     include $(VISUALIZER_PATH)/visualizer.mk
379 endif
380
381 CONFIG_H += $(POST_CONFIG_H)
382 ALL_CONFIGS := $(PROJECT_CONFIG) $(CONFIG_H)
383
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)
396
397 # Default target.
398 all: build check-size
399 build: elf cpfirmware
400 check-size: build
401 objs-size: build
402
403 include show_options.mk
404 include $(TMK_PATH)/rules.mk