targets.mk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #-------------------------------------------------------------------------------
  2. # Copyright (c) 2012 Freescale Semiconductor, Inc.
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without modification,
  6. # are permitted provided that the following conditions are met:
  7. #
  8. # o Redistributions of source code must retain the above copyright notice, this list
  9. # of conditions and the following disclaimer.
  10. #
  11. # o Redistributions in binary form must reproduce the above copyright notice, this
  12. # list of conditions and the following disclaimer in the documentation and/or
  13. # other materials provided with the distribution.
  14. #
  15. # o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from this
  17. # software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #-------------------------------------------------------------------------------
  30. #-------------------------------------------------------------------------------
  31. # Sources and objects
  32. #-------------------------------------------------------------------------------
  33. # Select our object root depending on whether we're building an app or lib.
  34. ifneq "$(APP_NAME)" ""
  35. OBJS_ROOT = $(APP_OBJS_ROOT)
  36. else
  37. OBJS_ROOT = $(LIB_OBJS_ROOT)
  38. endif
  39. # Strip sources.
  40. SOURCES := $(strip $(SOURCES))
  41. # Convert sources list to absolute paths and root-relative paths.
  42. SOURCES_ABS := $(foreach s,$(SOURCES),$(abspath $(s)))
  43. SOURCES_REL := $(subst $(SDK_ROOT)/,,$(SOURCES_ABS))
  44. # Get a list of unique directories containing the source files.
  45. SOURCE_DIRS_ABS := $(sort $(foreach f,$(SOURCES_ABS),$(dir $(f))))
  46. SOURCE_DIRS_REL := $(subst $(SDK_ROOT)/,,$(SOURCE_DIRS_ABS))
  47. OBJECTS_DIRS := $(addprefix $(OBJS_ROOT)/,$(SOURCE_DIRS_REL))
  48. # Filter source files list into separate source types.
  49. C_SOURCES = $(filter %.c,$(SOURCES_REL))
  50. CXX_SOURCES = $(filter %.cpp,$(SOURCES_REL))
  51. ASM_s_SOURCES = $(filter %.s,$(SOURCES_REL))
  52. ASM_S_SOURCES = $(filter %.S,$(SOURCES_REL))
  53. # Convert sources to objects.
  54. OBJECTS_C := $(addprefix $(OBJS_ROOT)/,$(C_SOURCES:.c=.o))
  55. OBJECTS_CXX := $(addprefix $(OBJS_ROOT)/,$(CXX_SOURCES:.cpp=.o))
  56. OBJECTS_ASM := $(addprefix $(OBJS_ROOT)/,$(ASM_s_SOURCES:.s=.o))
  57. OBJECTS_ASM_S := $(addprefix $(OBJS_ROOT)/,$(ASM_S_SOURCES:.S=.o))
  58. PREBUILT_OBJECTS = $(addprefix $(SDK_ROOT)/,$(filter %.o,$(SOURCES_REL)))
  59. # Complete list of all object files.
  60. OBJECTS_ALL := $(sort $(OBJECTS_C) $(OBJECTS_CXX) $(OBJECTS_ASM) $(OBJECTS_ASM_S) $(PREBUILT_OBJECTS))
  61. #-------------------------------------------------------------------------------
  62. # Target library
  63. #-------------------------------------------------------------------------------
  64. # Library where app objects are archived, if used.
  65. LIBAPP = $(APP_OUTPUT_ROOT)/lib$(APP_NAME).a
  66. # Build the target lib path from the lib name.
  67. ifneq "$(TARGET_LIB_NAME)" ""
  68. TARGET_LIB ?= $(LIBS_ROOT)/lib$(TARGET_LIB_NAME).a
  69. else ifeq "$(ARCHIVE_APP_OBJECTS)" "1"
  70. TARGET_LIB ?= $(LIBAPP)
  71. endif
  72. # Construct full path name to application output ELF file.
  73. ifneq "$(APP_NAME)" ""
  74. APP_ELF ?= $(APP_OUTPUT_ROOT)/$(APP_NAME).elf
  75. endif
  76. # Select the output target.
  77. ifneq "$(TARGET_LIB)" ""
  78. # Only use the target lib if there are actually objects to put into it.
  79. ifneq "$(strip $(OBJECTS_ALL))" ""
  80. archive_or_objs = $(TARGET_LIB)($(OBJECTS_ALL))
  81. endif
  82. else
  83. archive_or_objs = $(OBJECTS_ALL)
  84. endif
  85. #-------------------------------------------------------------------------------
  86. # Default target
  87. #-------------------------------------------------------------------------------
  88. # Note that prerequisite order is important here. The subdirectories must be built first, or you
  89. # may end up with files in the current directory not getting added to libraries. This would happen
  90. # if subdirs modified the library file after local files were compiled but before they were added
  91. # to the library.
  92. .PHONY: all
  93. all : $(SUBDIRS) $(archive_or_objs) $(APP_ELF)
  94. # Recipe to create the output object file directories.
  95. $(OBJECTS_DIRS) :
  96. $(at)mkdir -p $@
  97. # Everything depends upon the current makefile.
  98. $(OBJECTS_ALL) $(APP_ELF): $(this_makefile)
  99. # Object files depend on the directories where they will be created.
  100. #
  101. # The dirs are made order-only prerequisites (by being listed after the '|') so they won't cause
  102. # the objects to be rebuilt, as the modification date on a directory changes whenver its contents
  103. # change. This would cause the objects to always be rebuilt if the dirs were normal prerequisites.
  104. $(OBJECTS_ALL): | $(OBJECTS_DIRS)
  105. #-------------------------------------------------------------------------------
  106. # Pattern rules for compilation
  107. #-------------------------------------------------------------------------------
  108. # We cd into the source directory before calling the appropriate compiler. This must be done
  109. # on a single command line since make calls individual recipe lines in separate shells, so
  110. # '&&' is used to chain the commands.
  111. #
  112. # Generate make dependencies while compiling using the -MMD option, which excludes system headers.
  113. # If system headers are included, there are path problems on cygwin. The -MP option creates empty
  114. # targets for each header file so that a rebuild will be forced if the file goes missing, but
  115. # no error will occur.
  116. # Compile C sources.
  117. $(OBJS_ROOT)/%.o: $(SDK_ROOT)/%.c
  118. @$(call printmessage,c,Compiling, $(subst $(SDK_ROOT)/,,$<))
  119. $(at)cd $(dir $<) && $(CC) $(CFLAGS) $(SYSTEM_INC) $(INCLUDES) $(DEFINES) -MMD -MF $(basename $@).d -MP -o $@ -c $<
  120. # Compile C++ sources.
  121. $(OBJS_ROOT)/%.o: $(SDK_ROOT)/%.cpp
  122. @$(call printmessage,cxx,Compiling, $(subst $(SDK_ROOT)/,,$<))
  123. $(at)cd $(dir $<) && $(CXX) $(CXXFLAGS) $(SYSTEM_INC) $(INCLUDES) $(DEFINES) -MMD -MF $(basename $@).d -MP -o $@ -c $<
  124. # For .S assembly files, first run through the C preprocessor then assemble.
  125. $(OBJS_ROOT)/%.o: $(SDK_ROOT)/%.S
  126. @$(call printmessage,asm,Assembling, $(subst $(SDK_ROOT)/,,$<))
  127. $(at)cd $(dir $<) \
  128. && $(CPP) -D__LANGUAGE_ASM__ $(INCLUDES) $(DEFINES) -o $(basename $@).s $< \
  129. && $(AS) $(ASFLAGS) $(INCLUDES) -MD $(OBJS_ROOT)/$*.d -o $@ $(basename $@).s
  130. # Assembler sources.
  131. $(OBJS_ROOT)/%.o: $(SDK_ROOT)/%.s
  132. @$(call printmessage,asm,Assembling, $(subst $(SDK_ROOT)/,,$<))
  133. $(at)cd $(dir $<) && $(AS) $(ASFLAGS) $(INCLUDES) -MD $(basename $@).d -o $@ $<
  134. # Add objects to the target library.
  135. #
  136. # We use mkdir to explicitly ensure that the archive's directory exists before calling
  137. # the ar tool. The dir can't be made a dependancy because make will try to add it to the
  138. # archive.
  139. #
  140. # flock is used to protect the archive file from multiple processes trying to write to it
  141. # simultaneously, in case we're using parallel processes.
  142. #
  143. # The log message is disabled in order to reduce clutter in the build log, since you will get
  144. # one message for every file that is archived.
  145. $(TARGET_LIB)(%): %
  146. # @$(call printmessage,ar,Archiving, $(?F) in $(@F))
  147. $(at)mkdir -p $(dir $(@))
  148. $(at)flock $(@).lock $(AR) -rucs $@ $?
  149. #-------------------------------------------------------------------------------
  150. # Subdirs
  151. #-------------------------------------------------------------------------------
  152. # Recursively execute make in each of the subdirectories.
  153. # Subdirs are double-colon rules to allow additional recipes to be added to them.
  154. # This is used by the top-level makefile to print a message when starting to build
  155. # the sdk library.
  156. .PHONY: $(SUBDIRS)
  157. $(SUBDIRS)::
  158. @$(MAKE) $(silent_make) -r -C $@
  159. #-------------------------------------------------------------------------------
  160. # Linking
  161. #-------------------------------------------------------------------------------
  162. # Only link the application if LINK_APP is defined.
  163. ifeq "$(LINK_APP)" "1"
  164. # If app objects are being archived into a library, we don't need to specify the
  165. # actual .o files on the linker command line.
  166. ifeq "$(ARCHIVE_APP_OBJECTS)" "1"
  167. app_objs = $(TARGET_LIB)
  168. else
  169. app_objs = $(OBJECTS_ALL) $(TARGET_LIB)
  170. endif
  171. app_bin = $(basename $(APP_ELF)).bin
  172. app_map = $(basename $(APP_ELF)).map
  173. # Preprocess the linker script if it has an ".S" extension.
  174. ifeq "$(filter %.S,$(LD_FILE))" ""
  175. the_ld_file = $(LD_FILE)
  176. else
  177. rel_ld_file = $(basename $(subst $(SDK_ROOT)/,,$(abspath $(LD_FILE))))
  178. the_ld_file = $(addprefix $(OBJS_ROOT)/,$(rel_ld_file))
  179. the_ld_file_dir = $(dir $(the_ld_file))
  180. # Rule to preprocess the ld file. The ld file's parent directory is made an order-only
  181. # prerequisite so it cannot by itself cause this recipe to be invoked.
  182. $(the_ld_file): $(LD_FILE) | $(the_ld_file_dir)
  183. @$(call printmessage,cpp,Preprocessing, $(subst $(SDK_ROOT)/,,$<))
  184. $(at)cd $(dir $<) && $(CC) -E -P $(INCLUDES) $(DEFINES) -o $@ $<
  185. endif
  186. # Link the application.
  187. # Wrap the link objects in start/end group so that ld re-checks each
  188. # file for dependencies. Otherwise linking static libs can be a pain
  189. # since order matters.
  190. $(APP_ELF): $(SUBDIRS) $(app_objs) $(the_ld_file) $(LIBRARIES) $(APP_LIBS)
  191. @$(call printmessage,link,Linking, $(APP_NAME))
  192. $(at)$(LD) -Bstatic -nostartfiles -nostdlib $(LDFLAGS) \
  193. -T $(the_ld_file) \
  194. $(LDINC) \
  195. --start-group \
  196. $(app_objs) \
  197. $(LIBRARIES) \
  198. $(APP_LIBS) \
  199. $(LDADD) \
  200. --end-group \
  201. -o $@ \
  202. -Map $(app_map) --cref
  203. $(at)$(OBJCOPY) --gap-fill 0x00 -I elf32-little -O binary $@ $(app_bin)
  204. @echo "Output ELF:" ; echo " $(APP_ELF)"
  205. @echo "Output binary:" ; echo " $(app_bin)"
  206. else
  207. # Empty target to prevent an error. Needed because $(APP_ELF) is a prereq for the 'all' target.
  208. $(APP_ELF): ;
  209. endif
  210. # Include dependency files.
  211. -include $(OBJECTS_ALL:.o=.d)