Makefile.105.uno 8.8 KB

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