ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / scripts / Makefile.build
1 # ==========================================================================
2 # Building
3 # ==========================================================================
4
5 src := $(obj)
6
7 .PHONY: __build
8 __build:
9
10 # Read .config if it exist, otherwise ignore
11 -include .config
12
13 include $(obj)/Makefile
14
15 include scripts/Makefile.lib
16
17 ifneq ($(KBUILD_SRC),)
18 # Create output directory if not already present
19 _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
20
21 # Create directories for object files if directory does not exist
22 # Needed when obj-y := dir/file.o syntax is used
23 _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
24 endif
25
26
27 ifdef EXTRA_TARGETS
28 $(warning kbuild: $(obj)/Makefile - Usage of EXTRA_TARGETS is obsolete in 2.6. Please fix!)
29 endif
30
31 ifdef build-targets
32 $(warning kbuild: $(obj)/Makefile - Usage of build-targets is obsolete in 2.6. Please fix!)
33 endif
34
35 ifdef export-objs
36 $(warning kbuild: $(obj)/Makefile - Usage of export-objs is obsolete in 2.6. Please fix!)
37 endif
38
39 ifdef O_TARGET
40 $(warning kbuild: $(obj)/Makefile - Usage of O_TARGET := $(O_TARGET) is obsolete in 2.6. Please fix!)
41 endif
42
43 ifdef L_TARGET
44 $(error kbuild: $(obj)/Makefile - Use of L_TARGET is replaced by lib-y in 2.6. Please fix!)
45 endif
46
47 ifdef list-multi
48 $(warning kbuild: $(obj)/Makefile - list-multi := $(list-multi) is obsolete in 2.6. Please fix!)
49 endif
50
51 ifndef obj
52 $(warning kbuild: Makefile.build is included improperly)
53 endif
54
55 # ===========================================================================
56
57 ifneq ($(strip $(lib-y) $(lib-m) $(lib-n) $(lib-)),)
58 lib-target := $(obj)/lib.a
59 endif
60
61 ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(lib-target)),)
62 builtin-target := $(obj)/built-in.o
63 endif
64
65 # We keep a list of all modules in $(MODVERDIR)
66
67 __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
68          $(if $(KBUILD_MODULES),$(obj-m)) \
69          $(subdir-ym) $(always)
70         @:
71
72 # Linus' kernel sanity checking tool
73 ifneq ($(KBUILD_CHECKSRC),0)
74 quiet_cmd_checksrc = CHECK   $<
75       cmd_checksrc = $(CHECK) $(c_flags) $< ;
76 endif
77
78
79 # Compile C sources (.c)
80 # ---------------------------------------------------------------------------
81
82 # Default is built-in, unless we know otherwise
83 modkern_cflags := $(CFLAGS_KERNEL)
84 quiet_modtag := $(empty)   $(empty)
85
86 $(real-objs-m)        : modkern_cflags := $(CFLAGS_MODULE)
87 $(real-objs-m:.o=.i)  : modkern_cflags := $(CFLAGS_MODULE)
88 $(real-objs-m:.o=.s)  : modkern_cflags := $(CFLAGS_MODULE)
89 $(real-objs-m:.o=.lst): modkern_cflags := $(CFLAGS_MODULE)
90
91 $(real-objs-m)        : quiet_modtag := [M]
92 $(real-objs-m:.o=.i)  : quiet_modtag := [M]
93 $(real-objs-m:.o=.s)  : quiet_modtag := [M]
94 $(real-objs-m:.o=.lst): quiet_modtag := [M]
95
96 $(obj-m)              : quiet_modtag := [M]
97
98 # Default for not multi-part modules
99 modname = $(*F)
100
101 $(multi-objs-m)         : modname = $(modname-multi)
102 $(multi-objs-m:.o=.i)   : modname = $(modname-multi)
103 $(multi-objs-m:.o=.s)   : modname = $(modname-multi)
104 $(multi-objs-m:.o=.lst) : modname = $(modname-multi)
105 $(multi-objs-y)         : modname = $(modname-multi)
106 $(multi-objs-y:.o=.i)   : modname = $(modname-multi)
107 $(multi-objs-y:.o=.s)   : modname = $(modname-multi)
108 $(multi-objs-y:.o=.lst) : modname = $(modname-multi)
109
110 quiet_cmd_cc_s_c = CC $(quiet_modtag)  $@
111 cmd_cc_s_c       = $(CC) $(c_flags) -S -o $@ $< 
112
113 %.s: %.c FORCE
114         $(call if_changed_dep,cc_s_c)
115
116 quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@
117 cmd_cc_i_c       = $(CPP) $(c_flags)   -o $@ $<
118
119 %.i: %.c FORCE
120         $(call if_changed_dep,cc_i_c)
121
122 # C (.c) files
123 # The C file is compiled and updated dependency information is generated.
124 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
125
126 quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
127
128 ifndef CONFIG_MODVERSIONS
129 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
130
131 else
132 # When module versioning is enabled the following steps are executed:
133 # o compile a .tmp_<file>.o from <file>.c
134 # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
135 #   not export symbols, we just rename .tmp_<file>.o to <file>.o and
136 #   are done.
137 # o otherwise, we calculate symbol versions using the good old
138 #   genksyms on the preprocessed source and postprocess them in a way
139 #   that they are usable as a linker script
140 # o generate <file>.o from .tmp_<file>.o using the linker to
141 #   replace the unresolved symbols __crc_exported_symbol with
142 #   the actual value of the checksum generated by genksyms
143
144 cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
145 cmd_modversions =                                                       \
146         if ! $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then   \
147                 mv $(@D)/.tmp_$(@F) $@;                                 \
148         else                                                            \
149                 $(CPP) -D__GENKSYMS__ $(c_flags) $<                     \
150                 | $(GENKSYMS)                                           \
151                 > $(@D)/.tmp_$(@F:.o=.ver);                             \
152                                                                         \
153                 $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F)              \
154                         -T $(@D)/.tmp_$(@F:.o=.ver);                    \
155                 rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver);        \
156         fi;
157 endif
158
159 define rule_cc_o_c
160         $(if $($(quiet)cmd_checksrc),echo '  $($(quiet)cmd_checksrc)';)   \
161         $(cmd_checksrc)                                                   \
162         $(if $($(quiet)cmd_cc_o_c),echo '  $($(quiet)cmd_cc_o_c)';)       \
163         $(cmd_cc_o_c);                                                    \
164         $(cmd_modversions)                                                \
165         scripts/basic/fixdep $(depfile) $@ '$(cmd_cc_o_c)' > $(@D)/.$(@F).tmp;  \
166         rm -f $(depfile);                                                 \
167         mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd
168 endef
169
170 # Built-in and composite module parts
171
172 %.o: %.c FORCE
173         $(call if_changed_rule,cc_o_c)
174
175 # Single-part modules are special since we need to mark them in $(MODVERDIR)
176
177 $(single-used-m): %.o: %.c FORCE
178         $(call if_changed_rule,cc_o_c)
179         @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
180
181 quiet_cmd_cc_lst_c = MKLST   $@
182       cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
183                      $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
184                                      System.map $(OBJDUMP) > $@
185
186 %.lst: %.c FORCE
187         $(call if_changed_dep,cc_lst_c)
188
189 # Compile assembler sources (.S)
190 # ---------------------------------------------------------------------------
191
192 modkern_aflags := $(AFLAGS_KERNEL)
193
194 $(real-objs-m)      : modkern_aflags := $(AFLAGS_MODULE)
195 $(real-objs-m:.o=.s): modkern_aflags := $(AFLAGS_MODULE)
196
197 quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
198 cmd_as_s_S       = $(CPP) $(a_flags)   -o $@ $< 
199
200 %.s: %.S FORCE
201         $(call if_changed_dep,as_s_S)
202
203 quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
204 cmd_as_o_S       = $(CC) $(a_flags) -c -o $@ $<
205
206 %.o: %.S FORCE
207         $(call if_changed_dep,as_o_S)
208
209 targets += $(real-objs-y) $(real-objs-m) $(lib-y)
210 targets += $(extra-y) $(MAKECMDGOALS) $(always)
211
212 # Build the compiled-in targets
213 # ---------------------------------------------------------------------------
214
215 # To build objects in subdirs, we need to descend into the directories
216 $(sort $(subdir-obj-y)): $(subdir-ym) ;
217
218 #
219 # Rule to compile a set of .o files into one .o file
220 #
221 ifdef builtin-target
222 quiet_cmd_link_o_target = LD      $@
223 # If the list of objects to link is empty, just create an empty built-in.o
224 cmd_link_o_target = $(if $(strip $(obj-y)),\
225                       $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^),\
226                       rm -f $@; $(AR) rcs $@)
227
228 $(builtin-target): $(obj-y) FORCE
229         $(call if_changed,link_o_target)
230
231 targets += $(builtin-target)
232 endif # builtin-target
233
234 #
235 # Rule to compile a set of .o files into one .a file
236 #
237 ifdef lib-target
238 quiet_cmd_link_l_target = AR      $@
239 cmd_link_l_target = rm -f $@; $(AR) $(EXTRA_ARFLAGS) rcs $@ $(lib-y)
240
241 $(lib-target): $(lib-y) FORCE
242         $(call if_changed,link_l_target)
243
244 targets += $(lib-target)
245 endif
246
247 #
248 # Rule to link composite objects
249 #
250 #  Composite objects are specified in kbuild makefile as follows:
251 #    <composite-object>-objs := <list of .o files>
252 #  or
253 #    <composite-object>-y    := <list of .o files>
254 link_multi_deps =                     \
255 $(filter $(addprefix $(obj)/,         \
256 $($(subst $(obj)/,,$(@:.o=-objs)))    \
257 $($(subst $(obj)/,,$(@:.o=-y)))), $^)
258  
259 quiet_cmd_link_multi-y = LD      $@
260 cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps)
261
262 quiet_cmd_link_multi-m = LD [M]  $@
263 cmd_link_multi-m = $(LD) $(ld_flags) $(LDFLAGS_MODULE) -o $@ $(link_multi_deps)
264
265 # We would rather have a list of rules like
266 #       foo.o: $(foo-objs)
267 # but that's not so easy, so we rather make all composite objects depend
268 # on the set of all their parts
269 $(multi-used-y) : %.o: $(multi-objs-y) FORCE
270         $(call if_changed,link_multi-y)
271
272 $(multi-used-m) : %.o: $(multi-objs-m) FORCE
273         $(call if_changed,link_multi-m)
274         @{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod)
275
276 targets += $(multi-used-y) $(multi-used-m)
277
278 # Compile programs on the host
279 # ===========================================================================
280 # host-progs := bin2hex
281 # Will compile bin2hex.c and create an executable named bin2hex
282 #
283 # host-progs    := lxdialog
284 # lxdialog-objs := checklist.o lxdialog.o
285 # Will compile lxdialog.c and checklist.c, and then link the executable
286 # lxdialog, based on checklist.o and lxdialog.o
287 #
288 # host-progs      := qconf
289 # qconf-cxxobjs   := qconf.o
290 # qconf-objs      := menu.o
291 # Will compile qconf as a C++ program, and menu as a C program.
292 # They are linked as C++ code to the executable qconf
293
294 # host-progs := conf
295 # conf-objs  := conf.o libkconfig.so
296 # libkconfig-objs := expr.o type.o
297 # Will create a shared library named libkconfig.so that consist of
298 # expr.o and type.o (they are both compiled as C code and the object file
299 # are made as position independent code).
300 # conf.c is compiled as a c program, and conf.o is linked together with
301 # libkconfig.so as the executable conf.
302 # Note: Shared libraries consisting of C++ files are not supported  
303 #
304
305 # Create executable from a single .c file
306 # host-csingle -> Executable
307 quiet_cmd_host-csingle  = HOSTCC  $@
308       cmd_host-csingle  = $(HOSTCC) $(hostc_flags) $(HOST_LOADLIBES) -o $@ $<
309 $(host-csingle): %: %.c FORCE
310         $(call if_changed_dep,host-csingle)
311
312 # Link an executable based on list of .o files, all plain c
313 # host-cmulti -> executable
314 quiet_cmd_host-cmulti   = HOSTLD  $@
315       cmd_host-cmulti   = $(HOSTCC) $(HOSTLDFLAGS) -o $@ \
316                           $(addprefix $(obj)/,$($(@F)-objs)) \
317                           $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
318 $(host-cmulti): %: $(host-cobjs) $(host-cshlib) FORCE
319         $(call if_changed,host-cmulti)
320
321 # Create .o file from a single .c file
322 # host-cobjs -> .o
323 quiet_cmd_host-cobjs    = HOSTCC  $@
324       cmd_host-cobjs    = $(HOSTCC) $(hostc_flags) -c -o $@ $<
325 $(host-cobjs): %.o: %.c FORCE
326         $(call if_changed_dep,host-cobjs)
327
328 # Link an executable based on list of .o files, a mixture of .c and .cc
329 # host-cxxmulti -> executable
330 quiet_cmd_host-cxxmulti = HOSTLD  $@
331       cmd_host-cxxmulti = $(HOSTCXX) $(HOSTLDFLAGS) -o $@ \
332                           $(foreach o,objs cxxobjs,\
333                           $(addprefix $(obj)/,$($(@F)-$(o)))) \
334                           $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
335 $(host-cxxmulti): %: $(host-cobjs) $(host-cxxobjs) $(host-cshlib) FORCE
336         $(call if_changed,host-cxxmulti)
337
338 # Create .o file from a single .cc (C++) file
339 quiet_cmd_host-cxxobjs  = HOSTCXX $@
340       cmd_host-cxxobjs  = $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
341 $(host-cxxobjs): %.o: %.cc FORCE
342         $(call if_changed_dep,host-cxxobjs)
343
344 # Compile .c file, create position independent .o file
345 # host-cshobjs -> .o
346 quiet_cmd_host-cshobjs  = HOSTCC  -fPIC $@
347       cmd_host-cshobjs  = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
348 $(host-cshobjs): %.o: %.c FORCE
349         $(call if_changed_dep,host-cshobjs)
350
351 # Link a shared library, based on position independent .o files
352 # *.o -> .so shared library (host-cshlib)
353 quiet_cmd_host-cshlib   = HOSTLLD -shared $@
354       cmd_host-cshlib   = $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \
355                           $(addprefix $(obj)/,$($(@F:.so=-objs))) \
356                           $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
357 $(host-cshlib): %: $(host-cshobjs) FORCE
358         $(call if_changed,host-cshlib)
359
360 targets += $(host-csingle)  $(host-cmulti) $(host-cobjs)\
361            $(host-cxxmulti) $(host-cxxobjs) $(host-cshlib) $(host-cshobjs) 
362
363 # Descending
364 # ---------------------------------------------------------------------------
365
366 .PHONY: $(subdir-ym)
367 $(subdir-ym):
368         $(Q)$(MAKE) $(build)=$@
369
370 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
371 # ---------------------------------------------------------------------------
372
373 .PHONY: FORCE
374
375 FORCE:
376
377 # Read all saved command lines and dependencies for the $(targets) we
378 # may be building above, using $(if_changed{,_dep}). As an
379 # optimization, we don't need to read them if the target does not
380 # exist, we will rebuild anyway in that case.
381
382 targets := $(wildcard $(sort $(targets)))
383 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
384
385 ifneq ($(cmd_files),)
386   include $(cmd_files)
387 endif