Makefile.flags 7.6 KB

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