Makefile.build 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ###
  2. # Main build makefile.
  3. #
  4. # Lots of this code have been borrowed or heavily inspired from parts
  5. # of kbuild code, which is not credited, but mostly developed by:
  6. #
  7. # Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
  8. # Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
  9. #
  10. PHONY := __build
  11. __build:
  12. ifeq ($(V),1)
  13. quiet =
  14. Q =
  15. else
  16. quiet=quiet_
  17. Q=@
  18. endif
  19. ifneq ($(filter 4.%,$(MAKE_VERSION)),) # make-4
  20. ifneq ($(filter %s ,$(firstword x$(MAKEFLAGS))),)
  21. quiet=silent_
  22. endif
  23. else # make-3.8x
  24. ifneq ($(filter s% -s%,$(MAKEFLAGS)),)
  25. quiet=silent_
  26. endif
  27. endif
  28. build-dir := $(srctree)/tools/build
  29. # Define $(fixdep) for dep-cmd function
  30. ifeq ($(OUTPUT),)
  31. fixdep := $(build-dir)/fixdep
  32. else
  33. fixdep := $(OUTPUT)/fixdep
  34. endif
  35. # Generic definitions
  36. include $(build-dir)/Build.include
  37. # do not force detected configuration
  38. -include $(OUTPUT).config-detected
  39. # Init all relevant variables used in build files so
  40. # 1) they have correct type
  41. # 2) they do not inherit any value from the environment
  42. subdir-y :=
  43. obj-y :=
  44. subdir-y :=
  45. subdir-obj-y :=
  46. # Build definitions
  47. build-file := $(dir)/Build
  48. -include $(build-file)
  49. quiet_cmd_flex = FLEX $@
  50. quiet_cmd_bison = BISON $@
  51. # Create directory unless it exists
  52. quiet_cmd_mkdir = MKDIR $(dir $@)
  53. cmd_mkdir = mkdir -p $(dir $@)
  54. rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
  55. # Compile command
  56. quiet_cmd_cc_o_c = CC $@
  57. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  58. quiet_cmd_host_cc_o_c = HOSTCC $@
  59. cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
  60. quiet_cmd_cxx_o_c = CXX $@
  61. cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
  62. quiet_cmd_cpp_i_c = CPP $@
  63. cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $<
  64. quiet_cmd_cc_s_c = AS $@
  65. cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
  66. quiet_cmd_gen = GEN $@
  67. # Link agregate command
  68. # If there's nothing to link, create empty $@ object.
  69. quiet_cmd_ld_multi = LD $@
  70. cmd_ld_multi = $(if $(strip $(obj-y)),\
  71. $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
  72. quiet_cmd_host_ld_multi = HOSTLD $@
  73. cmd_host_ld_multi = $(if $(strip $(obj-y)),\
  74. $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
  75. ifneq ($(filter $(obj),$(hostprogs)),)
  76. host = host_
  77. endif
  78. # Build rules
  79. $(OUTPUT)%.o: %.c FORCE
  80. $(call rule_mkdir)
  81. $(call if_changed_dep,$(host)cc_o_c)
  82. $(OUTPUT)%.o: %.cpp FORCE
  83. $(call rule_mkdir)
  84. $(call if_changed_dep,cxx_o_c)
  85. $(OUTPUT)%.o: %.S FORCE
  86. $(call rule_mkdir)
  87. $(call if_changed_dep,$(host)cc_o_c)
  88. $(OUTPUT)%.i: %.c FORCE
  89. $(call rule_mkdir)
  90. $(call if_changed_dep,cpp_i_c)
  91. $(OUTPUT)%.s: %.S FORCE
  92. $(call rule_mkdir)
  93. $(call if_changed_dep,cpp_i_c)
  94. $(OUTPUT)%.s: %.c FORCE
  95. $(call rule_mkdir)
  96. $(call if_changed_dep,cc_s_c)
  97. # Gather build data:
  98. # obj-y - list of build objects
  99. # subdir-y - list of directories to nest
  100. # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
  101. obj-y := $($(obj)-y)
  102. subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
  103. obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
  104. subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
  105. # '$(OUTPUT)/dir' prefix to all objects
  106. objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
  107. obj-y := $(addprefix $(objprefix),$(obj-y))
  108. subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
  109. # Final '$(obj)-in.o' object
  110. in-target := $(objprefix)$(obj)-in.o
  111. PHONY += $(subdir-y)
  112. $(subdir-y):
  113. $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
  114. $(sort $(subdir-obj-y)): $(subdir-y) ;
  115. $(in-target): $(obj-y) FORCE
  116. $(call rule_mkdir)
  117. $(call if_changed,$(host)ld_multi)
  118. __build: $(in-target)
  119. @:
  120. PHONY += FORCE
  121. FORCE:
  122. # Include all cmd files to get all the dependency rules
  123. # for all objects included
  124. targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
  125. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  126. ifneq ($(cmd_files),)
  127. include $(cmd_files)
  128. endif
  129. .PHONY: $(PHONY)