Makefile.lib 6.7 KB

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