OSDN Git Service

- Improve prerequisites and avoid spurious reconfigurations.
[uclinux-h8/uClibc.git] / Makerules
1 #
2 # Licensed under LGPL v2.1, see the file COPYING.LIB in this tarball for details.
3 #
4
5 .SUFFIXES: .c .S .o .os .oS .so .a .s .i
6
7 PHONY := FORCE
8 # order is important, the stripping uses STRIP_FLAGS for lib-so, but not for lib-a
9 ifeq ($(HAVE_SHARED),y)
10 .LIBPATTERNS: "lib%.so"
11 libs: $(lib-so-y) $(lib-a-y)
12 $(lib-so-y): $(interp)
13 else
14 .LIBPATTERNS: "lib%.a"
15 ifeq ($(UCLIBC_FORMAT_SHARED_FLAT),y)
16 libs: $(lib-gdb-y)
17 endif
18 libs: $(lib-a-y)
19 endif
20 objs: all_objs
21
22 shared_objs =  $(ldso-y) $(libc-y:.o=.os) $(libc-shared-y) $(libc-nonshared-y) $(libdl-so-y)
23 shared_objs += $(libcrypt-so-y) $(libintl-so-y) $(libm-so-y) $(libnsl-so-y) $(libpthread-so-y) $(libpthread-nonshared-y)
24 shared_objs += $(libthread_db-so-y) $(libresolv-so-y) $(librt-so-y) $(libutil-so-y)
25 ar_objs =  $(libc-y) $(libc-static-y) $(libdl-a-y) $(libcrypt-a-y) $(libintl-a-y) $(libm-a-y) $(libnsl-a-y)
26 ar_objs += $(libpthread-a-y) $(libthread_db-a-y) $(libresolv-a-y) $(librt-a-y) $(libutil-a-y)
27 ifeq ($(DOPIC),y)
28 ar_objs := $(ar_objs:.o=.os)
29 endif
30
31 ifeq ($(HAVE_SHARED),y)
32 all_objs: $(sort $(shared_objs) $(ar_objs))
33 else
34 all_objs: $(ar_objs)
35 endif
36 $(shared_objs) $(ar_objs): | $(sub_headers)
37
38 headers-y: $(headers-y)
39         @true
40
41 MAKEFLAGS += --no-print-directory
42 SHELL_SET_X := set +x
43 ifneq ($(findstring s,$(MAKEFLAGS)),)
44 export MAKE_IS_SILENT := y
45 SECHO := -@false
46 DISP := sil
47 Q := @
48 else
49 export MAKE_IS_SILENT := n
50 SECHO := @echo
51 ifneq ($(V)$(VERBOSE),)
52 SHELL_SET_X := set -x
53 DISP := ver
54 Q :=
55 else
56 DISP := pur
57 Q := @
58 endif
59 endif
60
61 show_objs = $(subst ../,,$@)
62
63 pur_disp_compile.c = echo "  "CC $(show_objs)
64 pur_disp_compile.i = echo "  "CPP $(show_objs)
65 pur_disp_compile.s = echo "  "CC-S $(show_objs)
66 pur_disp_compile.u = echo "  "CC $(show_objs)
67 pur_disp_compile.S = echo "  "AS $(show_objs)
68 pur_disp_compile.m = $(pur_disp_compile.c)
69 pur_disp_compile-m = echo "  "CC-m $(show_objs)
70 pur_disp_strip     = echo "  "STRIP $(STRIP_FLAGS) $@
71 pur_disp_t_strip   = echo "  "STRIP $(STRIP_FLAGS) $@
72 pur_disp_ar        = echo "  "AR $(ARFLAGS) $@
73 pur_disp_ld        = echo "  "LD $(1)
74 pur_disp_ln        = echo "  "LN $@
75 pur_disp_mkdir     = echo "  "MKDIR $@
76 pur_disp_gen       = echo "  "GEN $@
77 pur_disp_unifdef   = echo "  "UNIFDEF $@
78
79 sil_disp_compile.c = true
80 sil_disp_compile.i = true
81 sil_disp_compile.s = true
82 sil_disp_compile.u = true
83 sil_disp_compile.S = true
84 sil_disp_compile.m = true
85 sil_disp_compile-m = true
86 sil_disp_strip     = true
87 sil_disp_t_strip   = true
88 sil_disp_ar        = true
89 sil_disp_ld        = true
90 sil_disp_ln        = true
91 sil_disp_mkdir     = true
92 sil_disp_gen       = true
93 sil_disp_unifdef   = true
94
95 ver_disp_compile.c = echo $(cmd_compile.c)
96 ver_disp_compile.i = echo $(cmd_compile.i)
97 ver_disp_compile.s = echo $(cmd_compile.s)
98 ver_disp_compile.u = echo $(cmd_compile.u)
99 ver_disp_compile.S = echo $(cmd_compile.S)
100 ver_disp_compile.m = echo $(cmd_compile.m)
101 ver_disp_compile-m = echo $(cmd_compile-m)
102 ver_disp_strip     = echo $(cmd_strip)
103 ver_disp_t_strip   = echo $(cmd_t_strip)
104 ver_disp_ar        = echo $(cmd_ar)
105 ver_disp_ld        =
106 ver_disp_ln        =
107 ver_disp_mkdir     =
108 ver_disp_gen       =
109 ver_disp_unifdef   = echo $(cmd_unifdef)
110
111 disp_compile.c = $($(DISP)_disp_compile.c)
112 disp_compile.i = $($(DISP)_disp_compile.i)
113 disp_compile.s = $($(DISP)_disp_compile.s)
114 disp_compile.u = $($(DISP)_disp_compile.u)
115 disp_compile.S = $($(DISP)_disp_compile.S)
116 disp_compile.m = $($(DISP)_disp_compile.m)
117 disp_compile-m = $($(DISP)_disp_compile-m)
118 disp_strip     = $($(DISP)_disp_strip)
119 disp_t_strip   = $($(DISP)_disp_t_strip)
120 disp_ar        = $($(DISP)_disp_ar)
121 disp_ld        = $($(DISP)_disp_ld)
122 disp_ln        = $($(DISP)_disp_ln)
123 disp_mkdir     = $($(DISP)_disp_mkdir)
124 disp_gen       = $($(DISP)_disp_gen)
125 disp_unifdef   = $($(DISP)_disp_unifdef)
126
127 any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
128
129 # True if not identical. Neither order nor whitespace nor identical flags
130 # matter.
131 compare_flags = $(strip $(filter-out $(cmd_$(1)), $(cmd_$(@))) \
132                         $(filter-out $(cmd_$(@)), $(cmd_$(1))))
133
134 # Rebuild if any prerequisite, the used CC or flags changed.
135 # Previously used flags are stored in the corresponding .%.dep files
136 maybe_exec = $(if $(strip $(compare_flags) $(any-prereq)), \
137                 @set -e; \
138                 $(disp_$(1)); \
139                 $(cmd_$(1)); \
140                 echo 'cmd_$@ := $(cmd_$1)' >> $(dir $@).$(notdir $@).dep)
141
142 CFLAGS_gen.dep = -MT $@ -MD -MP -MF $(dir $@).$(notdir $@).dep
143
144 cmd_compile.c = $(CC) -c $< -o $@ $(CFLAGS) $(ARCH_CFLAGS) \
145         $(CFLAGS-$(suffix $@)) \
146         $(filter-out $(CFLAGS-OMIT-$(notdir $<)),$(CFLAGS-$(notdir $(<D)))) \
147         $(CFLAGS-$(subst $(top_srcdir),,$(dir $<))) \
148         $(CFLAGS-$(notdir $<)) \
149         $(CFLAGS-$(notdir $@)) \
150         $(CFLAGS_gen.dep)
151 cmd_compile.i = $(cmd_compile.c:-c=-E -dD)
152 cmd_compile.s = $(cmd_compile.c:-c=-S)
153 cmd_compile.u = $(CC) $^ $(DEPS-$(notdir $@)) -o $@ $(CFLAGS) $(CFLAGS-$(notdir $(^D))) $(CFLAGS-$(notdir $@)) $(CFLAGS_gen.dep)
154 cmd_compile.S = $(filter-out -std=gnu99, $(cmd_compile.c)) -D__ASSEMBLER__ $(ASFLAGS) $(ARCH_ASFLAGS) $(ASFLAGS-$(suffix $@)) $(ASFLAGS-$(notdir $<)) $(ASFLAGS-$(notdir $@))
155 cmd_compile.m = $(cmd_compile.c) -DL_$(patsubst %$(suffix $(notdir $@)),%,$(notdir $@))
156
157 cmd_compile-m = $(CC) $^ -c -o $@ $(CFLAGS) $(ARCH_CFLAGS) $(CFLAGS-$(suffix $@)) $(CFLAGS-$(notdir $(@D))) $(CFLAGS-$(notdir $@))
158 cmd_strip     = $(STRIPTOOL) $(STRIP_FLAGS) $^
159 cmd_t_strip   = $(STRIPTOOL) $(STRIP_FLAGS) $@
160 cmd_ar        = $(AR) $(ARFLAGS) $@ $^
161
162 # special suckage alert. We try to avoid --check-symlink-times thus we touch
163 # the link-target.
164 define do_ln
165         @$(disp_ln)
166         $(Q)$(LN) -fs
167 endef
168
169 compile.c = @$(call maybe_exec,compile.c)
170 compile.i =  $(call maybe_exec,compile.i)
171 compile.s =  $(call maybe_exec,compile.s)
172 compile.S = @$(call maybe_exec,compile.S)
173 compile.m = @$(call maybe_exec,compile.m)
174 compile-m = @$(disp_compile-m) ; $(cmd_compile-m)
175 do_strip  = @$(disp_strip)     ; $(cmd_strip)
176 do_t_strip= @$(disp_t_strip)   ; $(cmd_t_strip)
177 do_ar     = @$(disp_ar)        ; $(cmd_ar)
178 do_unifdef= @$(disp_unifdef)   ; $(cmd_unifdef)
179
180 define compile.u
181         @$(disp_compile.u) ; $(cmd_compile.u)
182         @$(disp_t_strip)
183 endef
184 hcompile.u = $(HOSTCC) $^ $(DEPS-$(notdir $@)) -o $@ $(BUILD_LDFLAGS) $(BUILD_LDFLAGS-$(notdir $(^D))) $(BUILD_LDFLAGS-$(notdir $@)) $(BUILD_CFLAGS) $(BUILD_CFLAGS-$(notdir $(^D))) $(BUILD_CFLAGS-$(notdir $@))
185 hcompile.o = $(HOSTCC) $^ $(DEPS-$(notdir $@)) -c -o $@ $(BUILD_CFLAGS) $(BUILD_CFLAGS-$(notdir $(^D))) $(BUILD_CFLAGS-$(notdir $@))
186
187 define link.so
188         $(Q)$(INSTALL) -d $(dir $@)
189         $(Q)$(RM) $@ $@.$(2) $(dir $@)$(1)
190         @$(disp_ld)
191         $(Q)$(CC) $(LDFLAGS-$(notdir $@)) -Wl,-soname=$(notdir $@).$(2) \
192                 $(NOSTDLIB_CFLAGS) -o $(dir $@)$(1) $(START_FILE-$(notdir $@)) \
193                 -Wl,--whole-archive $(firstword $^) -Wl,--no-whole-archive \
194                 $(LIBS-$(notdir $@)) $(LIBGCC) $(END_FILE-$(notdir $@))
195         $(Q)$(LN) -sf $(1) $@.$(2)
196         $(Q)$(LN) -sf $(1) $@
197 endef
198
199 # CRT files needed by link-flat.so
200 LINK_FLAT_CRTS := $(top_builddir)lib/Scrt1.o $(top_builddir)lib/crti.o \
201         $(top_builddir)lib/crtn.o
202
203 # Create a shared flat library from the archive named by the first dependency.
204 # $@ names the shared library's .gdb file, not the flat file itself.
205 # (This is because the .gdb suffix makes the ELF file more distinctive
206 # than the suffixless flat file.)
207 #
208 # Argument 1 is the shared library file -- i.e. $(@:.gdb=) -- and argument 2
209 # is the shared library identifier.  If it wasn't for $(disp_ld), we could
210 # avoid passing $(@:.gdb=) as an argument and use $(@:.gdb=) instead of $(1).
211 #
212 # This is so far only used for libc, for which we want to link the entire
213 # libgcc into the shared object.
214 define link-flat.so
215         $(Q)$(INSTALL) -d $(dir $@)
216         $(Q)$(RM) $(1) $@
217         @$(disp_ld)
218         $(Q)$(CC) $(LDFLAGS-$(notdir $@)) $(NOSTDLIB_CFLAGS) -o $(1) \
219                 -Wl,-elf2flt -Wl,-shared-lib-id,$(2) $(top_builddir)lib/Scrt1.o \
220                 $(top_builddir)/lib/crti.o -Wl,--whole-archive $(firstword $^) \
221                 $(LIBGCC) -Wl,--no-whole-archive $(LIBS-$(notdir $@)) $(LIBGCC) \
222                 $(top_builddir)/lib/crtn.o
223 endef
224
225 define linkm.so
226         $(Q)$(INSTALL) -d $(dir $@)
227         $(Q)$(RM) $@ $@.$(2) $(dir $@)$(1)
228         @$(disp_ld)
229         $(Q)$(CC) $(LDFLAGS-$(notdir $@)) -Wl,-soname=$(notdir $@).$(2) \
230                 $(NOSTDLIB_CFLAGS) -o $(dir $@)$(1) $(START_FILE-$(notdir $@)) \
231                 $^ \
232                 $(LIBS-$(notdir $@)) $(LIBGCC) $(END_FILE-$(notdir $@))
233         $(Q)$(LN) -sf $(1) $@.$(2)
234         $(Q)$(LN) -sf $(1) $@
235 endef
236
237 CFLAGS-.os+=$(PICFLAG)
238 CFLAGS-.oS+=$(PICFLAG) -DSHARED
239
240 %.o:  %.c FORCE ; $(compile.c)
241 %.os: %.c FORCE ; $(compile.c)
242 %.oS: %.c FORCE ; $(compile.c)
243 %.o:  %.S FORCE ; $(compile.S)
244 %.os: %.S FORCE ; $(compile.S)
245 %.oS: %.S FORCE ; $(compile.S)
246 %.o:  %.s FORCE ; $(compile.S)
247 %.os: %.s FORCE ; $(compile.S)
248 %.oS: %.s FORCE ; $(compile.S)
249 %.i:  %.c FORCE ; $(compile.i)
250 %.i:  %.S FORCE ; $(compile.i)
251 %.s:  %.c FORCE ; $(compile.s)
252 %.s:  %.S FORCE ; $(compile.s)
253
254 $(top_builddir)lib/interp.c: | $(sub_headers)
255         $(Q)$(INSTALL) -d $(dir $@)
256         $(Q)echo "/* Force shared libraries to know about the correct library loader */" > $@
257         $(Q)echo "#include <features.h>" >> $@
258         $(Q)echo "const char __dl_ldso__[] __attribute__ ((section " \
259                 "(\".interp\"))) =\""$(SHARED_LIB_LOADER_PREFIX)/$(UCLIBC_LDSO)"\";" >> $@
260
261 $(interp): $(top_builddir)lib/interp.c
262         $(compile.c)
263         $(Q)$(STRIPTOOL) -x -R .note -R .comment $@
264
265 $(ldso):
266         @cd $(top_builddir); $(MAKE) lib/$(patsubst %.$(MAJOR_VERSION),%,$(notdir $@))
267
268 $(libc):
269         @cd $(top_builddir); $(MAKE) lib/$(patsubst %.$(MAJOR_VERSION),%,$(notdir $@))
270
271 CRT := crt1
272
273 ifeq ($(HAVE_SHARED)$(UCLIBC_FORMAT_SHARED_FLAT),y)
274 CRTS=$(top_builddir)lib/$(CRT).o $(top_builddir)lib/S$(CRT).o
275 else
276 CRTS=$(top_builddir)lib/$(CRT).o
277 endif
278
279 ASFLAGS-$(CRT).o := -DL_$(CRT)
280 ASFLAGS-S$(CRT).o := $(PIEFLAG) -DL_S$(CRT)
281 $(CRTS): $(top_srcdir)libc/sysdeps/linux/$(TARGET_ARCH)/$(CRT).S
282         $(Q)$(INSTALL) -d $(dir $@)
283         $(compile.S)
284         $(Q)$(STRIPTOOL) -x -R .note -R .comment $@
285
286 ifeq ($(UCLIBC_CTOR_DTOR),y)
287 CTOR_TARGETS=$(top_builddir)lib/crti.o $(top_builddir)lib/crtn.o
288 else
289 CTOR_TARGETS:=
290 endif
291
292 ifeq ($(UCLIBC_FORMAT_FDPIC_ELF),y)
293 CRTRELOC=$(top_builddir)lib/crtreloc.o
294 $(CRTRELOC): $(top_builddir)lib/%.o : $(top_srcdir)libc/sysdeps/linux/$(TARGET_ARCH)/%.c
295         $(Q)$(INSTALL) -d $(dir $@)
296         $(compile.c)
297 endif
298
299 ifneq ($(wildcard $(top_srcdir)libc/sysdeps/linux/$(TARGET_ARCH)/initfini.c),)
300 CFLAGS-initfini.s := -S -g0 $(PICFLAG) -fno-inline-functions -finhibit-size-directive
301 $(top_builddir)lib/initfini.s: $(top_srcdir)libc/sysdeps/linux/$(TARGET_ARCH)/initfini.c
302         $(compile.c)
303
304 $(top_builddir)lib/defs.h: $(top_builddir)lib/initfini.s
305         $(Q)sed -n -e '/@TESTS_BEGIN/,/@TESTS_END/p' $< | \
306                 gawk -f $(top_srcdir)extra/scripts/defs.awk > $@.tmp
307         $(Q)mv $@.tmp $@
308
309 $(top_builddir)lib/crti.S: $(top_builddir)lib/initfini.s $(top_builddir)lib/defs.h
310         $(Q)sed -n -e '1,/@HEADER_ENDS/p' \
311                 -e '/@_.*_PROLOG_BEGINS/,/@_.*_PROLOG_ENDS/p' \
312                 -e '/@TRAILER_BEGINS/,$$p' $< > $@
313
314 $(top_builddir)lib/crtn.S: $(top_builddir)lib/initfini.s
315         $(Q)sed -n -e '1,/@HEADER_ENDS/p' \
316                 -e '/@_.*_EPILOG_BEGINS/,/@_.*_EPILOG_ENDS/p' \
317                 -e '/@TRAILER_BEGINS/,$$p' $< > $@
318
319 $(CTOR_TARGETS): $(top_builddir)lib/%.o : $(top_builddir)lib/%.S
320         $(Q)$(INSTALL) -d $(dir $@)
321         $(compile.S) $(PICFLAG) $(SSP_DISABLE_FLAGS)
322 else
323 $(CTOR_TARGETS): $(top_builddir)lib/%.o : $(top_srcdir)libc/sysdeps/linux/$(TARGET_ARCH)/%.S
324         $(Q)$(INSTALL) -d $(dir $@)
325         $(compile.S) $(PICFLAG) $(SSP_DISABLE_FLAGS)
326 endif
327
328 #ifeq ($(TARGET_ARCH),nios)
329 #CRTS_COMPAT := $(top_builddir)lib/crt0.o
330 #$(CRTS_COMPAT):
331 #       ln -fs crt1.o $(top_builddir)lib/crt0.o
332 #else
333 CRTS_COMPAT :=
334 #endif
335
336 $(crt-y): $(CRTS) $(CTOR_TARGETS) $(CRTS_COMPAT) $(CRTRELOC)
337 $(CRTS) $(CTOR_TARGETS) $(CRTS_COMPAT) $(CRTRELOC): | headers
338
339 $(top_builddir)lib/$(NONSHARED_LIBNAME): $(libc-nonshared-y)
340         $(Q)$(INSTALL) -d $(dir $@)
341         $(Q)$(RM) $@
342         $(do_ar)
343         $(do_t_strip)
344
345 $(top_builddir)lib/libpthread_nonshared.a: $(libpthread-nonshared-y)
346         $(Q)$(INSTALL) -d $(dir $@)
347         $(Q)$(RM) $@
348         $(do_ar)
349         $(do_t_strip)
350
351 files.dep := $(libc-a-y) $(libc-so-y) $(libc-nonshared-y) \
352         $(libm-a-y) $(libm-so-y) \
353         $(libpthread-a-y) $(libpthread-so-y) $(libpthread-nonshared-y) \
354         $(libthread_db-a-y) $(libthread_db-so-y) \
355         $(librt-a-y) $(librt-so-y)  $(libresolv-a-y) $(libresolv-so-y) \
356         $(libcrypt-a-y) $(libcrypt-so-y) $(libutil-a-y) $(libutil-so-y) \
357         $(libnsl-a-y) $(libnsl-so-y) $(ldso-y) $(libdl-a-y) $(libdl-so-y)
358 .depends.dep := \
359         $(patsubst %.o,%.o.dep,$(filter %.o,$(files.dep))) \
360         $(patsubst %.os,%.os.dep,$(filter %.os,$(files.dep))) \
361         $(patsubst %.oS,%.oS.dep,$(filter %.oS,$(files.dep)))
362 # Oh, and prepend a dot to the basename so i don't have to change my habit of
363 # calling 'size thefile.o*'
364 .depends.dep := $(foreach f,$(.depends.dep),$(dir $(f)).$(notdir $(f)))
365 .depends.dep := $(wildcard $(.depends.dep))
366
367 ifneq ($(strip $(.depends.dep)),)
368 -include $(.depends.dep)
369 endif
370
371 .PHONY: dummy $(PHONY)
372 FORCE:
373
374 clean: objclean-y headers_clean-y
375 realclean: clean
376         $(Q)$(RM) $(.depends.dep)
377
378 objclean-y: $(objclean-y)
379 headers_clean-y: $(headers_clean-y)
380
381 .PHONY: \
382         all check clean realclean distclean test \
383         config dist menuconfig oldconfig release \
384         subdirs utils