xref: /openbmc/linux/tools/build/Makefile.build (revision b96fc2f3)
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
11PHONY := __build
12__build:
13
14ifeq ($(V),1)
15  quiet =
16  Q =
17else
18  quiet=quiet_
19  Q=@
20endif
21
22build-dir := $(srctree)/tools/build
23
24# Generic definitions
25include $(build-dir)/Build.include
26
27# do not force detected configuration
28-include $(OUTPUT).config-detected
29
30# Init all relevant variables used in build files so
31# 1) they have correct type
32# 2) they do not inherit any value from the environment
33subdir-y     :=
34obj-y        :=
35subdir-y     :=
36subdir-obj-y :=
37
38# Build definitions
39build-file := $(dir)/Build
40-include $(build-file)
41
42quiet_cmd_flex  = FLEX     $@
43quiet_cmd_bison = BISON    $@
44
45# Create directory unless it exists
46quiet_cmd_mkdir = MKDIR    $(dir $@)
47      cmd_mkdir = mkdir -p $(dir $@)
48     rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir))
49
50# Compile command
51quiet_cmd_cc_o_c = CC       $@
52      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
53
54quiet_cmd_cc_i_c = CPP      $@
55      cmd_cc_i_c = $(CC) $(c_flags) -E -o $@ $<
56
57quiet_cmd_cc_s_c = AS       $@
58      cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
59
60quiet_cmd_gen = GEN      $@
61
62# Link agregate command
63# If there's nothing to link, create empty $@ object.
64quiet_cmd_ld_multi = LD       $@
65      cmd_ld_multi = $(if $(strip $(obj-y)),\
66		       $(LD) -r -o $@  $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@)
67
68# Build rules
69$(OUTPUT)%.o: %.c FORCE
70	$(call rule_mkdir)
71	$(call if_changed_dep,cc_o_c)
72
73$(OUTPUT)%.o: %.S FORCE
74	$(call rule_mkdir)
75	$(call if_changed_dep,cc_o_c)
76
77$(OUTPUT)%.i: %.c FORCE
78	$(call rule_mkdir)
79	$(call if_changed_dep,cc_i_c)
80
81$(OUTPUT)%.i: %.S FORCE
82	$(call rule_mkdir)
83	$(call if_changed_dep,cc_i_c)
84
85$(OUTPUT)%.s: %.c FORCE
86	$(call rule_mkdir)
87	$(call if_changed_dep,cc_s_c)
88
89# Gather build data:
90#   obj-y        - list of build objects
91#   subdir-y     - list of directories to nest
92#   subdir-obj-y - list of directories objects 'dir/$(obj)-in.o'
93obj-y        := $($(obj)-y)
94subdir-y     := $(patsubst %/,%,$(filter %/, $(obj-y)))
95obj-y        := $(patsubst %/, %/$(obj)-in.o, $(obj-y))
96subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y))
97
98# '$(OUTPUT)/dir' prefix to all objects
99objprefix    := $(subst ./,,$(OUTPUT)$(dir)/)
100obj-y        := $(addprefix $(objprefix),$(obj-y))
101subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y))
102
103# Final '$(obj)-in.o' object
104in-target := $(objprefix)$(obj)-in.o
105
106PHONY += $(subdir-y)
107
108$(subdir-y):
109	$(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj)
110
111$(sort $(subdir-obj-y)): $(subdir-y) ;
112
113$(in-target): $(obj-y) FORCE
114	$(call rule_mkdir)
115	$(call if_changed,ld_multi)
116
117__build: $(in-target)
118	@:
119
120PHONY += FORCE
121FORCE:
122
123# Include all cmd files to get all the dependency rules
124# for all objects included
125targets   := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS)))
126cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
127
128ifneq ($(cmd_files),)
129  include $(cmd_files)
130endif
131
132.PHONY: $(PHONY)
133