From 042db0bd0f852b5643870588f48643db88b77035 Mon Sep 17 00:00:00 2001 From: redfish Date: Thu, 28 Jul 2016 21:19:01 -0400 Subject: [PATCH 1/3] cmake: cleanup logic that sets flags per target/subdir The previous logic that used a COMMON_*_FLAGS intermediate variable and then re-assigned CMAKE_*_FLAGS before including each subdirectory was confusing and ugly. This PR is the right way to do it. This commit is purely refactoring: built binaries unchanged. --- CMakeLists.txt | 56 ++++++++++++++++++++-------------------------- src/CMakeLists.txt | 12 ++++++++++ 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 63d39ca7..964cd1fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,17 +261,10 @@ option(STACK_TRACE "Install a hook that dumps stack on exception" ${DEFAULT_STAC if(STACK_TRACE) message(STATUS "Stack trace on exception enabled") - # Don't set CMAKE_*_FLAGS directly or add_definitions, because this flag must - # not be set for tests targets (TODO: per-target logic into nested CMakeLists) - set(STACK_TRACE_C_FLAG "-DSTACK_TRACE") - if (STATIC) - set(STACK_TRACE_LINK_FLAG "-Wl,--wrap=__cxa_throw") - endif() else() message(STATUS "Stack trace on exception disabled") endif() - if (UNIX AND NOT APPLE) # Note that at the time of this writing the -Wstrict-prototypes flag added below will make this fail set(THREADS_PREFER_PTHREAD_FLAG ON) @@ -309,8 +302,8 @@ link_directories(${LIBUNWIND_LIBRARY_DIRS}) if(MSVC) add_definitions("/bigobj /MP /W3 /GS- /D_CRT_SECURE_NO_WARNINGS /wd4996 /wd4345 /D_WIN32_WINNT=0x0600 /DWIN32_LEAN_AND_MEAN /DGTEST_HAS_TR1_TUPLE=0 /FIinline_c.h /D__SSE4_1__") - # set(COMMON_C_FLAGS "${COMMON_C_FLAGS} /Dinline=__inline") - set(COMMON_EXE_LINKER_FLAGS "${COMMON_EXE_LINKER_FLAGS} /STACK:10485760") + # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Dinline=__inline") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10485760") if(STATIC) foreach(VAR CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE) string(REPLACE "/MD" "/MT" ${VAR} "${${VAR}}") @@ -348,7 +341,7 @@ else() include_directories(SYSTEM src/platform/mingw) # mingw doesn't support LTO (multiple definition errors at link time) set(USE_LTO_DEFAULT false) - set(COMMON_EXE_LINKER_FLAGS "${COMMON_EXE_LINKER_FLAGS} -Wl,--stack,10485760") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,10485760") if(NOT BUILD_64) add_definitions(-DWINVER=0x0501 -D_WIN32_WINNT=0x0501) endif() @@ -362,20 +355,20 @@ else() set(STATIC_ASSERT_FLAG "-Dstatic_assert=_Static_assert") endif() - set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -std=c11 -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_WARNINGS} ${ARCH_FLAG}") - set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE ${MINGW_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${ARCH_FLAG}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_WARNINGS} ${ARCH_FLAG}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE ${MINGW_FLAG} ${WARNINGS} ${CXX_WARNINGS} ${ARCH_FLAG}") # With GCC 6.1.1 the compiled binary malfunctions due to aliasing. Until that # is fixed in the code (Issue #847), force compiler to be conservative. - set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -fno-strict-aliasing") - set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -fno-strict-aliasing") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing") option(NO_AES "Explicitly disable AES support" ${NO_AES}) if(NOT NO_AES AND NOT (ARM6 OR ARM7)) message(STATUS "AES support enabled") - set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -maes") - set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -maes") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes") elseif(ARM7 OR ARM6) message(STATUS "AES support disabled (not available on ARM)") else() @@ -384,18 +377,18 @@ else() if(ARM6) message(STATUS "Setting ARM6 C and C++ flags") - set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -mfpu=vfp -mfloat-abi=hard") - set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -mfpu=vfp -mfloat-abi=hard") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=vfp -mfloat-abi=hard") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=vfp -mfloat-abi=hard") endif() if(ARM7) message(STATUS "Setting ARM7 C and C++ flags") - set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -O2 -mfloat-abi=hard") - set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -O2 -mfloat-abi=hard") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -mfloat-abi=hard") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -mfloat-abi=hard") endif() if(APPLE) - set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -DGTEST_HAS_TR1_TUPLE=0") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_HAS_TR1_TUPLE=0") endif() if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)) set(DEBUG_FLAGS "-g3 -Og") @@ -413,8 +406,8 @@ else() set(USE_LTO false) # explicitly define stdlib for older versions of clang if(CMAKE_C_COMPILER_VERSION VERSION_LESS 3.7) - set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -stdlib=libc++") - set(COMMON_EXE_LINKER_FLAGS "${COMMON_EXE_LINKER_FLAGS} -stdlib=libc++") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++") endif() endif() @@ -437,7 +430,7 @@ else() set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${RELEASE_FLAGS}") if(STATIC AND NOT APPLE AND NOT FREEBSD AND NOT OPENBSD) - set(COMMON_EXE_LINKER_FLAGS "${COMMON_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++") endif() endif() @@ -486,22 +479,21 @@ endif() include(version.cmake) -# When building the following sources treat warnings as errors, install throw wrapper -set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} ${WARNINGS_AS_ERRORS_FLAG} ${STACK_TRACE_C_FLAG}") -set(CMAKE_CXX_FLAGS "${COMMON_CXX_FLAGS} ${WARNINGS_AS_ERRORS_FLAG} ${STACK_TRACE_C_FLAG}") -set(CMAKE_EXE_LINKER_FLAGS "${COMMON_EXE_LINKER_FLAGS} ${STACK_TRACE_LINK_FLAG}") +function (treat_warnings_as_errors dirs) + foreach(dir ${ARGV}) + set_property(DIRECTORY ${dir} + APPEND PROPERTY COMPILE_FLAGS "-Werror") + endforeach() +endfunction() add_subdirectory(contrib) add_subdirectory(src) +treat_warnings_as_errors(contrib src) option(BUILD_TESTS "Build tests." OFF) if(BUILD_TESTS) - # When building tests, don't add some of the flags added to source build - set(CMAKE_C_FLAGS "${COMMON_C_FLAGS}") - set(CMAKE_CXX_FLAGS "${COMMON_CXX_FLAGS}") - set(CMAKE_EXE_LINKER_FLAGS "${COMMON_CXX_FLAGS}") add_subdirectory(tests) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e2349744..8fa617af 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,6 +47,17 @@ function (bitmonero_install_headers subdir) COMPONENT development) endfunction () +function (enable_stack_trace target) + if(STACK_TRACE) + set_property(TARGET ${target} + APPEND PROPERTY COMPILER_DEFINITIONS "-DSTACK_TRACE") + if (STATIC) + set_property(TARGET "${target}" + APPEND PROPERTY LINK_FLAGS "-Wl,--wrap=__cxa_throw") + endif() + endif() +endfunction() + function (bitmonero_add_executable name) source_group("${name}" FILES @@ -63,6 +74,7 @@ function (bitmonero_add_executable name) set_property(TARGET "${name}" PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + enable_stack_trace("${name}") endfunction () function (bitmonero_add_library name) From 35dc40af421c66f574cdba601c0bb995bc8978dc Mon Sep 17 00:00:00 2001 From: redfish Date: Thu, 28 Jul 2016 23:26:51 -0400 Subject: [PATCH 2/3] cmake: libatomic only needed for 32-bit Clang builds --- CMakeLists.txt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 964cd1fe..93423462 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -470,11 +470,9 @@ endif() list(APPEND EXTRA_LIBRARIES ${CMAKE_DL_LIBS}) -if(CMAKE_C_COMPILER_ID STREQUAL "Clang") - if(NOT MINGW AND NOT APPLE) - find_library(ATOMIC atomic) - list(APPEND EXTRA_LIBRARIES ${ATOMIC}) - endif() +if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND ARCH_WIDTH EQUAL "32") + find_library(ATOMIC atomic) + list(APPEND EXTRA_LIBRARIES ${ATOMIC}) endif() include(version.cmake) From 33b5ebd055846c7568736aad9cda3bbe8515dd01 Mon Sep 17 00:00:00 2001 From: redfish Date: Thu, 28 Jul 2016 23:34:05 -0400 Subject: [PATCH 3/3] cmake: do not pass -O2 in debug build on ARM Also, minor cleanup of redundant flag-setting code. --- CMakeLists.txt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93423462..90d06c12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,13 +73,14 @@ if (NOT "${ARCH}" STREQUAL "") endif() if(WIN32 OR ARM7 OR ARM6) - set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG") - set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") + set(OPT_FLAGS_RELEASE "-O2") else() - set(CMAKE_C_FLAGS_RELEASE "-Ofast -DNDEBUG -Wno-unused-variable") - set(CMAKE_CXX_FLAGS_RELEASE "-Ofast -DNDEBUG -Wno-unused-variable") + set(OPT_FLAGS_RELEASE "-Ofast") endif() +set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG ${OPT_FLAGS_RELEASE}") +set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${OPT_FLAGS_RELEASE}") + # set this to 0 if per-block checkpoint needs to be disabled set(PER_BLOCK_CHECKPOINT 1) @@ -322,7 +323,7 @@ else() set(ARCH_FLAG "-march=${ARCH}") endif() endif() - set(WARNINGS "-Wall -Wextra -Wpointer-arith -Wundef -Wvla -Wwrite-strings -Wno-error=extra -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=undef -Wno-error=uninitialized") + set(WARNINGS "-Wall -Wextra -Wpointer-arith -Wundef -Wvla -Wwrite-strings -Wno-error=extra -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-unused-variable -Wno-error=unused-variable -Wno-error=undef -Wno-error=uninitialized") if(NOT MINGW) set(WARNINGS_AS_ERRORS_FLAG "-Werror") endif() @@ -383,8 +384,8 @@ else() if(ARM7) message(STATUS "Setting ARM7 C and C++ flags") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -mfloat-abi=hard") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -mfloat-abi=hard") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfloat-abi=hard") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=hard") endif() if(APPLE)