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.

libopencm3.rules.mk 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. # Colour stuff
  28. NO_COLOR=\033[0m
  29. OK_COLOR=\033[32;01m
  30. ERROR_COLOR=\033[31;01m
  31. WARN_COLOR=\033[33;01m
  32. OK_STRING=$(OK_COLOR)[OK]$(NO_COLOR)
  33. ERROR_STRING=$(ERROR_COLOR)[ERRORS]$(NO_COLOR)
  34. WARN_STRING=$(WARN_COLOR)[WARNINGS]$(NO_COLOR)
  35. AWK_CMD = awk '{ printf "%-30s %-10s\n",$$1, $$2; }'
  36. PRINT_ERROR = printf "$@ $(ERROR_STRING)\n" | $(AWK_CMD) && printf "$(CMD)\n$$LOG\n" && false
  37. PRINT_WARNING = printf "$@ $(WARN_STRING)\n" | $(AWK_CMD) && printf "$(CMD)\n$$LOG\n"
  38. PRINT_OK = printf "$@ $(OK_STRING)\n" | $(AWK_CMD)
  39. BUILD_CMD = LOG=$$($(CMD) 2>&1) ; if [ $$? -eq 1 ]; then $(PRINT_ERROR); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING); else $(PRINT_OK); fi;
  40. ## can use separately with '@$(PRINT_OK)'
  41. ## can use with '$(call colorecho,<string>)'
  42. define colorecho
  43. @tput setaf 6
  44. @echo $1
  45. @tput sgr0
  46. endef
  47. ## can use as '$(call buildcmd,<command_to_run_as_string>)'
  48. define buildcmd
  49. LOG=$$($1 2>&1) ; if [ $$? -eq 1 ]; then $(PRINT_ERROR); elif [ "$$LOG" != "" ] ; then $(PRINT_WARNING); else $(PRINT_OK); fi;
  50. endef
  51. ###############################################################################
  52. # Executables
  53. PREFIX ?= arm-none-eabi
  54. CC := $(PREFIX)-gcc
  55. CXX := $(PREFIX)-g++
  56. LD := $(PREFIX)-gcc
  57. AR := $(PREFIX)-ar
  58. AS := $(PREFIX)-as
  59. OBJCOPY := $(PREFIX)-objcopy
  60. OBJDUMP := $(PREFIX)-objdump
  61. GDB := $(PREFIX)-gdb
  62. STFLASH = $(shell which st-flash)
  63. STYLECHECK := /checkpatch.pl
  64. STYLECHECKFLAGS := --no-tree -f --terse --mailback
  65. STYLECHECKFILES := $(shell find . -name '*.[ch]')
  66. OPT ?= -Os
  67. CSTD ?= -std=c99
  68. ###############################################################################
  69. # Source files
  70. LDSCRIPT ?= $(BINARY).ld
  71. OBJS += $(BINARY).o
  72. ifeq ($(strip $(OPENCM3_DIR)),)
  73. # user has not specified the library path, so we try to detect it
  74. # where we search for the library
  75. LIBPATHS := ./libopencm3 ../../../../libopencm3 ../../../../../libopencm3
  76. OPENCM3_DIR := $(wildcard $(LIBPATHS:=/locm3.sublime-project))
  77. OPENCM3_DIR := $(firstword $(dir $(OPENCM3_DIR)))
  78. ifeq ($(strip $(OPENCM3_DIR)),)
  79. $(warning Cannot find libopencm3 library in the standard search paths.)
  80. $(error Please specify it through OPENCM3_DIR variable!)
  81. endif
  82. endif
  83. ifeq ($(V),1)
  84. $(info Using $(OPENCM3_DIR) path to library)
  85. endif
  86. define ERR_DEVICE_LDSCRIPT_CONFLICT
  87. You can either specify DEVICE=blah, and have the LDSCRIPT generated,
  88. or you can provide LDSCRIPT, and ensure CPPFLAGS, LDFLAGS and LDLIBS
  89. all contain the correct values for the target you wish to use.
  90. You cannot provide both!
  91. endef
  92. ifeq ($(strip $(DEVICE)),)
  93. # Old style, assume LDSCRIPT exists
  94. DEFS += -I$(OPENCM3_DIR)/include
  95. LDFLAGS += -L$(OPENCM3_DIR)/lib
  96. LDLIBS += -l$(LIBNAME)
  97. LDSCRIPT ?= $(BINARY).ld
  98. else
  99. # New style, assume device is provided, and we're generating the rest.
  100. ifneq ($(strip $(LDSCRIPT)),)
  101. $(error $(ERR_DEVICE_LDSCRIPT_CONFLICT))
  102. endif
  103. include $(OPENCM3_DIR)/mk/genlink-config.mk
  104. endif
  105. OPENCM3_SCRIPT_DIR = $(OPENCM3_DIR)/scripts
  106. LOCAL_SCRIPT_DIR ?= ./scripts
  107. ###############################################################################
  108. # C flags
  109. TGT_CFLAGS += $(OPT) $(CSTD) -g
  110. TGT_CFLAGS += $(ARCH_FLAGS)
  111. TGT_CFLAGS += -Wextra -Wshadow -Wimplicit-function-declaration
  112. TGT_CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
  113. TGT_CFLAGS += -fno-common -ffunction-sections -fdata-sections
  114. ###############################################################################
  115. # C++ flags
  116. TGT_CXXFLAGS += $(OPT) $(CXXSTD) -g
  117. TGT_CXXFLAGS += $(ARCH_FLAGS)
  118. TGT_CXXFLAGS += -Wextra -Wshadow -Wredundant-decls -Weffc++
  119. TGT_CXXFLAGS += -fno-common -ffunction-sections -fdata-sections
  120. ###############################################################################
  121. # C & C++ preprocessor common flags
  122. TGT_CPPFLAGS += -MD
  123. TGT_CPPFLAGS += -Wall -Wundef
  124. TGT_CPPFLAGS += $(DEFS)
  125. ###############################################################################
  126. # Linker flags
  127. TGT_LDFLAGS += --static -nostartfiles
  128. TGT_LDFLAGS += -T$(LDSCRIPT)
  129. TGT_LDFLAGS += $(ARCH_FLAGS)
  130. TGT_LDFLAGS += -Wl,-Map=$(*).map
  131. TGT_LDFLAGS += -Wl,--gc-sections
  132. ifeq ($(V),99)
  133. TGT_LDFLAGS += -Wl,--print-gc-sections
  134. endif
  135. ###############################################################################
  136. # Used libraries
  137. LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
  138. ###############################################################################
  139. ###############################################################################
  140. ###############################################################################
  141. .SUFFIXES: .elf .bin .hex .srec .list .map .images
  142. .SECONDEXPANSION:
  143. .SECONDARY:
  144. all: elf hex bin
  145. elf: $(BINARY).elf
  146. bin: $(BINARY).bin
  147. hex: $(BINARY).hex
  148. srec: $(BINARY).srec
  149. list: $(BINARY).list
  150. images: $(BINARY).images
  151. flash: $(BINARY).flash
  152. # Either verify the user provided LDSCRIPT exists, or generate it.
  153. ifeq ($(strip $(DEVICE)),)
  154. $(LDSCRIPT):
  155. ifeq (,$(wildcard $(LDSCRIPT)))
  156. $(error Unable to find specified linker script: $(LDSCRIPT))
  157. endif
  158. else
  159. include $(OPENCM3_DIR)/mk/genlink-rules.mk
  160. endif
  161. # Define a helper macro for debugging make errors online
  162. # you can type "make print-OPENCM3_DIR" and it will show you
  163. # how that ended up being resolved by all of the included
  164. # makefiles.
  165. print-%:
  166. @echo $*=$($*)
  167. %.images: %.bin %.hex %.srec %.list %.map
  168. @#printf "*** $* images generated ***\n"
  169. %.bin: %.elf
  170. @#printf " OBJCOPY $(*).bin\n"
  171. $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin
  172. %.hex: %.elf
  173. @#printf " OBJCOPY $(*).hex\n"
  174. $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex
  175. %.srec: %.elf
  176. @#printf " OBJCOPY $(*).srec\n"
  177. $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec
  178. %.list: %.elf
  179. @#printf " OBJDUMP $(*).list\n"
  180. $(Q)$(OBJDUMP) -S $(*).elf > $(*).list
  181. %.elf %.map: $(OBJS) $(LDSCRIPT)
  182. @#printf " LD $(*).elf\n"
  183. $(Q)$(LD) $(TGT_LDFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(*).elf
  184. %.o: %.c
  185. @#printf " CC $(*).c\n"
  186. $(Q)$(CC) $(TGT_CFLAGS) $(CFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).c
  187. %.o: %.cxx
  188. @#printf " CXX $(*).cxx\n"
  189. $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cxx
  190. %.o: %.cpp
  191. @#printf " CXX $(*).cpp\n"
  192. $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cpp
  193. clean:
  194. @#printf " CLEAN\n"
  195. $(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map generated.* ${OBJS} ${OBJS:%.o:%.d}
  196. stylecheck: $(STYLECHECKFILES:=.stylecheck)
  197. styleclean: $(STYLECHECKFILES:=.styleclean)
  198. # the cat is due to multithreaded nature - we like to have consistent chunks of text on the output
  199. %.stylecheck: %
  200. $(Q)$(OPENCM3_SCRIPT_DIR)$(STYLECHECK) $(STYLECHECKFLAGS) $* > $*.stylecheck; \
  201. if [ -s $*.stylecheck ]; then \
  202. cat $*.stylecheck; \
  203. else \
  204. rm -f $*.stylecheck; \
  205. fi;
  206. %.styleclean:
  207. $(Q)rm -f $*.stylecheck;
  208. %.stlink-flash: %.bin
  209. @printf " FLASH $<\n"
  210. $(Q)$(STFLASH) write $(*).bin 0x8000000
  211. ifeq ($(BMP_PORT),)
  212. ifeq ($(OOCD_FILE),)
  213. %.flash: %.elf
  214. @printf " FLASH $<\n"
  215. $(Q)(echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \
  216. $(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
  217. -f target/$(OOCD_TARGET).cfg \
  218. -c "program $(*).elf verify reset exit" \
  219. $(NULL)
  220. else
  221. %.flash: %.elf
  222. @printf " FLASH $<\n"
  223. $(Q)(echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \
  224. $(OOCD) -f $(OOCD_FILE) \
  225. -c "program $(*).elf verify reset exit" \
  226. $(NULL)
  227. endif
  228. else
  229. %.flash: %.elf
  230. @printf " GDB $(*).elf (flash)\n"
  231. $(GDB) --batch \
  232. -ex 'target extended-remote $(BMP_PORT)' \
  233. -x $(LOCAL_SCRIPT_DIR)/black_magic_probe_flash.scr \
  234. $(*).elf
  235. endif
  236. .PHONY: images clean stylecheck styleclean elf bin hex srec list
  237. -include $(OBJS:.o=.d)