- add target hosttools (see make help)
[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
60 # To compile vs some other alternative libc, you may need to use/adjust
61 # the following lines to meet your needs...
62 #
63 # If you are using Red Hat 6.x with the compatible RPMs (for developing under
64 # Red Hat 5.x and glibc 2.0) uncomment the following.  Be sure to read about
65 # using the compatible RPMs (compat-*) at http://www.redhat.com !
66 #LIBCDIR:=/usr/i386-glibc20-linux
67 #
68 # For other libraries, you are on your own.  But these may (or may not) help...
69 #LDFLAGS+=-nostdlib
70 #LIBRARIES:=$(LIBCDIR)/lib/libc.a -lgcc
71 #CROSS_CFLAGS+=-nostdinc -I$(LIBCDIR)/include -I$(GCCINCDIR) -funsigned-char
72 #GCCINCDIR:=$(shell gcc -print-search-dirs | $(SED) -ne "s/install: \(.*\)/\1include/gp")
73
74 # This must bind late because srcdir is reset for every source subdirectory.
75 INCS:=-I$(top_builddir)/include -I$(top_srcdir)/include
76 CFLAGS=$(INCS) -I$(srcdir) -D_GNU_SOURCE
77 CFLAGS+=$(CHECKED_CFLAGS)
78 ARFLAGS=cru
79
80 # gcc centric. Perhaps fiddle with findstring gcc,$(CC) for the rest
81 # get the CC MAJOR/MINOR version
82 CC_MAJOR:=$(shell printf "%02d" $(shell echo __GNUC__ | $(CC) -E -xc - | tail -n 1))
83 CC_MINOR:=$(shell printf "%02d" $(shell echo __GNUC_MINOR__ | $(CC) -E -xc - | tail -n 1))
84
85 #--------------------------------------------------------
86 export VERSION BUILDTIME HOSTCC HOSTCFLAGS CROSS CC AR AS LD NM STRIP CPP
87 ifeq ($(strip $(TARGET_ARCH)),)
88 TARGET_ARCH:=$(shell $(CC) -dumpmachine | $(SED) -e s'/-.*//' \
89                 -e 's/i.86/i386/' \
90                 -e 's/sparc.*/sparc/' \
91                 -e 's/arm.*/arm/g' \
92                 -e 's/m68k.*/m68k/' \
93                 -e 's/ppc/powerpc/g' \
94                 -e 's/v850.*/v850/g' \
95                 -e 's/sh[234]/sh/' \
96                 -e 's/mips-.*/mips/' \
97                 -e 's/mipsel-.*/mipsel/' \
98                 -e 's/cris.*/cris/' \
99                 )
100 endif
101
102 # A nifty macro to make testing gcc features easier, but note that everything
103 # that uses this _must_ use := or it will be re-evaluated everytime it is
104 # referenced.
105 ifeq ($(strip $(V)),2)
106 VERBOSE_CHECK_CC=echo CC=\"$(1)\" check_cc $(2) >&2;
107 endif
108 check_cc=$(shell \
109         $(VERBOSE_CHECK_CC) \
110         if [ "x$(1)" != "x" ] && [ "x$(2)" != "x" ]; then \
111                 echo "int i;" > ./conftest.c; \
112                 if $(1) $(2) -c -o conftest.o conftest.c > /dev/null 2>&1; \
113                 then echo "$(2)"; else echo "$(3)"; fi ; \
114                 rm -f conftest.c conftest.o; \
115         fi)
116
117 # A not very robust macro to check for available ld flags
118 ifeq ($(strip $(V)),2)
119 VERBOSE_CHECK_LD=echo LD=\"$(1)\" check_ld $(2) >&2;
120 endif
121 check_ld=$(shell \
122         $(VERBOSE_CHECK_LD) \
123         if [ "x$(1)" != "x" ] && [ "x$(2)" != "x" ]; then \
124                 $(1) -o /dev/null -b binary /dev/null > /dev/null 2>&1 && \
125                 echo "-Wl,$(2)" ; \
126         fi)
127
128 # A not very robust macro to check for available strip flags
129 ifeq ($(strip $(V)),2)
130 VERBOSE_CHECK_STRIP=echo STRIPCMD=\"$(1)\" check_strip $(2) >&2;
131 endif
132 check_strip=$(shell \
133         $(VERBOSE_CHECK_STRIP) \
134         if [ "x$(1)" != "x" ] && [ "x$(2)" != "x" ]; then \
135                 echo "int i;" > ./conftest.c ; \
136                 $(CC) -c -o conftest.o conftest.c > /dev/null 2>&1 ; \
137                 $(1) $(2) conftest.o > /dev/null 2>&1 && \
138                 echo "$(1) $(2)" || echo "$(3)"; \
139                 rm -f conftest.c conftest.o > /dev/null 2>&1 ; \
140         fi)
141
142
143
144 # Select the compiler needed to build binaries for your development system
145 HOSTCC     = gcc
146 HOSTCFLAGS:=$(call check_cc,$(HOSTCC),-Wall,)
147 HOSTCFLAGS+=$(call check_cc,$(HOSTCC),-Wstrict-prototypes,)
148 HOSTCFLAGS+=$(call check_cc,$(HOSTCC),-O2,)
149 HOSTCFLAGS+=$(call check_cc,$(HOSTCC),-fomit-frame-pointer,)
150
151 LD_WHOLE_ARCHIVE:=$(shell echo "int i;" > conftest.c ; \
152         $(CC) -c -o conftest.o conftest.c ; \
153         echo "int main(void){return 0;}" > conftest_main.c ; \
154         $(CC) -c -o conftest_main.o conftest_main.c ; \
155         $(AR) $(ARFLAGS) conftest.a conftest.o ; \
156         $(CC) -Wl,--whole-archive conftest.a -Wl,--no-whole-archive \
157          conftest_main.o -o conftest > /dev/null 2>&1 \
158          && echo "-Wl,--whole-archive" ; \
159         rm conftest_main.o conftest_main.c conftest.o conftest.c \
160          conftest.a conftest > /dev/null 2>&1 ; )
161 ifneq ($(findstring whole-archive,$(LD_WHOLE_ARCHIVE)),)
162 LD_NO_WHOLE_ARCHIVE:= -Wl,--no-whole-archive
163 endif
164
165 LD_START_GROUP:=$(shell echo "int bar(void){return 0;}" > conftest.c ; \
166         $(CC) -c -o conftest.o conftest.c ; \
167         echo "int main(void){return bar();}" > conftest_main.c ; \
168         $(CC) -c -o conftest_main.o conftest_main.c ; \
169         $(AR) $(ARFLAGS) conftest.a conftest.o ; \
170         $(CC) -Wl,--start-group conftest.a conftest_main.o -Wl,--end-group \
171          -o conftest > /dev/null 2>&1 && echo "-Wl,--start-group" ; \
172         echo rm conftest_main.o conftest_main.c conftest.o conftest.c \
173          conftest.a conftest > /dev/null 2>&1 ; )
174 ifneq ($(findstring start-group,$(LD_START_GROUP)),)
175 LD_END_GROUP:= -Wl,--end-group
176 endif
177
178 CHECKED_LDFLAGS := $(call check_ld,$(LD),--warn-common,)
179
180 # Pin CHECKED_CFLAGS with := so it's only evaluated once.
181 CHECKED_CFLAGS:=$(call check_cc,$(CC),-Wall,)
182 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wstrict-prototypes,)
183 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wshadow,)
184 CHECKED_CFLAGS+=$(call check_cc,$(CC),-funsigned-char,)
185 CHECKED_CFLAGS+=$(call check_cc,$(CC),-mmax-stack-frame=256,)
186 CHECKED_CFLAGS+=$(call check_cc,$(CC),-fno-builtin-strlen)
187
188 # Preemptively pin this too.
189 PROG_CFLAGS:=
190
191
192 #--------------------------------------------------------
193 # Arch specific compiler optimization stuff should go here.
194 # Unless you want to override the defaults, do not set anything
195 # for OPTIMIZATION...
196
197 # use '-Os' optimization if available, else use -O2
198 OPTIMIZATION:=$(call check_cc,$(CC),-Os,-O2)
199
200 ifeq ($(CONFIG_BUILD_AT_ONCE),y)
201 # gcc 2.95 exits with 0 for "unrecognized option"
202 ifeq ($(strip $(shell [ $(CC_MAJOR) -ge 3 ] ; echo $$?)),0)
203         CFLAGS_COMBINE:=$(call check_cc,$(CC),--combine,)
204 endif
205 OPTIMIZATION+=$(call check_cc,$(CC),-funit-at-a-time,)
206 OPTIMIZATION+=$(call check_cc,$(CC),-fgcse-after-reload,)
207 ifneq ($(CONFIG_BUILD_LIBBUSYBOX),y)
208 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25795
209 # This prevents us from using -fwhole-program when we build the lib
210 PROG_CFLAGS+=$(call check_cc,$(CC),-fwhole-program,)
211 endif # CONFIG_BUILD_LIBBUSYBOX
212 endif # CONFIG_BUILD_AT_ONCE
213
214 LIB_LDFLAGS:=$(call check_ld,$(LD),--enable-new-dtags,)
215 #LIB_LDFLAGS+=$(call check_ld,$(LD),--reduce-memory-overheads,)
216 #LIB_LDFLAGS+=$(call check_ld,$(LD),--as-needed,)
217 #LIB_LDFLAGS+=$(call check_ld,$(LD),--warn-shared-textrel,)
218
219
220 # Some nice architecture specific optimizations
221 ifeq ($(strip $(TARGET_ARCH)),arm)
222         OPTIMIZATION+=-fstrict-aliasing
223 endif
224 ifeq ($(strip $(TARGET_ARCH)),i386)
225         OPTIMIZATION+=$(call check_cc,$(CC),-march=i386,)
226 # gcc-4.0 and older seem to benefit from these
227 #ifneq ($(strip $(shell [ $(CC_MAJOR) -ge 4 -a $(CC_MINOR) -ge 1 ] ; echo $$?)),0)
228         OPTIMIZATION+=$(call check_cc,$(CC),-mpreferred-stack-boundary=2,)
229         OPTIMIZATION+=$(call check_cc,$(CC),-falign-functions=1 -falign-jumps=1 -falign-loops=1,\
230                 -malign-functions=0 -malign-jumps=0 -malign-loops=0)
231 #endif # gcc-4.0 and older
232
233 # gcc-4.1 and beyond seem to benefit from these
234 ifeq ($(strip $(shell [ $(CC_MAJOR) -ge 4 -a $(CC_MINOR) -ge 1 ] ; echo $$?)),0)
235         # turn off flags which hurt -Os
236         OPTIMIZATION+=$(call check_cc,$(CC),-fno-tree-loop-optimize,)
237         OPTIMIZATION+=$(call check_cc,$(CC),-fno-tree-dominator-opts,)
238         OPTIMIZATION+=$(call check_cc,$(CC),-fno-strength-reduce,)
239
240         OPTIMIZATION+=$(call check_cc,$(CC),-fno-branch-count-reg,)
241 endif # gcc-4.1 and beyond
242 endif
243 OPTIMIZATION+=$(call check_cc,$(CC),-fomit-frame-pointer,)
244
245 #
246 #--------------------------------------------------------
247 # If you're going to do a lot of builds with a non-vanilla configuration,
248 # it makes sense to adjust parameters above, so you can type "make"
249 # by itself, instead of following it by the same half-dozen overrides
250 # every time.  The stuff below, on the other hand, is probably less
251 # prone to casual user adjustment.
252 #
253
254 ifeq ($(strip $(CONFIG_LFS)),y)
255     # For large file summit support
256     CFLAGS+=-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
257 endif
258 ifeq ($(strip $(CONFIG_DMALLOC)),y)
259     # For testing mem leaks with dmalloc
260     CFLAGS+=-DDMALLOC
261     LIBRARIES:=-ldmalloc
262 else
263     ifeq ($(strip $(CONFIG_EFENCE)),y)
264         LIBRARIES:=-lefence
265     endif
266 endif
267
268 # Debugging info
269
270 ifeq ($(strip $(CONFIG_DEBUG)),y)
271     CFLAGS +=-g
272 else
273     CFLAGS +=-DNDEBUG
274     CHECKED_LDFLAGS += $(call check_ld,$(LD),--sort-common,)
275 endif
276
277 ifneq ($(strip $(CONFIG_DEBUG_PESSIMIZE)),y)
278     CFLAGS += $(OPTIMIZATION)
279 endif
280
281 # warn a bit more verbosely for non-release versions
282 ifneq ($(EXTRAVERSION),)
283     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wstrict-prototypes,)
284     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wmissing-prototypes,)
285     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wmissing-declarations,)
286     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wunused,)
287     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Winit-self,)
288     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wshadow,)
289     CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wcast-align,)
290 endif
291 STRIPCMD:=$(call check_strip,$(STRIP),-s --remove-section=.note --remove-section=.comment,$(STRIP))
292 ifeq ($(strip $(CONFIG_STATIC)),y)
293     PROG_CFLAGS += $(call check_cc,$(CC),-static,)
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
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 CFLAGS += $(CFLAGS_EXTRA)
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 ($(V)$(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 = true
376 sil_disp_compile.h = true
377 sil_disp_strip     = true
378 sil_disp_link      = true
379 sil_disp_link.h    = true
380 sil_disp_ar        = true
381 sil_disp_elf2flt   = true
382 ver_disp_compile.c = echo $(cmd_compile.c)
383 ver_disp_compile.h = echo $(cmd_compile.h)
384 ver_disp_strip     = echo $(cmd_strip)
385 ver_disp_link      = echo $(cmd_link)
386 ver_disp_link.h    = echo $(cmd_link.h)
387 ver_disp_ar        = echo $(cmd_ar)
388 ver_disp_elf2flt   = echo $(cmd_elf2flt)
389 disp_compile.c     = $($(DISP)_disp_compile.c)
390 disp_compile.h     = $($(DISP)_disp_compile.h)
391 disp_strip         = $($(DISP)_disp_strip)
392 disp_link          = $($(DISP)_disp_link)
393 disp_link.h        = $($(DISP)_disp_link.h)
394 disp_ar            = $($(DISP)_disp_ar)
395 disp_gen           = $(SECHO) "  "GEN $@ ; true
396 disp_doc           = $(SECHO) "  "DOC $(subst docs/,,$@) ; true
397 disp_elf2flt       = $($(DISP)_disp_elf2flt)
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 cmd_link.h         = $(HOSTCC) $(HOSTCFLAGS) $(INCS) $< -o $@
403 cmd_ar             = $(AR) $(ARFLAGS) $@ $^
404 cmd_elf2flt        = $(ELF2FLT) $(ELF2FLTFLAGS) $< -o $@
405 compile.c          = @$(disp_compile.c) ; $(cmd_compile.c)
406 compile.h          = @$(disp_compile.h) ; $(cmd_compile.h)
407 do_strip           = @$(disp_strip)     ; $(cmd_strip)
408 do_link            = @$(disp_link)      ; $(cmd_link)
409 do_link.h          = @$(disp_link.h)    ; $(cmd_link.h)
410 do_ar              = @$(disp_ar)        ; $(cmd_ar)
411 do_elf2flt         = @$(disp_elf2flt)   ; $(cmd_elf2flt)
412
413 uppercase = $(shell echo $1 | tr '[:lower:]' '[:upper:]')
414 %.a:
415         @if test -z "$($(call uppercase,$*)_DIR)" ; then \
416                 echo "Invalid target $@" ; \
417                 exit 1 ; \
418         fi
419         $(Q)$(MAKE) $($(call uppercase,$*)_DIR)$@
420
421 .PHONY: dummy