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