d0039b082ff1587b4745bd389eb76fa4e7752559
[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           =$(subst ",, $(strip $(CROSS_COMPILER_PREFIX)))
35 CC             = $(CROSS)gcc
36 AR             = $(CROSS)ar
37 AS             = $(CROSS)as
38 LD             = $(CROSS)ld
39 NM             = $(CROSS)nm
40 STRIP          = $(CROSS)strip
41 CPP            = $(CC) -E
42 SED           ?= sed
43
44
45 # What OS are you compiling busybox for?  This allows you to include
46 # OS specific things, syscall overrides, etc.
47 TARGET_OS=linux
48
49 # Select the compiler needed to build binaries for your development system
50 HOSTCC    = gcc
51 HOSTCFLAGS= -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer
52
53 # Ensure consistent sort order, 'gcc -print-search-dirs' behavior, etc.
54 LC_ALL:= C
55
56 # If you want to add some simple compiler switches (like -march=i686),
57 # especially from the command line, use this instead of CFLAGS directly.
58 # For optimization overrides, it's better still to set OPTIMIZATION.
59 CFLAGS_EXTRA=$(subst ",, $(strip $(EXTRA_CFLAGS_OPTIONS)))
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 CFLAGS=-I$(top_builddir)/include -I$(top_srcdir)/include -I$(srcdir)
77 CFLAGS+=$(CHECKED_CFLAGS)
78 ARFLAGS=cru
79
80 # Warnings
81
82 CFLAGS+=-Wall -Wstrict-prototypes -Wshadow
83
84 # gcc centric. Perhaps fiddle with findstring gcc,$(CC) for the rest
85 # get the CC MAJOR/MINOR version
86 CC_MAJOR:=$(shell printf "%02d" $(shell echo __GNUC__ | $(CC) -E -xc - | tail -n 1))
87 CC_MINOR:=$(shell printf "%02d" $(shell echo __GNUC_MINOR__ | $(CC) -E -xc - | tail -n 1))
88
89 #--------------------------------------------------------
90 export VERSION BUILDTIME HOSTCC HOSTCFLAGS CROSS CC AR AS LD NM STRIP CPP
91 ifeq ($(strip $(TARGET_ARCH)),)
92 TARGET_ARCH:=$(shell $(CC) -dumpmachine | sed -e s'/-.*//' \
93                 -e 's/i.86/i386/' \
94                 -e 's/sparc.*/sparc/' \
95                 -e 's/arm.*/arm/g' \
96                 -e 's/m68k.*/m68k/' \
97                 -e 's/ppc/powerpc/g' \
98                 -e 's/v850.*/v850/g' \
99                 -e 's/sh[234]/sh/' \
100                 -e 's/mips-.*/mips/' \
101                 -e 's/mipsel-.*/mipsel/' \
102                 -e 's/cris.*/cris/' \
103                 )
104 endif
105
106 # A nifty macro to make testing gcc features easier, but note that everything
107 # that uses this _must_ use := or it will be re-evaluated for every file.
108 ifeq ($(strip $(V)),2)
109 VERBOSE_CHECK_GCC=echo check_gcc $(1) >> /dev/stderr;
110 endif
111 check_gcc=$(shell \
112         $(VERBOSE_CHECK_GCC)\
113         if [ "$(1)" != "" ]; then \
114                 if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; \
115                 then echo "$(1)"; else echo "$(2)"; fi \
116         fi)
117
118 # A not very robust macro to check for available ld flags
119 check_ld=$(shell \
120         if [ "x$(1)" != "x" ]; then \
121                 $(LD) --help | grep -q "\$(1)" && echo "-Wl,$(1)" ; \
122         fi)
123
124 # Pin CHECKED_CFLAGS with := so it's only evaluated once.
125 CHECKED_CFLAGS:=$(call check_gcc,-funsigned-char,)
126 CHECKED_CFLAGS+=$(call check_gcc,-mmax-stack-frame=256,)
127
128 # Preemptively pin this too.
129 PROG_CFLAGS:=
130
131
132 #--------------------------------------------------------
133 # Arch specific compiler optimization stuff should go here.
134 # Unless you want to override the defaults, do not set anything
135 # for OPTIMIZATION...
136
137 # use '-Os' optimization if available, else use -O2
138 OPTIMIZATION:=$(call check_gcc,-Os,-O2)
139
140 ifeq ($(CONFIG_BUILD_AT_ONCE),y)
141 # gcc 2.95 exits with 0 for "unrecognized option"
142 ifeq ($(strip $(shell [ $(CC_MAJOR) -ge 3 ] ; echo $$?)),0)
143         CFLAGS_COMBINE:=$(call check_gcc,--combine,)
144 endif
145 OPTIMIZATION+=$(call check_gcc,-funit-at-a-time,)
146 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25795
147 #PROG_CFLAGS+=$(call check_gcc,-fwhole-program,)
148 endif # CONFIG_BUILD_AT_ONCE
149
150 LIB_LDFLAGS:=$(call check_ld,--enable-new-dtags,)
151 #LIB_LDFLAGS+=$(call check_ld,--reduce-memory-overheads,)
152 #LIB_LDFLAGS+=$(call check_ld,--as-needed,)
153 #LIB_LDFLAGS+=$(call check_ld,--warn-shared-textrel,)
154
155
156 # Some nice architecture specific optimizations
157 ifeq ($(strip $(TARGET_ARCH)),arm)
158         OPTIMIZATION+=-fstrict-aliasing
159 endif
160 ifeq ($(strip $(TARGET_ARCH)),i386)
161         OPTIMIZATION+=$(call check_gcc,-march=i386,)
162 # gcc-4.0 and older seem to benefit from these
163 #ifneq ($(strip $(shell [ $(CC_MAJOR) -ge 4 -a $(CC_MINOR) -ge 1 ] ; echo $$?)),0)
164         OPTIMIZATION+=$(call check_gcc,-mpreferred-stack-boundary=2,)
165         OPTIMIZATION+=$(call check_gcc,-falign-functions=1 -falign-jumps=1 -falign-loops=1,\
166                 -malign-functions=0 -malign-jumps=0 -malign-loops=0)
167 #endif # gcc-4.0 and older
168
169 # gcc-4.1 and beyond seem to benefit from these
170 ifeq ($(strip $(shell [ $(CC_MAJOR) -ge 4 -a $(CC_MINOR) -ge 1 ] ; echo $$?)),0)
171         # turn off flags which hurt -Os
172         OPTIMIZATION+=$(call check_gcc,-fno-tree-loop-optimize,)
173         OPTIMIZATION+=$(call check_gcc,-fno-tree-dominator-opts,)
174         OPTIMIZATION+=$(call check_gcc,-fno-strength-reduce,)
175
176         OPTIMIZATION+=$(call check_gcc,-fno-branch-count-reg,)
177 endif # gcc-4.1 and beyond
178 endif
179 OPTIMIZATIONS:=$(OPTIMIZATION) $(call check_gcc,-fomit-frame-pointer,)
180
181 #
182 #--------------------------------------------------------
183 # If you're going to do a lot of builds with a non-vanilla configuration,
184 # it makes sense to adjust parameters above, so you can type "make"
185 # by itself, instead of following it by the same half-dozen overrides
186 # every time.  The stuff below, on the other hand, is probably less
187 # prone to casual user adjustment.
188 #
189
190 ifeq ($(strip $(CONFIG_LFS)),y)
191     # For large file summit support
192     CFLAGS+=-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
193 endif
194 ifeq ($(strip $(CONFIG_DMALLOC)),y)
195     # For testing mem leaks with dmalloc
196     CFLAGS+=-DDMALLOC
197     LIBRARIES:=-ldmalloc
198 else
199     ifeq ($(strip $(CONFIG_EFENCE)),y)
200         LIBRARIES:=-lefence
201     endif
202 endif
203 ifeq ($(strip $(CONFIG_DEBUG)),y)
204     CFLAGS  +=-g -D_GNU_SOURCE
205     LDFLAGS += $(call check_ld,--warn-common,)
206 else
207     CFLAGS+=$(OPTIMIZATIONS) -D_GNU_SOURCE -DNDEBUG
208     LDFLAGS += $(call check_ld,--warn-common,)
209     LDFLAGS += $(call check_ld,--sort-common,)
210 endif
211 # warn a bit more verbosely for non-release versions
212 ifneq ($(EXTRAVERSION),)
213     CHECKED_CFLAGS+=$(call check_gcc,-Wstrict-prototypes,)
214     CHECKED_CFLAGS+=$(call check_gcc,-Wmissing-prototypes,)
215     CHECKED_CFLAGS+=$(call check_gcc,-Wmissing-declarations,)
216 endif
217 STRIPCMD:=$(STRIP) -s --remove-section=.note --remove-section=.comment
218 ifeq ($(strip $(CONFIG_STATIC)),y)
219     PROG_CFLAGS += $(call check_gcc,-static,)
220 endif
221 CFLAGS_SHARED := $(call check_gcc,-shared,)
222 LIB_CFLAGS+=$(CFLAGS_SHARED)
223
224 ifeq ($(strip $(CONFIG_BUILD_LIBBUSYBOX)),y)
225     CFLAGS_PIC:= $(call check_gcc,-fPIC,)
226     LIB_CFLAGS+=$(CFLAGS_PIC)
227 endif
228
229
230 ifeq ($(strip $(CONFIG_SELINUX)),y)
231     LIBRARIES += -lselinux
232 endif
233
234 ifeq ($(strip $(PREFIX)),)
235     PREFIX:=`pwd`/_install
236 endif
237
238 # Additional complications due to support for pristine source dir.
239 # Include files in the build directory should take precedence over
240 # the copy in top_srcdir, both during the compilation phase and the
241 # shell script that finds the list of object files.
242 # Work in progress by <ldoolitt@recycle.lbl.gov>.
243
244
245 OBJECTS:=$(APPLET_SOURCES:.c=.o) busybox.o usage.o applets.o
246 CFLAGS    += $(CHECKED_CFLAGS) $(CROSS_CFLAGS)
247 ifdef BB_INIT_SCRIPT
248     CFLAGS += -DINIT_SCRIPT='"$(BB_INIT_SCRIPT)"'
249 endif
250
251 # Put user-supplied flags at the end, where they
252 # have a chance of winning.
253 CFLAGS += $(CFLAGS_EXTRA)
254
255 #------------------------------------------------------------
256 # Installation options
257 ifeq ($(strip $(CONFIG_INSTALL_APPLET_HARDLINKS)),y)
258 INSTALL_OPTS=--hardlinks
259 endif
260 ifeq ($(strip $(CONFIG_INSTALL_APPLET_SYMLINKS)),y)
261 INSTALL_OPTS=--symlinks
262 endif
263 ifeq ($(strip $(CONFIG_INSTALL_APPLET_DONT)),y)
264 INSTALL_OPTS=
265 endif
266
267 #------------------------------------------------------------
268 # Make the output nice and tight
269 MAKEFLAGS += --no-print-directory
270 export MAKE_IS_SILENT=n
271 ifneq ($(findstring s,$(MAKEFLAGS)),)
272 export MAKE_IS_SILENT=y
273 SECHO := @-false
274 DISP  := sil
275 Q     := @
276 else
277 ifneq ($(V)$(VERBOSE),)
278 SECHO := @-false
279 DISP  := ver
280 Q     := 
281 else
282 SECHO := @echo
283 DISP  := pur
284 Q     := @
285 endif
286 endif
287
288 show_objs = $(subst $(top_builddir)/,,$(subst ../,,$@))
289 pur_disp_compile.c = echo "  "CC $(show_objs)
290 pur_disp_compile.h = echo "  "HOSTCC $(show_objs)
291 pur_disp_strip     = echo "  "STRIP $(show_objs)
292 pur_disp_link      = echo "  "LINK $(show_objs)
293 pur_disp_ar        = echo "  "AR $(ARFLAGS) $(show_objs)
294 sil_disp_compile.c = true
295 sil_disp_compile.h = true
296 sil_disp_strip     = true
297 sil_disp_link      = true
298 sil_disp_ar        = true
299 ver_disp_compile.c = echo $(cmd_compile.c)
300 ver_disp_compile.h = echo $(cmd_compile.h)
301 ver_disp_strip     = echo $(cmd_strip)
302 ver_disp_link      = echo $(cmd_link)
303 ver_disp_ar        = echo $(cmd_ar)
304 disp_compile.c     = $($(DISP)_disp_compile.c)
305 disp_compile.h     = $($(DISP)_disp_compile.h)
306 disp_strip         = $($(DISP)_disp_strip)
307 disp_link          = $($(DISP)_disp_link)
308 disp_ar            = $($(DISP)_disp_ar)
309 disp_gen           = $(SECHO) "  "GEN $@ ; true
310 disp_doc           = $(SECHO) "  "DOC $(subst docs/,,$@) ; true
311 cmd_compile.c      = $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
312 cmd_compile.h      = $(HOSTCC) $(HOSTCFLAGS) -c -o $@ $<
313 cmd_strip          = $(STRIPCMD) $@
314 cmd_link           = $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS)
315 cmd_ar             = $(AR) $(ARFLAGS) $@ $^
316 compile.c          = @$(disp_compile.c) ; $(cmd_compile.c)
317 compile.h          = @$(disp_compile.h) ; $(cmd_compile.h)
318 do_strip           = @$(disp_strip)     ; $(cmd_strip)
319 do_link            = @$(disp_link)      ; $(cmd_link)
320 do_ar              = @$(disp_ar)        ; $(cmd_ar)
321
322 .PHONY: dummy