Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 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 | |
| 11 | PHONY := __build |
| 12 | __build: |
| 13 | |
| 14 | ifeq ($(V),1) |
| 15 | quiet = |
Jiri Olsa | ceed252 | 2015-01-11 23:59:55 +0100 | [diff] [blame] | 16 | Q = |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 17 | else |
| 18 | quiet=quiet_ |
Jiri Olsa | ceed252 | 2015-01-11 23:59:55 +0100 | [diff] [blame] | 19 | Q=@ |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 20 | endif |
| 21 | |
Josh Poimboeuf | e572d08 | 2017-01-18 22:16:55 -0600 | [diff] [blame] | 22 | ifneq ($(filter 4.%,$(MAKE_VERSION)),) # make-4 |
| 23 | ifneq ($(filter %s ,$(firstword x$(MAKEFLAGS))),) |
| 24 | quiet=silent_ |
| 25 | endif |
| 26 | else # make-3.8x |
| 27 | ifneq ($(filter s% -s%,$(MAKEFLAGS)),) |
| 28 | quiet=silent_ |
| 29 | endif |
| 30 | endif |
| 31 | |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 32 | build-dir := $(srctree)/tools/build |
| 33 | |
Jiri Olsa | 9fb81323 | 2015-09-23 12:34:00 +0200 | [diff] [blame] | 34 | # Define $(fixdep) for dep-cmd function |
| 35 | ifeq ($(OUTPUT),) |
| 36 | fixdep := $(build-dir)/fixdep |
| 37 | else |
| 38 | fixdep := $(OUTPUT)/fixdep |
| 39 | endif |
| 40 | |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 41 | # Generic definitions |
| 42 | include $(build-dir)/Build.include |
| 43 | |
Jiri Olsa | fcfd661 | 2014-12-31 17:37:00 +0100 | [diff] [blame] | 44 | # do not force detected configuration |
Aaro Koskinen | 6422737 | 2015-07-01 14:54:42 +0300 | [diff] [blame] | 45 | -include $(OUTPUT).config-detected |
Jiri Olsa | fcfd661 | 2014-12-31 17:37:00 +0100 | [diff] [blame] | 46 | |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 47 | # Init all relevant variables used in build files so |
| 48 | # 1) they have correct type |
| 49 | # 2) they do not inherit any value from the environment |
| 50 | subdir-y := |
| 51 | obj-y := |
| 52 | subdir-y := |
| 53 | subdir-obj-y := |
| 54 | |
| 55 | # Build definitions |
| 56 | build-file := $(dir)/Build |
Jiri Olsa | d7a3d85 | 2015-05-29 17:42:58 +0200 | [diff] [blame] | 57 | -include $(build-file) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 58 | |
Jiri Olsa | 9352aab | 2014-12-29 17:42:46 +0100 | [diff] [blame] | 59 | quiet_cmd_flex = FLEX $@ |
| 60 | quiet_cmd_bison = BISON $@ |
| 61 | |
Jiri Olsa | 579ff6d | 2014-12-30 16:44:11 +0100 | [diff] [blame] | 62 | # Create directory unless it exists |
| 63 | quiet_cmd_mkdir = MKDIR $(dir $@) |
| 64 | cmd_mkdir = mkdir -p $(dir $@) |
| 65 | rule_mkdir = $(if $(wildcard $(dir $@)),,@$(call echo-cmd,mkdir) $(cmd_mkdir)) |
| 66 | |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 67 | # Compile command |
| 68 | quiet_cmd_cc_o_c = CC $@ |
| 69 | cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< |
| 70 | |
Jiri Olsa | 0c3b7e4 | 2016-09-27 16:18:46 +0200 | [diff] [blame] | 71 | quiet_cmd_host_cc_o_c = HOSTCC $@ |
| 72 | cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $< |
| 73 | |
Wang Nan | f61bdc3 | 2016-09-26 07:26:55 +0000 | [diff] [blame] | 74 | quiet_cmd_cxx_o_c = CXX $@ |
| 75 | cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $< |
| 76 | |
Masahiro Yamada | 23d4384 | 2016-04-08 11:24:47 +0900 | [diff] [blame] | 77 | quiet_cmd_cpp_i_c = CPP $@ |
| 78 | cmd_cpp_i_c = $(CC) $(c_flags) -E -o $@ $< |
Jiri Olsa | 1999307 | 2014-12-30 18:44:38 +0100 | [diff] [blame] | 79 | |
| 80 | quiet_cmd_cc_s_c = AS $@ |
| 81 | cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $< |
| 82 | |
Adrian Hunter | 237fae7 | 2015-08-13 10:14:55 +0300 | [diff] [blame] | 83 | quiet_cmd_gen = GEN $@ |
| 84 | |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 85 | # Link agregate command |
| 86 | # If there's nothing to link, create empty $@ object. |
| 87 | quiet_cmd_ld_multi = LD $@ |
| 88 | cmd_ld_multi = $(if $(strip $(obj-y)),\ |
Jiri Olsa | 0c3b7e4 | 2016-09-27 16:18:46 +0200 | [diff] [blame] | 89 | $(LD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(AR) rcs $@) |
| 90 | |
| 91 | quiet_cmd_host_ld_multi = HOSTLD $@ |
| 92 | cmd_host_ld_multi = $(if $(strip $(obj-y)),\ |
| 93 | $(HOSTLD) -r -o $@ $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@) |
| 94 | |
| 95 | ifneq ($(filter $(obj),$(hostprogs)),) |
| 96 | host = host_ |
| 97 | endif |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 98 | |
| 99 | # Build rules |
| 100 | $(OUTPUT)%.o: %.c FORCE |
Jiri Olsa | 579ff6d | 2014-12-30 16:44:11 +0100 | [diff] [blame] | 101 | $(call rule_mkdir) |
Jiri Olsa | 0c3b7e4 | 2016-09-27 16:18:46 +0200 | [diff] [blame] | 102 | $(call if_changed_dep,$(host)cc_o_c) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 103 | |
Wang Nan | f61bdc3 | 2016-09-26 07:26:55 +0000 | [diff] [blame] | 104 | $(OUTPUT)%.o: %.cpp FORCE |
| 105 | $(call rule_mkdir) |
| 106 | $(call if_changed_dep,cxx_o_c) |
| 107 | |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 108 | $(OUTPUT)%.o: %.S FORCE |
Jiri Olsa | 579ff6d | 2014-12-30 16:44:11 +0100 | [diff] [blame] | 109 | $(call rule_mkdir) |
Jiri Olsa | 0c3b7e4 | 2016-09-27 16:18:46 +0200 | [diff] [blame] | 110 | $(call if_changed_dep,$(host)cc_o_c) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 111 | |
Jiri Olsa | 1999307 | 2014-12-30 18:44:38 +0100 | [diff] [blame] | 112 | $(OUTPUT)%.i: %.c FORCE |
| 113 | $(call rule_mkdir) |
Masahiro Yamada | 23d4384 | 2016-04-08 11:24:47 +0900 | [diff] [blame] | 114 | $(call if_changed_dep,cpp_i_c) |
Jiri Olsa | 1999307 | 2014-12-30 18:44:38 +0100 | [diff] [blame] | 115 | |
Masahiro Yamada | 6767879 | 2016-02-01 02:59:00 +0900 | [diff] [blame] | 116 | $(OUTPUT)%.s: %.S FORCE |
Jiri Olsa | 1999307 | 2014-12-30 18:44:38 +0100 | [diff] [blame] | 117 | $(call rule_mkdir) |
Masahiro Yamada | 23d4384 | 2016-04-08 11:24:47 +0900 | [diff] [blame] | 118 | $(call if_changed_dep,cpp_i_c) |
Jiri Olsa | 1999307 | 2014-12-30 18:44:38 +0100 | [diff] [blame] | 119 | |
| 120 | $(OUTPUT)%.s: %.c FORCE |
| 121 | $(call rule_mkdir) |
| 122 | $(call if_changed_dep,cc_s_c) |
| 123 | |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 124 | # Gather build data: |
| 125 | # obj-y - list of build objects |
| 126 | # subdir-y - list of directories to nest |
| 127 | # subdir-obj-y - list of directories objects 'dir/$(obj)-in.o' |
| 128 | obj-y := $($(obj)-y) |
| 129 | subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) |
| 130 | obj-y := $(patsubst %/, %/$(obj)-in.o, $(obj-y)) |
| 131 | subdir-obj-y := $(filter %/$(obj)-in.o, $(obj-y)) |
| 132 | |
| 133 | # '$(OUTPUT)/dir' prefix to all objects |
Lukas Wunner | 75e84ab | 2015-06-18 13:00:32 +0200 | [diff] [blame] | 134 | objprefix := $(subst ./,,$(OUTPUT)$(dir)/) |
| 135 | obj-y := $(addprefix $(objprefix),$(obj-y)) |
| 136 | subdir-obj-y := $(addprefix $(objprefix),$(subdir-obj-y)) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 137 | |
| 138 | # Final '$(obj)-in.o' object |
Lukas Wunner | 75e84ab | 2015-06-18 13:00:32 +0200 | [diff] [blame] | 139 | in-target := $(objprefix)$(obj)-in.o |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 140 | |
| 141 | PHONY += $(subdir-y) |
| 142 | |
| 143 | $(subdir-y): |
Jiri Olsa | ceed252 | 2015-01-11 23:59:55 +0100 | [diff] [blame] | 144 | $(Q)$(MAKE) -f $(build-dir)/Makefile.build dir=$(dir)/$@ obj=$(obj) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 145 | |
| 146 | $(sort $(subdir-obj-y)): $(subdir-y) ; |
| 147 | |
| 148 | $(in-target): $(obj-y) FORCE |
| 149 | $(call rule_mkdir) |
Jiri Olsa | 0c3b7e4 | 2016-09-27 16:18:46 +0200 | [diff] [blame] | 150 | $(call if_changed,$(host)ld_multi) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 151 | |
| 152 | __build: $(in-target) |
| 153 | @: |
| 154 | |
| 155 | PHONY += FORCE |
| 156 | FORCE: |
| 157 | |
| 158 | # Include all cmd files to get all the dependency rules |
| 159 | # for all objects included |
Jiri Olsa | 1999307 | 2014-12-30 18:44:38 +0100 | [diff] [blame] | 160 | targets := $(wildcard $(sort $(obj-y) $(in-target) $(MAKECMDGOALS))) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 161 | cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd)) |
| 162 | |
| 163 | ifneq ($(cmd_files),) |
| 164 | include $(cmd_files) |
| 165 | endif |
| 166 | |
| 167 | .PHONY: $(PHONY) |