Makefile.common 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright (c) 2016 Nordic Semiconductor. All Rights Reserved.
  2. #
  3. # The information contained herein is property of Nordic Semiconductor ASA.
  4. # Terms and conditions of usage are described in detail in NORDIC
  5. # SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. #
  7. # Licensees are granted free, non-transferable use of the information. NO
  8. # WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. # the file.
  10. PLATFORM_SUFFIX := $(if $(filter Windows%,$(OS)),windows,posix)
  11. TOOLCHAIN_CONFIG_FILE := $(TEMPLATE_PATH)/Makefile.$(PLATFORM_SUFFIX)
  12. include $(TOOLCHAIN_CONFIG_FILE)
  13. # Toolchain commands
  14. CC := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-gcc"
  15. CXX := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-c++"
  16. AS := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-as"
  17. AR := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ar" -r
  18. LD := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ld"
  19. NM := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-nm"
  20. OBJDUMP := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objdump"
  21. OBJCOPY := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objcopy"
  22. SIZE := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-size"
  23. $(if $(shell $(CC) --version),,$(info Cannot find: $(CC).) \
  24. $(info Please set values in: "$(abspath $(TOOLCHAIN_CONFIG_FILE))") \
  25. $(info according to the actual configuration of your system.) \
  26. $(error Cannot continue))
  27. # Use ccache on linux if available
  28. CCACHE := $(if $(filter Windows%,$(OS)),, \
  29. $(if $(wildcard /usr/bin/ccache),ccache))
  30. CC := $(CCACHE) $(CC)
  31. MK := mkdir
  32. RM := rm -rf
  33. # echo suspend
  34. ifeq ($(VERBOSE),1)
  35. NO_ECHO :=
  36. else
  37. NO_ECHO := @
  38. endif
  39. # $1 type of item
  40. # $2 path to check
  41. define ensure_exists
  42. $(if $(wildcard $(2)),, $(warning Cannot find $(1): $(2)))
  43. endef
  44. # $1 object file
  45. # $2 source file
  46. define bind_obj_with_src
  47. $(eval $(1) := $(2))
  48. endef
  49. # $1 object file
  50. # $2 target name
  51. define bind_obj_with_target
  52. $(eval $(1)T := $(2))
  53. endef
  54. # $1 target name
  55. # $2 source file name
  56. # Note: this additional .o for .s-files is a workaround for issues with make 4.1
  57. # from MinGW (it does nothing to remake .s.o files when a rule for .S.o
  58. # files is defined as well).
  59. define get_object_file_name
  60. $(OUTPUT_DIRECTORY)/$(strip $(1))_$(patsubst %.s,%.s.o,$(notdir $(2))).o
  61. endef
  62. # $1 target name
  63. # $2 list of source files
  64. define get_object_files
  65. $(foreach src_file, $(2), \
  66. $(call ensure_exists,source file, $(src_file)) \
  67. $(eval obj_file := $(call get_object_file_name, $(1), $(src_file))) \
  68. $(eval DEPENDENCIES += $(obj_file:.o=.d)) \
  69. $(call bind_obj_with_src, $(obj_file), $(src_file)) \
  70. $(call bind_obj_with_target, $(obj_file), $(1)) \
  71. $(eval $(obj_file): Makefile) \
  72. $(obj_file))
  73. endef
  74. # $1 variable name
  75. # $2 target name
  76. define target_specific
  77. $($(addsuffix _$(strip $(2)), $(1)))
  78. endef
  79. # $1 target name
  80. # $2 link target name
  81. define prepare_build
  82. $(eval DEPENDENCIES :=) \
  83. $(eval $(2): \
  84. $(call get_object_files, $(1), $(SRC_FILES) \
  85. $(call target_specific, SRC_FILES, $(1)))) \
  86. $(eval -include $(DEPENDENCIES)) \
  87. $(eval INC_PATHS_$(strip $(1)) := \
  88. $(foreach folder, $(INC_FOLDERS) $(call target_specific, INC_FOLDERS, $(1)), \
  89. $(call ensure_exists,include folder, $(folder)) \
  90. -I"$(folder)"))
  91. endef
  92. INC_PATHS = $(call target_specific, INC_PATHS, $($@T))
  93. # $1 target name
  94. define define_target
  95. $(eval OUTPUT_FILE := $(OUTPUT_DIRECTORY)/$(strip $(1))) \
  96. $(eval $(1): $(OUTPUT_FILE).out $(OUTPUT_FILE).hex $(OUTPUT_FILE).bin) \
  97. $(call prepare_build, $(1), $(OUTPUT_FILE).out)
  98. endef
  99. # $1 target name
  100. # $2 library file name
  101. define define_library
  102. $(eval $(1) := $(2)) \
  103. $(call prepare_build, $(1), $(1))
  104. endef
  105. .PHONY: $(TARGETS) default all clean help flash
  106. all: $(TARGETS)
  107. clean:
  108. $(RM) $(OUTPUT_DIRECTORY)
  109. # Create build directories
  110. $(OUTPUT_DIRECTORY):
  111. $(MK) $@
  112. # Create objects from C source files
  113. $(OUTPUT_DIRECTORY)/%.c.o: | $(OUTPUT_DIRECTORY)
  114. @echo Compiling file: $(notdir $($@))
  115. $(NO_ECHO)$(CC) -MP -MD -std=c99 $(CFLAGS) $(INC_PATHS) -c -o $@ "$($@)"
  116. # Create objects from C++ source files
  117. $(OUTPUT_DIRECTORY)/%.cpp.o: | $(OUTPUT_DIRECTORY)
  118. @echo Compiling file: $(notdir $($@))
  119. $(NO_ECHO)$(CXX) -MP -MD $(CFLAGS) $(CXXFLAGS) $(INC_PATHS) -c -o $@ "$($@)"
  120. # Create objects from assembly files
  121. $(OUTPUT_DIRECTORY)/%.S.o \
  122. $(OUTPUT_DIRECTORY)/%.s.o.o: | $(OUTPUT_DIRECTORY)
  123. @echo Assembling file: $(notdir $($@))
  124. $(NO_ECHO)$(CC) -MP -MD -std=c99 $(ASMFLAGS) $(INC_PATHS) -c -o $@ "$($@)"
  125. export FILE_LIST
  126. DUMP_FILE_LIST := \
  127. "$(MAKE)" -s --no-print-directory -f $(TEMPLATE_PATH)/file_list.mk
  128. # Link object files
  129. %.out:
  130. $(eval FILE_LIST := $^ $(LIB_FILES))
  131. $(NO_ECHO)$(DUMP_FILE_LIST) > $(@:.out=.in)
  132. @echo Linking target: $@
  133. $(NO_ECHO)$(CC) -Wl,-Map=$(@:.out=.map) $(LDFLAGS) @$(@:.out=.in) -lm -o $@
  134. -@echo ''
  135. $(NO_ECHO)$(SIZE) $@
  136. -@echo ''
  137. # Create binary .bin file from the .out file
  138. %.bin: %.out
  139. @echo Preparing: $@
  140. $(NO_ECHO)$(OBJCOPY) -O binary $< $@
  141. # Create binary .hex file from the .out file
  142. %.hex: %.out
  143. @echo Preparing: $@
  144. $(NO_ECHO)$(OBJCOPY) -O ihex $< $@