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