EthCompilerSettings.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #------------------------------------------------------------------------------
  2. # EthCompilerSettings.cmake
  3. #
  4. # CMake file for cpp-ethereum project which specifies our compiler settings
  5. # for each supported platform and build configuration.
  6. #
  7. # The documentation for cpp-ethereum is hosted at http://cpp-ethereum.org
  8. #
  9. # Copyright (c) 2014-2016 cpp-ethereum contributors.
  10. #------------------------------------------------------------------------------
  11. # Clang seeks to be command-line compatible with GCC as much as possible, so
  12. # most of our compiler settings are common between GCC and Clang.
  13. #
  14. # These settings then end up spanning all POSIX platforms (Linux, OS X, BSD, etc)
  15. # Use ccache if available
  16. find_program(CCACHE_FOUND ccache)
  17. if(CCACHE_FOUND)
  18. set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
  19. set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
  20. message("Using ccache")
  21. endif(CCACHE_FOUND)
  22. if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
  23. # Use ISO C++11 standard language.
  24. set(CMAKE_CXX_FLAGS -std=c++11)
  25. # Enables all the warnings about constructions that some users consider questionable,
  26. # and that are easy to avoid. Also enable some extra warning flags that are not
  27. # enabled by -Wall. Finally, treat at warnings-as-errors, which forces developers
  28. # to fix warnings as they arise, so they don't accumulate "to be fixed later".
  29. add_compile_options(-Wall)
  30. add_compile_options(-Wextra)
  31. add_compile_options(-Werror)
  32. # Disable warnings about unknown pragmas (which is enabled by -Wall). I assume we have external
  33. # dependencies (probably Boost) which have some of these. Whatever the case, we shouldn't be
  34. # disabling these globally. Instead, we should pragma around just the problem #includes.
  35. #
  36. # TODO - Track down what breaks if we do NOT do this.
  37. add_compile_options(-Wno-unknown-pragmas)
  38. # To get the code building on FreeBSD and Arch Linux we seem to need the following
  39. # warning suppression to work around some issues in Boost headers.
  40. #
  41. # See the following reports:
  42. # https://github.com/ethereum/webthree-umbrella/issues/384
  43. # https://github.com/ethereum/webthree-helpers/pull/170
  44. #
  45. # The issue manifest as warnings-as-errors like the following:
  46. #
  47. # /usr/local/include/boost/multiprecision/cpp_int.hpp:181:4: error:
  48. # right operand of shift expression '(1u << 63u)' is >= than the precision of the left operand
  49. #
  50. # -fpermissive is a pretty nasty way to address this. It is described as follows:
  51. #
  52. # Downgrade some diagnostics about nonconformant code from errors to warnings.
  53. # Thus, using -fpermissive will allow some nonconforming code to compile.
  54. #
  55. # NB: Have to use this form for the setting, so that it only applies to C++ builds.
  56. # Applying -fpermissive to a C command-line (ie. secp256k1) gives a build error.
  57. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive")
  58. # Build everything as shared libraries (.so files)
  59. add_definitions(-DSHAREDLIB)
  60. # If supported for the target machine, emit position-independent code, suitable for dynamic
  61. # linking and avoiding any limit on the size of the global offset table.
  62. add_compile_options(-fPIC)
  63. # Configuration-specific compiler settings.
  64. set(CMAKE_CXX_FLAGS_DEBUG "-Og -g -DETH_DEBUG")
  65. set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG -DETH_RELEASE")
  66. set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -DETH_RELEASE")
  67. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DETH_RELEASE")
  68. # Additional GCC-specific compiler settings.
  69. if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
  70. # Check that we've got GCC 4.7 or newer.
  71. execute_process(
  72. COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
  73. if (NOT (GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7))
  74. message(FATAL_ERROR "${PROJECT_NAME} requires g++ 4.7 or greater.")
  75. endif ()
  76. # Strong stack protection was only added in GCC 4.9.
  77. # Use it if we have the option to do so.
  78. # See https://lwn.net/Articles/584225/
  79. if (GCC_VERSION VERSION_GREATER 4.9 OR GCC_VERSION VERSION_EQUAL 4.9)
  80. add_compile_options(-fstack-protector-strong)
  81. add_compile_options(-fstack-protector)
  82. endif()
  83. # Additional Clang-specific compiler settings.
  84. elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  85. add_compile_options(-fstack-protector)
  86. # Enable strong stack protection only on Mac and only for OS X Yosemite
  87. # or newer (AppleClang 7.0+). We should be able to re-enable this setting
  88. # on non-Apple Clang as well, if we can work out what expression to use for
  89. # the version detection.
  90. # The fact that the version-reporting for AppleClang loses the original
  91. # Clang versioning is rather annoying. Ideally we could just have
  92. # a single cross-platform "if version >= 3.4.1" check.
  93. #
  94. # There is debug text in the else clause below, to help us work out what
  95. # such an expression should be, if we can get this running on a Trusty box
  96. # with Clang. Greg Colvin previously replicated the issue there too.
  97. #
  98. # See https://github.com/ethereum/webthree-umbrella/issues/594
  99. if (APPLE)
  100. if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
  101. add_compile_options(-fstack-protector-strong)
  102. endif()
  103. else()
  104. message(WARNING "CMAKE_CXX_COMPILER_VERSION = ${CMAKE_CXX_COMPILER_VERSION}")
  105. endif()
  106. # A couple of extra warnings suppressions which we seemingly
  107. # need when building with Clang.
  108. #
  109. # TODO - Nail down exactly where these warnings are manifesting and
  110. # try to suppress them in a more localized way. Notes in this file
  111. # indicate that the first is needed for sepc256k1 and that the
  112. # second is needed for the (clog, cwarn) macros. These will need
  113. # testing on at least OS X and Ubuntu.
  114. add_compile_options(-Wno-unused-function)
  115. add_compile_options(-Wno-dangling-else)
  116. # Some Linux-specific Clang settings. We don't want these for OS X.
  117. if ("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
  118. # TODO - Is this even necessary? Why?
  119. # See http://stackoverflow.com/questions/19774778/when-is-it-necessary-to-use-use-the-flag-stdlib-libstdc.
  120. add_compile_options(-stdlib=libstdc++)
  121. # Tell Boost that we're using Clang's libc++. Not sure exactly why we need to do.
  122. add_definitions(-DBOOST_ASIO_HAS_CLANG_LIBCXX)
  123. # Use fancy colors in the compiler diagnostics
  124. add_compile_options(-fcolor-diagnostics)
  125. # See "How to silence unused command line argument error with clang without disabling it?"
  126. # When using -Werror with clang, it transforms "warning: argument unused during compilation" messages
  127. # into errors, which makes sense.
  128. # http://stackoverflow.com/questions/21617158/how-to-silence-unused-command-line-argument-error-with-clang-without-disabling-i
  129. add_compile_options(-Qunused-arguments)
  130. endif()
  131. if (EMSCRIPTEN)
  132. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --memory-init-file 0 -O3 -s LINKABLE=1 -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -s ALLOW_MEMORY_GROWTH=1 -s NO_DYNAMIC_EXECUTION=1")
  133. add_definitions(-DETH_EMSCRIPTEN=1)
  134. endif()
  135. endif()
  136. # The major alternative compiler to GCC/Clang is Microsoft's Visual C++ compiler, only available on Windows.
  137. elseif (MSVC)
  138. add_compile_options(/MP) # enable parallel compilation
  139. add_compile_options(/EHsc) # specify Exception Handling Model in msvc
  140. add_compile_options(/WX) # enable warnings-as-errors
  141. add_compile_options(/wd4068) # disable unknown pragma warning (4068)
  142. add_compile_options(/wd4996) # disable unsafe function warning (4996)
  143. add_compile_options(/wd4503) # disable decorated name length exceeded, name was truncated (4503)
  144. add_compile_options(/wd4267) # disable conversion from 'size_t' to 'type', possible loss of data (4267)
  145. add_compile_options(/wd4180) # disable qualifier applied to function type has no meaning; ignored (4180)
  146. add_compile_options(/wd4290) # disable C++ exception specification ignored except to indicate a function is not __declspec(nothrow) (4290)
  147. add_compile_options(/wd4297) # disable <vector>'s function assumed not to throw an exception but does (4297)
  148. add_compile_options(/wd4244) # disable conversion from 'type1' to 'type2', possible loss of data (4244)
  149. add_compile_options(/wd4800) # disable forcing value to bool 'true' or 'false' (performance warning) (4800)
  150. add_compile_options(-D_WIN32_WINNT=0x0600) # declare Windows Vista API requirement
  151. add_compile_options(-DNOMINMAX) # undefine windows.h MAX && MIN macros cause it cause conflicts with std::min && std::max functions
  152. add_compile_options(-DMINIUPNP_STATICLIB) # define miniupnp static library
  153. # Always use Release variant of C++ runtime.
  154. # We don't want to provide Debug variants of all dependencies. Some default
  155. # flags set by CMake must be tweaked.
  156. string(REPLACE "/MDd" "/MD" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
  157. string(REPLACE "/D_DEBUG" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
  158. string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
  159. string(REPLACE "/MDd" "/MD" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
  160. string(REPLACE "/D_DEBUG" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
  161. string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
  162. set_property(GLOBAL PROPERTY DEBUG_CONFIGURATIONS OFF)
  163. # disable empty object file warning
  164. set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221")
  165. # warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification
  166. # warning LNK4099: pdb was not found with lib
  167. # stack size 16MB
  168. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ignore:4099,4075 /STACK:16777216")
  169. # windows likes static
  170. if (NOT ETH_STATIC)
  171. message("Forcing static linkage for MSVC.")
  172. set(ETH_STATIC 1)
  173. endif ()
  174. # If you don't have GCC, Clang or VC++ then you are on your own. Good luck!
  175. else ()
  176. message(WARNING "Your compiler is not tested, if you run into any issues, we'd welcome any patches.")
  177. endif ()
  178. if (SANITIZE)
  179. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=${SANITIZE}")
  180. if (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
  181. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize-blacklist=${CMAKE_SOURCE_DIR}/sanitizer-blacklist.txt")
  182. endif()
  183. endif()
  184. if (PROFILING AND (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")))
  185. set(CMAKE_CXX_FLAGS "-g ${CMAKE_CXX_FLAGS}")
  186. set(CMAKE_C_FLAGS "-g ${CMAKE_C_FLAGS}")
  187. add_definitions(-DETH_PROFILING_GPERF)
  188. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lprofiler")
  189. # set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -lprofiler")
  190. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lprofiler")
  191. endif ()
  192. if (PROFILING AND (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")))
  193. set(CMAKE_CXX_FLAGS "-g --coverage ${CMAKE_CXX_FLAGS}")
  194. set(CMAKE_C_FLAGS "-g --coverage ${CMAKE_C_FLAGS}")
  195. set(CMAKE_SHARED_LINKER_FLAGS "--coverage ${CMAKE_SHARED_LINKER_FLAGS} -lprofiler")
  196. set(CMAKE_EXE_LINKER_FLAGS "--coverage ${CMAKE_EXE_LINKER_FLAGS} -lprofiler")
  197. endif ()
  198. if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
  199. option(USE_LD_GOLD "Use GNU gold linker" ON)
  200. if (USE_LD_GOLD)
  201. execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
  202. if ("${LD_VERSION}" MATCHES "GNU gold")
  203. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fuse-ld=gold")
  204. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fuse-ld=gold")
  205. endif ()
  206. endif ()
  207. endif ()
  208. if(ETH_STATIC)
  209. set(BUILD_SHARED_LIBS OFF)
  210. else()
  211. set(BUILD_SHARED_LIBS ON)
  212. endif(ETH_STATIC)