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