Reorganized CMake script; unit tests and cppcheck will be used only if CMAKE_BUILD_TYPE=Debug, otherwise they are not required

This commit is contained in:
Pavel Kirienko
2014-03-28 22:36:55 +04:00
parent b25efbb996
commit 0e2965e6fb
+27 -19
View File
@@ -1,13 +1,20 @@
#
# Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
#
cmake_minimum_required(VERSION 2.6)
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Default build type is RelWithDebInfo" FORCE)
if(DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Debug Release RelWithDebInfo MinSizeRel")
else()
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Debug Release RelWithDebInfo MinSizeRel")
endif()
project(libuavcan)
#
# DSDSL compiler invocation
# DSDL compiler invocation
# Probably output files should be saved into CMAKE_BINARY_DIR?
#
set(DSDLC_INPUTS "test/dsdl_test/root_ns_a" "test/dsdl_test/root_ns_b" "${CMAKE_SOURCE_DIR}/../dsdl/uavcan")
set(DSDLC_OUTPUT "include/dsdlc_generated")
@@ -16,12 +23,13 @@ add_custom_target(dsdlc dsdl_compiler/dsdlc.py ${DSDLC_INPUTS} -O${DSDLC_OUTPUT}
include_directories(${DSDLC_OUTPUT})
#
# -DUAVCAN_DEBUG=1 enables the tracing feature that writes debug info into stdout.
# Normally this feature should be used only for library development.
# Compiler flags
#
set(CMAKE_CXX_FLAGS_RELEASE "-O1 -DNDEBUG")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O1 -g")
set(CMAKE_CXX_FLAGS_RELEASE "-O1 -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -DUAVCAN_DEBUG=1")
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -DUAVCAN_DEBUG=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -pedantic")
set(CPP11_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
@@ -48,7 +56,7 @@ install(DIRECTORY include/dsdlc_generated/uavcan DESTINATION include) # Merging
install(DIRECTORY src/ DESTINATION src/uavcan)
#
# Unit tests with gtest (optional)
# Tests and static analysis - only for debug builds
#
function(add_test name library flags)
find_package(Threads REQUIRED)
@@ -71,20 +79,20 @@ function(add_test name library flags)
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endfunction()
find_package(GTest QUIET)
if (GTEST_FOUND)
string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type_lower)
if (build_type_lower STREQUAL "debug")
message(STATUS "Debug build (note: requires gtest, cppcheck)")
find_package(GTest REQUIRED)
add_libuavcan(uavcan_optimized "-Wall -Wextra -Werror -pedantic -O3 -DNDEBUG -std=c++0x")
add_test(libuavcan_test_optimized uavcan_optimized ${CPP11_FLAGS})
add_test(libuavcan_test uavcan ${CPP11_FLAGS})
add_test(libuavcan_test_cpp03 uavcan_cpp03 ${CPP03_FLAGS})
else (GTEST_FOUND)
message(">> Google test library is not found, you will not be able to run tests")
endif (GTEST_FOUND)
#
# Static analysis with cppcheck (required), both library and unit test sources
#
add_custom_command(TARGET uavcan POST_BUILD
COMMAND ./cppcheck.sh
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
# Static analysis with cppcheck, both library and unit test sources
add_custom_command(TARGET uavcan POST_BUILD COMMAND ./cppcheck.sh WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
else ()
message(STATUS "Release build type: " ${CMAKE_BUILD_TYPE})
endif ()