Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/scripts/Kbuild.klibc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1297 - (show annotations) (download)
Fri May 27 15:12:11 2011 UTC (12 years, 11 months ago) by niro
File size: 13901 byte(s)
-updated to klibc-1.5.22 with mntproc definitions patch included
1 # ==========================================================================
2 # Support for building klibc programs and klibc library
3 # ==========================================================================
4 #
5 # To create a kbuild file for a userspace program do the following:
6 #
7 # Kbuild:
8 #
9 # static-y := cat
10 # # This will compile a file named cat.c -> the executable 'cat'
11 # # The executable will be linked statically
12 #
13 # shared-y := cats
14 # # This will compile a file named cats.c -> the executable 'cats'
15 # # The executable will be linked shared
16 #
17 # If the userspace program consist of composite files do the following:
18 # Kbuild:
19 #
20 # static-y := kinit
21 # kinit-y := main.o netdev.c
22 # So kinit will be linked statically using the two .o files
23 # specified with kinit-y.
24 #
25 # Are part of the program located in a sub-directory do like this:
26 # kinit-y += ipconfig/
27 #
28 # And in the subdirectory:
29 # ipconfig/Kbuild:
30 # lib-y := packet.o dhcp_proto.o
31 # # All .o files listed with lib-y will be used to create a single .a file.
32 # # The .a file is created before any subdirectories are visited so it
33 # # may be used in the sub-directory programs.
34 #
35 #####
36 # For a klibc libary file do like this
37 # klibc/Kbuild
38 # klib-y := error.o pipe.o zlib/
39 #
40 #####
41 # Handling of compiler/linker options
42 #
43 # To set directory wide CFLAGS use:
44 # EXTRA_KLIBCCFLAGS := -DDEBUG
45 # To set directory wide AFLAGS use:
46 # EXTRA_KLIBCAFLAGS := -DDEBUG
47 #
48 # To set target specific CFLAGS (for .c files) use
49 # KLIBCCFLAGS-main.o := -DDEBUG=3
50 # To set target specific AFLAGS (for .s files) use
51 # KLIBCAFLAGS-main.o := -DDEBUG=3
52
53 src := $(obj)
54 # Preset target and make sure it is a ':=' variable
55 targets :=
56
57 .phony: __build
58 __build:
59
60 # Read .config if it exist, otherwise ignore
61 -include $(objtree)/.config
62
63 # Generic Kbuild routines
64 include $(srctree)/scripts/Kbuild.include
65
66 # Defines used when compiling early userspace (klibc programs)
67 # ---------------------------------------------------------------------------
68
69 KLIBCREQFLAGS := $(call cc-option, -fno-stack-protector, ) \
70 $(call cc-option, -fwrapv, )
71 KLIBCARCHREQFLAGS :=
72 KLIBCOPTFLAGS :=
73 KLIBCWARNFLAGS := -W -Wall -Wno-sign-compare -Wno-unused-parameter
74 KLIBCSHAREDFLAGS :=
75 KLIBCBITSIZE :=
76 KLIBCLDFLAGS :=
77 KLIBCCFLAGS :=
78
79 # Defaults for arch to override
80 KLIBCARCHINCFLAGS = -I$(KLIBCKERNELOBJ)/arch/$(KLIBCARCH)/include
81
82 # Arch specific definitions for klibc
83 include $(srctree)/$(KLIBCSRC)/arch/$(KLIBCARCHDIR)/MCONFIG
84
85 # include/asm-* architecture
86 KLIBCASMARCH ?= $(KLIBCARCH)
87
88 # klibc version
89 KLIBCMAJOR := $(shell cut -d. -f1 $(srctree)/usr/klibc/version)
90 KLIBCMINOR := $(shell cut -d. -f2 $(srctree)/usr/klibc/version)
91
92 # binutils
93 KLIBCLD := $(LD)
94 KLIBCCC := $(CC)
95 KLIBCAR := $(AR)
96
97 # klibc-ar is a macro that invokes KLIBCAR and takes 2 arguments of ar commands.
98 # The second will be used for reproducible builds, the first otherwise.
99 klibc-ar = $(KLIBCAR) $(if $(KBUILD_REPRODUCIBLE),$(2),$(1))
100
101 KLIBCRANLIB := $(call klibc-ar,s,Ds)
102 KLIBCSTRIP := $(STRIP)
103 KLIBCNM := $(NM)
104 KLIBCOBJCOPY := $(OBJCOPY)
105 KLIBCOBJDUMP := $(OBJDUMP)
106
107 # klibc include paths
108 KLIBCCPPFLAGS := -nostdinc -iwithprefix include \
109 -I$(KLIBCINC)/arch/$(KLIBCARCHDIR) \
110 -I$(KLIBCINC)/bits$(KLIBCBITSIZE) \
111 -I$(KLIBCOBJ)/../include \
112 -I$(KLIBCINC)
113 # kernel include paths
114 KLIBCKERNELSRC ?= $(srctree)
115 KLIBCKERNELOBJ ?= $(objtree)
116 KLIBCCPPFLAGS += -I$(KLIBCKERNELSRC)/include \
117 $(if $(KBUILD_SRC),-I$(KLIBCKERNELOBJ)/include2 \
118 -I$(KLIBCKERNELOBJ)/include -I$(srctree)/include) \
119 $(KLIBCARCHINCFLAGS)
120
121 # klibc definitions
122 KLIBCDEFS += -D__KLIBC__=$(KLIBCMAJOR) \
123 -D__KLIBC_MINOR__=$(KLIBCMINOR) \
124 -D_BITSIZE=$(KLIBCBITSIZE)
125 KLIBCCPPFLAGS += $(KLIBCDEFS)
126 KLIBCCFLAGS += $(KLIBCCPPFLAGS) $(KLIBCREQFLAGS) $(KLIBCARCHREQFLAGS) \
127 $(KLIBCOPTFLAGS) $(KLIBCWARNFLAGS)
128 KLIBCAFLAGS += -D__ASSEMBLY__ $(KLIBCCFLAGS)
129 KLIBCSTRIPFLAGS += --strip-all -R .comment -R .note
130
131 KLIBCLIBGCC_DEF := $(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-libgcc)
132 KLIBCLIBGCC ?= $(KLIBCLIBGCC_DEF)
133 KLIBCCRT0 := $(KLIBCOBJ)/arch/$(KLIBCARCHDIR)/crt0.o
134 KLIBCLIBC := $(KLIBCOBJ)/libc.a
135 KLIBCCRTSHARED := $(KLIBCOBJ)/interp.o
136 KLIBCLIBCSHARED := $(KLIBCOBJ)/libc.so
137 # How to tell the linker main() is the entrypoint
138 KLIBCEMAIN ?= -e main
139
140 #
141 # This indicates the location of the final version of the shared library.
142 # THIS MUST BE AN ABSOLUTE PATH WITH NO FINAL SLASH.
143 # Leave this empty to make it the root.
144 #
145 SHLIBDIR = /lib
146
147 export KLIBCLD KLIBCCC KLIBCAR KLIBCSTRIP KLIBCNM
148 export KLIBCCFLAGS KLIBCAFLAGS KLIBCLIBGCC KLIBCSHAREDFLAGS KLIBCSTRIPFLAGS
149 export KLIBCCRT0 KLIBCLIBC SHLIBDIR
150
151 # Add $(obj)/ for paths that is not absolute
152 objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
153
154 # Kbuild file in the directory that is being build
155 include $(srctree)/$(obj)/Kbuild
156
157 #####
158 # static-y + shared-y handling
159 kprogs := $(static-y) $(shared-y)
160 # kprogs based on a single .o file (with same name + .o)
161 kprog-objs := $(foreach p, $(kprogs), $(if $($(p)-y),,$(p)))
162 kprog-objs := $(addsuffix .o, $(kprog-objs))
163 # kprogs which is based on several .o files
164 kprog-multi := $(foreach p, $(kprogs), $(if $($(p)-y),$(p)))
165 # objects used for kprogs with more then one .o file
166 kprog-objs += $(foreach p, $(kprog-multi), $($(p)-y))
167 # objects build in this dir
168 kprog-real-objs := $(patsubst %/,,$(kprog-objs))
169 # Directories we need to visit before kprogs-objs are up-to-date
170 kprog-dirs := $(patsubst %/,%,$(filter %/, $(kprog-objs)))
171 # replace all dir/ with dir/lib.a
172 kprog-objs := $(patsubst %/, %/lib.a, $(kprog-objs))
173
174 targets += $(static-y) $(shared-y)
175
176 #####
177 # klib-y handling
178 # .o files to build in this dir
179 klib-real-objs := $(patsubst %/,,$(klib-y))
180 # Directories we need to visit before libs are up-to-date
181 klib-dirs := $(patsubst %/,%,$(filter %/, $(klib-y)))
182 # replace all dir/ with dir/klib.list
183 klib-objs := $(patsubst %/, %/klib.list, $(klib-y))
184
185 # $(output-dirs) are a list of directories that contain object files
186 output-dirs := $(dir $(kprog-dirs) $(kprog-objs))
187 output-dirs += $(foreach f, $(hostprogs-y) $(targets), \
188 $(if $(dir $(f)), $(dir $(f))))
189 output-dirs += $(dir $(klib-objs))
190 output-dirs := $(strip $(sort $(filter-out ./,$(output-dirs))))
191
192 # prefix so we get full dir
193 static-y := $(addprefix $(obj)/,$(static-y))
194 shared-y := $(addprefix $(obj)/,$(shared-y))
195 kprog-objs := $(addprefix $(obj)/,$(kprog-objs))
196 kprog-real-objs := $(addprefix $(obj)/,$(kprog-real-objs))
197 output-dirs := $(addprefix $(obj)/,$(output-dirs))
198 kprog-dirs := $(addprefix $(obj)/,$(kprog-dirs))
199 subdir-y := $(addprefix $(obj)/,$(subdir-y))
200 always := $(addprefix $(obj)/,$(always))
201 targets := $(addprefix $(obj)/,$(targets))
202 lib-y := $(addprefix $(obj)/,$(lib-y))
203 klib-y := $(addprefix $(obj)/,$(klib-y))
204 klib-objs := $(addprefix $(obj)/,$(klib-objs))
205 klib-real-objs := $(addprefix $(obj)/,$(klib-real-objs))
206 klib-dirs := $(addprefix $(obj)/,$(klib-dirs))
207
208 #####
209 # Handle options to gcc. Support building with separate output directory
210
211 __klibccflags = $(KLIBCCFLAGS) $(EXTRA_KLIBCCFLAGS) $(KLIBCCFLAGS_$(*F).o)
212 __klibcaflags = $(KLIBCAFLAGS) $(EXTRA_KLIBCAFLAGS) $(KLIBCAFLAGS_$(*F).o)
213
214 _klibccflags = $(call flags,__klibccflags)
215 _klibcaflags = $(call flags,__klibcaflags)
216
217 klibccflags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(_klibccflags)
218 klibcaflags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(_klibcaflags)
219
220 # Create output directory if not already present
221 _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
222
223 # Create directories for object files if directory does not exist
224 # Needed when lib-y := dir/file.o syntax is used
225 _dummy := $(foreach d,$(output-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
226
227 # Do we have to make a lib.a in this dir?
228 ifneq ($(strip $(lib-y) $(lib-n) $(lib-)),)
229 lib-target := $(obj)/lib.a
230 endif
231
232 __build: $(subdir-y) $(lib-target) $(always)
233 $(Q):
234
235 # Compile C sources (.c)
236 # ---------------------------------------------------------------------------
237
238 quiet_cmd_cc_s_c = KLIBCCC $@
239 cmd_cc_s_c = $(KLIBCCC) $(klibccflags) -S -o $@ $<
240
241 %.s: %.c FORCE
242 $(call if_changed_dep,cc_s_c)
243
244 quiet_cmd_cc_o_c = KLIBCCC $@
245 cmd_cc_o_c = $(KLIBCCC) $(klibccflags) -c -o $@ $<
246
247 %.o: %.c FORCE
248 $(call if_changed_dep,cc_o_c)
249
250 quiet_cmd_cc_i_c = CPP $@
251 cmd_cc_i_c = $(KLIBCCC) -E $(klibccflags) -o $@ $<
252 %.i: %.c FORCE
253 $(call if_changed_dep,cc_i_c)
254
255 # Compile assembler sources (.S)
256 # ---------------------------------------------------------------------------
257
258 quiet_cmd_as_o_S = KLIBCAS $@
259 cmd_as_o_S = $(KLIBCCC) $(klibcaflags) -c -o $@ $<
260
261 %.o: %.S FORCE
262 $(call if_changed_dep,as_o_S)
263
264 targets += $(real-objs-y)
265
266 #
267 # Rule to compile a set of .o files into one .o file
268 #
269 ifdef lib-target
270 quiet_cmd_link_o_target = LD $@
271 # If the list of objects to link is empty, just create an empty lib.a
272 cmd_link_o_target = $(if $(strip $(lib-y)),\
273 rm -f $@; $(call klibc-ar,cru,Dcr) $@ $(filter $(lib-y), $^),\
274 rm -f $@; $(call klibc-ar,crs,Dcrs) $@)
275
276 $(lib-target): $(lib-y) FORCE
277 $(call if_changed,link_o_target)
278 targets += $(lib-target) $(lib-y)
279 endif # lib-target
280
281 #
282 # Create klib.list
283 #
284 # Do we have to create a klibc library file in this dir?
285 ifneq ($(strip $(klib-y) $(klib-n) $(klib-)),)
286 klib-target := $(obj)/klib.list
287 endif
288
289 ifdef klib-target
290 # include this in build
291 __build: $(klib-target) $(klib-dirs)
292
293 # descend if needed
294 $(sort $(addsuffix /klib.list,$(klib-dirs))): $(klib-dirs) ;
295
296 # create klib.list
297 quiet_cmd_klib-list = LIST $@
298 cmd_klib-list = echo $(klib-real-objs) > $@
299 $(klib-target): $(klib-objs) FORCE
300 $(call if_changed,klib-list)
301 targets += $(klib-target) $(klib-real-objs)
302 endif # klib-target
303
304 ifdef kprogs
305 # Compile klibc-programs for the target
306 # ===========================================================================
307
308 __build : $(kprog-dirs) $(static-y) $(shared-y)
309
310 # Descend if needed
311 $(sort $(addsuffix /lib.a,$(kprog-dirs))): $(kprog-dirs) ;
312
313 # Define dependencies for link of progs
314 # For the simple program:
315 # file.o => file
316 # A program with multiple objects
317 # filea.o, fileb.o => file
318 # A program with .o files in another dir
319 # dir/lib.a filea.o => file
320
321 stripobj = $(subst $(obj)/,,$@)
322 addliba = $(addprefix $(obj)/, $(patsubst %/, %/lib.a, $(1)))
323 link-deps = $(if $($(stripobj)-y), $(call addliba, $($(stripobj)-y)), $@.o) \
324 $(call objectify,$($(stripobj)-lib))
325
326 quiet_cmd_ld-static = KLIBCLD $@
327 cmd_ld-static = $(KLIBCLD) $(KLIBCLDFLAGS) -o $@ \
328 $(EXTRA_KLIBCLDFLAGS) \
329 $(KLIBCCRT0) \
330 --start-group \
331 $(link-deps) \
332 $(KLIBCLIBC) \
333 $(KLIBCLIBGCC) \
334 --end-group ; \
335 cp -f $@ $@.g ; \
336 $(KLIBCSTRIP) $(KLIBCSTRIPFLAGS) $@
337
338
339 $(static-y): $(kprog-objs) $(lib-target) $(KLIBCCRT0) $(KLIBCLIBC) FORCE
340 $(call if_changed,ld-static)
341
342 quiet_cmd_ld-shared = KLIBCLD $@
343 cmd_ld-shared = $(KLIBCLD) $(KLIBCLDFLAGS) -o $@ \
344 $(EXTRA_KLIBCLDFLAGS) \
345 $(KLIBCEMAIN) $(KLIBCCRTSHARED) \
346 --start-group \
347 $(link-deps) \
348 -R $(KLIBCLIBCSHARED) \
349 $(KLIBCLIBGCC) \
350 --end-group ; \
351 cp -f $@ $@.g ; \
352 $(KLIBCSTRIP) $(KLIBCSTRIPFLAGS) $@
353
354
355 $(shared-y): $(kprog-objs) $(lib-target) $(KLIBCCRTSHARED) \
356 $(KLIBCLIBCSHARED) FORCE
357 $(call if_changed,ld-shared)
358
359 # Do not try to build KLIBC libaries if we are building klibc
360 ifeq ($(klibc-build),)
361 $(KLIBCCRT0) $(KLIBCLIBC): ;
362 $(KLIBCCRTSHARED) $(KLIBCLIBCSHARED): ;
363 endif
364
365 targets += $(kprog-real-objs)
366 endif
367
368 # Compile programs on the host
369 # ===========================================================================
370 ifdef hostprogs-y
371 include $(srctree)/scripts/Makefile.host
372 endif
373
374 # Descending
375 # ---------------------------------------------------------------------------
376
377 .PHONY: $(subdir-y) $(kprog-dirs) $(klib-dirs)
378 $(sort $(subdir-y) $(kprog-dirs) $(klib-dirs)): $(lib-target)
379 $(Q)$(MAKE) $(klibc)=$@
380
381 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
382 # ---------------------------------------------------------------------------
383
384 .PHONY: FORCE
385
386 FORCE:
387
388 # Linking
389 # Create a reloctable composite object file
390 # ---------------------------------------------------------------------------
391 quiet_cmd_klibcld = KLIBCLD $@
392 cmd_klibcld = $(KLIBCLD) -r $(KLIBCLDFLAGS) \
393 $(EXTRA_KLIBCLDFLAGS) $(KLIBCLDFLAGS_$(@F)) \
394 $(filter-out FORCE,$^) -o $@
395
396
397 # Link target to a new name
398 # ---------------------------------------------------------------------------
399 quiet_cmd_ln = LN $@
400 cmd_ln = rm -f $@ && ln $< $@
401
402 # Strip target (remove all debugging info)
403 quiet_cmd_strip = STRIP $@
404 cmd_strip = $(KLIBCSTRIP) $(KLIBCSTRIPFLAGS) $< -o $@
405
406
407 # Read all saved command lines and dependencies for the $(targets) we
408 # may be building above, using $(if_changed{,_dep}). As an
409 # optimization, we don't need to read them if the target does not
410 # exist, we will rebuild anyway in that case.
411 targets := $(wildcard $(sort $(targets)))
412 cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
413
414 ifneq ($(cmd_files),)
415 include $(cmd_files)
416 endif
417
418 # Shorthand for $(Q)$(MAKE) -f scripts/Kbuild.klibc obj
419 # Usage:
420 # $(Q)$(MAKE) $(klibc)=dir
421 klibc := -rR -f $(srctree)/scripts/Kbuild.klibc obj