Support separate kbuild source and output directories.
[ksplice.git] / Makefile.ksplice
blob8175dc0a4711137e11accf87b2894f87d514f82c
1 ksplice-makefile := $(word $(words $(MAKEFILE_LIST)), $(MAKEFILE_LIST))
2 ksplice-script = $(dir $(ksplice-makefile))ksplice-obj.pl
4 KSPLICE_MODE ?= diff
5 $(if $(filter_out snap diff revert modinst,$(KSPLICE_MODE)), \
6   $(error Invalid KSPLICE_MODE $(KSPLICE_MODE).))
8 KSPLICE_ONLY_TARGETS ?= %
10 PHONY :=
12 __ksplice:
14 ksplice-extra = $(filter $(KSPLICE_EXTRA_MATCH),$(lib-y) $(real-objs-m) $(real-objs-y))
15 ksplice-objs = $(foreach o,$(1),$(o:=.KSPLICE) $(addsuffix .KSPLICE_helper,$(filter $(ksplice-extra),$(o))))
17 escsq ?= $(subst ','\'',$(1))
18 echo-cmd ?= $(if $($(quiet)cmd_$(1)),echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
20 quiet_cmd_ksplice-combine = COMBINE $(@:.KSPLICE=)
21 cmd_ksplice-combine = $(ksplice-script) combine $@ $(filter $(call ksplice-objs,$(ksplice-link-deps)),$^)
22 quiet_cmd_ksplice-snap = SNAP    $(@:.KSPLICE=)
23 cmd_ksplice-snap = $(ksplice-script) snap $@
24 quiet_cmd_ksplice-diff = DIFF    $(@:.KSPLICE=)
25 cmd_ksplice-diff = $(ksplice-script) diff $@
26 quiet_cmd_ksplice-ignore = IGNORE  $(@:.KSPLICE=)
27 cmd_ksplice-ignore = touch $@
28 quiet_cmd_ksplice-cow = COW     $@
29 cmd_ksplice-cow = cp -a $@ $@.KSPLICE_pre
30 quiet_cmd_ksplice-mod = MOD     $(@:$(KSPLICE_KMODSRC)/%.mod.KSPLICE=%)
31 cmd_ksplice-mod = echo $(<:.o.KSPLICE=) > $@; cp -a $< $(<:.KSPLICE=.KSPLICE_primary) $(<:.KSPLICE=.KSPLICE_helper) $(KSPLICE_KMODSRC)/
32 rule_ksplice-mod = if [ -s $< ]; then $(echo-cmd) $(cmd_$(1)); fi
33 quiet_cmd_ksplice-helper = HELPER  $(@:.KSPLICE_helper=)
34 cmd_ksplice-helper = $(ksplice-script) helper $@
35 quiet_cmd_ksplice-freeze = FREEZE  $(@:_ksplice-revert_%.KSPLICE_pre=%)
36 cmd_ksplice-freeze = rm -f $(@:_ksplice-revert_%=%)
37 quiet_cmd_ksplice-revert = REVERT  $(@:_ksplice-revert_%.KSPLICE_pre=%)
38 cmd_ksplice-revert = mv $(@:_ksplice-revert_%=%) $(@:_ksplice-revert_%.KSPLICE_pre=%)
39 quiet_cmd_ksplice-revert-snap = SNAP    $(@:_ksplice-revert-snap_%.KSPLICE=%)
40 cmd_ksplice-revert-snap = $(ksplice-script) snap $(@:_ksplice-revert-snap_%=%)
41 quiet_cmd_ksplice-clean = CLEAN   $(@:_ksplice-clean_%=%)
42 cmd_ksplice-clean = rm -f $(@:_ksplice-clean_%=%)
44 ifeq ($(obj),)
46 ifneq ($(wildcard include/linux/compile.h),)
47 MAKE += --old-file=include/linux/compile.h
48 endif
49 ifneq ($(filter snap diff,$(KSPLICE_MODE)),)
50 # Makefile.lib must be included before Makefile because they contain
51 # different filechk definitions in 2.6.12.
52 include $(if $(KBUILD_SRC),$(KBUILD_SRC)/)scripts/Makefile.lib
53 endif
54 include $(if $(KBUILD_SRC),$(KBUILD_SRC)/)Makefile
56 ifneq ($(skip-makefile),)
58 PHONY += __ksplice
59 __ksplice: FORCE
60         $(if $(KBUILD_VERBOSE:1=),@)$(MAKE) -C $(KBUILD_OUTPUT) \
61         KBUILD_SRC=$(CURDIR) \
62         KBUILD_EXTMOD="$(KBUILD_EXTMOD)" -f $(ksplice-makefile) \
63         $(if $(wildcard $(KBUILD_OUTPUT)/include/config/kernel.release),--old-file=include/config/kernel.release)
65 else    # skip-makefile
67 CC := ksplice-cc.pl $(CC)
68 CFLAGS_KSPLICE = -ffunction-sections -fdata-sections -ksplice-cflags-api=1
69 CFLAGS_KERNEL += $(CFLAGS_KSPLICE)
70 CFLAGS_MODULE += $(CFLAGS_KSPLICE)
72 ifeq ($(KSPLICE_MODE),revert)
74 ksplice-dirs += $(vmlinux-alldirs:%=_ksplice_%)
76 endif   # KSPLICE_MODE
78 ifneq ($(filter snap diff,$(KSPLICE_MODE)),)
80 ksplice-mods += vmlinux
81 ifdef KSPLICE_BUILD_MODULES
82 ksplice-deps += ksplice_modpost
83 endif
84 ksplice-dirs += $(vmlinux-dirs:%=_ksplice_%)
85 ifeq ($(KSPLICE_MODE),snap)
86 ksplice-before += prepare0 prepare scripts
87 endif
89 ksplice-vmlinux-objs = $(filter-out $(head-y),$(if $(vmlinux-all),$(vmlinux-all),$(vmlinux-objs)))
90 $(obj)/vmlinux.o.KSPLICE: ksplice-link-deps = $(ksplice-vmlinux-objs)
91 $(obj)/vmlinux.o.KSPLICE: $(call ksplice-objs,$(ksplice-vmlinux-objs)) FORCE
92         $(call if_changed,ksplice-combine)
93 ksplice-targets += $(obj)/vmlinux.o.KSPLICE
94 $(ksplice-vmlinux-objs:=.KSPLICE): $(ksplice-dirs) ;
96 PHONY += ksplice_modpost
97 ksplice_modpost: $(ksplice-dirs)
98         $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
100 endif   # KSPLICE_MODE
102 ifeq ($(KSPLICE_MODE),modinst)
104 ksplice-deps += ksplice_modinst
105 PHONY += ksplice_modinst
106 ksplice_modinst: $(ksplice-dirs)
107         $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modinst
109 endif   # KSPLICE_MODE
111 endif   # skip-makefile
113 else    # obj
115 ifeq ($(KSPLICE_MODE),revert)
117 -include .config        # workaround for missing obj- = subdir/ declarations
118 include $(srctree)/scripts/Makefile.clean
119 ksplice-dirs += $(subdir-ymn:%=_ksplice_%)
121 endif   # KSPLICE_MODE
123 ifneq ($(filter snap diff,$(KSPLICE_MODE)),)
125 ifdef KSPLICE_BUILD_MODULES
126 KBUILD_MODULES = 1
127 endif
129 include $(srctree)/scripts/Makefile.build
130 ksplice-mods += $(obj-m:.o=)
131 ksplice-deps += $(if $(filter $(KSPLICE_ONLY_TARGETS),vmlinux),$(builtin-target:=.KSPLICE) $(lib-target:=.KSPLICE))
132 ifdef KSPLICE_BUILD_MODULES
133 ksplice-deps += $(obj-m)
134 endif
135 ksplice-dirs += $(subdir-ym:%=_ksplice_%)
137 ifdef builtin-target
138 $(builtin-target:=.KSPLICE): ksplice-link-deps = $(obj-y)
139 $(builtin-target:=.KSPLICE): $(call ksplice-objs,$(obj-y)) FORCE
140         $(call if_changed,ksplice-combine)
141 ksplice-targets += $(builtin-target:=.KSPLICE)
142 endif
144 ifdef lib-target
145 $(lib-target:=.KSPLICE): ksplice-link-deps = $(lib-y)
146 $(lib-target:=.KSPLICE): $(call ksplice-objs,$(lib-y)) FORCE
147         $(call if_changed,ksplice-combine)
148 ksplice-targets += $(lib-target:=.KSPLICE)
149 endif
151 $(sort $(multi-used-y:=.KSPLICE) $(multi-used-m:=.KSPLICE)): ksplice-link-deps = $($(@:$(obj)/%.o.KSPLICE=%-objs):%=$(obj)/%) $($(@:$(obj)/%.o.KSPLICE=%-y):%=$(obj)/%)
152 $(sort $(multi-used-y:=.KSPLICE)): $(obj)/%.o.KSPLICE: $(call ksplice-objs,$(multi-objs-y)) FORCE
153         $(call if_changed,ksplice-combine)
154 $(sort $(multi-used-m:=.KSPLICE)): $(obj)/%.o.KSPLICE: $(call ksplice-objs,$(multi-objs-m)) FORCE
155         $(call if_changed,ksplice-combine)
156 ksplice-targets += $(sort $(multi-used-y:=.KSPLICE) $(multi-used-m:=.KSPLICE))
158 ifeq ($(KSPLICE_MODE),snap)
159 $(obj)/%.o.KSPLICE: $(obj)/%.o FORCE
160         $(if $(strip $(wildcard $<.KSPLICE_pre) $(filter $<,$?)), \
161           $(call cmd,ksplice-snap))
162 else
163 $(obj)/%.o.KSPLICE: $(obj)/%.o
164         $(call cmd,ksplice-diff)
165 endif
167 $(obj)/%.lds.KSPLICE:
168         $(call cmd,ksplice-ignore)
170 $(sort $(subdir-obj-y:=.KSPLICE)): $(ksplice-dirs) ;
172 endif   # KSPLICE_MODE
174 endif   # obj
176 ifeq ($(skip-makefile),)
178 ifeq ($(KSPLICE_MODE),revert)
180 ksplice-revert-obj := $(wildcard $(obj)/*.KSPLICE_pre)
181 ksplice-revert-files := $(ksplice-revert-obj)
182 ifneq ($(obj),$(src))
183 ksplice-revert-files += $(wildcard $(src)/*.KSPLICE_pre)
184 endif
185 ksplice-revert-deps := $(ksplice-revert-files:%=_ksplice-revert_%)
186 ksplice-deps += $(ksplice-revert-deps)
187 PHONY += $(ksplice-revert-deps)
188 $(ksplice-revert-deps): FORCE
189 ifdef KSPLICE_SERIES
190         $(call cmd,ksplice-freeze)
191 else
192         $(call cmd,ksplice-revert)
193 endif
195 ksplice-revert-snap-files := $(wildcard $(ksplice-revert-obj:.KSPLICE_pre=.KSPLICE))
196 ksplice-revert-snap-deps := $(ksplice-revert-snap-files:%=_ksplice-revert-snap_%)
197 ksplice-deps += $(ksplice-revert-snap-deps)
198 PHONY += $(ksplice-revert-snap-deps)
199 $(ksplice-revert-snap-deps): $(ksplice-revert-snap-files:%.KSPLICE=_ksplice-revert_%.KSPLICE_pre) FORCE
200         $(call cmd,ksplice-revert-snap)
202 ksplice-clean-files := $(filter-out $(ksplice-revert-snap-files:.KSPLICE_pre=.KSPLICE_primary) $(ksplice-revert-snap-files:.KSPLICE_pre=.KSPLICE_helper),$(wildcard $(obj)/*.KSPLICE_primary $(obj)/*.KSPLICE_helper))
203 ksplice-clean-deps = $(ksplice-clean-files:%=_ksplice-clean_%)
204 ksplice-deps += $(ksplice-clean-deps)
205 PHONY += $(ksplice-clean-deps)
206 $(ksplice-clean-deps): FORCE
207         $(call cmd,ksplice-clean)
209 endif   # KSPLICE_MODE
211 ifneq ($(filter snap diff,$(KSPLICE_MODE)),)
213 ksplice-modnames = $(filter $(KSPLICE_ONLY_TARGETS),$(notdir $(ksplice-mods)))
214 ifdef KSPLICE_KMODSRC
215 ksplice-deps += $(ksplice-modnames:%=$(KSPLICE_KMODSRC)/%.mod.KSPLICE)
216 $(KSPLICE_KMODSRC)/%.mod.KSPLICE: $(obj)/%.o.KSPLICE
217         $(Q)$(call rule_ksplice-mod,ksplice-mod)
218 else
219 ksplice-deps += $(ksplice-modnames:%=$(obj)/%.o.KSPLICE)
220 endif
221 .PRECIOUS: $(obj)/%.o.KSPLICE
223 $(obj)/%.o.KSPLICE_helper: $(obj)/%.o.KSPLICE
224         $(call cmd,ksplice-helper)
226 ifeq ($(KSPLICE_MODE),diff)
228 define ksplice-cow-check
229         $(if $(filter-out %.KSPLICE,$@), \
230           $(if $(strip $(filter-out $(PHONY),$?) $(filter-out $(cmd_$(1)),$(cmd_$@)) $(filter-out $(cmd_$@),$(cmd_$(1)))), \
231             $(if $(wildcard $@), \
232               $(if $(wildcard $@.KSPLICE_pre),, \
233                 $(call cmd,ksplice-cow)))))
235 endef
237 define ksplice-add-cow-check
238 $(v) = $$(ksplice-cow-check)$(value $(v))
240 endef
242 ksplice-cow-eval += $(foreach v,if_changed if_changed_dep if_changed_rule,$(ksplice-add-cow-check))
244 endif   # KSPLICE_MODE
246 ksplice-cmd-files := $(wildcard $(foreach f,$(sort $(ksplice-targets)),$(dir $(f)).$(notdir $(f)).cmd))
247 ifneq ($(ksplice-cmd-files),)
248 $(ksplice-cmd-files): ;
249 include $(ksplice-cmd-files)
250 endif
252 endif   # KSPLICE_MODE
254 PHONY += __ksplice
255 __ksplice: $(ksplice-deps) $(ksplice-dirs)
256         @:
258 PHONY += $(ksplice-dirs)
259 $(ksplice-dirs): $(ksplice-before)
260         $(Q)$(MAKE) -f $(ksplice-makefile) obj=$(@:_ksplice_%=%)
262 endif   # skip-makefile
264 $(eval $(ksplice-cow-eval))
266 PHONY += FORCE
267 FORCE:
269 .PHONY: $(PHONY)