Makefile 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # $OpenBSD: Makefile,v 1.3 2014/10/30 21:44:30 edd Exp $
  2. # Arduino Makefile
  3. # Arduino adaptation by mellis, eighthave, oli.keller
  4. #
  5. # Adapted for BSD make(1) by Seth Wright (seth@crosse.org)
  6. # Adapted for OpenBSD ports by Chris Kuethe (ckuethe@openbsd.org)
  7. # Later maintained by the OpenBSD ports team (ports@openbsd.org)
  8. #
  9. # This makefile allows you to build sketches from the command line
  10. # without the Arduino environment (or Java).
  11. #
  12. # Detailed instructions for using the makefile:
  13. #
  14. # 1. Copy this file into the folder with your sketch. There should be a
  15. # file with the same name as the folder and with the extension .ino
  16. # (e.g. foo.ino in the foo/ folder).
  17. #
  18. # 2. Modify the line containing "PORT" to refer to the filename
  19. # representing the USB or serial connection to your Arduino board
  20. # (e.g. PORT = /dev/tty.USB0). If the exact name of this file
  21. # changes, you can use * as a wildcard (e.g. PORT = /dev/tty.usb*).
  22. #
  23. # 3. Set the line containing "MCU" to match your board's processor.
  24. # Older one's are atmega8 based, newer ones like Arduino Mini, Bluetooth
  25. # or Diecimila have the atmega168. If you're using a LilyPad Arduino,
  26. # change F_CPU to 8000000.
  27. #
  28. # 4. At the command line, change to the directory containing your
  29. # program's file and the makefile.
  30. #
  31. # 5. Type "make" and press enter to compile/verify your program.
  32. #
  33. # 6. Type "make upload", reset your Arduino board, and press enter to
  34. # upload your program to the Arduino board.
  35. #
  36. # $Id: Makefile,v 1.3 2014/10/30 21:44:30 edd Exp $
  37. TARGET = ${.CURDIR:C/.*\///g}
  38. # Target options.
  39. #
  40. # You will need to specify the following options to compile and upload
  41. # code to your Arduino board:
  42. #
  43. # UPLOAD_RATE: baud rate for programming.
  44. # PORT: device to program over.
  45. # AVRDUDE_PROGRAMMER: Kind of programming interface.
  46. # MCU: AVR CPU on the board. See avrdude config file for possible values.
  47. # F_CPU: CPU frequency. Usually 16000000.
  48. # VARIANT: Arduino peripheral configuration, one of:
  49. # {eightanaloginputs, leonardo, mega, micro, standard}
  50. # Below are some known working hardware configurations. If you find other
  51. # working configurations, please feed them back to the OpenBSD porting team.
  52. ## you might use this for newer boards like the UNO
  53. UPLOAD_RATE = 115200
  54. PORT = /dev/cuaU0
  55. AVRDUDE_PROGRAMMER = arduino
  56. MCU = atmega328p
  57. F_CPU = 16000000
  58. VARIANT = standard
  59. ## for an arduino mega
  60. #UPLOAD_RATE = 57600
  61. #PORT = /dev/cuaU0
  62. #AVRDUDE_PROGRAMMER = arduino
  63. #MCU = atmega1280
  64. #F_CPU = 16000000
  65. #VARIANT = mega
  66. ## or this for an older arduino
  67. #UPLOAD_RATE = 19200
  68. #PORT = /dev/cuaU0
  69. #AVRDUDE_PROGRAMMER = stk500
  70. #MCU = atmega328p
  71. #F_CPU = 16000000
  72. #VARIANT = standard
  73. # If your sketch uses any libraries, list them here, eg.
  74. # LIBRARIES=EEPROM LiquidCrystal Wire
  75. #
  76. # If you want to use the Ethernet library, use:
  77. # LIBRARIES=SPI Ethernet IPAddress Dhcp Dns EthernetClient EthernetServer \
  78. # EthernetUdp utility/w5100 utility/socket new
  79. #
  80. # To use the SD library:
  81. # LIBRARIES=SD File utility/SdFile utility/SdVolume utility/Sd2Card
  82. LIBRARIES=LiquidCrystal
  83. ############################################################################
  84. # Below here nothing should be changed...
  85. ARDUINO = /usr/local/share/arduino/
  86. .PATH: $(ARDUINO)/cores/arduino ${LIBRARIES:S|^|$(ARDUINO)/libraries/|g}
  87. AVR_TOOLS_PATH = /usr/local/bin
  88. SRC = wiring.c wiring_analog.c wiring_digital.c \
  89. wiring_pulse.c wiring_shift.c WInterrupts.c
  90. CXXSRC = HardwareSerial.cpp WMath.cpp Print.cpp WString.cpp \
  91. ${LIBRARIES:S|$|.cpp|g}
  92. FORMAT = ihex
  93. # Name of this Makefile (used for "make depend").
  94. MAKEFILE = Makefile
  95. # Debugging format.
  96. # Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
  97. # AVR (extended) COFF requires stabs, plus an avr-objcopy run.
  98. DEBUG = stabs
  99. # C options
  100. COPT = s
  101. CDEFS = -DF_CPU=$(F_CPU) -DARDUINO=100
  102. CINCS = -I$(ARDUINO)/cores/arduino $(LIBINC) \
  103. -I$(ARDUINO)/variants/$(VARIANT)
  104. CSTANDARD = -std=gnu99
  105. CDEBUG = -g$(DEBUG)
  106. CWARN = -Wall -Wstrict-prototypes
  107. CTUNING = -ffunction-sections -fdata-sections
  108. #CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
  109. #CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
  110. CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) \
  111. $(CSTANDARD) $(CEXTRA) $(CTUNING)
  112. # C++ options
  113. CXXOPT = ${COPT}
  114. CXXDEFS = -DF_CPU=$(F_CPU) -DARDUINO=100
  115. CXXINCS = ${CINCS}
  116. CXXSTANDARD =
  117. CXXDEBUG = ${CDEBUG}
  118. CXXWARN =
  119. CXXTUNING = ${CTUNING}
  120. CXXEXTRA = ${CEXTRA}
  121. CXXFLAGS = $(CXXDEBUG) $(CXXDEFS) $(CXXINCS) -O$(CXXOPT) $(CXXWARN) \
  122. $(CXXSTANDARD) $(CXXEXTRA) $(CXXTUNING)
  123. # Linker stuff
  124. LDFLAGS = -lm -Wl,--gc-sections
  125. ROOTLIBINCS=${LIBRARIES:S|^|-I$(ARDUINO)/libraries/|g}
  126. UTILITYLIBINCS=${ROOTLIBINCS:S|$|/utility/|g}
  127. LIBINC=${ROOTLIBINCS} ${UTILITYLIBINCS}
  128. # Assembler stuff
  129. #ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
  130. # Programming support using avrdude. Settings and variables.
  131. AVRDUDE_PORT = $(PORT)
  132. AVRDUDE_WRITE_FLASH = -U flash:w:applet/$(TARGET).hex
  133. AVRDUDE_CONF = /etc/avrdude.conf
  134. AVRDUDE_FLAGS = -V -F -C $(AVRDUDE_CONF) -p $(MCU) -P $(AVRDUDE_PORT) \
  135. -c $(AVRDUDE_PROGRAMMER) -b $(UPLOAD_RATE)
  136. # Program settings
  137. CC = $(AVR_TOOLS_PATH)/avr-gcc
  138. CXX = $(AVR_TOOLS_PATH)/avr-g++
  139. OBJCOPY = $(AVR_TOOLS_PATH)/avr-objcopy
  140. OBJDUMP = $(AVR_TOOLS_PATH)/avr-objdump
  141. AR = $(AVR_TOOLS_PATH)/avr-ar
  142. SIZE = $(AVR_TOOLS_PATH)/avr-size
  143. NM = $(AVR_TOOLS_PATH)/avr-nm
  144. AVRDUDE = $(AVR_TOOLS_PATH)/avrdude
  145. REMOVE = rm -f
  146. REMOVEDIR = rmdir
  147. MKDIR = mkdir -p
  148. MV = mv -f
  149. # Define all object files.
  150. OBJ = $(SRC:.c=.o) $(CXXSRC:.cpp=.o) $(ASRC:.S=.o)
  151. # Define all listing files.
  152. LST = $(ASRC:.S=.lst) $(CXXSRC:.cpp=.lst) $(SRC:.c=.lst)
  153. # Combine all necessary flags and optional flags.
  154. # Add target processor to flags.
  155. ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
  156. ALL_CXXFLAGS = -mmcu=$(MCU) -I. $(CXXFLAGS)
  157. ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
  158. # Default target.
  159. all: applet_files build sizeafter
  160. build: mkdirs elf hex
  161. mkdirs:
  162. $(MKDIR) utility
  163. # Here is the "preprocessing".
  164. # It creates a .cpp file based with the same name as the .ino file.
  165. # On top of the new .cpp file comes the Arduino.h header.
  166. # Then comes a stdc++ workaround, see:
  167. # http://stackoverflow.com/questions/920500/what-is-the-purpose-of-cxa-pure-virtual
  168. # At the end there is a generic main() function attached.
  169. # Then the .cpp file will be compiled. Errors during compile will
  170. # refer to this new, automatically generated, file.
  171. # Not the original .ino file you actually edit...
  172. applet_files: $(TARGET).ino
  173. test -d applet || mkdir applet
  174. echo '#include "Arduino.h"' > applet/$(TARGET).cpp
  175. echo '#ifdef __cplusplus' >> applet/$(TARGET).cpp
  176. echo 'extern "C" void __cxa_pure_virtual(void) { while(1); }' \
  177. >> applet/$(TARGET).cpp
  178. echo '#endif\n' >> applet/$(TARGET).cpp
  179. cat $(TARGET).ino >> applet/$(TARGET).cpp
  180. cat $(ARDUINO)/cores/arduino/main.cpp >> applet/$(TARGET).cpp
  181. elf: applet/$(TARGET).elf
  182. hex: applet/$(TARGET).hex
  183. eep: applet/$(TARGET).eep
  184. lss: applet/$(TARGET).lss
  185. sym: applet/$(TARGET).sym
  186. # Program the device.
  187. upload: applet/$(TARGET).hex
  188. $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
  189. # Display size of file.
  190. HEXSIZE = $(SIZE) --target=$(FORMAT) applet/$(TARGET).hex
  191. ELFSIZE = $(SIZE) applet/$(TARGET).elf
  192. sizebefore:
  193. @if [ -f applet/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(HEXSIZE); echo; fi
  194. sizeafter: applet/$(TARGET).hex
  195. @if [ -f applet/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(HEXSIZE); echo; fi
  196. # Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
  197. COFFCONVERT=$(OBJCOPY) --debugging \
  198. --change-section-address .data-0x800000 \
  199. --change-section-address .bss-0x800000 \
  200. --change-section-address .noinit-0x800000 \
  201. --change-section-address .eeprom-0x810000
  202. coff: applet/$(TARGET).elf
  203. $(COFFCONVERT) -O coff-avr applet/$(TARGET).elf $(TARGET).cof
  204. extcoff: $(TARGET).elf
  205. $(COFFCONVERT) -O coff-ext-avr applet/$(TARGET).elf $(TARGET).cof
  206. .SUFFIXES: .elf .hex .eep .lss .sym .cpp .o .c .s .S
  207. .elf.hex:
  208. $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
  209. .elf.eep:
  210. -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
  211. --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
  212. # Create extended listing file from ELF output file.
  213. .elf.lss:
  214. $(OBJDUMP) -h -S $< > $@
  215. # Create a symbol table from ELF output file.
  216. .elf.sym:
  217. $(NM) -n $< > $@
  218. # Link: create ELF output file from library.
  219. applet/$(TARGET).elf: $(TARGET).ino applet/core.a
  220. $(CXX) $(ALL_CXXFLAGS) -o $@ applet/$(TARGET).cpp -L. applet/core.a $(LDFLAGS)
  221. applet/core.a: $(OBJ)
  222. @for i in $(OBJ); do echo $(AR) rcs applet/core.a $$i; $(AR) rcs applet/core.a $$i; done
  223. # Compile: create object files from C++ source files.
  224. .cpp.o:
  225. $(CXX) -c $(ALL_CXXFLAGS) $< -o $@
  226. # Compile: create object files from C source files.
  227. .c.o:
  228. $(CC) -c $(ALL_CFLAGS) $< -o $@
  229. # Compile: create assembler files from C source files.
  230. .c.s:
  231. $(CC) -S $(ALL_CFLAGS) $< -o $@
  232. # Assemble: create object files from assembler source files.
  233. .S.o:
  234. $(CC) -c $(ALL_ASFLAGS) $< -o $@
  235. # Automatic dependencies
  236. %.d: %.c
  237. $(CC) -M $(ALL_CFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@
  238. %.d: %.cpp
  239. $(CXX) -M $(ALL_CXXFLAGS) $< | sed "s;$(notdir $*).o:;$*.o $*.d:;" > $@
  240. # Target: clean project.
  241. clean:
  242. $(REMOVE) applet/$(TARGET).hex applet/$(TARGET).eep applet/$(TARGET).cof applet/$(TARGET).elf \
  243. applet/$(TARGET).map applet/$(TARGET).sym applet/$(TARGET).lss applet/core.a \
  244. $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) $(CXXSRC:.cpp=.s) $(CXXSRC:.cpp=.d) utility/*
  245. if [ -d utility ]; then $(REMOVEDIR) utility; fi
  246. .PHONY: all build elf hex eep lss sym program coff extcoff clean applet_files sizebefore sizeafter