rules.mk 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. # Hey Emacs, this is a -*- makefile -*-
  2. #----------------------------------------------------------------------------
  3. # WinAVR Makefile Template written by Eric B. Weddington, Jg Wunsch, et al.
  4. #
  5. # Released to the Public Domain
  6. #
  7. # Additional material for this makefile was written by:
  8. # Peter Fleury
  9. # Tim Henigan
  10. # Colin O'Flynn
  11. # Reiner Patommel
  12. # Markus Pfaff
  13. # Sander Pool
  14. # Frederik Rouleau
  15. # Carlos Lamas
  16. #
  17. # Output format. (can be srec, ihex, binary)
  18. FORMAT = ihex
  19. BUILD_DIR = .build
  20. # Object files directory
  21. # To put object files in current directory, use a dot (.), do NOT make
  22. # this an empty or blank macro!
  23. OBJDIR = $(BUILD_DIR)/obj_$(TARGET)
  24. # Optimization level, can be [0, 1, 2, 3, s].
  25. # 0 = turn off optimization. s = optimize for size.
  26. # (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
  27. OPT = s
  28. # Debugging format.
  29. # Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
  30. # AVR Studio 4.10 requires dwarf-2.
  31. # AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
  32. DEBUG = dwarf-2
  33. COLOR ?= true
  34. ifeq ($(COLOR),true)
  35. NO_COLOR=\033[0m
  36. OK_COLOR=\033[32;01m
  37. ERROR_COLOR=\033[31;01m
  38. WARN_COLOR=\033[33;01m
  39. BLUE=\033[0;34m
  40. BOLD=\033[1m
  41. endif
  42. ifdef quick
  43. QUICK = $(quick)
  44. endif
  45. QUICK ?= false
  46. AUTOGEN ?= false
  47. ifneq ($(shell awk --version 2>/dev/null),)
  48. AWK=awk
  49. else
  50. AWK=cat && test
  51. endif
  52. OK_STRING=$(OK_COLOR)[OK]$(NO_COLOR)\n
  53. ERROR_STRING=$(ERROR_COLOR)[ERRORS]$(NO_COLOR)\n
  54. WARN_STRING=$(WARN_COLOR)[WARNINGS]$(NO_COLOR)\n
  55. ifndef $(SILENT)
  56. SILENT = false
  57. endif
  58. TAB_LOG = printf "\n$$LOG\n\n" | $(AWK) '{ sub(/^/," | "); print }'
  59. TAB_LOG_PLAIN = printf "$$LOG\n"
  60. AWK_STATUS = $(AWK) '{ printf " %-10s\n", $$1; }'
  61. AWK_CMD = $(AWK) '{ printf "%-69s", $$0; }'
  62. PRINT_ERROR = ($(SILENT) ||printf " $(ERROR_STRING)" | $(AWK_STATUS)) && $(TAB_LOG) && false
  63. PRINT_WARNING = ($(SILENT) || printf " $(WARN_STRING)" | $(AWK_STATUS)) && $(TAB_LOG)
  64. PRINT_ERROR_PLAIN = ($(SILENT) ||printf " $(ERROR_STRING)" | $(AWK_STATUS)) && $(TAB_LOG_PLAIN) && false
  65. PRINT_WARNING_PLAIN = ($(SILENT) || printf " $(WARN_STRING)" | $(AWK_STATUS)) && $(TAB_LOG_PLAIN)
  66. PRINT_OK = $(SILENT) || printf " $(OK_STRING)" | $(AWK_STATUS)
  67. BUILD_CMD = LOG=$$($(CMD) 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING); else $(PRINT_OK); fi;
  68. # List any extra directories to look for include files here.
  69. # Each directory must be seperated by a space.
  70. # Use forward slashes for directory separators.
  71. # For a directory that has spaces, enclose it in quotes.
  72. EXTRAINCDIRS = $(subst :, ,$(VPATH))
  73. # Compiler flag to set the C Standard level.
  74. # c89 = "ANSI" C
  75. # gnu89 = c89 plus GCC extensions
  76. # c99 = ISO C99 standard (not yet fully implemented)
  77. # gnu99 = c99 plus GCC extensions
  78. CSTANDARD = -std=gnu99
  79. # Place -D or -U options here for C sources
  80. CDEFS = -DF_CPU=$(F_CPU)UL
  81. CDEFS += $(OPT_DEFS)
  82. # Place -D or -U options here for ASM sources
  83. ADEFS = -DF_CPU=$(F_CPU)
  84. ADEFS += $(OPT_DEFS)
  85. # Place -D or -U options here for C++ sources
  86. CPPDEFS = -DF_CPU=$(F_CPU)UL
  87. #CPPDEFS += -D__STDC_LIMIT_MACROS
  88. #CPPDEFS += -D__STDC_CONSTANT_MACROS
  89. CPPDEFS += $(OPT_DEFS)
  90. #---------------- Compiler Options C ----------------
  91. # -g*: generate debugging information
  92. # -O*: optimization level
  93. # -f...: tuning, see GCC manual and avr-libc documentation
  94. # -Wall...: warning level
  95. # -Wa,...: tell GCC to pass this to the assembler.
  96. # -adhlns...: create assembler listing
  97. CFLAGS = -g$(DEBUG)
  98. CFLAGS += $(CDEFS)
  99. CFLAGS += -O$(OPT)
  100. CFLAGS += -funsigned-char
  101. CFLAGS += -funsigned-bitfields
  102. CFLAGS += -ffunction-sections
  103. CFLAGS += -fdata-sections
  104. CFLAGS += -fno-inline-small-functions
  105. CFLAGS += -fpack-struct
  106. CFLAGS += -fshort-enums
  107. CFLAGS += -fno-strict-aliasing
  108. # add color
  109. ifeq ($(COLOR),true)
  110. ifeq ("$(shell echo "int main(){}" | $(CC) -fdiagnostics-color -x c - -o /dev/null 2>&1)", "")
  111. CFLAGS+= -fdiagnostics-color
  112. endif
  113. endif
  114. CFLAGS += -Wall
  115. CFLAGS += -Wstrict-prototypes
  116. #CFLAGS += -mshort-calls
  117. #CFLAGS += -fno-unit-at-a-time
  118. #CFLAGS += -Wundef
  119. #CFLAGS += -Wunreachable-code
  120. #CFLAGS += -Wsign-compare
  121. CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
  122. CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
  123. CFLAGS += $(CSTANDARD)
  124. ifdef CONFIG_H
  125. CFLAGS += -include $(CONFIG_H)
  126. endif
  127. #---------------- Compiler Options C++ ----------------
  128. # -g*: generate debugging information
  129. # -O*: optimization level
  130. # -f...: tuning, see GCC manual and avr-libc documentation
  131. # -Wall...: warning level
  132. # -Wa,...: tell GCC to pass this to the assembler.
  133. # -adhlns...: create assembler listing
  134. CPPFLAGS = -g$(DEBUG)
  135. CPPFLAGS += $(CPPDEFS)
  136. CPPFLAGS += -O$(OPT)
  137. CPPFLAGS += -funsigned-char
  138. CPPFLAGS += -funsigned-bitfields
  139. CPPFLAGS += -fpack-struct
  140. CPPFLAGS += -fshort-enums
  141. CPPFLAGS += -fno-exceptions
  142. CPPFLAGS += -ffunction-sections
  143. CPPFLAGS += -fdata-sections
  144. # to supress "warning: only initialized variables can be placed into program memory area"
  145. CPPFLAGS += -w
  146. CPPFLAGS += -Wall
  147. CPPFLAGS += -Wundef
  148. #CPPFLAGS += -mshort-calls
  149. #CPPFLAGS += -fno-unit-at-a-time
  150. #CPPFLAGS += -Wstrict-prototypes
  151. #CPPFLAGS += -Wunreachable-code
  152. #CPPFLAGS += -Wsign-compare
  153. CPPFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
  154. CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
  155. #CPPFLAGS += $(CSTANDARD)
  156. ifdef CONFIG_H
  157. CPPFLAGS += -include $(CONFIG_H)
  158. endif
  159. #---------------- Assembler Options ----------------
  160. # -Wa,...: tell GCC to pass this to the assembler.
  161. # -adhlns: create listing
  162. # -gstabs: have the assembler create line number information; note that
  163. # for use in COFF files, additional information about filenames
  164. # and function names needs to be present in the assembler source
  165. # files -- see avr-libc docs [FIXME: not yet described there]
  166. # -listing-cont-lines: Sets the maximum number of continuation lines of hex
  167. # dump that will be displayed for a given single line of source input.
  168. ASFLAGS = $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
  169. ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
  170. ifdef CONFIG_H
  171. ASFLAGS += -include $(CONFIG_H)
  172. endif
  173. #---------------- Library Options ----------------
  174. # Minimalistic printf version
  175. PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
  176. # Floating point printf version (requires MATH_LIB = -lm below)
  177. PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
  178. # If this is left blank, then it will use the Standard printf version.
  179. PRINTF_LIB =
  180. #PRINTF_LIB = $(PRINTF_LIB_MIN)
  181. #PRINTF_LIB = $(PRINTF_LIB_FLOAT)
  182. # Minimalistic scanf version
  183. SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
  184. # Floating point + %[ scanf version (requires MATH_LIB = -lm below)
  185. SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
  186. # If this is left blank, then it will use the Standard scanf version.
  187. SCANF_LIB =
  188. #SCANF_LIB = $(SCANF_LIB_MIN)
  189. #SCANF_LIB = $(SCANF_LIB_FLOAT)
  190. MATH_LIB = -lm
  191. # List any extra directories to look for libraries here.
  192. # Each directory must be seperated by a space.
  193. # Use forward slashes for directory separators.
  194. # For a directory that has spaces, enclose it in quotes.
  195. EXTRALIBDIRS =
  196. #---------------- External Memory Options ----------------
  197. # 64 KB of external RAM, starting after internal RAM (ATmega128!),
  198. # used for variables (.data/.bss) and heap (malloc()).
  199. #EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
  200. # 64 KB of external RAM, starting after internal RAM (ATmega128!),
  201. # only used for heap (malloc()).
  202. #EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
  203. EXTMEMOPTS =
  204. #---------------- Linker Options ----------------
  205. # -Wl,...: tell GCC to pass this to linker.
  206. # -Map: create map file
  207. # --cref: add cross reference to map file
  208. #
  209. # Comennt out "--relax" option to avoid a error such:
  210. # (.vectors+0x30): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_12'
  211. #
  212. LDFLAGS = -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
  213. #LDFLAGS += -Wl,--relax
  214. LDFLAGS += -Wl,--gc-sections
  215. LDFLAGS += $(EXTMEMOPTS)
  216. LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
  217. LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
  218. #LDFLAGS += -T linker_script.x
  219. # You can give EXTRALDFLAGS at 'make' command line.
  220. LDFLAGS += $(EXTRALDFLAGS)
  221. #---------------- Debugging Options ----------------
  222. # For simulavr only - target MCU frequency.
  223. DEBUG_MFREQ = $(F_CPU)
  224. # Set the DEBUG_UI to either gdb or insight.
  225. # DEBUG_UI = gdb
  226. DEBUG_UI = insight
  227. # Set the debugging back-end to either avarice, simulavr.
  228. DEBUG_BACKEND = avarice
  229. #DEBUG_BACKEND = simulavr
  230. # GDB Init Filename.
  231. GDBINIT_FILE = __avr_gdbinit
  232. # When using avarice settings for the JTAG
  233. JTAG_DEV = /dev/com1
  234. # Debugging port used to communicate between GDB / avarice / simulavr.
  235. DEBUG_PORT = 4242
  236. # Debugging host used to communicate between GDB / avarice / simulavr, normally
  237. # just set to localhost unless doing some sort of crazy debugging when
  238. # avarice is running on a different computer.
  239. DEBUG_HOST = localhost
  240. #============================================================================
  241. # Define programs and commands.
  242. SHELL = sh
  243. CC = avr-gcc
  244. OBJCOPY = avr-objcopy
  245. OBJDUMP = avr-objdump
  246. SIZE = avr-size
  247. AR = avr-ar rcs
  248. NM = avr-nm
  249. REMOVE = rm -f
  250. REMOVEDIR = rmdir
  251. COPY = cp
  252. WINSHELL = cmd
  253. SECHO = $(SILENT) || echo
  254. # Autodecct teensy loader
  255. ifneq (, $(shell which teensy-loader-cli 2>/dev/null))
  256. TEENSY_LOADER_CLI = teensy-loader-cli
  257. else
  258. TEENSY_LOADER_CLI = teensy_loader_cli
  259. endif
  260. # Define Messages
  261. # English
  262. MSG_ERRORS_NONE = Errors: none
  263. MSG_BEGIN = -------- begin --------
  264. MSG_END = -------- end --------
  265. MSG_SIZE_BEFORE = Size before:
  266. MSG_SIZE_AFTER = Size after:
  267. MSG_COFF = Converting to AVR COFF:
  268. MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
  269. MSG_FLASH = Creating load file for Flash:
  270. MSG_EEPROM = Creating load file for EEPROM:
  271. MSG_EXTENDED_LISTING = Creating Extended Listing:
  272. MSG_SYMBOL_TABLE = Creating Symbol Table:
  273. MSG_LINKING = Linking:
  274. MSG_COMPILING = Compiling:
  275. MSG_COMPILING_CPP = Compiling:
  276. MSG_ASSEMBLING = Assembling:
  277. MSG_CLEANING = Cleaning project:
  278. MSG_CREATING_LIBRARY = Creating library:
  279. # Define all object files.
  280. OBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(patsubst %.cpp,$(OBJDIR)/%.o,$(patsubst %.S,$(OBJDIR)/%.o,$(SRC))))
  281. # Define all listing files.
  282. LST = $(patsubst %.c,$(OBJDIR)/%.lst,$(patsubst %.cpp,$(OBJDIR)/%.lst,$(patsubst %.S,$(OBJDIR)/%.lst,$(SRC))))
  283. # Compiler flags to generate dependency files.
  284. #GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
  285. GENDEPFLAGS = -MMD -MP -MF $(BUILD_DIR)/.dep/$(subst /,_,$@).d
  286. # Combine all necessary flags and optional flags.
  287. # Add target processor to flags.
  288. # You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
  289. ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
  290. ALL_CPPFLAGS = -mmcu=$(MCU) -x c++ $(CPPFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
  291. ALL_ASFLAGS = -mmcu=$(MCU) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
  292. # Default target.
  293. all:
  294. @$(MAKE) begin
  295. @$(MAKE) gccversion
  296. @$(MAKE) sizebefore
  297. @$(MAKE) clean_list # force clean each time
  298. @$(MAKE) build
  299. @$(MAKE) sizeafter
  300. @$(MAKE) end
  301. # Quick make that doesn't clean
  302. quick:
  303. @$(MAKE) begin
  304. @$(MAKE) gccversion
  305. @$(MAKE) sizebefore
  306. @$(MAKE) build
  307. @$(MAKE) sizeafter
  308. @$(MAKE) end
  309. # Change the build target to build a HEX file or a library.
  310. build: elf hex
  311. #build: elf hex eep lss sym
  312. #build: lib
  313. elf: $(BUILD_DIR)/$(TARGET).elf
  314. hex: $(BUILD_DIR)/$(TARGET).hex
  315. eep: $(BUILD_DIR)/$(TARGET).eep
  316. lss: $(BUILD_DIR)/$(TARGET).lss
  317. sym: $(BUILD_DIR)/$(TARGET).sym
  318. LIBNAME=lib$(TARGET).a
  319. lib: $(LIBNAME)
  320. # Eye candy.
  321. # AVR Studio 3.x does not check make's exit code but relies on
  322. # the following magic strings to be generated by the compile job.
  323. begin:
  324. @$(SECHO) $(MSG_BEGIN)
  325. end:
  326. @$(SECHO) $(MSG_END)
  327. # Display size of file.
  328. HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
  329. #ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
  330. ELFSIZE = $(SIZE) $(BUILD_DIR)/$(TARGET).elf
  331. sizebefore:
  332. @if test -f $(TARGET).hex; then $(SECHO) $(MSG_SIZE_BEFORE); $(SILENT) || $(HEXSIZE); \
  333. 2>/dev/null; $(SECHO); fi
  334. sizeafter:
  335. @if test -f $(TARGET).hex; then $(SECHO); $(SECHO) $(MSG_SIZE_AFTER); $(SILENT) || $(HEXSIZE); \
  336. 2>/dev/null; $(SECHO); fi
  337. # test file sizes eventually
  338. # @if [[ $($(SIZE) --target=$(FORMAT) $(TARGET).hex | $(AWK) 'NR==2 {print "0x"$5}') -gt 0x200 ]]; then $(SECHO) "File is too big!"; fi
  339. # Display compiler version information.
  340. gccversion :
  341. @$(SILENT) || $(CC) --version
  342. # Program the device.
  343. program: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  344. $(PROGRAM_CMD)
  345. teensy: $(BUILD_DIR)/$(TARGET).hex
  346. $(TEENSY_LOADER_CLI) -mmcu=$(MCU) -w -v $(BUILD_DIR)/$(TARGET).hex
  347. flip: $(BUILD_DIR)/$(TARGET).hex
  348. batchisp -hardware usb -device $(MCU) -operation erase f
  349. batchisp -hardware usb -device $(MCU) -operation loadbuffer $(BUILD_DIR)/$(TARGET).hex program
  350. batchisp -hardware usb -device $(MCU) -operation start reset 0
  351. dfu: $(BUILD_DIR)/$(TARGET).hex sizeafter
  352. ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
  353. dfu-programmer $(MCU) erase --force
  354. else
  355. dfu-programmer $(MCU) erase
  356. endif
  357. dfu-programmer $(MCU) flash $(BUILD_DIR)/$(TARGET).hex
  358. dfu-programmer $(MCU) reset
  359. dfu-start:
  360. dfu-programmer $(MCU) reset
  361. dfu-programmer $(MCU) start
  362. flip-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  363. $(COPY) $(BUILD_DIR)/$(TARGET).eep $(BUILD_DIR)/$(TARGET)eep.hex
  364. batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
  365. batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(BUILD_DIR)/$(TARGET)eep.hex program
  366. batchisp -hardware usb -device $(MCU) -operation start reset 0
  367. $(REMOVE) $(BUILD_DIR)/$(TARGET)eep.hex
  368. dfu-ee: $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).eep
  369. ifneq (, $(findstring 0.7, $(shell dfu-programmer --version 2>&1)))
  370. dfu-programmer $(MCU) flash --eeprom $(BUILD_DIR)/$(TARGET).eep
  371. else
  372. dfu-programmer $(MCU) flash-eeprom $(BUILD_DIR)/$(TARGET).eep
  373. endif
  374. dfu-programmer $(MCU) reset
  375. # Generate avr-gdb config/init file which does the following:
  376. # define the reset signal, load the target file, connect to target, and set
  377. # a breakpoint at main().
  378. gdb-config:
  379. @$(REMOVE) $(GDBINIT_FILE)
  380. @echo define reset >> $(GDBINIT_FILE)
  381. @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
  382. @echo end >> $(GDBINIT_FILE)
  383. @echo file $(BUILD_DIR)/$(TARGET).elf >> $(GDBINIT_FILE)
  384. @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
  385. ifeq ($(DEBUG_BACKEND),simulavr)
  386. @echo load >> $(GDBINIT_FILE)
  387. endif
  388. @echo break main >> $(GDBINIT_FILE)
  389. debug: gdb-config $(BUILD_DIR)/$(TARGET).elf
  390. ifeq ($(DEBUG_BACKEND), avarice)
  391. @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
  392. @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
  393. $(BUILD_DIR)/$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
  394. @$(WINSHELL) /c pause
  395. else
  396. @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
  397. $(DEBUG_MFREQ) --port $(DEBUG_PORT)
  398. endif
  399. @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
  400. # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
  401. COFFCONVERT = $(OBJCOPY) --debugging
  402. COFFCONVERT += --change-section-address .data-0x800000
  403. COFFCONVERT += --change-section-address .bss-0x800000
  404. COFFCONVERT += --change-section-address .noinit-0x800000
  405. COFFCONVERT += --change-section-address .eeprom-0x810000
  406. coff: $(BUILD_DIR)/$(TARGET).elf
  407. @$(SECHO) $(MSG_COFF) $(BUILD_DIR)/$(TARGET).cof
  408. $(COFFCONVERT) -O coff-avr $< $(BUILD_DIR)/$(TARGET).cof
  409. extcoff: $(BUILD_DIR)/$(TARGET).elf
  410. @$(SECHO) $(MSG_EXTENDED_COFF) $(BUILD_DIR)/$(TARGET).cof
  411. $(COFFCONVERT) -O coff-ext-avr $< $(BUILD_DIR)/$(TARGET).cof
  412. # Create final output files (.hex, .eep) from ELF output file.
  413. %.hex: %.elf
  414. @$(SILENT) || printf "$(MSG_FLASH) $@" | $(AWK_CMD)
  415. $(eval CMD=$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@)
  416. @$(BUILD_CMD)
  417. @if $(AUTOGEN); then \
  418. $(SILENT) || printf "Copying $(TARGET).hex to keymaps/$(KEYMAP)/$(KEYBOARD)_$(KEYMAP).hex\n"; \
  419. $(COPY) $@ $(KEYMAP_PATH)/$(KEYBOARD)_$(KEYMAP).hex; \
  420. else \
  421. $(COPY) $@ $(TARGET).hex; \
  422. fi
  423. %.eep: %.elf
  424. @$(SILENT) || printf "$(MSG_EEPROM) $@" | $(AWK_CMD)
  425. $(eval CMD=$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0)
  426. @$(BUILD_CMD)
  427. # Create extended listing file from ELF output file.
  428. %.lss: %.elf
  429. @$(SILENT) || printf "$(MSG_EXTENDED_LISTING) $@" | $(AWK_CMD)
  430. $(eval CMD=$(OBJDUMP) -h -S -z $< > $@)
  431. @$(BUILD_CMD)
  432. # Create a symbol table from ELF output file.
  433. %.sym: %.elf
  434. @$(SILENT) || printf "$(MSG_SYMBOL_TABLE) $@" | $(AWK_CMD)
  435. $(eval CMD=$(NM) -n $< > $@ )
  436. @$(BUILD_CMD)
  437. # Create library from object files.
  438. .SECONDARY : $(BUILD_DIR)/$(TARGET).a
  439. .PRECIOUS : $(OBJ)
  440. %.a: $(OBJ)
  441. @$(SILENT) || printf "$(MSG_CREATING_LIBRARY) $@" | $(AWK_CMD)
  442. $(eval CMD=$(AR) $@ $(OBJ) )
  443. @$(BUILD_CMD)
  444. # Link: create ELF output file from object files.
  445. .SECONDARY : $(BUILD_DIR)/$(TARGET).elf
  446. .PRECIOUS : $(OBJ)
  447. %.elf: $(OBJ)
  448. @$(SILENT) || printf "$(MSG_LINKING) $@" | $(AWK_CMD)
  449. $(eval CMD=$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS))
  450. @$(BUILD_CMD)
  451. # Compile: create object files from C source files.
  452. $(OBJDIR)/%.o : %.c
  453. @mkdir -p $(@D)
  454. @$(SILENT) || printf "$(MSG_COMPILING) $<" | $(AWK_CMD)
  455. $(eval CMD=$(CC) -c $(ALL_CFLAGS) $< -o $@)
  456. @$(BUILD_CMD)
  457. # Compile: create object files from C++ source files.
  458. $(OBJDIR)/%.o : %.cpp
  459. @mkdir -p $(@D)
  460. @$(SILENT) || printf "$(MSG_COMPILING_CPP) $<" | $(AWK_CMD)
  461. $(eval CMD=$(CC) -c $(ALL_CPPFLAGS) $< -o $@)
  462. @$(BUILD_CMD)
  463. # Compile: create assembler files from C source files.
  464. %.s : %.c
  465. @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
  466. $(eval CMD=$(CC) -S $(ALL_CFLAGS) $< -o $@)
  467. @$(BUILD_CMD)
  468. # Compile: create assembler files from C++ source files.
  469. %.s : %.cpp
  470. @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
  471. $(eval CMD=$(CC) -S $(ALL_CPPFLAGS) $< -o $@)
  472. @$(BUILD_CMD)
  473. # Assemble: create object files from assembler source files.
  474. $(OBJDIR)/%.o : %.S
  475. @mkdir -p $(@D)
  476. @$(SILENT) || printf "$(MSG_ASSEMBLING) $<" | $(AWK_CMD)
  477. $(eval CMD=$(CC) -c $(ALL_ASFLAGS) $< -o $@)
  478. @$(BUILD_CMD)
  479. # Create preprocessed source for use in sending a bug report.
  480. %.i : %.c
  481. $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
  482. # Target: clean project.
  483. clean: begin clean_list end
  484. clean_list :
  485. @$(REMOVE) -r $(BUILD_DIR)
  486. @$(REMOVE) -r $(TOP_DIR)/$(BUILD_DIR)
  487. @$(REMOVE) -r $(KEYBOARD_PATH)/$(BUILD_DIR)
  488. @if $$SUBPROJECT; then $(REMOVE) -r $(SUBPROJECT_PATH)/$(BUILD_DIR); fi
  489. @$(REMOVE) -r $(KEYMAP_PATH)/$(BUILD_DIR)
  490. show_path:
  491. @echo VPATH=$(VPATH)
  492. @echo SRC=$(SRC)
  493. SUBDIRS := $(sort $(dir $(wildcard $(TOP_DIR)/keyboards/*/.)))
  494. all-keyboards-defaults-%:
  495. @for x in $(SUBDIRS) ; do \
  496. printf "Compiling with default: $$x" | $(AWK_CMD); \
  497. LOG=$$($(MAKE) -C $$x $(subst all-keyboards-defaults-,,$@) VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
  498. done
  499. all-keyboards-defaults: all-keyboards-defaults-all
  500. KEYBOARDS := $(SUBDIRS:$(TOP_DIR)/keyboards/%/=/keyboards/%)
  501. all-keyboards-all: $(addsuffix -all,$(KEYBOARDS))
  502. all-keyboards-quick: $(addsuffix -quick,$(KEYBOARDS))
  503. all-keyboards-clean: $(addsuffix -clean,$(KEYBOARDS))
  504. all-keyboards: all-keyboards-all
  505. define make_keyboard
  506. $(eval KEYBOARD=$(patsubst /keyboards/%,%,$1))
  507. $(eval KEYMAPS=$(notdir $(patsubst %/.,%,$(wildcard $(TOP_DIR)$1/keymaps/*/.))))
  508. @for x in $(KEYMAPS) ; do \
  509. printf "Compiling $(BOLD)$(KEYBOARD)$(NO_COLOR) with $(BOLD)$$x$(NO_COLOR)" | $(AWK) '{ printf "%-88s", $$0; }'; \
  510. LOG=$$($(MAKE) -C $(TOP_DIR)$1 $2 keymap=$$x VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
  511. done
  512. endef
  513. define make_keyboard_helper
  514. # Just remove the -quick, -all and so on from the first argument and pass it forward
  515. $(call make_keyboard,$(subst -$2,,$1),$2)
  516. endef
  517. /keyboards/%-quick:
  518. $(call make_keyboard_helper,$@,quick)
  519. /keyboards/%-all:
  520. $(call make_keyboard_helper,$@,all)
  521. /keyboards/%-clean:
  522. $(call make_keyboard_helper,$@,clean)
  523. /keyboards/%:
  524. $(call make_keyboard_helper,$@,all)
  525. all-keymaps-%:
  526. $(eval MAKECONFIG=$(call get_target,all-keymaps,$@))
  527. $(eval KEYMAPS=$(notdir $(patsubst %/.,%,$(wildcard $(TOP_DIR)/keyboards/$(KEYBOARD)/keymaps/*/.))))
  528. @for x in $(KEYMAPS) ; do \
  529. printf "Compiling $(BOLD)$(KEYBOARD)$(NO_COLOR) with $(BOLD)$$x$(NO_COLOR)" | $(AWK) '{ printf "%-88s", $$0; }'; \
  530. LOG=$$($(MAKE) $(subst all-keymaps-,,$@) keyboard=$(KEYBOARD) keymap=$$x VERBOSE=$(VERBOSE) COLOR=$(COLOR) SILENT=true 2>&1) ; if [ $$? -gt 0 ]; then $(PRINT_ERROR_PLAIN); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING_PLAIN); else $(PRINT_OK); fi; \
  531. done
  532. all-keymaps: all-keymaps-all
  533. # Create build directory
  534. $(shell mkdir $(BUILD_DIR) 2>/dev/null)
  535. # Create object files directory
  536. $(shell mkdir $(OBJDIR) 2>/dev/null)
  537. # Include the dependency files.
  538. -include $(shell mkdir $(BUILD_DIR)/.dep 2>/dev/null) $(wildcard $(BUILD_DIR)/.dep/*)
  539. # Listing of phony targets.
  540. .PHONY : all quick begin finish end sizebefore sizeafter gccversion \
  541. build elf hex eep lss sym coff extcoff \
  542. clean clean_list debug gdb-config show_path \
  543. program teensy dfu flip dfu-ee flip-ee dfu-start \
  544. all-keyboards-defaults all-keyboards all-keymaps \
  545. all-keyboards-defaults-% all-keyboards-% all-keymaps-%