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.
pull/95/head
redfish 8 years ago
parent 0fbe9cfcdb
commit 042db0bd0f

@ -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()

@ -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)

Loading…
Cancel
Save