Makefile.lib 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Figure out what we need to build from the various variables
  2. # ===========================================================================
  3. # When an object is listed to be built compiled-in and modular,
  4. # only build the compiled-in version
  5. obj-m := $(filter-out $(obj-y),$(obj-m))
  6. # Libraries are always collected in one lib file.
  7. # Filter out objects already built-in
  8. lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
  9. # Handle objects in subdirs
  10. # ---------------------------------------------------------------------------
  11. # o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
  12. # and add the directory to the list of dirs to descend into: $(subdir-y)
  13. # o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
  14. # and add the directory to the list of dirs to descend into: $(subdir-m)
  15. __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
  16. subdir-y += $(__subdir-y)
  17. __subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
  18. subdir-m += $(__subdir-m)
  19. obj-y := $(patsubst %/, %/built-in.o, $(obj-y))
  20. obj-m := $(filter-out %/, $(obj-m))
  21. # Subdirectories we need to descend into
  22. subdir-ym := $(sort $(subdir-y) $(subdir-m))
  23. # if $(foo-objs) exists, foo.o is a composite object
  24. multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
  25. multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
  26. multi-used := $(multi-used-y) $(multi-used-m)
  27. single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
  28. # Build list of the parts of our composite objects, our composite
  29. # objects depend on those (obviously)
  30. multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
  31. multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
  32. multi-objs := $(multi-objs-y) $(multi-objs-m)
  33. # $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
  34. # tell kbuild to descend
  35. subdir-obj-y := $(filter %/built-in.o, $(obj-y))
  36. # $(obj-dirs) is a list of directories that contain object files
  37. obj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
  38. # Replace multi-part objects by their individual parts, look at local dir only
  39. real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y)
  40. real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
  41. # Add subdir path
  42. extra-y := $(addprefix $(obj)/,$(extra-y))
  43. always := $(addprefix $(obj)/,$(always))
  44. targets := $(addprefix $(obj)/,$(targets))
  45. obj-y := $(addprefix $(obj)/,$(obj-y))
  46. obj-m := $(addprefix $(obj)/,$(obj-m))
  47. lib-y := $(addprefix $(obj)/,$(lib-y))
  48. subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y))
  49. real-objs-y := $(addprefix $(obj)/,$(real-objs-y))
  50. real-objs-m := $(addprefix $(obj)/,$(real-objs-m))
  51. single-used-m := $(addprefix $(obj)/,$(single-used-m))
  52. multi-used-y := $(addprefix $(obj)/,$(multi-used-y))
  53. multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
  54. multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))
  55. multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))
  56. subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
  57. obj-dirs := $(addprefix $(obj)/,$(obj-dirs))
  58. # These flags are needed for modversions and compiling, so we define them here
  59. # already
  60. # $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
  61. # end up in (or would, if it gets compiled in)
  62. # Note: It's possible that one object gets potentially linked into more
  63. # than one module. In that case KBUILD_MODNAME will be set to foo_bar,
  64. # where foo and bar are the name of the modules.
  65. name-fix = $(subst $(comma),_,$(subst -,_,$1))
  66. basename_flags = -D"KBUILD_BASENAME=KBUILD_STR($(call name-fix,$(basetarget)))"
  67. modname_flags = $(if $(filter 1,$(words $(modname))),\
  68. -D"KBUILD_MODNAME=KBUILD_STR($(call name-fix,$(modname)))")
  69. _c_flags = $(KBUILD_CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(basetarget).o)
  70. _a_flags = $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(basetarget).o)
  71. _cpp_flags = $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS_$(@F))
  72. # If building the kernel in a separate objtree expand all occurrences
  73. # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
  74. ifeq ($(KBUILD_SRC),)
  75. __c_flags = $(_c_flags)
  76. __a_flags = $(_a_flags)
  77. __cpp_flags = $(_cpp_flags)
  78. else
  79. # -I$(obj) locates generated .h files
  80. # $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
  81. # and locates generated .h files
  82. # FIXME: Replace both with specific CFLAGS* statements in the makefiles
  83. __c_flags = $(call addtree,-I$(obj)) $(call flags,_c_flags)
  84. __a_flags = $(call flags,_a_flags)
  85. __cpp_flags = $(call flags,_cpp_flags)
  86. endif
  87. c_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
  88. $(__c_flags) $(modkern_cflags) \
  89. -D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags)
  90. a_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
  91. $(__a_flags) $(modkern_aflags)
  92. cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
  93. ld_flags = $(LDFLAGS) $(EXTRA_LDFLAGS)
  94. # Finds the multi-part object the current object will be linked into
  95. modname-multi = $(sort $(foreach m,$(multi-used),\
  96. $(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
  97. # Shipped files
  98. # ===========================================================================
  99. quiet_cmd_shipped = SHIPPED $@
  100. cmd_shipped = cat $< > $@
  101. $(obj)/%:: $(src)/%_shipped
  102. $(call cmd,shipped)
  103. # Commands useful for building a boot image
  104. # ===========================================================================
  105. #
  106. # Use as following:
  107. #
  108. # target: source(s) FORCE
  109. # $(if_changed,ld/objcopy/gzip)
  110. #
  111. # and add target to extra-y so that we know we have to
  112. # read in the saved command line
  113. # Linking
  114. # ---------------------------------------------------------------------------
  115. quiet_cmd_ld = LD $@
  116. cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
  117. $(filter-out FORCE,$^) -o $@
  118. # Objcopy
  119. # ---------------------------------------------------------------------------
  120. quiet_cmd_objcopy = OBJCOPY $@
  121. cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
  122. # Gzip
  123. # ---------------------------------------------------------------------------
  124. quiet_cmd_gzip = GZIP $@
  125. cmd_gzip = gzip -f -9 < $< > $@