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.
This commit is contained in:
redfish 2016-07-28 21:19:01 -04:00
parent 0fbe9cfcdb
commit 042db0bd0f
2 changed files with 36 additions and 32 deletions

View File

@ -261,17 +261,10 @@ option(STACK_TRACE "Install a hook that dumps stack on exception" ${DEFAULT_STAC
if(STACK_TRACE) if(STACK_TRACE)
message(STATUS "Stack trace on exception enabled") 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() else()
message(STATUS "Stack trace on exception disabled") message(STATUS "Stack trace on exception disabled")
endif() endif()
if (UNIX AND NOT APPLE) if (UNIX AND NOT APPLE)
# Note that at the time of this writing the -Wstrict-prototypes flag added below will make this fail # Note that at the time of this writing the -Wstrict-prototypes flag added below will make this fail
set(THREADS_PREFER_PTHREAD_FLAG ON) set(THREADS_PREFER_PTHREAD_FLAG ON)
@ -309,8 +302,8 @@ link_directories(${LIBUNWIND_LIBRARY_DIRS})
if(MSVC) 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__") 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(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Dinline=__inline")
set(COMMON_EXE_LINKER_FLAGS "${COMMON_EXE_LINKER_FLAGS} /STACK:10485760") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10485760")
if(STATIC) if(STATIC)
foreach(VAR CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE) foreach(VAR CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE)
string(REPLACE "/MD" "/MT" ${VAR} "${${VAR}}") string(REPLACE "/MD" "/MT" ${VAR} "${${VAR}}")
@ -348,7 +341,7 @@ else()
include_directories(SYSTEM src/platform/mingw) include_directories(SYSTEM src/platform/mingw)
# mingw doesn't support LTO (multiple definition errors at link time) # mingw doesn't support LTO (multiple definition errors at link time)
set(USE_LTO_DEFAULT false) 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) if(NOT BUILD_64)
add_definitions(-DWINVER=0x0501 -D_WIN32_WINNT=0x0501) add_definitions(-DWINVER=0x0501 -D_WIN32_WINNT=0x0501)
endif() endif()
@ -362,20 +355,20 @@ else()
set(STATIC_ASSERT_FLAG "-Dstatic_assert=_Static_assert") set(STATIC_ASSERT_FLAG "-Dstatic_assert=_Static_assert")
endif() endif()
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -std=c11 -D_GNU_SOURCE ${MINGW_FLAG} ${STATIC_ASSERT_FLAG} ${WARNINGS} ${C_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(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -std=c++11 -D_GNU_SOURCE ${MINGW_FLAG} ${WARNINGS} ${CXX_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 # 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. # is fixed in the code (Issue #847), force compiler to be conservative.
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -fno-strict-aliasing") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -fno-strict-aliasing") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
option(NO_AES "Explicitly disable AES support" ${NO_AES}) option(NO_AES "Explicitly disable AES support" ${NO_AES})
if(NOT NO_AES AND NOT (ARM6 OR ARM7)) if(NOT NO_AES AND NOT (ARM6 OR ARM7))
message(STATUS "AES support enabled") message(STATUS "AES support enabled")
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -maes") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes")
set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -maes") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes")
elseif(ARM7 OR ARM6) elseif(ARM7 OR ARM6)
message(STATUS "AES support disabled (not available on ARM)") message(STATUS "AES support disabled (not available on ARM)")
else() else()
@ -384,18 +377,18 @@ else()
if(ARM6) if(ARM6)
message(STATUS "Setting ARM6 C and C++ flags") message(STATUS "Setting ARM6 C and C++ flags")
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -mfpu=vfp -mfloat-abi=hard") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=vfp -mfloat-abi=hard")
set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -mfpu=vfp -mfloat-abi=hard") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=vfp -mfloat-abi=hard")
endif() endif()
if(ARM7) if(ARM7)
message(STATUS "Setting ARM7 C and C++ flags") message(STATUS "Setting ARM7 C and C++ flags")
set(COMMON_C_FLAGS "${COMMON_C_FLAGS} -O2 -mfloat-abi=hard") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -mfloat-abi=hard")
set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -O2 -mfloat-abi=hard") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -mfloat-abi=hard")
endif() endif()
if(APPLE) 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() endif()
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)) if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8))
set(DEBUG_FLAGS "-g3 -Og") set(DEBUG_FLAGS "-g3 -Og")
@ -413,8 +406,8 @@ else()
set(USE_LTO false) set(USE_LTO false)
# explicitly define stdlib for older versions of clang # explicitly define stdlib for older versions of clang
if(CMAKE_C_COMPILER_VERSION VERSION_LESS 3.7) if(CMAKE_C_COMPILER_VERSION VERSION_LESS 3.7)
set(COMMON_CXX_FLAGS "${COMMON_CXX_FLAGS} -stdlib=libc++") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(COMMON_EXE_LINKER_FLAGS "${COMMON_EXE_LINKER_FLAGS} -stdlib=libc++") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
endif() endif()
endif() endif()
@ -437,7 +430,7 @@ else()
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${RELEASE_FLAGS}") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${RELEASE_FLAGS}")
if(STATIC AND NOT APPLE AND NOT FREEBSD AND NOT OPENBSD) 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()
endif() endif()
@ -486,22 +479,21 @@ endif()
include(version.cmake) include(version.cmake)
# When building the following sources treat warnings as errors, install throw wrapper function (treat_warnings_as_errors dirs)
set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} ${WARNINGS_AS_ERRORS_FLAG} ${STACK_TRACE_C_FLAG}") foreach(dir ${ARGV})
set(CMAKE_CXX_FLAGS "${COMMON_CXX_FLAGS} ${WARNINGS_AS_ERRORS_FLAG} ${STACK_TRACE_C_FLAG}") set_property(DIRECTORY ${dir}
set(CMAKE_EXE_LINKER_FLAGS "${COMMON_EXE_LINKER_FLAGS} ${STACK_TRACE_LINK_FLAG}") APPEND PROPERTY COMPILE_FLAGS "-Werror")
endforeach()
endfunction()
add_subdirectory(contrib) add_subdirectory(contrib)
add_subdirectory(src) add_subdirectory(src)
treat_warnings_as_errors(contrib src)
option(BUILD_TESTS "Build tests." OFF) option(BUILD_TESTS "Build tests." OFF)
if(BUILD_TESTS) 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) add_subdirectory(tests)
endif() endif()

View File

@ -47,6 +47,17 @@ function (bitmonero_install_headers subdir)
COMPONENT development) COMPONENT development)
endfunction () 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) function (bitmonero_add_executable name)
source_group("${name}" source_group("${name}"
FILES FILES
@ -63,6 +74,7 @@ function (bitmonero_add_executable name)
set_property(TARGET "${name}" set_property(TARGET "${name}"
PROPERTY PROPERTY
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
enable_stack_trace("${name}")
endfunction () endfunction ()
function (bitmonero_add_library name) function (bitmonero_add_library name)