Makefile.build 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_cc_i_c = CPP $@
  59. cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
  60. quiet_cmd_cc_s_c = AS $@
  61. cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
  62. quiet_cmd_gen = GEN $@
  63. # Link agregate command
  64. # If there's nothing to link, create empty $@ object.
  65. quiet_cmd_ld_multi = LD $@
  66. cmd_ld_multi = $(if $(strip $(obj-y)),\
  67. $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
  68. # Build rules
  69. $(OUTPUT)%.o: %.c FORCE
  70. $(call rule_mkdir)
  71. $(call if_changed_dep,cc_o_c)
  72. $(OUTPUT)%.o: %.S FORCE
  73. $(call rule_mkdir)
  74. $(call if_changed_dep,cc_o_c)
  75. $(OUTPUT)%.i: %.c FORCE
  76. $(call rule_mkdir)
  77. $(call if_changed_dep,cc_i_c)
  78. $(OUTPUT)%.i: %.S FORCE
  79. $(call rule_mkdir)
  80. $(call if_changed_dep,cc_i_c)
  81. $(OUTPUT)%.s: %.c FORCE
  82. $(call rule_mkdir)
  83. $(call if_changed_dep,cc_s_c)
  84. # Gather build data:
  85. # obj-y - list of build objects
  86. # subdir-y - list of directories to nest
  87. # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
  88. obj-y := $($(obj)-y)
  89. subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
  90. obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
  91. subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
  92. # '$(OUTPUT)/dir' prefix to all objects
  93. objprefix := $(subst ./,,$(OUTPUT)$(dir)/)
  94. obj-y := $(addprefix $(objprefix),$(obj-y))
  95. subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
  96. # Final '$(obj)-in.o' object
  97. in-target := $(objprefix)$(obj)-in.o
  98. PHONY += $(subdir-y)
  99. $(subdir-y):
  100. $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
  101. $(sort $(subdir-obj-y)): $(subdir-y) ;
  102. $(in-target): $(obj-y) FORCE
  103. $(call rule_mkdir)
  104. $(call if_changed,ld_multi)
  105. __build: $(in-target)
  106. @:
  107. PHONY += FORCE
  108. FORCE:
  109. # Include all cmd files to get all the dependency rules
  110. # for all objects included
  111. targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
  112. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  113. ifneq ($(cmd_files),)
  114. include $(cmd_files)
  115. endif
  116. .PHONY: $(PHONY)