- fix copy'n paste errors that got introduced when switching to the shorter boilerplate.
[oweals/busybox.git] / Rules.mak
1 # Rules.make for busybox
2 #
3 # Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
4 #
5 # Licensed under GPLv2, see the file LICENSE in this tarball for details.
6 #
7
8 # Pull in the user's busybox configuration
9 ifeq ($(filter $(noconfig_targets),$(MAKECMDGOALS)),)
10 -include $(top_builddir)/.config
11 endif
12
13 #--------------------------------------------------------
14 PROG      := busybox
15 MAJOR_VERSION   :=1
16 MINOR_VERSION   :=2
17 SUBLEVEL_VERSION:=0
18 EXTRAVERSION    :=-svn
19 VERSION   :=$(MAJOR_VERSION).$(MINOR_VERSION).$(SUBLEVEL_VERSION)$(EXTRAVERSION)
20 BUILDTIME := $(shell TZ=UTC date -u "+%Y.%m.%d-%H:%M%z")
21
22
23 #--------------------------------------------------------
24 # With a modern GNU make(1) (highly recommended, that's what all the
25 # developers use), all of the following configuration values can be
26 # overridden at the command line.  For example:
27 #   make CROSS_COMPILE=powerpc-linux- top_srcdir="$HOME/busybox" PREFIX=/mnt/app
28 #--------------------------------------------------------
29
30 # If you are running a cross compiler, you will want to set CROSS_COMPILE
31 # to something more interesting...  Target architecture is determined
32 # by asking the CC compiler what arch it compiles things for, so unless
33 # your compiler is broken, you should not need to specify TARGET_ARCH
34 CC             = $(CROSS_COMPILE)gcc
35 AR             = $(CROSS_COMPILE)ar
36 AS             = $(CROSS_COMPILE)as
37 LD             = $(CROSS_COMPILE)ld
38 NM             = $(CROSS_COMPILE)nm
39 STRIP          = $(CROSS_COMPILE)strip
40 ELF2FLT        = $(CROSS_COMPILE)elf2flt
41 CPP            = $(CC) -E
42 SED           ?= sed
43 BZIP2         ?= bzip2
44
45
46 # What OS are you compiling busybox for?  This allows you to include
47 # OS specific things, syscall overrides, etc.
48 TARGET_OS=linux
49
50 # Ensure consistent sort order, 'gcc -print-search-dirs' behavior, etc.
51 LC_ALL:= C
52
53 # This must bind late because srcdir is reset for every source subdirectory.
54 INCS:=-I$(top_builddir)/include -I$(top_srcdir)/include
55 CFLAGS=$(INCS) -I$(srcdir) -D_GNU_SOURCE
56 CFLAGS+=$(CHECKED_CFLAGS)
57 ARFLAGS=cru
58
59 # gcc centric. Perhaps fiddle with findstring gcc,$(CC) for the rest
60 # get the CC MAJOR/MINOR version
61 CC_MAJOR:=$(shell printf "%02d" $(shell echo __GNUC__ | $(CC) -E -xc - | tail -n 1))
62 CC_MINOR:=$(shell printf "%02d" $(shell echo __GNUC_MINOR__ | $(CC) -E -xc - | tail -n 1))
63
64 #--------------------------------------------------------
65 export VERSION BUILDTIME HOSTCC HOSTCFLAGS CROSS_COMPILE CC AR AS LD NM STRIP CPP
66 ifeq ($(strip $(TARGET_ARCH)),)
67 TARGET_ARCH:=$(shell $(CC) -dumpmachine | $(SED) -e s'/-.*//' \
68                 -e 's/i.86/i386/' \
69                 -e 's/sparc.*/sparc/' \
70                 -e 's/arm.*/arm/g' \
71                 -e 's/m68k.*/m68k/' \
72                 -e 's/ppc/powerpc/g' \
73                 -e 's/v850.*/v850/g' \
74                 -e 's/sh[234]/sh/' \
75                 -e 's/mips-.*/mips/' \
76                 -e 's/mipsel-.*/mipsel/' \
77                 -e 's/cris.*/cris/' \
78                 )
79 endif
80
81 # A nifty macro to make testing gcc features easier, but note that everything
82 # that uses this _must_ use := or it will be re-evaluated everytime it is
83 # referenced.
84 ifeq ($(strip $(BUILD_VERBOSE)),2)
85 VERBOSE_CHECK_CC=echo CC=\"$(1)\" check_cc $(2) >&2;
86 endif
87 check_cc=$(shell \
88         $(VERBOSE_CHECK_CC) \
89         if [ "x$(1)" != "x" ] && [ "x$(2)" != "x" ]; then \
90                 echo "int i;" > ./conftest.c; \
91                 if $(1) $(2) -c -o conftest.o conftest.c > /dev/null 2>&1; \
92                 then echo "$(2)"; else echo "$(3)"; fi ; \
93                 rm -f conftest.c conftest.o; \
94         fi)
95
96 ifneq ($(filter $(nocheck_targets),$(MAKECMDGOALS)),)
97 check_cc:=
98 endif
99
100 # A not very robust macro to check for available ld flags
101 ifeq ($(strip $(BUILD_VERBOSE)),2)
102 VERBOSE_CHECK_LD=echo LD=\"$(1)\" check_ld $(2) >&2;
103 endif
104 check_ld=$(shell \
105         $(VERBOSE_CHECK_LD) \
106         if [ "x$(1)" != "x" ] && [ "x$(2)" != "x" ]; then \
107                 $(1) -o /dev/null -b binary /dev/null > /dev/null 2>&1 && \
108                 echo "-Wl,$(2)" ; \
109         fi)
110
111 ifneq ($(filter $(nocheck_targets),$(MAKECMDGOALS)),)
112 check_ld:=
113 endif
114
115 # A not very robust macro to check for available strip flags
116 ifeq ($(strip $(BUILD_VERBOSE)),2)
117 VERBOSE_CHECK_STRIP=echo STRIPCMD=\"$(1)\" check_strip $(2) >&2;
118 endif
119 check_strip=$(shell \
120         $(VERBOSE_CHECK_STRIP) \
121         if [ "x$(1)" != "x" ] && [ "x$(2)" != "x" ]; then \
122                 echo "int i;" > ./conftest.c ; \
123                 $(CC) -c -o conftest.o conftest.c > /dev/null 2>&1 ; \
124                 $(1) $(2) conftest.o > /dev/null 2>&1 && \
125                 echo "$(1) $(2)" || echo "$(3)"; \
126                 rm -f conftest.c conftest.o > /dev/null 2>&1 ; \
127         fi)
128
129 ifneq ($(filter $(nocheck_targets),$(MAKECMDGOALS)),)
130 check_strip:=
131 endif
132
133
134 # Select the compiler needed to build binaries for your development system
135 HOSTCC     = gcc
136 HOSTCFLAGS:=$(call check_cc,$(HOSTCC),-Wall,)
137 HOSTCFLAGS+=$(call check_cc,$(HOSTCC),-Wstrict-prototypes,)
138 HOSTCFLAGS+=$(call check_cc,$(HOSTCC),-O2,)
139 HOSTCFLAGS+=$(call check_cc,$(HOSTCC),-fomit-frame-pointer,)
140
141 LD_WHOLE_ARCHIVE:=$(shell echo "int i;" > conftest.c ; \
142         $(CC) -c -o conftest.o conftest.c ; \
143         echo "int main(void){return 0;}" > conftest_main.c ; \
144         $(CC) -c -o conftest_main.o conftest_main.c ; \
145         $(AR) $(ARFLAGS) conftest.a conftest.o ; \
146         $(CC) -Wl,--whole-archive conftest.a -Wl,--no-whole-archive \
147          conftest_main.o -o conftest > /dev/null 2>&1 \
148          && echo "-Wl,--whole-archive" ; \
149         rm conftest_main.o conftest_main.c conftest.o conftest.c \
150          conftest.a conftest > /dev/null 2>&1 ; )
151 ifneq ($(findstring whole-archive,$(LD_WHOLE_ARCHIVE)),)
152 LD_NO_WHOLE_ARCHIVE:= -Wl,--no-whole-archive
153 endif
154
155 LD_START_GROUP:=$(shell echo "int bar(void){return 0;}" > conftest.c ; \
156         $(CC) -c -o conftest.o conftest.c ; \
157         echo "int main(void){return bar();}" > conftest_main.c ; \
158         $(CC) -c -o conftest_main.o conftest_main.c ; \
159         $(AR) $(ARFLAGS) conftest.a conftest.o ; \
160         $(CC) -Wl,--start-group conftest.a conftest_main.o -Wl,--end-group \
161          -o conftest > /dev/null 2>&1 && echo "-Wl,--start-group" ; \
162         rm conftest_main.o conftest_main.c conftest.o conftest.c \
163          conftest.a conftest > /dev/null 2>&1 ; )
164 ifneq ($(findstring start-group,$(LD_START_GROUP)),)
165 LD_END_GROUP:= -Wl,--end-group
166 endif
167
168 CHECKED_LDFLAGS := $(call check_ld,$(LD),--warn-common,)
169 #CHECKED_LDFLAGS := $(call check_ld,$(LD),-static-libgcc,)
170
171 # Pin CHECKED_CFLAGS with := so it's only evaluated once.
172 CHECKED_CFLAGS:=$(call check_cc,$(CC),-Wall,)
173 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wstrict-prototypes,)
174 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wshadow,)
175 CHECKED_CFLAGS+=$(call check_cc,$(CC),-funsigned-char,)
176 CHECKED_CFLAGS+=$(call check_cc,$(CC),-fno-builtin-strlen,)
177 CHECKED_CFLAGS+=$(call check_cc,$(CC),-finline-limit=0,)
178 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Winline,)
179 CHECKED_CFLAGS+=$(call check_cc,$(CC),-static-libgcc,)
180
181 # Preemptively pin this too.
182 PROG_CFLAGS:=
183
184
185 #--------------------------------------------------------
186 # Arch specific compiler optimization stuff should go here.
187 # Unless you want to override the defaults, do not set anything
188 # for OPTIMIZATION...
189
190 # use '-Os' optimization if available, else use -O2
191 OPTIMIZATION:=$(call check_cc,$(CC),-Os,-O2)
192
193 ifeq ($(CONFIG_BUILD_AT_ONCE),y)
194 # gcc 2.95 exits with 0 for "unrecognized option"
195 ifeq ($(strip $(shell [ $(CC_MAJOR) -ge 3 ] ; echo $$?)),0)
196         CFLAGS_COMBINE:=$(call check_cc,$(CC),--combine,)
197 endif
198 OPTIMIZATION+=$(call check_cc,$(CC),-funit-at-a-time,)
199 OPTIMIZATION+=$(call check_cc,$(CC),-fgcse-after-reload,)
200 ifneq ($(CONFIG_BUILD_LIBBUSYBOX),y)
201 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25795
202 # This prevents us from using -fwhole-program when we build the lib
203 PROG_CFLAGS+=$(call check_cc,$(CC),-fwhole-program,)
204 endif # CONFIG_BUILD_LIBBUSYBOX
205 endif # CONFIG_BUILD_AT_ONCE
206
207 LIB_LDFLAGS:=$(call check_ld,$(LD),--enable-new-dtags,)
208 #LIB_LDFLAGS+=$(call check_ld,$(LD),--reduce-memory-overheads,)
209 #LIB_LDFLAGS+=$(call check_ld,$(LD),--as-needed,)
210 #LIB_LDFLAGS+=$(call check_ld,$(LD),--warn-shared-textrel,)
211
212
213 # Some nice architecture specific optimizations
214 ifeq ($(strip $(TARGET_ARCH)),arm)
215         OPTIMIZATION+=-fstrict-aliasing
216 endif
217 ifeq ($(strip $(TARGET_ARCH)),i386)
218         OPTIMIZATION+=$(call check_cc,$(CC),-march=i386,)
219 # gcc-4.0 and older seem to benefit from these
220 ifneq ($(strip $(shell [ $(CC_MAJOR) -ge 4 -a $(CC_MINOR) -ge 1 ] ; echo $$?)),0)
221         OPTIMIZATION+=$(call check_cc,$(CC),-mpreferred-stack-boundary=2,)
222         OPTIMIZATION+=$(call check_cc,$(CC),-falign-functions=1 -falign-jumps=1 -falign-loops=1, -malign-functions=0 -malign-jumps=0 -malign-loops=0,)
223
224         # gcc 4.1 produces many broken, totally invalid warnings
225         CHECKED_CFLAGS+=$(call check_cc,$(CC),-Werror,)
226 endif # gcc-4.0 and older
227
228 # gcc-4.1 and beyond seem to benefit from these
229 ifeq ($(strip $(shell [ $(CC_MAJOR) -ge 4 -a $(CC_MINOR) -ge 1 ] ; echo $$?)),0)
230         # turn off flags which hurt -Os
231         OPTIMIZATION+=$(call check_cc,$(CC),-fno-tree-loop-optimize,)
232         OPTIMIZATION+=$(call check_cc,$(CC),-fno-tree-dominator-opts,)
233         OPTIMIZATION+=$(call check_cc,$(CC),-fno-strength-reduce,)
234
235         OPTIMIZATION+=$(call check_cc,$(CC),-fno-branch-count-reg,)
236 endif # gcc-4.1 and beyond
237 endif
238 OPTIMIZATION+=$(call check_cc,$(CC),-fomit-frame-pointer,)
239 CHECKED_LDFLAGS += $(call check_ld,$(LD),--sort-common,)
240
241 #
242 #--------------------------------------------------------
243 # If you're going to do a lot of builds with a non-vanilla configuration,
244 # it makes sense to adjust parameters above, so you can type "make"
245 # by itself, instead of following it by the same half-dozen overrides
246 # every time.  The stuff below, on the other hand, is probably less
247 # prone to casual user adjustment.
248 #
249
250 ifeq ($(strip $(CONFIG_LFS)),y)
251     # For large file summit support
252     CFLAGS+=-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
253 endif
254 ifeq ($(strip $(CONFIG_DMALLOC)),y)
255     # For testing mem leaks with dmalloc
256     CFLAGS+=-DDMALLOC
257     LIBRARIES:=-ldmalloc
258 else
259     ifeq ($(strip $(CONFIG_EFENCE)),y)
260         LIBRARIES:=-lefence
261     endif
262 endif
263
264 # Debugging info
265
266 ifeq ($(strip $(CONFIG_DEBUG)),y)
267     CFLAGS +=-g
268 else
269     CFLAGS +=-DNDEBUG
270 endif
271
272 ifneq ($(strip $(CONFIG_DEBUG_PESSIMIZE)),y)
273     CFLAGS += $(OPTIMIZATION)
274 endif
275
276 # warn a bit more verbosely for non-release versions
277 ifneq ($(EXTRAVERSION),)
278     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wstrict-prototypes,)
279     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wmissing-prototypes,)
280     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wmissing-declarations,)
281     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wunused,)
282     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Winit-self,)
283     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wshadow,)
284     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wcast-align,)
285 endif
286 STRIPCMD:=$(call check_strip,$(STRIP),-s --remove-section=.note --remove-section=.comment,$(STRIP))
287 ifeq ($(strip $(CONFIG_STATIC)),y)
288     PROG_CFLAGS += $(call check_cc,$(CC),-static,)
289 else
290     ifneq ($(strip $(CONFIG_DEBUG)),y)
291         OPTIMIZATION+=$(call check_cc,$(CC),-ffunction-sections -fdata-sections,)
292         CHECKED_LDFLAGS += $(call check_ld,$(LD),--gc-sections,)
293     endif
294 endif
295 CFLAGS_SHARED := $(call check_cc,$(CC),-shared,)
296 LIB_CFLAGS+=$(CFLAGS_SHARED)
297
298 ifeq ($(strip $(CONFIG_BUILD_LIBBUSYBOX)),y)
299     CFLAGS_PIC:= $(call check_cc,$(CC),-fPIC,)
300     LIB_CFLAGS+=$(CFLAGS_PIC)
301 endif
302
303 ifeq ($(strip $(CONFIG_SELINUX)),y)
304     LIBRARIES += -lselinux -lsepol
305 endif
306
307 ifeq ($(strip $(PREFIX)),)
308     PREFIX:=`pwd`/_install
309 endif
310
311 ifneq ($(strip $(CONFIG_GETOPT_LONG)),y)
312     CFLAGS += -D__need_getopt
313 endif
314
315 # Additional complications due to support for pristine source dir.
316 # Include files in the build directory should take precedence over
317 # the copy in top_srcdir, both during the compilation phase and the
318 # shell script that finds the list of object files.
319 # Work in progress by <ldoolitt@recycle.lbl.gov>.
320
321
322 OBJECTS:=$(APPLET_SOURCES:.c=.o) busybox.o usage.o applets.o
323 CFLAGS  += $(CHECKED_CFLAGS) $(CROSS_CFLAGS)
324 LDFLAGS += $(CHECKED_LDFLAGS)
325
326 ifdef BB_INIT_SCRIPT
327     CFLAGS += -DINIT_SCRIPT='"$(BB_INIT_SCRIPT)"'
328 endif
329
330 # Put user-supplied flags at the end, where they
331 # have a chance of winning.
332 -include $(top_builddir)/.config.mak
333
334 #------------------------------------------------------------
335 # Installation options
336 ifeq ($(strip $(CONFIG_INSTALL_APPLET_HARDLINKS)),y)
337 INSTALL_OPTS=--hardlinks
338 endif
339 ifeq ($(strip $(CONFIG_INSTALL_APPLET_SYMLINKS)),y)
340 INSTALL_OPTS=--symlinks
341 endif
342 ifeq ($(strip $(CONFIG_INSTALL_APPLET_DONT)),y)
343 INSTALL_OPTS=
344 endif
345
346 #------------------------------------------------------------
347 # Make the output nice and tight
348 MAKEFLAGS += --no-print-directory
349 export MAKE_IS_SILENT=n
350 ifneq ($(findstring s,$(MAKEFLAGS)),)
351 export MAKE_IS_SILENT=y
352 SECHO := @-false
353 DISP  := sil
354 Q     := @
355 else
356 ifneq ($(BUILD_VERBOSE),)
357 SECHO := @-false
358 DISP  := ver
359 Q     := 
360 else
361 SECHO := @echo
362 DISP  := pur
363 Q     := @
364 endif
365 endif
366
367 show_objs = $(subst $(top_builddir)/,,$(subst ../,,$@))
368 pur_disp_compile.c = echo "  "CC $(show_objs) ;
369 pur_disp_compile.h = echo "  "HOSTCC $(show_objs) ;
370 pur_disp_strip     = echo "  "STRIP $(show_objs) ;
371 pur_disp_link      = echo "  "LINK $(show_objs) ;
372 pur_disp_link.h    = echo "  "HOSTLINK $(show_objs) ;
373 pur_disp_ar        = echo "  "AR $(ARFLAGS) $(show_objs) ;
374 pur_disp_elf2flt   = echo "  "ELF2FLT $(ELF2FLTFLAGS) $(show_objs) ;
375 sil_disp_compile.c = 
376 sil_disp_compile.h = 
377 sil_disp_strip     = 
378 sil_disp_link      = 
379 sil_disp_link.h    = 
380 sil_disp_ar        = 
381 sil_disp_elf2flt   = 
382 ver_disp_compile.c = 
383 ver_disp_compile.h = 
384 ver_disp_strip     = 
385 ver_disp_link      = 
386 ver_disp_link.h    = 
387 ver_disp_ar        = 
388 ver_disp_elf2flt   = 
389 disp_compile.c     = $(Q)$($(DISP)_disp_compile.c)
390 disp_compile.h     = $(Q)$($(DISP)_disp_compile.h)
391 disp_strip         = $(Q)$($(DISP)_disp_strip)
392 disp_link          = $(Q)$($(DISP)_disp_link)
393 disp_link.h        = $(Q)$($(DISP)_disp_link.h)
394 disp_ar            = $(Q)$($(DISP)_disp_ar)
395 disp_elf2flt       = $(Q)$($(DISP)_disp_elf2flt)
396 disp_gen           = $(SECHO) "  "GEN $@ ; true
397 disp_doc           = $(SECHO) "  "DOC $(subst docs/,,$@) ; true
398 cmd_compile.c      = $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
399 cmd_compile.h      = $(HOSTCC) $(HOSTCFLAGS) $(INCS) -c -o $@ $<
400 cmd_strip          = $(STRIPCMD) $@
401 cmd_link           = $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) \
402                         $(PROG_CFLAGS) $(PROG_LDFLAGS) $(CFLAGS_COMBINE) \
403                         -o $@ $(LD_START_GROUP)  \
404                         $(APPLETS_DEFINE) $(APPLET_SRC) \
405                         $(BUSYBOX_DEFINE) $(BUSYBOX_SRC) $(libraries-y) \
406                         $(LDBUSYBOX) $(LIBRARIES) \
407                         $(LD_END_GROUP)
408 cmd_link.so        = $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) \
409                         $(LIB_CFLAGS) $(CFLAGS_COMBINE) $(LIB_LDFLAGS) \
410                         -o $(@) $(LD_START_GROUP) $(LD_WHOLE_ARCHIVE) \
411                         $(LIBRARY_DEFINE) $(^) \
412                         $(LD_NO_WHOLE_ARCHIVE) $(LD_END_GROUP)
413 cmd_link.h         = $(HOSTCC) $(HOSTCFLAGS) $(INCS) $< -o $@
414 cmd_ar             = $(AR) $(ARFLAGS) $@ $^
415 cmd_elf2flt        = $(ELF2FLT) $(ELF2FLTFLAGS) $< -o $@
416 compile.c          = $(disp_compile.c) $(cmd_compile.c)
417 compile.h          = $(disp_compile.h) $(cmd_compile.h)
418 do_strip           = $(disp_strip)     $(cmd_strip)
419 do_link            = $(disp_link)      $(cmd_link)
420 do_link.so         = $(disp_link)      $(cmd_link.so)
421 do_link.h          = $(disp_link.h)    $(cmd_link.h)
422 do_ar              = $(disp_ar)        $(cmd_ar)
423 do_elf2flt         = $(disp_elf2flt)   $(cmd_elf2flt)
424
425 uppercase = $(shell echo $1 | $(SED) -e "y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/")
426 %.a:
427         @if test -z "$($(call uppercase,$*)_DIR)" ; then \
428                 echo "Invalid target $@" ; \
429                 exit 1 ; \
430         fi
431         $(Q)$(MAKE) $($(call uppercase,$*)_DIR)$@
432
433 .PHONY: dummy