1# ==========================================================================
2# Build system
3# ==========================================================================
4
5BB_VER = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
6export BB_VER
7SKIP_STRIP ?= n
8
9# -std=gnu99 needed for [U]LLONG_MAX on some systems
10CPPFLAGS += $(call cc-option,-std=gnu99,)
11
12CPPFLAGS += \
13	-Iinclude -Ilibbb \
14	$(if $(KBUILD_SRC),-Iinclude2 -I$(srctree)/include -I$(srctree)/libbb) \
15	-include include/autoconf.h \
16	-D_GNU_SOURCE -DNDEBUG \
17	$(if $(CONFIG_LFS),-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64) \
18	-DBB_VER=$(squote)$(quote)$(BB_VER)$(quote)$(squote)
19
20CFLAGS += $(call cc-option,-Wall,)
21CFLAGS += $(call cc-option,-Wshadow,)
22CFLAGS += $(call cc-option,-Wwrite-strings,)
23CFLAGS += $(call cc-option,-Wundef,)
24CFLAGS += $(call cc-option,-Wstrict-prototypes,)
25CFLAGS += $(call cc-option,-Wunused -Wunused-parameter,)
26CFLAGS += $(call cc-option,-Wunused-function -Wunused-value,)
27CFLAGS += $(call cc-option,-Wmissing-prototypes -Wmissing-declarations,)
28CFLAGS += $(call cc-option,-Wno-format-security,)
29# warn about C99 declaration after statement
30CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
31# If you want to add more -Wsomething above, make sure that it is
32# still possible to build bbox without warnings.
33
34ifeq ($(CONFIG_WERROR),y)
35CFLAGS += $(call cc-option,-Werror,)
36## TODO:
37## gcc version 4.4.0 20090506 (Red Hat 4.4.0-4) (GCC) is a PITA:
38## const char *ptr; ... off_t v = *(off_t*)ptr; -> BOOM
39## and no easy way to convince it to shut the hell up.
40## We have a lot of such things all over the place.
41## Classic *(off_t*)(void*)ptr does not work,
42## and I am unwilling to do crazy gcc specific ({ void *ppp = ...; })
43## stuff in macros. This would obfuscate the code too much.
44## Maybe try __attribute__((__may_alias__))?
45#CFLAGS += $(call cc-ifversion, -eq, 0404, -fno-strict-aliasing)
46endif
47# gcc 3.x emits bogus "old style proto" warning on find.c:alloc_action()
48CFLAGS += $(call cc-ifversion, -ge, 0400, -Wold-style-definition)
49
50ifneq ($(CC),clang)
51# "clang-9: warning: optimization flag '-finline-limit=0' is not supported
52CFLAGS += $(call cc-option,-finline-limit=0,)
53endif
54
55CFLAGS += $(call cc-option,-fno-builtin-strlen -fomit-frame-pointer -ffunction-sections -fdata-sections,)
56# -fno-guess-branch-probability: prohibit pseudo-random guessing
57# of branch probabilities (hopefully makes bloatcheck more stable):
58CFLAGS += $(call cc-option,-fno-guess-branch-probability,)
59CFLAGS += $(call cc-option,-funsigned-char,)
60
61ifeq ($(CONFIG_STATIC_LIBGCC),y)
62# Disable it, for example, if you get
63# "clang-9: warning: argument unused during compilation: '-static-libgcc'"
64CFLAGS += $(call cc-option,-static-libgcc,)
65endif
66
67CFLAGS += $(call cc-option,-falign-functions=1,)
68ifneq ($(CC),clang)
69# "clang-9: warning: optimization flag '-falign-jumps=1' is not supported" (and same for other two)
70CFLAGS += $(call cc-option,-falign-jumps=1 -falign-labels=1 -falign-loops=1,)
71endif
72
73# Defeat .eh_frame bloat (gcc 4.6.3 x86-32 defconfig: 20% smaller busybox binary):
74CFLAGS += $(call cc-option,-fno-unwind-tables,)
75CFLAGS += $(call cc-option,-fno-asynchronous-unwind-tables,)
76# No automatic printf->puts,putchar conversions
77# (try disabling this and comparing assembly, it's instructive)
78CFLAGS += $(call cc-option,-fno-builtin-printf,)
79
80# clang-9 does not like "str" + N and "if (CONFIG_ITEM && cond)" constructs
81ifeq ($(CC),clang)
82CFLAGS += $(call cc-option,-Wno-string-plus-int -Wno-constant-logical-operand)
83endif
84
85# FIXME: These warnings are at least partially to be concerned about and should
86# be fixed..
87#CFLAGS += $(call cc-option,-Wconversion,)
88
89ifneq ($(CONFIG_DEBUG),y)
90CFLAGS += $(call cc-option,-Os,$(call cc-option,-O2,))
91else
92CFLAGS += $(call cc-option,-g,)
93#CFLAGS += "-D_FORTIFY_SOURCE=2"
94ifeq ($(CONFIG_DEBUG_PESSIMIZE),y)
95CFLAGS += $(call cc-option,-O0,)
96else
97CFLAGS += $(call cc-option,-Os,$(call cc-option,-O2,))
98endif
99endif
100ifeq ($(CONFIG_DEBUG_SANITIZE),y)
101CFLAGS += $(call cc-option,-fsanitize=address,)
102CFLAGS += $(call cc-option,-fsanitize=leak,)
103CFLAGS += $(call cc-option,-fsanitize=undefined,)
104endif
105
106# If arch/$(ARCH)/Makefile did not override it (with, say, -fPIC)...
107ARCH_FPIC ?= -fpic
108ARCH_FPIE ?= -fpie
109ARCH_PIE ?= -pie
110
111# Usage: $(eval $(call pkg_check_modules,VARIABLE-PREFIX,MODULES))
112define pkg_check_modules
113$(1)_CFLAGS := $(shell $(PKG_CONFIG) $(PKG_CONFIG_FLAGS) --cflags $(2))
114$(1)_LIBS := $(shell $(PKG_CONFIG) $(PKG_CONFIG_FLAGS) --libs $(2))
115endef
116
117ifeq ($(CONFIG_BUILD_LIBBUSYBOX),y)
118# on i386: 14% smaller libbusybox.so
119# (code itself is 9% bigger, we save on relocs/PLT/GOT)
120CFLAGS += $(ARCH_FPIC)
121# and another 4% reduction of libbusybox.so:
122# (external entry points must be marked EXTERNALLY_VISIBLE)
123CFLAGS += $(call cc-option,-fvisibility=hidden)
124endif
125
126ifeq ($(CONFIG_STATIC),y)
127CFLAGS_busybox += -static
128PKG_CONFIG_FLAGS += --static
129endif
130
131ifeq ($(CONFIG_PIE),y)
132CFLAGS_busybox += $(ARCH_PIE)
133CFLAGS += $(ARCH_FPIE)
134endif
135
136ifneq ($(CONFIG_EXTRA_CFLAGS),)
137CFLAGS += $(strip $(subst ",,$(CONFIG_EXTRA_CFLAGS)))
138#"))
139endif
140
141# Note: both "" (string consisting of two quote chars) and empty string
142# are possible, and should be skipped below.
143ifneq ($(subst "",,$(CONFIG_SYSROOT)),)
144CFLAGS += --sysroot=$(CONFIG_SYSROOT)
145export SYSROOT=$(CONFIG_SYSROOT)
146endif
147
148# libm may be needed for dc, awk, ntpd
149LDLIBS += m
150# Android has no separate crypt library
151# gcc-4.2.1 fails if we try to feed C source on stdin:
152#  echo 'int main(void){return 0;}' | $(CC) $(CFLAGS) -lcrypt -o /dev/null -xc -
153# fall back to using a temp file:
154CRYPT_AVAILABLE := $(shell echo 'int main(void){return 0;}' >bb_libtest.c; $(CC) $(CFLAGS) $(CFLAGS_busybox) -lcrypt -o /dev/null bb_libtest.c >/dev/null 2>&1 && echo "y"; rm bb_libtest.c)
155RT_AVAILABLE    := $(shell echo 'int main(void){return 0;}' >bb_libtest.c; $(CC) $(CFLAGS) $(CFLAGS_busybox) -lrt    -o /dev/null bb_libtest.c >/dev/null 2>&1 && echo "y"; rm bb_libtest.c)
156ifeq ($(CRYPT_AVAILABLE),y)
157LDLIBS += crypt
158endif
159# librt may be needed for clock_gettime()
160ifeq ($(RT_AVAILABLE),y)
161LDLIBS += rt
162endif
163
164# libpam may use libpthread, libdl and/or libaudit.
165# On some platforms that requires an explicit -lpthread, -ldl, -laudit.
166# However, on *other platforms* it fails when some of those flags
167# given needlessly. On some systems, crypt needs pthread.
168#
169# I even had a system where a runtime test for pthread
170# (similar to CRYPT_AVAILABLE test above) was not reliable.
171#
172# Do not propagate this mess by adding libraries to CONFIG_PAM/CRYPT_AVAILABLE blocks.
173# Add libraries you need to CONFIG_EXTRA_LDLIBS instead.
174
175ifeq ($(CONFIG_PAM),y)
176LDLIBS += pam pam_misc
177endif
178
179ifeq ($(CONFIG_SELINUX),y)
180SELINUX_PC_MODULES = libselinux libsepol
181$(eval $(call pkg_check_modules,SELINUX,$(SELINUX_PC_MODULES)))
182CPPFLAGS += $(SELINUX_CFLAGS)
183LDLIBS += $(if $(SELINUX_LIBS),$(SELINUX_LIBS:-l%=%),$(SELINUX_PC_MODULES:lib%=%))
184endif
185
186ifeq ($(CONFIG_FEATURE_NSLOOKUP_BIG),y)
187ifneq (,$(findstring linux,$(shell $(CC) $(CFLAGS) -dumpmachine)))
188LDLIBS += resolv
189endif
190endif
191
192ifeq ($(CONFIG_EFENCE),y)
193LDLIBS += efence
194endif
195
196ifeq ($(CONFIG_DMALLOC),y)
197LDLIBS += dmalloc
198endif
199
200# If a flat binary should be built, CFLAGS_busybox="-elf2flt"
201# env var should be set for make invocation.
202# Here we check whether CFLAGS_busybox indeed contains that flag.
203# (For historical reasons, we also check LDFLAGS, which doesn't
204# seem to be entirely correct variable to put "-elf2flt" into).
205W_ELF2FLT = -elf2flt
206ifneq (,$(findstring $(W_ELF2FLT),$(LDFLAGS) $(CFLAGS_busybox)))
207SKIP_STRIP = y
208endif
209
210ifneq ($(CONFIG_EXTRA_LDFLAGS),)
211LDFLAGS += $(strip $(subst ",,$(CONFIG_EXTRA_LDFLAGS)))
212#"))
213endif
214
215# Busybox is a stack-fatty so make sure we increase default size
216# TODO: use "make stksizes" to find & fix big stack users
217# (we stole scripts/checkstack.pl from the kernel... thanks guys!)
218# Reduced from 20k to 16k in 1.9.0.
219FLTFLAGS += -s 16000
220