Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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