Makefile.165.uno 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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))
  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.
  125. COMMON_FLAGS += -I$(ARDUINO_PATH)hardware/arduino/cores/arduino
  126. COMMON_FLAGS += -I$(ARDUINO_PATH)hardware/arduino/variants/$(VARIANT)
  127. COMMON_FLAGS += -I. -I$(U8G_PATH)
  128. COMMON_FLAGS += $(addprefix -I,$(EXTRA_DIRS))
  129. COMMON_FLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections
  130. COMMON_FLAGS += -Wl,--Map=output.map
  131. COMMON_FLAGS += -Wl,--relax
  132. COMMON_FLAGS += -mcall-prologues
  133. CFLAGS = $(COMMON_FLAGS) -std=gnu99 -Wstrict-prototypes
  134. CXXFLAGS = $(COMMON_FLAGS)
  135. # Replace standard build tools by avr tools
  136. CC = $(AVRTOOLSPATH)avr-gcc
  137. CXX = $(AVRTOOLSPATH)avr-g++
  138. AR = @$(AVRTOOLSPATH)avr-ar
  139. # "rm" must be able to delete a directory tree
  140. RM = rm -rf
  141. #=== rules ===
  142. # add rules for the C/C++ files where the .o file is placed in the TMPDIRPATH
  143. # reuse existing variables as far as possible
  144. $(TMPDIRPATH)%.o: %.c
  145. @echo compile $<
  146. @$(COMPILE.c) $(OUTPUT_OPTION) $<
  147. $(TMPDIRPATH)%.o: %.cc
  148. @echo compile $<
  149. @$(COMPILE.cc) $(OUTPUT_OPTION) $<
  150. $(TMPDIRPATH)%.o: %.cpp
  151. @echo compile $<
  152. @$(COMPILE.cpp) $(OUTPUT_OPTION) $<
  153. $(TMPDIRPATH)%.s: %.c
  154. @$(COMPILE.c) $(OUTPUT_OPTION) -S $<
  155. $(TMPDIRPATH)%.s: %.cc
  156. @$(COMPILE.cc) $(OUTPUT_OPTION) -S $<
  157. $(TMPDIRPATH)%.s: %.cpp
  158. @$(COMPILE.cpp) $(OUTPUT_OPTION) -S $<
  159. $(TMPDIRPATH)%.dis: $(TMPDIRPATH)%.o
  160. @$(OBJDUMP) -S $< > $@
  161. .SUFFIXES: .elf .hex .ino
  162. .elf.hex:
  163. @$(OBJCOPY) -O ihex -R .eeprom $< $@
  164. $(TMPDIRPATH)%.cpp: %.ino
  165. @cat $(ARDUINO_PATH)hardware/arduino/cores/arduino/main.cpp > $@
  166. @cat $< >> $@
  167. @echo >> $@
  168. @echo 'extern "C" void __cxa_pure_virtual() { while (1); }' >> $@
  169. .PHONY: all
  170. all: tmpdir $(HEXNAME) assemblersource showsize
  171. ls -al $(HEXNAME) $(ELFNAME)
  172. $(ELFNAME): $(LIBNAME)($(addprefix $(TMPDIRPATH),$(OBJFILES)))
  173. $(LINK.o) $(COMMON_FLAGS) $(LIBNAME) $(LOADLIBES) $(LDLIBS) -o $@
  174. $(LIBNAME)(): $(addprefix $(TMPDIRPATH),$(OBJFILES))
  175. #=== create temp directory ===
  176. # not really required, because it will be also created during the dependency handling
  177. .PHONY: tmpdir
  178. tmpdir:
  179. @test -d $(TMPDIRPATH) || mkdir $(TMPDIRPATH)
  180. #=== create assembler files for each C/C++ file ===
  181. .PHONY: assemblersource
  182. assemblersource: $(addprefix $(TMPDIRPATH),$(ASSFILES)) $(addprefix $(TMPDIRPATH),$(DISFILES))
  183. #=== show the section sizes of the ELF file ===
  184. .PHONY: showsize
  185. showsize: $(ELFNAME)
  186. $(SIZE) $<
  187. #=== clean up target ===
  188. # this is simple: the TMPDIRPATH is removed
  189. .PHONY: clean
  190. clean:
  191. $(RM) $(TMPDIRPATH)
  192. # Program the device.
  193. # step 1: reset the arduino board with the stty command
  194. # step 2: user avrdude to upload the software
  195. .PHONY: upload
  196. upload: $(HEXNAME)
  197. stty -F $(AVRDUDE_PORT) hupcl
  198. $(AVRDUDE) $(AVRDUDE_FLAGS)
  199. # === dependency handling ===
  200. # From the gnu make manual (section 4.14, Generating Prerequisites Automatically)
  201. # Additionally (because this will be the first executed rule) TMPDIRPATH is created here.
  202. # Instead of "sed" the "echo" command is used
  203. # cd $(TMPDIRPATH); mkdir -p $(DIRS) 2> /dev/null; cd ..
  204. DEPACTION=test -d $(TMPDIRPATH) || mkdir $(TMPDIRPATH);\
  205. mkdir -p $(addprefix $(TMPDIRPATH),$(DIRS));\
  206. set -e; echo -n $@ $(dir $@) > $@; $(CC) -MM $(COMMON_FLAGS) $< >> $@
  207. $(TMPDIRPATH)%.d: %.c
  208. @$(DEPACTION)
  209. $(TMPDIRPATH)%.d: %.cc
  210. @$(DEPACTION)
  211. $(TMPDIRPATH)%.d: %.cpp
  212. @$(DEPACTION)
  213. # Include dependency files. If a .d file is missing, a warning is created and the .d file is created
  214. # This warning is not a problem (gnu make manual, section 3.3 Including Other Makefiles)
  215. -include $(addprefix $(TMPDIRPATH),$(DEPFILES))