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