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