flabbergast's ARM/STM32 examples using libopencm3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rules.mk 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ##
  2. ## This file is part of the libopencm3 project.
  3. ##
  4. ## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
  5. ## Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
  6. ## Copyright (C) 2013 Frantisek Burian <BuFran@seznam.cz>
  7. ##
  8. ## This library is free software: you can redistribute it and/or modify
  9. ## it under the terms of the GNU Lesser General Public License as published by
  10. ## the Free Software Foundation, either version 3 of the License, or
  11. ## (at your option) any later version.
  12. ##
  13. ## This library is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ## GNU Lesser General Public License for more details.
  17. ##
  18. ## You should have received a copy of the GNU Lesser General Public License
  19. ## along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. ##
  21. # Be silent per default, but 'make V=1' will show all compiler calls.
  22. ifneq ($(V),1)
  23. Q := @
  24. NULL := 2>/dev/null
  25. endif
  26. ###############################################################################
  27. # Executables
  28. PREFIX ?= arm-none-eabi
  29. CC := $(PREFIX)-gcc
  30. CXX := $(PREFIX)-g++
  31. LD := $(PREFIX)-gcc
  32. AR := $(PREFIX)-ar
  33. AS := $(PREFIX)-as
  34. OBJCOPY := $(PREFIX)-objcopy
  35. OBJDUMP := $(PREFIX)-objdump
  36. GDB := $(PREFIX)-gdb
  37. STFLASH = $(shell which st-flash)
  38. STYLECHECK := /checkpatch.pl
  39. STYLECHECKFLAGS := --no-tree -f --terse --mailback
  40. STYLECHECKFILES := $(shell find . -name '*.[ch]')
  41. OPT := -Os
  42. CSTD ?= -std=c99
  43. ###############################################################################
  44. # Source files
  45. OBJS += $(BINARY).o
  46. ifeq ($(strip $(OPENCM3_DIR)),)
  47. # user has not specified the library path, so we try to detect it
  48. # where we search for the library
  49. LIBPATHS := ./libopencm3 ../../../../libopencm3 ../../../../../libopencm3
  50. OPENCM3_DIR := $(wildcard $(LIBPATHS:=/locm3.sublime-project))
  51. OPENCM3_DIR := $(firstword $(dir $(OPENCM3_DIR)))
  52. ifeq ($(strip $(OPENCM3_DIR)),)
  53. $(warning Cannot find libopencm3 library in the standard search paths.)
  54. $(error Please specify it through OPENCM3_DIR variable!)
  55. endif
  56. endif
  57. ifeq ($(V),1)
  58. $(info Using $(OPENCM3_DIR) path to library)
  59. endif
  60. define ERR_DEVICE_LDSCRIPT_CONFLICT
  61. You can either specify DEVICE=blah, and have the LDSCRIPT generated,
  62. or you can provide LDSCRIPT, and ensure CPPFLAGS, LDFLAGS and LDLIBS
  63. all contain the correct values for the target you wish to use.
  64. You cannot provide both!
  65. endef
  66. ifeq ($(strip $(DEVICE)),)
  67. # Old style, assume LDSCRIPT exists
  68. DEFS += -I$(OPENCM3_DIR)/include
  69. LDFLAGS += -L$(OPENCM3_DIR)/lib
  70. LDLIBS += -l$(LIBNAME)
  71. LDSCRIPT ?= $(BINARY).ld
  72. else
  73. # New style, assume device is provided, and we're generating the rest.
  74. ifneq ($(strip $(LDSCRIPT)),)
  75. $(error $(ERR_DEVICE_LDSCRIPT_CONFLICT))
  76. endif
  77. include $(OPENCM3_DIR)/mk/genlink-config.mk
  78. endif
  79. SCRIPT_DIR ?= $(OPENCM3_DIR)/scripts
  80. ###############################################################################
  81. # C flags
  82. TGT_CFLAGS += $(OPT) $(CSTD) -g
  83. TGT_CFLAGS += $(ARCH_FLAGS)
  84. TGT_CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration
  85. TGT_CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
  86. TGT_CFLAGS += -fno-common -ffunction-sections -fdata-sections
  87. ###############################################################################
  88. # C++ flags
  89. TGT_CXXFLAGS += $(OPT) $(CXXSTD) -g
  90. TGT_CXXFLAGS += $(ARCH_FLAGS)
  91. TGT_CXXFLAGS += -Wextra -Wshadow -Wredundant-decls -Weffc++
  92. TGT_CXXFLAGS += -fno-common -ffunction-sections -fdata-sections
  93. ###############################################################################
  94. # C & C++ preprocessor common flags
  95. TGT_CPPFLAGS += -MD
  96. TGT_CPPFLAGS += -Wall -Wundef
  97. TGT_CPPFLAGS += $(DEFS)
  98. ###############################################################################
  99. # Linker flags
  100. TGT_LDFLAGS += --static -nostartfiles
  101. TGT_LDFLAGS += -T$(LDSCRIPT)
  102. TGT_LDFLAGS += $(ARCH_FLAGS)
  103. TGT_LDFLAGS += -Wl,-Map=$(*).map
  104. TGT_LDFLAGS += -Wl,--gc-sections
  105. ifeq ($(V),99)
  106. TGT_LDFLAGS += -Wl,--print-gc-sections
  107. endif
  108. ###############################################################################
  109. # Used libraries
  110. LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
  111. ###############################################################################
  112. ###############################################################################
  113. ###############################################################################
  114. .SUFFIXES: .elf .bin .hex .srec .list .map .images
  115. .SECONDEXPANSION:
  116. .SECONDARY:
  117. all: elf
  118. elf: $(BINARY).elf
  119. bin: $(BINARY).bin
  120. hex: $(BINARY).hex
  121. srec: $(BINARY).srec
  122. list: $(BINARY).list
  123. images: $(BINARY).images
  124. flash: $(BINARY).flash
  125. # Either verify the user provided LDSCRIPT exists, or generate it.
  126. ifeq ($(strip $(DEVICE)),)
  127. $(LDSCRIPT):
  128. ifeq (,$(wildcard $(LDSCRIPT)))
  129. $(error Unable to find specified linker script: $(LDSCRIPT))
  130. endif
  131. else
  132. include $(OPENCM3_DIR)/mk/genlink-rules.mk
  133. endif
  134. %.images: %.bin %.hex %.srec %.list %.map
  135. @printf "*** $* images generated ***\n"
  136. %.bin: %.elf
  137. @printf " OBJCOPY $(*).bin\n"
  138. $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin
  139. %.hex: %.elf
  140. @printf " OBJCOPY $(*).hex\n"
  141. $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex
  142. %.srec: %.elf
  143. @printf " OBJCOPY $(*).srec\n"
  144. $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec
  145. %.list: %.elf
  146. @printf " OBJDUMP $(*).list\n"
  147. $(Q)$(OBJDUMP) -S $(*).elf > $(*).list
  148. %.elf %.map: $(OBJS) $(LDSCRIPT)
  149. @printf " LD $(*).elf\n"
  150. $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(*).elf
  151. %.o: %.c
  152. @printf " CC $(*).c\n"
  153. $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).c
  154. %.o: %.cxx
  155. @printf " CXX $(*).cxx\n"
  156. $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cxx
  157. %.o: %.cpp
  158. @printf " CXX $(*).cpp\n"
  159. $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cpp
  160. clean:
  161. @printf " CLEAN\n"
  162. $(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map generated.* ${OBJS} ${OBJS:%.o:%.d}
  163. stylecheck: $(STYLECHECKFILES:=.stylecheck)
  164. styleclean: $(STYLECHECKFILES:=.styleclean)
  165. # the cat is due to multithreaded nature - we like to have consistent chunks of text on the output
  166. %.stylecheck: %
  167. $(Q)$(SCRIPT_DIR)$(STYLECHECK) $(STYLECHECKFLAGS) $* > $*.stylecheck; \
  168. if [ -s $*.stylecheck ]; then \
  169. cat $*.stylecheck; \
  170. else \
  171. rm -f $*.stylecheck; \
  172. fi;
  173. %.styleclean:
  174. $(Q)rm -f $*.stylecheck;
  175. %.stlink-flash: %.bin
  176. @printf " FLASH $<\n"
  177. $(Q)$(STFLASH) write $(*).bin 0x8000000
  178. ifeq ($(STLINK_PORT),)
  179. ifeq ($(BMP_PORT),)
  180. ifeq ($(OOCD_FILE),)
  181. %.flash: %.elf
  182. @printf " FLASH $<\n"
  183. $(Q)(echo "halt; program $(*).elf verify reset" | nc -4 localhost 4444 2>/dev/null) || \
  184. $(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
  185. -f target/$(OOCD_TARGET).cfg \
  186. -c "program $(*).elf verify reset exit" \
  187. $(NULL)
  188. else
  189. %.flash: %.elf
  190. @printf " FLASH $<\n"
  191. $(Q)(echo "halt; program $(*).elf verify reset" | nc -4 localhost 4444 2>/dev/null) || \
  192. $(OOCD) -f $(OOCD_FILE) \
  193. -c "program $(*).elf verify reset exit" \
  194. $(NULL)
  195. endif
  196. else
  197. %.flash: %.elf
  198. @printf " GDB $(*).elf (flash)\n"
  199. $(Q)$(GDB) --batch \
  200. -ex 'target extended-remote $(BMP_PORT)' \
  201. -x $(SCRIPT_DIR)/black_magic_probe_flash.scr \
  202. $(*).elf
  203. endif
  204. else
  205. %.flash: %.elf
  206. @printf " GDB $(*).elf (flash)\n"
  207. $(Q)$(GDB) --batch \
  208. -ex 'target extended-remote $(STLINK_PORT)' \
  209. -x $(SCRIPT_DIR)/stlink_flash.scr \
  210. $(*).elf
  211. endif
  212. .PHONY: images clean stylecheck styleclean elf bin hex srec list
  213. -include $(OBJS:.o=.d)