2016-08-01 05:25:37 +00:00
#------------------------------------------------------------------------------
# EthCompilerSettings.cmake
#
# CMake file for cpp-ethereum project which specifies our compiler settings
# for each supported platform and build configuration.
#
2017-09-21 10:41:06 +00:00
# The documentation for cpp-ethereum is hosted at http://cpp-ethereum.org
2016-08-01 05:25:37 +00:00
#
# Copyright (c) 2014-2016 cpp-ethereum contributors.
#------------------------------------------------------------------------------
# Clang seeks to be command-line compatible with GCC as much as possible, so
# most of our compiler settings are common between GCC and Clang.
#
# These settings then end up spanning all POSIX platforms (Linux, OS X, BSD, etc)
2017-09-21 10:41:06 +00:00
include ( EthCheckCXXCompilerFlag )
2017-08-23 12:01:51 +00:00
2020-06-03 17:22:17 +00:00
if ( NOT EMSCRIPTEN )
eth_add_cxx_compiler_flag_if_supported ( -fstack-protector-strong have_stack_protector_strong_support )
if ( NOT have_stack_protector_strong_support )
eth_add_cxx_compiler_flag_if_supported ( -fstack-protector )
endif ( )
2017-08-23 12:01:51 +00:00
endif ( )
2017-09-21 10:41:06 +00:00
eth_add_cxx_compiler_flag_if_supported ( -Wimplicit-fallthrough )
2019-10-02 15:25:10 +00:00
# Prevent the path of the source directory from ending up in the binary via __FILE__ macros.
eth_add_cxx_compiler_flag_if_supported ( "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=/solidity" )
2020-04-02 00:11:03 +00:00
# -Wpessimizing-move warns when a call to std::move would prevent copy elision
# if the argument was not wrapped in a call. This happens when moving a local
# variable in a return statement when the variable is the same type as the
# return type or using a move to create a new object from a temporary object.
eth_add_cxx_compiler_flag_if_supported ( -Wpessimizing-move )
# -Wredundant-move warns when an implicit move would already be made, so the
# std::move call is not needed, such as when moving a local variable in a return
# that is different from the return type.
eth_add_cxx_compiler_flag_if_supported ( -Wredundant-move )
2016-08-01 05:25:37 +00:00
if ( ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" ) OR ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) )
# Enables all the warnings about constructions that some users consider questionable,
# and that are easy to avoid. Also enable some extra warning flags that are not
# enabled by -Wall. Finally, treat at warnings-as-errors, which forces developers
# to fix warnings as they arise, so they don't accumulate "to be fixed later".
add_compile_options ( -Wall )
add_compile_options ( -Wextra )
add_compile_options ( -Werror )
2019-12-12 23:25:12 +00:00
add_compile_options ( -pedantic )
add_compile_options ( -Wno-unknown-pragmas )
add_compile_options ( -Wimplicit-fallthrough )
2020-06-03 08:25:46 +00:00
add_compile_options ( -Wsign-conversion )
2016-08-01 05:25:37 +00:00
# Configuration-specific compiler settings.
2019-04-16 15:47:06 +00:00
set ( CMAKE_CXX_FLAGS_DEBUG "-O0 -g3 -DETH_DEBUG" )
2016-08-01 05:25:37 +00:00
set ( CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG" )
set ( CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG" )
2019-04-16 15:47:06 +00:00
set ( CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g3" )
2016-08-01 05:25:37 +00:00
# Additional GCC-specific compiler settings.
if ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" )
2019-06-05 09:55:26 +00:00
# Check that we've got GCC 5.0 or newer.
2016-08-01 05:25:37 +00:00
execute_process (
C O M M A N D $ { C M A K E _ C X X _ C O M P I L E R } - d u m p v e r s i o n O U T P U T _ V A R I A B L E G C C _ V E R S I O N )
2019-06-05 09:55:26 +00:00
if ( NOT ( GCC_VERSION VERSION_GREATER 5.0 OR GCC_VERSION VERSION_EQUAL 5.0 ) )
message ( FATAL_ERROR "${PROJECT_NAME} requires g++ 5.0 or greater." )
2016-08-01 05:25:37 +00:00
endif ( )
2020-01-22 16:12:41 +00:00
# Use fancy colors in the compiler diagnostics
add_compile_options ( -fdiagnostics-color )
2016-08-01 05:25:37 +00:00
# Additional Clang-specific compiler settings.
elseif ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )
2018-02-05 21:15:05 +00:00
if ( "${CMAKE_SYSTEM_NAME}" MATCHES "Darwin" )
2018-05-15 13:11:38 +00:00
# Set stack size to 32MB - by default Apple's clang defines a stack size of 8MB.
# Normally 16MB is enough to run all tests, but it will exceed the stack, if -DSANITIZE=address is used.
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-stack_size -Wl,0x2000000" )
2019-04-30 11:38:28 +00:00
# Boost libraries use visibility=hidden to reduce unnecessary DWARF entries.
# Unless we match visibility, ld will give a warning message like:
# ld: warning: direct access in function 'boost::filesystem... from file ...
# means the weak symbol cannot be overridden at runtime. This was likely caused by different translation units being compiled with different visibility settings.
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden" )
2018-02-05 21:15:05 +00:00
endif ( )
2016-08-01 05:25:37 +00:00
# Some Linux-specific Clang settings. We don't want these for OS X.
if ( "${CMAKE_SYSTEM_NAME}" MATCHES "Linux" )
2018-02-05 21:15:05 +00:00
2016-08-01 05:25:37 +00:00
# TODO - Is this even necessary? Why?
# See http://stackoverflow.com/questions/19774778/when-is-it-necessary-to-use-use-the-flag-stdlib-libstdc.
add_compile_options ( -stdlib=libstdc++ )
2018-07-21 22:08:47 +00:00
2016-08-01 05:25:37 +00:00
# Tell Boost that we're using Clang's libc++. Not sure exactly why we need to do.
add_definitions ( -DBOOST_ASIO_HAS_CLANG_LIBCXX )
2018-07-21 22:08:47 +00:00
2016-08-01 05:25:37 +00:00
# Use fancy colors in the compiler diagnostics
add_compile_options ( -fcolor-diagnostics )
2018-07-21 22:08:47 +00:00
2016-08-01 05:25:37 +00:00
# See "How to silence unused command line argument error with clang without disabling it?"
# When using -Werror with clang, it transforms "warning: argument unused during compilation" messages
# into errors, which makes sense.
# http://stackoverflow.com/questions/21617158/how-to-silence-unused-command-line-argument-error-with-clang-without-disabling-i
add_compile_options ( -Qunused-arguments )
2017-08-28 15:04:41 +00:00
elseif ( EMSCRIPTEN )
2017-07-18 20:57:47 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --memory-init-file 0" )
2018-07-10 07:18:59 +00:00
# Leave only exported symbols as public and aggressively remove others
2019-11-28 12:36:25 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdata-sections -ffunction-sections -fvisibility=hidden" )
2017-07-18 20:57:47 +00:00
# Optimisation level
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3" )
# Re-enable exception catching (optimisations above -O1 disable it)
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s DISABLE_EXCEPTION_CATCHING=0" )
# Remove any code related to exit (such as atexit)
2020-06-03 17:22:17 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s EXIT_RUNTIME=0" )
2017-07-18 20:57:47 +00:00
# Remove any code related to filesystem access
2020-06-03 17:22:17 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s FILESYSTEM=0" )
2017-07-18 20:57:47 +00:00
# Allow memory growth, but disable some optimisations
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ALLOW_MEMORY_GROWTH=1" )
# Disable eval()
2020-06-03 17:22:17 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s DYNAMIC_EXECUTION=0" )
2018-02-09 17:10:21 +00:00
# Disable greedy exception catcher
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s NODEJS_CATCH_EXIT=0" )
2018-02-20 22:23:30 +00:00
# Abort if linking results in any undefined symbols
# Note: this is on by default in the CMake Emscripten module which we aren't using
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s ERROR_ON_UNDEFINED_SYMBOLS=1" )
2018-12-07 10:36:54 +00:00
# Disallow deprecated emscripten build options.
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s STRICT=1" )
2018-07-11 15:15:03 +00:00
# Export the Emscripten-generated auxiliary methods which are needed by solc-js.
# Which methods of libsolc itself are exported is specified in libsolc/CMakeLists.txt.
2019-11-28 18:47:08 +00:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s EXTRA_EXPORTED_RUNTIME_METHODS=['cwrap','addFunction','removeFunction','UTF8ToString','lengthBytesUTF8','stringToUTF8','setValue']" )
# Build for webassembly target.
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM=1" )
# Set webassembly build to synchronous loading.
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM_ASYNC_COMPILATION=0" )
# Output a single js file with the wasm binary embedded as base64 string.
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s SINGLE_FILE=1" )
2019-11-28 19:41:28 +00:00
# Disable warnings about not being pure asm.js due to memory growth.
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-almost-asm" )
2016-08-01 05:25:37 +00:00
endif ( )
endif ( )
# The major alternative compiler to GCC/Clang is Microsoft's Visual C++ compiler, only available on Windows.
elseif ( DEFINED MSVC )
add_compile_options ( /MP ) # enable parallel compilation
add_compile_options ( /EHsc ) # specify Exception Handling Model in msvc
add_compile_options ( /WX ) # enable warnings-as-errors
add_compile_options ( /wd4068 ) # disable unknown pragma warning (4068)
add_compile_options ( /wd4996 ) # disable unsafe function warning (4996)
add_compile_options ( /wd4503 ) # disable decorated name length exceeded, name was truncated (4503)
add_compile_options ( /wd4267 ) # disable conversion from 'size_t' to 'type', possible loss of data (4267)
add_compile_options ( /wd4180 ) # disable qualifier applied to function type has no meaning; ignored (4180)
add_compile_options ( /wd4290 ) # disable C++ exception specification ignored except to indicate a function is not __declspec(nothrow) (4290)
add_compile_options ( /wd4244 ) # disable conversion from 'type1' to 'type2', possible loss of data (4244)
add_compile_options ( /wd4800 ) # disable forcing value to bool 'true' or 'false' (performance warning) (4800)
add_compile_options ( -D_WIN32_WINNT=0x0600 ) # declare Windows Vista API requirement
add_compile_options ( -DNOMINMAX ) # undefine windows.h MAX && MIN macros cause it cause conflicts with std::min && std::max functions
2020-08-21 22:01:29 +00:00
add_compile_options ( /utf-8 ) # enable utf-8 encoding (solves warning 4819)
2019-10-16 00:31:41 +00:00
add_compile_options ( -DBOOST_REGEX_NO_LIB ) # disable automatic boost::regex library selection
add_compile_options ( -D_REGEX_MAX_STACK_COUNT=200000L ) # increase std::regex recursion depth limit
2020-08-21 22:01:29 +00:00
add_compile_options ( /permissive- ) # specify standards conformance mode to the compiler
2016-08-01 05:25:37 +00:00
# disable empty object file warning
set ( CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221" )
# warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification
# warning LNK4099: pdb was not found with lib
# stack size 16MB
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ignore:4099,4075 /STACK:16777216" )
# If you don't have GCC, Clang or VC++ then you are on your own. Good luck!
else ( )
message ( WARNING "Your compiler is not tested, if you run into any issues, we'd welcome any patches." )
endif ( )
if ( SANITIZE )
2019-05-22 09:13:31 +00:00
# Perform case-insensitive string compare
string ( TOLOWER "${SANITIZE}" san )
# -fno-omit-frame-pointer gives more informative stack trace in case of an error
# -fsanitize-address-use-after-scope throws an error when a variable is used beyond its scope
if ( san STREQUAL "address" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address -fsanitize-address-use-after-scope" )
endif ( )
2016-08-01 05:25:37 +00:00
endif ( )
2018-07-04 13:12:08 +00:00
# Code coverage support.
# Copied from Cable:
# https://github.com/ethereum/cable/blob/v0.2.4/CableCompilerSettings.cmake#L118-L132
option ( COVERAGE "Build with code coverage support" OFF )
if ( COVERAGE )
# Set the linker flags first, they are required to properly test the compiler flag.
set ( CMAKE_SHARED_LINKER_FLAGS "--coverage ${CMAKE_SHARED_LINKER_FLAGS}" )
set ( CMAKE_EXE_LINKER_FLAGS "--coverage ${CMAKE_EXE_LINKER_FLAGS}" )
set ( CMAKE_REQUIRED_LIBRARIES "--coverage ${CMAKE_REQUIRED_LIBRARIES}" )
check_cxx_compiler_flag ( --coverage have_coverage )
string ( REPLACE "--coverage " "" CMAKE_REQUIRED_LIBRARIES ${ CMAKE_REQUIRED_LIBRARIES } )
if ( NOT have_coverage )
message ( FATAL_ERROR "Coverage not supported" )
endif ( )
add_compile_options ( -g --coverage )
endif ( )
2016-08-01 05:25:37 +00:00
2018-08-08 14:46:17 +00:00
# SMT Solvers integration
option ( USE_Z3 "Allow compiling with Z3 SMT solver integration" ON )
option ( USE_CVC4 "Allow compiling with CVC4 SMT solver integration" ON )
2016-08-01 05:25:37 +00:00
if ( ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" ) OR ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) )
option ( USE_LD_GOLD "Use GNU gold linker" ON )
if ( USE_LD_GOLD )
2018-12-19 16:28:05 +00:00
execute_process ( COMMAND ${ CMAKE_CXX_COMPILER } -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION )
2016-08-01 05:25:37 +00:00
if ( "${LD_VERSION}" MATCHES "GNU gold" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fuse-ld=gold" )
endif ( )
endif ( )
endif ( )