Makefile.165.uno 8.6 KB

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