Makefile.169.uno 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #
  2. # Arduino-1.6.9 Makefile
  3. #
  4. # written by olikraus@gmail.com
  5. #
  6. # Features:
  7. # - boards.txt is used to derive parameters
  8. # - All intermediate files are put into a separate directory (TMPDIRNAME)
  9. # - Simple use: Copy Makefile into the same directory of the .pde file
  10. #
  11. # Limitations:
  12. # - requires UNIX environment
  13. # - TMPDIRNAME must be subdirectory of the current directory.
  14. #
  15. # Targets
  16. # all build everything
  17. # upload build and upload to arduino
  18. # clean remove all temporary files (includes final hex file)
  19. #
  20. # History
  21. # 001 28 Apr 2010 first release
  22. # 002 05 Oct 2010 added 'uno'
  23. # 003 06 Dec 2011 arduino 1.0
  24. # 004 11 Feb 2012 u8glib
  25. # 005 09 Aug 2015 Arduino 1.6.5
  26. # 006 11 May 2016 Arduino 1.6.9
  27. #
  28. #=== user configuration ===
  29. # All ...PATH variables must have a '/' at the end
  30. # Board (and prozessor) information: see $(ARDUINO_PATH)hardware/arduino/avr/boards.txt
  31. # Some examples:
  32. # BOARD DESCRIPTION
  33. # uno Arduino Uno
  34. # atmega328 Arduino Duemilanove or Nano w/ ATmega328
  35. # diecimila Arduino Diecimila, Duemilanove, or Nano w/ ATmega168
  36. # mega Arduino Mega
  37. # mega2560 Arduino Mega2560
  38. # mini Arduino Mini
  39. # lilypad328 LilyPad Arduino w/ ATmega328
  40. BOARD:=uno
  41. # additional definitions
  42. #DEFS:=-DARDUINO=169
  43. U8G_PATH:=$(shell cd ../../../.. && pwd)/csrc/
  44. U8G_CPP_PATH:=$(shell cd ../../../.. && pwd)/cppsrc/
  45. # The location where the avr tools (e.g. avr-gcc) are located. Requires a '/' at the end.
  46. # Can be empty if all tools are accessable through the search path
  47. AVR_TOOLS_PATH:=/usr/bin/
  48. # Install path of the arduino software. Requires a '/' at the end.
  49. ARDUINO_PATH:=/home/kraus/prg/arduino-1.6.9/
  50. # Install path for avrdude. Requires a '/' at the end. Can be empty if avrdude is in the search path.
  51. AVRDUDE_PATH:=$(ARDUINO_PATH)hardware/tools/avr/bin/
  52. # The unix device where we can reach the arduino board
  53. # Uno: /dev/ttyACM0
  54. # Duemilanove: /dev/ttyUSB0
  55. AVRDUDE_PORT:=/dev/ttyACM0
  56. # List of all libaries which should be included.
  57. EXTRA_DIRS=$(ARDUINO_PATH)libraries/LiquidCrystal/
  58. EXTRA_DIRS+=$(ARDUINO_PATH)libraries/SD/
  59. EXTRA_DIRS+=$(ARDUINO_PATH)libraries/SD/utility/
  60. EXTRA_DIRS+=$(ARDUINO_PATH)hardware/arduino/avr/libraries/SPI/src/
  61. EXTRA_DIRS+=$(ARDUINO_PATH)hardware/arduino/avr/libraries/Wire/src/
  62. #EXTRA_DIRS+=$(ARDUINO_PATH)libraries/.../
  63. #=== fetch parameter from boards.txt processor parameter ===
  64. # the basic idea is to get most of the information from boards.txt
  65. BOARDS_TXT:=$(ARDUINO_PATH)hardware/arduino/avr/boards.txt
  66. # get the MCU value from the $(BOARD).build.mcu variable. For the atmega328 board this is atmega328p
  67. MCU:=$(shell sed -n -e "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_TXT))
  68. # get the F_CPU value from the $(BOARD).build.f_cpu variable. For the atmega328 board this is 16000000
  69. F_CPU:=$(shell sed -n -e "s/$(BOARD).build.f_cpu=\(.*\)/\1/p" $(BOARDS_TXT))
  70. # get variant subfolder
  71. VARIANT:=$(shell sed -n -e "s/$(BOARD).build.variant=\(.*\)/\1/p" $(BOARDS_TXT))
  72. # avrdude
  73. # get the AVRDUDE_UPLOAD_RATE value from the $(BOARD).upload.speed variable. For the atmega328 board this is 57600
  74. AVRDUDE_UPLOAD_RATE:=$(shell sed -n -e "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_TXT))
  75. # get the AVRDUDE_PROGRAMMER value from the $(BOARD).upload.protocol variable. For the atmega328 board this is stk500
  76. AVRDUDE_PROGRAMMER:=$(shell sed -n -e "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_TXT))
  77. # use stk500v1, because stk500 will default to stk500v2
  78. #AVRDUDE_PROGRAMMER:=stk500v1
  79. #=== identify user files ===
  80. PDESRC:=$(shell ls *.ino)
  81. TARGETNAME=$(basename $(PDESRC))
  82. CDIRS:=$(EXTRA_DIRS) $(addsuffix utility/,$(EXTRA_DIRS))
  83. CDIRS:=*.c utility/*.c $(U8G_PATH)*.c $(addsuffix *.c,$(CDIRS)) $(ARDUINO_PATH)hardware/arduino//avr/cores/arduino/*.c
  84. CSRC:=$(shell ls $(CDIRS) 2>/dev/null)
  85. CCSRC:=$(shell ls *.cc 2>/dev/null)
  86. CPPDIRS:=$(EXTRA_DIRS) $(addsuffix utility/,$(EXTRA_DIRS))
  87. CPPDIRS:=*.cpp utility/*.cpp $(addsuffix *.cpp,$(CPPDIRS)) $(U8G_CPP_PATH)/*.cpp $(ARDUINO_PATH)hardware/arduino/cores/arduino/*.cpp
  88. CPPSRC:=$(shell ls $(CPPDIRS) 2>/dev/null)
  89. #=== build internal variables ===
  90. # the name of the subdirectory where everything is stored
  91. TMPDIRNAME:=tmp
  92. TMPDIRPATH:=$(TMPDIRNAME)/
  93. AVRTOOLSPATH:=$(AVR_TOOLS_PATH)
  94. OBJCOPY:=$(AVRTOOLSPATH)avr-objcopy
  95. OBJDUMP:=$(AVRTOOLSPATH)avr-objdump
  96. SIZE:=$(AVRTOOLSPATH)avr-size
  97. CPPSRC:=$(addprefix $(TMPDIRPATH),$(PDESRC:.ino=.cpp)) $(CPPSRC)
  98. COBJ:=$(CSRC:.c=.o)
  99. CCOBJ:=$(CCSRC:.cc=.o)
  100. CPPOBJ:=$(CPPSRC:.cpp=.o)
  101. OBJFILES:=$(COBJ) $(CCOBJ) $(CPPOBJ)
  102. DIRS:= $(dir $(OBJFILES))
  103. DEPFILES:=$(OBJFILES:.o=.d)
  104. # assembler files from avr-gcc -S
  105. ASSFILES:=$(OBJFILES:.o=.s)
  106. # disassembled object files with avr-objdump -S
  107. DISFILES:=$(OBJFILES:.o=.dis)
  108. LIBNAME:=$(TMPDIRPATH)$(TARGETNAME).a
  109. ELFNAME:=$(TMPDIRPATH)$(TARGETNAME).elf
  110. HEXNAME:=$(TMPDIRPATH)$(TARGETNAME).hex
  111. AVRDUDE_FLAGS = -V -F
  112. AVRDUDE_FLAGS += -C $(ARDUINO_PATH)hardware/tools/avr/etc/avrdude.conf
  113. AVRDUDE_FLAGS += -p $(MCU)
  114. AVRDUDE_FLAGS += -P $(AVRDUDE_PORT)
  115. AVRDUDE_FLAGS += -c $(AVRDUDE_PROGRAMMER)
  116. AVRDUDE_FLAGS += -b $(AVRDUDE_UPLOAD_RATE)
  117. AVRDUDE_FLAGS += -U flash:w:$(HEXNAME)
  118. AVRDUDE = $(AVRDUDE_PATH)avrdude
  119. #=== predefined variable override ===
  120. # use "make -p -f/dev/null" to see the default rules and definitions
  121. # Build C and C++ flags. Include path information must be placed here
  122. COMMON_FLAGS = -DF_CPU=$(F_CPU) -mmcu=$(MCU) $(DEFS) -DARDUINO=169
  123. # COMMON_FLAGS += -gdwarf-2
  124. COMMON_FLAGS += -Os
  125. COMMON_FLAGS += -Wall -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
  126. COMMON_FLAGS += -I.
  127. COMMON_FLAGS += -I$(ARDUINO_PATH)hardware/arduino/avr/cores/arduino
  128. COMMON_FLAGS += -I$(ARDUINO_PATH)hardware/arduino/avr/variants/$(VARIANT)
  129. COMMON_FLAGS += -I. -I$(U8G_PATH) -I$(U8G_CPP_PATH)
  130. COMMON_FLAGS += $(addprefix -I,$(EXTRA_DIRS))
  131. COMMON_FLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections
  132. COMMON_FLAGS += -Wl,--Map=output.map
  133. COMMON_FLAGS += -Wl,--relax
  134. COMMON_FLAGS += -mcall-prologues
  135. CFLAGS = $(COMMON_FLAGS) -std=gnu99 -Wstrict-prototypes
  136. CXXFLAGS = $(COMMON_FLAGS)
  137. # Replace standard build tools by avr tools
  138. CC = $(AVRTOOLSPATH)avr-gcc
  139. CXX = $(AVRTOOLSPATH)avr-g++
  140. AR = @$(AVRTOOLSPATH)avr-ar
  141. # "rm" must be able to delete a directory tree
  142. RM = rm -rf
  143. #=== rules ===
  144. # add rules for the C/C++ files where the .o file is placed in the TMPDIRPATH
  145. # reuse existing variables as far as possible
  146. $(TMPDIRPATH)%.o: %.c
  147. @echo compile $<
  148. @$(COMPILE.c) $(OUTPUT_OPTION) $<
  149. $(TMPDIRPATH)%.o: %.cc
  150. @echo compile $<
  151. @$(COMPILE.cc) $(OUTPUT_OPTION) $<
  152. $(TMPDIRPATH)%.o: %.cpp
  153. @echo compile $<
  154. @$(COMPILE.cpp) $(OUTPUT_OPTION) $<
  155. $(TMPDIRPATH)%.s: %.c
  156. @$(COMPILE.c) $(OUTPUT_OPTION) -S $<
  157. $(TMPDIRPATH)%.s: %.cc
  158. @$(COMPILE.cc) $(OUTPUT_OPTION) -S $<
  159. $(TMPDIRPATH)%.s: %.cpp
  160. @$(COMPILE.cpp) $(OUTPUT_OPTION) -S $<
  161. $(TMPDIRPATH)%.dis: $(TMPDIRPATH)%.o
  162. @$(OBJDUMP) -S $< > $@
  163. .SUFFIXES: .elf .hex .ino
  164. .elf.hex:
  165. @$(OBJCOPY) -O ihex -R .eeprom $< $@
  166. $(TMPDIRPATH)%.cpp: %.ino
  167. @cat $(ARDUINO_PATH)hardware/arduino/avr/cores/arduino/main.cpp > $@
  168. @cat $< >> $@
  169. @echo >> $@
  170. @echo 'extern "C" void __cxa_pure_virtual() { while (1); }' >> $@
  171. .PHONY: all
  172. all: tmpdir $(HEXNAME) assemblersource showsize
  173. ls -al $(HEXNAME) $(ELFNAME)
  174. $(ELFNAME): $(LIBNAME)($(addprefix $(TMPDIRPATH),$(OBJFILES)))
  175. $(LINK.o) $(COMMON_FLAGS) $(LIBNAME) $(LOADLIBES) $(LDLIBS) -o $@
  176. $(LIBNAME)(): $(addprefix $(TMPDIRPATH),$(OBJFILES))
  177. #=== create temp directory ===
  178. # not really required, because it will be also created during the dependency handling
  179. .PHONY: tmpdir
  180. tmpdir:
  181. @test -d $(TMPDIRPATH) || mkdir $(TMPDIRPATH)
  182. #=== create assembler files for each C/C++ file ===
  183. .PHONY: assemblersource
  184. assemblersource: $(addprefix $(TMPDIRPATH),$(ASSFILES)) $(addprefix $(TMPDIRPATH),$(DISFILES))
  185. #=== show the section sizes of the ELF file ===
  186. .PHONY: showsize
  187. showsize: $(ELFNAME)
  188. $(SIZE) $<
  189. #=== clean up target ===
  190. # this is simple: the TMPDIRPATH is removed
  191. .PHONY: clean
  192. clean:
  193. $(RM) $(TMPDIRPATH)
  194. # Program the device.
  195. # step 1: reset the arduino board with the stty command
  196. # step 2: user avrdude to upload the software
  197. .PHONY: upload
  198. upload: $(HEXNAME)
  199. stty -F $(AVRDUDE_PORT) hupcl
  200. $(AVRDUDE) $(AVRDUDE_FLAGS)
  201. # === dependency handling ===
  202. # From the gnu make manual (section 4.14, Generating Prerequisites Automatically)
  203. # Additionally (because this will be the first executed rule) TMPDIRPATH is created here.
  204. # Instead of "sed" the "echo" command is used
  205. # cd $(TMPDIRPATH); mkdir -p $(DIRS) 2> /dev/null; cd ..
  206. DEPACTION=test -d $(TMPDIRPATH) || mkdir $(TMPDIRPATH);\
  207. mkdir -p $(addprefix $(TMPDIRPATH),$(DIRS));\
  208. set -e; echo -n $@ $(dir $@) > $@; $(CC) -MM $(COMMON_FLAGS) $< >> $@
  209. $(TMPDIRPATH)%.d: %.c
  210. @$(DEPACTION)
  211. $(TMPDIRPATH)%.d: %.cc
  212. @$(DEPACTION)
  213. $(TMPDIRPATH)%.d: %.cpp
  214. @$(DEPACTION)
  215. # Include dependency files. If a .d file is missing, a warning is created and the .d file is created
  216. # This warning is not a problem (gnu make manual, section 3.3 Including Other Makefiles)
  217. -include $(addprefix $(TMPDIRPATH),$(DEPFILES))