1 # Rules.make for busybox
3 # Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
5 # Licensed under GPLv2, see the file LICENSE in this tarball for details.
8 # Pull in the user's busybox configuration
9 ifeq ($(filter $(noconfig_targets),$(MAKECMDGOALS)),)
10 -include $(top_builddir)/.config
13 #--------------------------------------------------------
19 VERSION :=$(MAJOR_VERSION).$(MINOR_VERSION).$(SUBLEVEL_VERSION)$(EXTRAVERSION)
20 BUILDTIME := $(shell TZ=UTC date -u "+%Y.%m.%d-%H:%M%z")
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 #--------------------------------------------------------
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
46 # What OS are you compiling busybox for? This allows you to include
47 # OS specific things, syscall overrides, etc.
50 # Ensure consistent sort order, 'gcc -print-search-dirs' behavior, etc.
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)
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))
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'/-.*//' \
69 -e 's/sparc.*/sparc/' \
72 -e 's/ppc/powerpc/g' \
73 -e 's/v850.*/v850/g' \
75 -e 's/mips-.*/mips/' \
76 -e 's/mipsel-.*/mipsel/' \
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
84 ifeq ($(strip $(BUILD_VERBOSE)),2)
85 VERBOSE_CHECK_CC=echo CC=\"$(1)\" check_cc $(2) >&2;
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; \
96 ifneq ($(filter $(nocheck_targets),$(MAKECMDGOALS)),)
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;
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 && \
111 ifneq ($(filter $(nocheck_targets),$(MAKECMDGOALS)),)
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;
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 ; \
129 ifneq ($(filter $(nocheck_targets),$(MAKECMDGOALS)),)
134 # Select the compiler needed to build binaries for your development system
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,)
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
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
168 CHECKED_LDFLAGS := $(call check_ld,$(LD),--warn-common,)
170 # Pin CHECKED_CFLAGS with := so it's only evaluated once.
171 CHECKED_CFLAGS:=$(call check_cc,$(CC),-Wall,)
172 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wstrict-prototypes,)
173 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wshadow,)
174 CHECKED_CFLAGS+=$(call check_cc,$(CC),-funsigned-char,)
175 CHECKED_CFLAGS+=$(call check_cc,$(CC),-mmax-stack-frame=256,)
176 CHECKED_CFLAGS+=$(call check_cc,$(CC),-fno-builtin-strlen)
177 CHECKED_CFLAGS+=$(call check_cc,$(CC),-finline-limit=0)
179 # Preemptively pin this too.
183 #--------------------------------------------------------
184 # Arch specific compiler optimization stuff should go here.
185 # Unless you want to override the defaults, do not set anything
186 # for OPTIMIZATION...
188 # use '-Os' optimization if available, else use -O2
189 OPTIMIZATION:=$(call check_cc,$(CC),-Os,-O2)
191 ifeq ($(CONFIG_BUILD_AT_ONCE),y)
192 # gcc 2.95 exits with 0 for "unrecognized option"
193 ifeq ($(strip $(shell [ $(CC_MAJOR) -ge 3 ] ; echo $$?)),0)
194 CFLAGS_COMBINE:=$(call check_cc,$(CC),--combine,)
196 OPTIMIZATION+=$(call check_cc,$(CC),-funit-at-a-time,)
197 OPTIMIZATION+=$(call check_cc,$(CC),-fgcse-after-reload,)
198 ifneq ($(CONFIG_BUILD_LIBBUSYBOX),y)
199 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25795
200 # This prevents us from using -fwhole-program when we build the lib
201 PROG_CFLAGS+=$(call check_cc,$(CC),-fwhole-program,)
202 endif # CONFIG_BUILD_LIBBUSYBOX
203 endif # CONFIG_BUILD_AT_ONCE
205 LIB_LDFLAGS:=$(call check_ld,$(LD),--enable-new-dtags,)
206 #LIB_LDFLAGS+=$(call check_ld,$(LD),--reduce-memory-overheads,)
207 #LIB_LDFLAGS+=$(call check_ld,$(LD),--as-needed,)
208 #LIB_LDFLAGS+=$(call check_ld,$(LD),--warn-shared-textrel,)
211 # Some nice architecture specific optimizations
212 ifeq ($(strip $(TARGET_ARCH)),arm)
213 OPTIMIZATION+=-fstrict-aliasing
215 ifeq ($(strip $(TARGET_ARCH)),i386)
216 OPTIMIZATION+=$(call check_cc,$(CC),-march=i386,)
217 # gcc-4.0 and older seem to benefit from these
218 #ifneq ($(strip $(shell [ $(CC_MAJOR) -ge 4 -a $(CC_MINOR) -ge 1 ] ; echo $$?)),0)
219 OPTIMIZATION+=$(call check_cc,$(CC),-mpreferred-stack-boundary=2,)
220 OPTIMIZATION+=$(call check_cc,$(CC),-falign-functions=1 -falign-jumps=1 -falign-loops=1,\
221 -malign-functions=0 -malign-jumps=0 -malign-loops=0)
222 #endif # gcc-4.0 and older
224 # gcc-4.1 and beyond seem to benefit from these
225 ifeq ($(strip $(shell [ $(CC_MAJOR) -ge 4 -a $(CC_MINOR) -ge 1 ] ; echo $$?)),0)
226 # turn off flags which hurt -Os
227 OPTIMIZATION+=$(call check_cc,$(CC),-fno-tree-loop-optimize,)
228 OPTIMIZATION+=$(call check_cc,$(CC),-fno-tree-dominator-opts,)
229 OPTIMIZATION+=$(call check_cc,$(CC),-fno-strength-reduce,)
231 OPTIMIZATION+=$(call check_cc,$(CC),-fno-branch-count-reg,)
232 endif # gcc-4.1 and beyond
234 OPTIMIZATION+=$(call check_cc,$(CC),-fomit-frame-pointer,)
235 OPTIMIZATION+=$(call check_cc,$(CC),-ffunction-sections -fdata-sections,)
238 #--------------------------------------------------------
239 # If you're going to do a lot of builds with a non-vanilla configuration,
240 # it makes sense to adjust parameters above, so you can type "make"
241 # by itself, instead of following it by the same half-dozen overrides
242 # every time. The stuff below, on the other hand, is probably less
243 # prone to casual user adjustment.
246 ifeq ($(strip $(CONFIG_LFS)),y)
247 # For large file summit support
248 CFLAGS+=-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
250 ifeq ($(strip $(CONFIG_DMALLOC)),y)
251 # For testing mem leaks with dmalloc
255 ifeq ($(strip $(CONFIG_EFENCE)),y)
262 ifeq ($(strip $(CONFIG_DEBUG)),y)
266 CHECKED_LDFLAGS += $(call check_ld,$(LD),--sort-common,)
267 CHECKED_LDFLAGS += $(call check_ld,--gc-sections,)
270 ifneq ($(strip $(CONFIG_DEBUG_PESSIMIZE)),y)
271 CFLAGS += $(OPTIMIZATION)
274 # warn a bit more verbosely for non-release versions
275 ifneq ($(EXTRAVERSION),)
276 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wstrict-prototypes,)
277 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wmissing-prototypes,)
278 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wmissing-declarations,)
279 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wunused,)
280 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Winit-self,)
281 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wshadow,)
282 CHECKED_CFLAGS+=$(call check_cc,$(CC),-Wcast-align,)
284 STRIPCMD:=$(call check_strip,$(STRIP),-s --remove-section=.note --remove-section=.comment,$(STRIP))
285 ifeq ($(strip $(CONFIG_STATIC)),y)
286 PROG_CFLAGS += $(call check_cc,$(CC),-static,)
288 CFLAGS_SHARED := $(call check_cc,$(CC),-shared,)
289 LIB_CFLAGS+=$(CFLAGS_SHARED)
291 ifeq ($(strip $(CONFIG_BUILD_LIBBUSYBOX)),y)
292 CFLAGS_PIC:= $(call check_cc,$(CC),-fPIC,)
293 LIB_CFLAGS+=$(CFLAGS_PIC)
296 ifeq ($(strip $(CONFIG_SELINUX)),y)
297 LIBRARIES += -lselinux
300 ifeq ($(strip $(PREFIX)),)
301 PREFIX:=`pwd`/_install
304 ifneq ($(strip $(CONFIG_GETOPT_LONG)),y)
305 CFLAGS += -D__need_getopt
308 # Additional complications due to support for pristine source dir.
309 # Include files in the build directory should take precedence over
310 # the copy in top_srcdir, both during the compilation phase and the
311 # shell script that finds the list of object files.
312 # Work in progress by <ldoolitt@recycle.lbl.gov>.
315 OBJECTS:=$(APPLET_SOURCES:.c=.o) busybox.o usage.o applets.o
316 CFLAGS += $(CHECKED_CFLAGS) $(CROSS_CFLAGS)
317 LDFLAGS += $(CHECKED_LDFLAGS)
320 CFLAGS += -DINIT_SCRIPT='"$(BB_INIT_SCRIPT)"'
323 # Put user-supplied flags at the end, where they
324 # have a chance of winning.
325 -include $(top_builddir)/.config.mak
327 #------------------------------------------------------------
328 # Installation options
329 ifeq ($(strip $(CONFIG_INSTALL_APPLET_HARDLINKS)),y)
330 INSTALL_OPTS=--hardlinks
332 ifeq ($(strip $(CONFIG_INSTALL_APPLET_SYMLINKS)),y)
333 INSTALL_OPTS=--symlinks
335 ifeq ($(strip $(CONFIG_INSTALL_APPLET_DONT)),y)
339 #------------------------------------------------------------
340 # Make the output nice and tight
341 MAKEFLAGS += --no-print-directory
342 export MAKE_IS_SILENT=n
343 ifneq ($(findstring s,$(MAKEFLAGS)),)
344 export MAKE_IS_SILENT=y
349 ifneq ($(BUILD_VERBOSE),)
360 show_objs = $(subst $(top_builddir)/,,$(subst ../,,$@))
361 pur_disp_compile.c = echo " "CC $(show_objs)
362 pur_disp_compile.h = echo " "HOSTCC $(show_objs)
363 pur_disp_strip = echo " "STRIP $(show_objs)
364 pur_disp_link = echo " "LINK $(show_objs)
365 pur_disp_link.h = echo " "HOSTLINK $(show_objs)
366 pur_disp_ar = echo " "AR $(ARFLAGS) $(show_objs)
367 pur_disp_elf2flt = echo " "ELF2FLT $(ELF2FLTFLAGS) $(show_objs)
368 sil_disp_compile.c = true
369 sil_disp_compile.h = true
370 sil_disp_strip = true
372 sil_disp_link.h = true
374 sil_disp_elf2flt = true
375 ver_disp_compile.c = echo $(cmd_compile.c)
376 ver_disp_compile.h = echo $(cmd_compile.h)
377 ver_disp_strip = echo $(cmd_strip)
378 ver_disp_link = echo $(cmd_link)
379 ver_disp_link.h = echo $(cmd_link.h)
380 ver_disp_ar = echo $(cmd_ar)
381 ver_disp_elf2flt = echo $(cmd_elf2flt)
382 disp_compile.c = $($(DISP)_disp_compile.c)
383 disp_compile.h = $($(DISP)_disp_compile.h)
384 disp_strip = $($(DISP)_disp_strip)
385 disp_link = $($(DISP)_disp_link)
386 disp_link.h = $($(DISP)_disp_link.h)
387 disp_ar = $($(DISP)_disp_ar)
388 disp_gen = $(SECHO) " "GEN $@ ; true
389 disp_doc = $(SECHO) " "DOC $(subst docs/,,$@) ; true
390 disp_elf2flt = $($(DISP)_disp_elf2flt)
391 cmd_compile.c = $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
392 cmd_compile.h = $(HOSTCC) $(HOSTCFLAGS) $(INCS) -c -o $@ $<
393 cmd_strip = $(STRIPCMD) $@
394 cmd_link = $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) \
395 $(PROG_CFLAGS) $(PROG_LDFLAGS) $(CFLAGS_COMBINE) \
396 -o $@ $(LD_START_GROUP) \
397 $(APPLETS_DEFINE) $(APPLET_SRC) \
398 $(BUSYBOX_DEFINE) $(BUSYBOX_SRC) $(libraries-y) \
399 $(LDBUSYBOX) $(LIBRARIES) \
401 cmd_link.so = $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) \
402 $(LIB_CFLAGS) $(CFLAGS_COMBINE) $(LIB_LDFLAGS) \
403 -o $(@) $(LD_START_GROUP) $(LD_WHOLE_ARCHIVE) \
404 $(LIBRARY_DEFINE) $(^) \
405 $(LD_NO_WHOLE_ARCHIVE) $(LD_END_GROUP)
406 cmd_link.h = $(HOSTCC) $(HOSTCFLAGS) $(INCS) $< -o $@
407 cmd_ar = $(AR) $(ARFLAGS) $@ $^
408 cmd_elf2flt = $(ELF2FLT) $(ELF2FLTFLAGS) $< -o $@
409 compile.c = @$(disp_compile.c) ; $(cmd_compile.c)
410 compile.h = @$(disp_compile.h) ; $(cmd_compile.h)
411 do_strip = @$(disp_strip) ; $(cmd_strip)
412 do_link = @$(disp_link) ; $(cmd_link)
413 do_link.so = @$(disp_link) ; $(cmd_link.so)
414 do_link.h = @$(disp_link.h) ; $(cmd_link.h)
415 do_ar = @$(disp_ar) ; $(cmd_ar)
416 do_elf2flt = @$(disp_elf2flt) ; $(cmd_elf2flt)
418 uppercase = $(shell echo $1 | $(SED) -e "y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/")
420 @if test -z "$($(call uppercase,$*)_DIR)" ; then \
421 echo "Invalid target $@" ; \
424 $(Q)$(MAKE) $($(call uppercase,$*)_DIR)$@