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