Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake cleanup & MinGW support #1706

Merged
merged 13 commits into from
Sep 23, 2019
121 changes: 74 additions & 47 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.3)
message(FATAL_ERROR "Clang version must be at least 3.3!")
endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
if ("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "19.0")
message(FATAL_ERROR "MSVC version must be at least 19.0 (Visual Studio 2015 Update 1)!")
endif()

# allow MSVC VS2015 with Update 1, other 2015 versions are not supported
if ("${CMAKE_CXX_COMPILER_VERSION}" VERSION_EQUAL "19.0")
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "19.0.23506.0")
pmconrad marked this conversation as resolved.
Show resolved Hide resolved
message(FATAL_ERROR "Your version ${CMAKE_CXX_COMPILER_VERSION} of MSVC is not supported, use version 19.0.23506.0 (Visual Studio 2015 Update 1)!")
endif()
endif()
endif()

list( APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules" )
Expand All @@ -46,6 +57,20 @@ macro(FIND_CURL)
else (NOT WIN32 AND NOT APPLE AND CURL_STATICLIB)
find_package(CURL REQUIRED)
endif (NOT WIN32 AND NOT APPLE AND CURL_STATICLIB)

if( WIN32 )
if ( MSVC )
list( APPEND CURL_LIBRARIES Wldap32 )
endif( MSVC )

if( MINGW )
# MinGW requires a specific order of included libraries ( CURL before ZLib )
find_package( ZLIB REQUIRED )
list( APPEND CURL_LIBRARIES ${ZLIB_LIBRARY} pthread )
endif( MINGW )

list( APPEND CURL_LIBRARIES ${PLATFORM_SPECIFIC_LIBS} )
endif( WIN32 )
endmacro()

# Save the old value of CMAKE_REQUIRED_FLAGS
Expand All @@ -54,40 +79,40 @@ set( TEMP_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} )
# Fortify source
if (CMAKE_COMPILER_IS_GNUCXX)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
message ("-- Setting optimizations for clang++")
message (STATUS "Setting optimizations for clang++")
set(CMAKE_CXX_FLAGS_RELEASE "-D_FORTIFY_SOURCE=2 -O3 -DNDEBUG=1")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-D_FORTIFY_SOURCE=2 -O3 -DNDEBUG=1 -g")

# check and add data execution prevention
message ("-- Enabling data execution prevention")
message (STATUS "Enabling data execution prevention")
add_linker_flag("-fsanitize=safe-stack")

# check and add Stack-based buffer overrun detection
set(CMAKE_REQUIRED_FLAGS "-fstack-protector")
check_c_compiler_flag("" HAVE_STACKPROTECTOR)
if(HAVE_STACKPROTECTOR)
message ("-- Enabling stack-based buffer overrun detection")
message (STATUS "Enabling stack-based buffer overrun detection")
add_flag_append(CMAKE_C_FLAGS "-fstack-protector")
add_flag_append(CMAKE_CXX_FLAGS "-fstack-protector")
endif()
else ()
message ("-- Setting optimizations for g++")
message (STATUS "Setting optimizations for g++")
set(CMAKE_CXX_FLAGS_RELEASE "-D_FORTIFY_SOURCE=2 -O3 -DNDEBUG=1")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-D_FORTIFY_SOURCE=2 -O3 -DNDEBUG=1 -g")

# check and add data execution prevention
set(CMAKE_REQUIRED_FLAGS "-Wl,-znoexecstack")
check_c_compiler_flag("" HAVE_NOEXECSTACK)
if(HAVE_NOEXECSTACK)
message ("-- Enabling data execution prevention")
message (STATUS "Enabling data execution prevention")
add_linker_flag("-znoexecstack")
endif()

# check and add Stack-based buffer overrun detection
set(CMAKE_REQUIRED_FLAGS "-fstack-protector-strong")
check_c_compiler_flag("" HAVE_STACKPROTECTOR)
if(HAVE_STACKPROTECTOR)
message ("-- Enabling stack-based buffer overrun detection")
message (STATUS "Enabling stack-based buffer overrun detection")
add_flag_append(CMAKE_C_FLAGS "-fstack-protector-strong")
add_flag_append(CMAKE_CXX_FLAGS "-fstack-protector-strong")
endif()
Expand All @@ -99,15 +124,15 @@ endif ()
set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro,-znow")
check_c_compiler_flag("" HAVE_RELROFULL)
if(HAVE_RELROFULL)
message ("-- Enabling full data relocation and protection")
message (STATUS "Enabling full data relocation and protection")
add_linker_flag("-zrelro")
add_linker_flag("-znow")
else()
#if full relro is not available, try partial relro
set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro")
check_c_compiler_flag("" HAVE_RELROPARTIAL)
if(HAVE_RELROPARTIAL)
message ("-- Enabling partial data relocation and protection")
message (STATUS "Enabling partial data relocation and protection")
add_linker_flag("-zrelro")
endif()
endif()
Expand All @@ -116,7 +141,9 @@ set(CMAKE_REQUIRED_FLAGS ${TEMP_REQUIRED_FLAGS} )

