Makefile.build 3.1 KB
Newer Older
J
Jiri Olsa 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
###
# Main build makefile.
#
#  Lots of this code have been borrowed or heavily inspired from parts
#  of kbuild code, which is not credited, but mostly developed by:
#
#  Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
#  Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
#

PHONY := __build
__build:

ifeq ($(V),1)
  quiet =
16
  Q =
J
Jiri Olsa 已提交
17 18
else
  quiet=quiet_
19
  Q=@
J
Jiri Olsa 已提交
20 21 22 23 24 25 26
endif

build-dir := $(srctree)/tools/build

# Generic definitions
include $(build-dir)/Build.include

27
# do not force detected configuration
28
-include $(OUTPUT).config-detected
29

J
Jiri Olsa 已提交
30 31 32 33 34 35 36 37 38 39
# Init all relevant variables used in build files so
# 1) they have correct type
# 2) they do not inherit any value from the environment
subdir-y     :=
obj-y        :=
subdir-y     :=
subdir-obj-y :=

# Build definitions
build-file := $(dir)/Build
40
-include $(build-file)
J
Jiri Olsa 已提交
41

42 43 44
quiet_cmd_flex  = FLEX     $@
quiet_cmd_bison = BISON    $@

J
Jiri Olsa 已提交
45 46 47 48 49
# Create directory unless it exists
quiet_cmd_mkdir = MKDIR    $(dir $@)
      cmd_mkdir = mkdir -p $(dir $@)
     rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))

J
Jiri Olsa 已提交
50 51 52 53
# Compile command
quiet_cmd_cc_o_c = CC       $@
      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<

54 55 56 57 58 59
quiet_cmd_cc_i_c = CPP      $@
      cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<

quiet_cmd_cc_s_c = AS       $@
      cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<

60 61
quiet_cmd_gen = GEN      $@

J
Jiri Olsa 已提交
62 63 64 65 66 67 68 69
# Link agregate command
# If there's nothing to link, create empty $@ object.
quiet_cmd_ld_multi = LD       $@
      cmd_ld_multi = $(if $(strip $(obj-y)),\
		       $(LD) -r -o $@ $(obj-y),rm -f $@; $(AR) rcs $@)

# Build rules
$(OUTPUT)%.o: %.c FORCE
J
Jiri Olsa 已提交
70
	$(call rule_mkdir)
J
Jiri Olsa 已提交
71 72 73
	$(call if_changed_dep,cc_o_c)

$(OUTPUT)%.o: %.S FORCE
J
Jiri Olsa 已提交
74
	$(call rule_mkdir)
J
Jiri Olsa 已提交
75 76
	$(call if_changed_dep,cc_o_c)

77 78 79 80 81 82 83 84 85 86 87 88
$(OUTPUT)%.i: %.c FORCE
	$(call rule_mkdir)
	$(call if_changed_dep,cc_i_c)

$(OUTPUT)%.i: %.S FORCE
	$(call rule_mkdir)
	$(call if_changed_dep,cc_i_c)

$(OUTPUT)%.s: %.c FORCE
	$(call rule_mkdir)
	$(call if_changed_dep,cc_s_c)

J
Jiri Olsa 已提交
89 90 91 92 93 94 95 96 97 98
# Gather build data:
#   obj-y        - list of build objects
#   subdir-y     - list of directories to nest
#   subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
obj-y        := $($(obj)-y)
subdir-y     := $(patsubst %/,%,$(filter %/, $(obj-y)))
obj-y        := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))

# '$(OUTPUT)/dir' prefix to all objects
99 100 101
objprefix    := $(subst ./,,$(OUTPUT)$(dir)/)
obj-y        := $(addprefix $(objprefix),$(obj-y))
subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
J
Jiri Olsa 已提交
102 103

# Final '$(obj)-in.o' object
104
in-target := $(objprefix)$(obj)-in.o
J
Jiri Olsa 已提交
105 106 107 108

PHONY += $(subdir-y)

$(subdir-y):
109
	$(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
J
Jiri Olsa 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

$(sort $(subdir-obj-y)): $(subdir-y) ;

$(in-target): $(obj-y) FORCE
	$(call rule_mkdir)
	$(call if_changed,ld_multi)

__build: $(in-target)
	@:

PHONY += FORCE
FORCE:

# Include all cmd files to get all the dependency rules
# for all objects included
125
targets   := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
J
Jiri Olsa 已提交
126 127 128 129 130 131 132
cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))

ifneq ($(cmd_files),)
  include $(cmd_files)
endif

.PHONY: $(PHONY)