compiler-opts.m4 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. dnl This Source Code Form is subject to the terms of the Mozilla Public
  2. dnl License, v. 2.0. If a copy of the MPL was not distributed with this
  3. dnl file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. dnl Add compiler specific options
  5. AC_DEFUN([MOZ_DEFAULT_COMPILER],
  6. [
  7. dnl set DEVELOPER_OPTIONS early; MOZ_DEFAULT_COMPILER is usually the first non-setup directive
  8. if test -z "$MC_OFFICIAL"; then
  9. DEVELOPER_OPTIONS=1
  10. fi
  11. MOZ_ARG_ENABLE_BOOL(release,
  12. [ --enable-release Build with more conservative, release engineering-oriented options.
  13. This may slow down builds.],
  14. DEVELOPER_OPTIONS=,
  15. DEVELOPER_OPTIONS=1)
  16. dnl Default to MSVC for win32 and gcc-4.2 for darwin
  17. dnl ==============================================================
  18. if test -z "$CROSS_COMPILE"; then
  19. case "$target" in
  20. *-mingw*)
  21. if test -z "$CPP"; then CPP="$CC -E -nologo"; fi
  22. if test -z "$CXXCPP"; then CXXCPP="$CXX -TP -E -nologo"; ac_cv_prog_CXXCPP="$CXXCPP"; fi
  23. if test -z "$AS"; then
  24. case "${target_cpu}" in
  25. i*86)
  26. AS=ml;
  27. ;;
  28. x86_64)
  29. AS=ml64;
  30. ;;
  31. esac
  32. fi
  33. if test -z "$MIDL"; then MIDL=midl; fi
  34. # need override this flag since we don't use $(LDFLAGS) for this.
  35. if test -z "$HOST_LDFLAGS" ; then
  36. HOST_LDFLAGS=" "
  37. fi
  38. ;;
  39. esac
  40. fi
  41. ])
  42. dnl ============================================================================
  43. dnl C++ rtti
  44. dnl We don't use it in the code, but it can be usefull for debugging, so give
  45. dnl the user the option of enabling it.
  46. dnl ============================================================================
  47. AC_DEFUN([MOZ_RTTI],
  48. [
  49. MOZ_ARG_ENABLE_BOOL(cpp-rtti,
  50. [ --enable-cpp-rtti Enable C++ RTTI ],
  51. [ _MOZ_USE_RTTI=1 ],
  52. [ _MOZ_USE_RTTI= ])
  53. if test -z "$_MOZ_USE_RTTI"; then
  54. if test "$GNU_CC"; then
  55. CXXFLAGS="$CXXFLAGS -fno-rtti"
  56. else
  57. case "$target" in
  58. *-mingw*)
  59. CXXFLAGS="$CXXFLAGS -GR-"
  60. esac
  61. fi
  62. fi
  63. ])
  64. dnl ========================================================
  65. dnl =
  66. dnl = Debugging Options
  67. dnl =
  68. dnl ========================================================
  69. AC_DEFUN([MOZ_DEBUGGING_OPTS],
  70. [
  71. if test -z "$MOZ_DEBUG" -o -n "$MOZ_ASAN"; then
  72. MOZ_NO_DEBUG_RTL=1
  73. fi
  74. AC_SUBST(MOZ_NO_DEBUG_RTL)
  75. MOZ_DEBUG_ENABLE_DEFS="DEBUG TRACING"
  76. MOZ_ARG_WITH_STRING(debug-label,
  77. [ --with-debug-label=LABELS
  78. Define DEBUG_<value> for each comma-separated
  79. value given.],
  80. [ for option in `echo $withval | sed 's/,/ /g'`; do
  81. MOZ_DEBUG_ENABLE_DEFS="$MOZ_DEBUG_ENABLE_DEFS DEBUG_${option}"
  82. done])
  83. if test -n "$MOZ_DEBUG"; then
  84. if test -n "$COMPILE_ENVIRONMENT"; then
  85. AC_MSG_CHECKING([for valid debug flags])
  86. _SAVE_CFLAGS=$CFLAGS
  87. CFLAGS="$CFLAGS $MOZ_DEBUG_FLAGS"
  88. AC_TRY_COMPILE([#include <stdio.h>],
  89. [printf("Hello World\n");],
  90. _results=yes,
  91. _results=no)
  92. AC_MSG_RESULT([$_results])
  93. if test "$_results" = "no"; then
  94. AC_MSG_ERROR([These compiler flags are invalid: $MOZ_DEBUG_FLAGS])
  95. fi
  96. CFLAGS=$_SAVE_CFLAGS
  97. fi
  98. MOZ_DEBUG_DEFINES="$MOZ_DEBUG_ENABLE_DEFS"
  99. else
  100. MOZ_DEBUG_DEFINES="NDEBUG TRIMMED"
  101. fi
  102. AC_SUBST_LIST(MOZ_DEBUG_DEFINES)
  103. ])
  104. dnl A high level macro for selecting compiler options.
  105. AC_DEFUN([MOZ_COMPILER_OPTS],
  106. [
  107. MOZ_DEBUGGING_OPTS
  108. MOZ_RTTI
  109. if test "$CLANG_CXX"; then
  110. ## We disable return-type-c-linkage because jsval is defined as a C++ type but is
  111. ## returned by C functions. This is possible because we use knowledge about the ABI
  112. ## to typedef it to a C type with the same layout when the headers are included
  113. ## from C.
  114. _WARNINGS_CXXFLAGS="${_WARNINGS_CXXFLAGS} -Wno-unknown-warning-option -Wno-return-type-c-linkage"
  115. fi
  116. if test -n "$DEVELOPER_OPTIONS"; then
  117. MOZ_FORCE_GOLD=1
  118. fi
  119. MOZ_ARG_ENABLE_BOOL(gold,
  120. [ --enable-gold Enable GNU Gold Linker when it is not already the default],
  121. MOZ_FORCE_GOLD=1,
  122. MOZ_FORCE_GOLD=
  123. )
  124. if test "$GNU_CC" -a -n "$MOZ_FORCE_GOLD"; then
  125. dnl if the default linker is BFD ld, check if gold is available and try to use it
  126. dnl for local builds only.
  127. if $CC -Wl,--version 2>&1 | grep -q "GNU ld"; then
  128. GOLD=$($CC -print-prog-name=ld.gold)
  129. case "$GOLD" in
  130. /*)
  131. ;;
  132. *)
  133. GOLD=$(which $GOLD)
  134. ;;
  135. esac
  136. if test -n "$GOLD"; then
  137. mkdir -p $_objdir/build/unix/gold
  138. rm -f $_objdir/build/unix/gold/ld
  139. ln -s "$GOLD" $_objdir/build/unix/gold/ld
  140. if $CC -B $_objdir/build/unix/gold -Wl,--version 2>&1 | grep -q "GNU gold"; then
  141. LDFLAGS="$LDFLAGS -B $_objdir/build/unix/gold"
  142. else
  143. rm -rf $_objdir/build/unix/gold
  144. fi
  145. fi
  146. fi
  147. fi
  148. if test "$GNU_CC"; then
  149. if $CC $LDFLAGS -Wl,--version 2>&1 | grep -q "GNU ld"; then
  150. LD_IS_BFD=1
  151. fi
  152. fi
  153. AC_SUBST([LD_IS_BFD])
  154. if test "$GNU_CC"; then
  155. if test -z "$DEVELOPER_OPTIONS"; then
  156. CFLAGS="$CFLAGS -ffunction-sections -fdata-sections"
  157. CXXFLAGS="$CXXFLAGS -ffunction-sections -fdata-sections"
  158. fi
  159. CFLAGS="$CFLAGS -fno-math-errno"
  160. CXXFLAGS="$CXXFLAGS -fno-exceptions -fno-math-errno"
  161. if test "$CPU_ARCH" = "x86" -o "$CPU_ARCH" = "x86_64"; then
  162. CFLAGS="$CFLAGS -msse2 -mfpmath=sse"
  163. CXXFLAGS="$CXXFLAGS -msse2 -mfpmath=sse"
  164. fi
  165. if test -z "$CLANG_CC"; then
  166. case "$CC_VERSION" in
  167. 4.* | 5.*)
  168. ;;
  169. *)
  170. # Lifetime Dead Store Elimination level 2 (default in GCC6+) breaks Gecko.
  171. # Instead of completely disabling this optimization on newer GCC's,
  172. # we'll force them to use level 1 optimization with -flifetime-dse=1.
  173. # Add it first so that a mozconfig can override by setting CFLAGS/CXXFLAGS.
  174. CFLAGS="-flifetime-dse=1 $CFLAGS"
  175. CXXFLAGS="-flifetime-dse=1 $CXXFLAGS"
  176. ;;
  177. esac
  178. fi
  179. fi
  180. dnl ========================================================
  181. dnl = Identical Code Folding
  182. dnl ========================================================
  183. MOZ_ARG_DISABLE_BOOL(icf,
  184. [ --disable-icf Disable Identical Code Folding],
  185. MOZ_DISABLE_ICF=1,
  186. MOZ_DISABLE_ICF= )
  187. if test "$GNU_CC" -a "$GCC_USE_GNU_LD" -a -z "$MOZ_DISABLE_ICF" -a -z "$DEVELOPER_OPTIONS"; then
  188. AC_CACHE_CHECK([whether the linker supports Identical Code Folding],
  189. LD_SUPPORTS_ICF,
  190. [echo 'int foo() {return 42;}' \
  191. 'int bar() {return 42;}' \
  192. 'int main() {return foo() - bar();}' > conftest.${ac_ext}
  193. # If the linker supports ICF, foo and bar symbols will have
  194. # the same address
  195. if AC_TRY_COMMAND([${CC-cc} -o conftest${ac_exeext} $LDFLAGS -Wl,--icf=safe -ffunction-sections conftest.${ac_ext} $LIBS 1>&2]) &&
  196. test -s conftest${ac_exeext} &&
  197. objdump -t conftest${ac_exeext} | awk changequote(<<, >>)'{a[<<$>>6] = <<$>>1} END {if (a["foo"] && (a["foo"] != a["bar"])) { exit 1 }}'changequote([, ]); then
  198. LD_SUPPORTS_ICF=yes
  199. else
  200. LD_SUPPORTS_ICF=no
  201. fi
  202. rm -rf conftest*])
  203. if test "$LD_SUPPORTS_ICF" = yes; then
  204. _SAVE_LDFLAGS="$LDFLAGS -Wl,--icf=safe"
  205. LDFLAGS="$LDFLAGS -Wl,--icf=safe -Wl,--print-icf-sections"
  206. AC_TRY_LINK([], [],
  207. [LD_PRINT_ICF_SECTIONS=-Wl,--print-icf-sections],
  208. [LD_PRINT_ICF_SECTIONS=])
  209. AC_SUBST([LD_PRINT_ICF_SECTIONS])
  210. LDFLAGS="$_SAVE_LDFLAGS"
  211. fi
  212. fi
  213. dnl ========================================================
  214. dnl = Automatically remove dead symbols
  215. dnl ========================================================
  216. if test "$GNU_CC" -a "$GCC_USE_GNU_LD" -a -z "$DEVELOPER_OPTIONS"; then
  217. if test -n "$MOZ_DEBUG_FLAGS"; then
  218. dnl See bug 670659
  219. AC_CACHE_CHECK([whether removing dead symbols breaks debugging],
  220. GC_SECTIONS_BREAKS_DEBUG_RANGES,
  221. [echo 'int foo() {return 42;}' \
  222. 'int bar() {return 1;}' \
  223. 'int main() {return foo();}' > conftest.${ac_ext}
  224. if AC_TRY_COMMAND([${CC-cc} -o conftest.${ac_objext} $CFLAGS $MOZ_DEBUG_FLAGS -c conftest.${ac_ext} 1>&2]) &&
  225. AC_TRY_COMMAND([${CC-cc} -o conftest${ac_exeext} $LDFLAGS $MOZ_DEBUG_FLAGS -Wl,--gc-sections conftest.${ac_objext} $LIBS 1>&2]) &&
  226. test -s conftest${ac_exeext} -a -s conftest.${ac_objext}; then
  227. if test "`$PYTHON -m mozbuild.configure.check_debug_ranges conftest.${ac_objext} conftest.${ac_ext}`" = \
  228. "`$PYTHON -m mozbuild.configure.check_debug_ranges conftest${ac_exeext} conftest.${ac_ext}`"; then
  229. GC_SECTIONS_BREAKS_DEBUG_RANGES=no
  230. else
  231. GC_SECTIONS_BREAKS_DEBUG_RANGES=yes
  232. fi
  233. else
  234. dnl We really don't expect to get here, but just in case
  235. GC_SECTIONS_BREAKS_DEBUG_RANGES="no, but it's broken in some other way"
  236. fi
  237. rm -rf conftest*])
  238. if test "$GC_SECTIONS_BREAKS_DEBUG_RANGES" = no; then
  239. DSO_LDOPTS="$DSO_LDOPTS -Wl,--gc-sections"
  240. fi
  241. else
  242. DSO_LDOPTS="$DSO_LDOPTS -Wl,--gc-sections"
  243. fi
  244. fi
  245. # bionic in Android < 4.1 doesn't support PIE
  246. # On OSX, the linker defaults to building PIE programs when targetting OSX 10.7+,
  247. # but not when targetting OSX < 10.7. OSX < 10.7 doesn't support running PIE
  248. # programs, so as long as support for OSX 10.6 is kept, we can't build PIE.
  249. # Even after dropping 10.6 support, MOZ_PIE would not be useful since it's the
  250. # default (and clang says the -pie option is not used).
  251. # On other Unix systems, some file managers (Nautilus) can't start PIE programs
  252. if test -n "$gonkdir" && test "$ANDROID_VERSION" -ge 16; then
  253. MOZ_PIE=1
  254. else
  255. MOZ_PIE=
  256. fi
  257. MOZ_ARG_ENABLE_BOOL(pie,
  258. [ --enable-pie Enable Position Independent Executables],
  259. MOZ_PIE=1,
  260. MOZ_PIE= )
  261. if test "$GNU_CC" -a -n "$MOZ_PIE"; then
  262. AC_MSG_CHECKING([for PIE support])
  263. _SAVE_LDFLAGS=$LDFLAGS
  264. LDFLAGS="$LDFLAGS $DSO_PIC_CFLAGS -pie"
  265. AC_TRY_LINK(,,AC_MSG_RESULT([yes])
  266. [MOZ_PROGRAM_LDFLAGS="$MOZ_PROGRAM_LDFLAGS -pie"],
  267. AC_MSG_RESULT([no])
  268. AC_MSG_ERROR([--enable-pie requires PIE support from the linker.]))
  269. LDFLAGS=$_SAVE_LDFLAGS
  270. fi
  271. AC_SUBST(MOZ_PROGRAM_LDFLAGS)
  272. dnl ASan assumes no symbols are being interposed, and when that happens,
  273. dnl it's not happy with it. Unconveniently, since Firefox is exporting
  274. dnl libffi symbols and Gtk+3 pulls system libffi via libwayland-client,
  275. dnl system libffi interposes libffi symbols that ASan assumes are in
  276. dnl libxul, so it barfs about buffer overflows.
  277. dnl Using -Wl,-Bsymbolic ensures no exported symbol can be interposed.
  278. if test -n "$GCC_USE_GNU_LD"; then
  279. case "$LDFLAGS" in
  280. *-fsanitize=address*)
  281. LDFLAGS="$LDFLAGS -Wl,-Bsymbolic"
  282. ;;
  283. esac
  284. fi
  285. ])