# position independent executetable (PIE)
# position independent code (PIC)
add_definitions (-fPIC)
if (NOT MSVC)
add_definitions (-fPIC)
endif(NOT MSVC)

set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")
set( GRAPHENE_EGENESIS_JSON "${CMAKE_CURRENT_SOURCE_DIR}/libraries/egenesis/genesis.json"
Expand All @@ -128,9 +155,9 @@ endif(USE_PCH)

option(USE_PROFILER "Build with GPROF support(Linux)." OFF)

IF( NOT WIN32 )
list( APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/libraries/fc/CMakeModules" )
ENDIF( NOT WIN32 )
# Use Boost config file from fc
set(Boost_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libraries/fc/CMakeModules/Boost")

list( APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/libraries/fc/GitVersionGen" )
include( GetGitRevisionDescription )
get_git_head_revision( GIT_REFSPEC GIT_SHA2 )
Expand All @@ -144,14 +171,20 @@ LIST(APPEND BOOST_COMPONENTS thread
program_options
chrono
unit_test_framework
context)
context
coroutine
regex)
# boost::endian is also required, but FindBoost can't handle header-only libs
SET( Boost_USE_STATIC_LIBS ON CACHE STRING "ON or OFF" )

IF( WIN32 )
SET(BOOST_ROOT $ENV{BOOST_ROOT})
IF(WIN32)
if($ENV{BOOST_ROOT})
SET(BOOST_ROOT $ENV{BOOST_ROOT})
endif($ENV{BOOST_ROOT})
set(Boost_USE_MULTITHREADED ON)
set(BOOST_ALL_DYN_LINK OFF) # force dynamic linking for all libraries
add_definitions("-DCURL_STATICLIB")
list(APPEND PLATFORM_SPECIFIC_LIBS ws2_32 crypt32 mswsock userenv )
ELSE( WIN32 )
IF( APPLE )
set( CMAKE_THREAD_LIBS_INIT "-lpthread" )
Expand All @@ -162,28 +195,36 @@ ELSE( WIN32 )
ENDIF( APPLE )
ENDIF(WIN32)

FIND_PACKAGE(Boost 1.57 REQUIRED COMPONENTS ${BOOST_COMPONENTS})
# For Boost 1.53 on windows, coroutine was not in BOOST_LIBRARYDIR and do not need it to build, but if boost versin >= 1.54, find coroutine otherwise will cause link errors
IF(NOT "${Boost_VERSION}" MATCHES "1.53(.*)")
SET(BOOST_LIBRARIES_TEMP ${Boost_LIBRARIES})
FIND_PACKAGE(Boost 1.54 REQUIRED COMPONENTS coroutine)
LIST(APPEND BOOST_COMPONENTS coroutine)
SET(Boost_LIBRARIES ${BOOST_LIBRARIES_TEMP} ${Boost_LIBRARIES})
ENDIF()
FIND_PACKAGE(Boost CONFIG REQUIRED COMPONENTS ${BOOST_COMPONENTS})

# enforce more strict compiler warnings and errors
add_compiler_flag_if_available("-Wall")
add_compiler_flag_if_available("-Wclobbered")
add_compiler_flag_if_available("-Wempty-body")
add_compiler_flag_if_available("-Wformat-security")
add_compiler_flag_if_available("-Wignored-qualifiers")
add_compiler_flag_if_available("-Wimplicit-fallthrough=5")
add_compiler_flag_if_available("-Wmissing-field-initializers")
add_compiler_flag_if_available("-Wpointer-arith")
add_compiler_flag_if_available("-Wshift-negative-value")
add_compiler_flag_if_available("-Wtype-limits")
add_compiler_flag_if_available("-Wunused-but-set-parameter")

if( WIN32 )

message( STATUS "Configuring BitShares on WIN32")
set( DB_VERSION 60 )
set( BDB_STATIC_LIBS 1 )

set( ZLIB_LIBRARIES "" )
SET( DEFAULT_EXECUTABLE_INSTALL_DIR bin/ )
if ( MINGW )
message( STATUS "Windows build using MinGW" )
set( FULL_STATIC_BUILD TRUE )
else( MINGW )
set( ZLIB_LIBRARIES "" )
endif( MINGW )

set(CRYPTO_LIB)
SET( DEFAULT_EXECUTABLE_INSTALL_DIR bin/ )

if( MSVC )
add_definitions(-DWIN32_LEAN_AND_MEAN)
add_definitions(-DWIN32_LEAN_AND_MEAN)
#looks like this flag can have different default on some machines.
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
Expand All @@ -192,21 +233,6 @@ if( WIN32 )
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /DEBUG")
endif ( MSVC )

# On windows tcl should be installed to the directory pointed by setenv.bat script
SET(TCL_INCLUDE_PATH $ENV{TCL_ROOT}/include)
MESSAGE(STATUS "tcl INCLUDE PATH: ${TCL_INCLUDE_PATH}")

FIND_PACKAGE(TCL)
MESSAGE(STATUS "tcl_library: ${TCL_LIBRARY}")

SET(TCL_LIBS "optimized;${TCL_LIBRARY};debug;")
get_filename_component(TCL_LIB_PATH "${TCL_LIBRARY}" PATH)
get_filename_component(TCL_LIB_NAME "${TCL_LIBRARY}" NAME_WE)
get_filename_component(TCL_LIB_EXT "${TCL_LIBRARY}" EXT)

SET(TCL_LIBS "${TCL_LIBS}${TCL_LIB_PATH}/${TCL_LIB_NAME}g${TCL_LIB_EXT}")
SET(TCL_LIBRARY ${TCL_LIBS})

else( WIN32 ) # Apple AND Linux

if( APPLE )
Expand All @@ -233,9 +259,6 @@ else( WIN32 ) # Apple AND Linux
# if you have a normal install, you can define crypto_library to the empty string to avoid a build error
set( crypto_library crypto)
endif ()
if ( FULL_STATIC_BUILD )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
endif ( FULL_STATIC_BUILD )
endif( APPLE )

if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )
Expand All @@ -258,6 +281,10 @@ else( WIN32 ) # Apple AND Linux

endif( WIN32 )

if ( NOT MSVC AND FULL_STATIC_BUILD )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libstdc++ -static-libgcc" )
pmconrad marked this conversation as resolved.
Show resolved Hide resolved
endif ( NOT MSVC AND FULL_STATIC_BUILD )

set(ENABLE_COVERAGE_TESTING FALSE CACHE BOOL "Build BitShares for code coverage analysis")

if(ENABLE_COVERAGE_TESTING)
Expand Down
12 changes: 12 additions & 0 deletions CMakeModules/Utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ macro(add_linker_flag _FLAG)
add_flag_append(CMAKE_SHARED_LIBRARY_CXX_FLAGS "-Wl,${_FLAG}")
endmacro(add_linker_flag _FLAG)

include(CheckCXXCompilerFlag)

macro(add_compiler_flag_if_available _FLAG)
string(TOUPPER "${_FLAG}" _TEMPVAR1)
string(REPLACE "-" "_" _TEMPVAR2 "${_TEMPVAR1}")
string(CONCAT _TEMPVAR3 "HAVE" "${_TEMPVAR2}")
set(CMAKE_REQUIRED_FLAGS "${_FLAG}")
check_cxx_compiler_flag("${_FLAG}" ${_TEMPVAR3})
if(${_TEMPVAR3})
add_flag_append(CMAKE_CXX_FLAGS ${_FLAG})
endif()
endmacro(add_compiler_flag_if_available _VAR_NAME _FLAG)
10 changes: 5 additions & 5 deletions libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ if(MSVC)
set_source_files_properties( db_init.cpp db_block.cpp database.cpp block_database.cpp PROPERTIES COMPILE_FLAGS "/bigobj" )
endif(MSVC)

# Support for CMake < 3.7
if (${CMAKE_VERSION} VERSION_LESS "3.7.0")
MESSAGE(STATUS "Configuring for legacy CMake (CMake version ${CMAKE_VERSION} older than 3.7.0)")
# Support for CMake < 3.4
if (${CMAKE_VERSION} VERSION_LESS "3.4.0")
MESSAGE(STATUS "Configuring for legacy CMake (CMake version ${CMAKE_VERSION} older than 3.4.0)")
MESSAGE(STATUS " Target propertes not supported (SOURCE_DIR, BINARY_DIR)")

set(GRAPHENE_CHAIN_BIN_LEGACY "${PROJECT_BINARY_DIR}")
set(GRAPHENE_CHAIN_SOURCE_LEGACY "${PROJECT_SOURCE_DIR}")
set(GRAPHENE_CHAIN_BIN_LEGACY "${PROJECT_BINARY_DIR}/libraries/chain" CACHE STRING "Path to fc chain")
set(GRAPHENE_CHAIN_SOURCE_LEGACY "${PROJECT_SOURCE_DIR}/libraries/chain" CACHE STRING "Path to fc chain")
endif ()

INSTALL( TARGETS
Expand Down
2 changes: 1 addition & 1 deletion libraries/plugins/elasticsearch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if(CURL_STATICLIB)
endif(CURL_STATICLIB)
target_link_libraries( graphene_elasticsearch graphene_chain graphene_app ${CURL_LIBRARIES} )
target_include_directories( graphene_elasticsearch
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
PUBLIC "${CURL_INCLUDE_DIR}" )


Expand Down
4 changes: 4 additions & 0 deletions libraries/plugins/witness/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ target_link_libraries( graphene_witness graphene_chain graphene_app )
target_include_directories( graphene_witness
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )

if (MSVC)
set_target_properties( graphene_witness PROPERTIES COMPILE_FLAGS "/bigobj" )
endif(MSVC)

install( TARGETS
graphene_witness

Expand Down
2 changes: 1 addition & 1 deletion libraries/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if(CURL_STATICLIB)
SET_TARGET_PROPERTIES(graphene_utilities PROPERTIES
COMPILE_DEFINITIONS "CURL_STATICLIB")
endif(CURL_STATICLIB)
target_link_libraries( graphene_utilities fc ${CURL_LIBRARIES})
target_link_libraries( graphene_utilities fc ${CURL_LIBRARIES} )
target_include_directories( graphene_utilities
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )
if (USE_PCH)
Expand Down
4 changes: 2 additions & 2 deletions programs/build_helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
get_target_property(GRAPHENE_CHAIN_SOURCE graphene_chain SOURCE_DIR)
get_target_property(GRAPHENE_CHAIN_BIN graphene_chain BINARY_DIR)

# This is here only to support CMake < 3.7
# This is here only to support CMake < 3.4
if (NOT GRAPHENE_CHAIN_SOURCE)
set(GRAPHENE_CHAIN_SOURCE ${GRAPHENE_CHAIN_SOURCE_LEGACY})
endif()
# This is here only to support CMake < 3.7
# This is here only to support CMake < 3.4
if (NOT GRAPHENE_CHAIN_BIN)
set(GRAPHENE_CHAIN_BIN ${GRAPHENE_CHAIN_BIN_LEGACY})
endif()
Expand Down
4 changes: 4 additions & 0 deletions programs/witness_node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ PRIVATE graphene_app graphene_delayed_node graphene_account_history graphene_ela
graphene_api_helper_indexes graphene_custom_operations
fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} )

if (MSVC)
set_target_properties( witness_node PROPERTIES COMPILE_FLAGS "/bigobj" )
endif(MSVC)

install( TARGETS
witness_node

Expand Down
4 changes: 2 additions & 2 deletions tests/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

#include <boost/filesystem/path.hpp>

#include "../../libraries/app/application_impl.hxx"

#define BOOST_TEST_MODULE Test Application
#include <boost/test/included/unit_test.hpp>

Expand Down Expand Up @@ -332,8 +334,6 @@ BOOST_AUTO_TEST_CASE( two_node_network )

// a contrived example to test the breaking out of application_impl to a header file

#include "../../libraries/app/application_impl.hxx"

BOOST_AUTO_TEST_CASE(application_impl_breakout) {
class test_impl : public graphene::app::detail::application_impl {
// override the constructor, just to test that we can
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#define _WIN32_WINNT 0x0501
#endif
#include <winsock2.h>
#include <WS2tcpip.h>
#include <ws2tcpip.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/database_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(failed_modify_test)
BOOST_CHECK_THROW(db.modify(obj, [](account_balance_object& obj) {
throw 5;
}), int);
BOOST_CHECK_NE((long)db.find_object(obj_id), (long)nullptr);
BOOST_CHECK(db.find_object(obj_id));
} FC_LOG_AND_RETHROW() }

BOOST_AUTO_TEST_CASE( flat_index_test )
Expand Down