summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Kiagiadakis <george.kiagiadakis@collabora.co.uk>2010-12-07 18:47:05 +0200
committerGeorge Kiagiadakis <george.kiagiadakis@collabora.co.uk>2010-12-11 12:18:50 +0200
commitc4894eaf45765137ec46d552cb4628bf58289191 (patch)
treea413a0f0a734c721da903b4c747446df61a0bba8
parent5a36eaf266192b85dd1da5caaa31cf7ba2529b18 (diff)
Implement a system for doing compilation tests.
Compilation tests are basically to test if a certain piece of code compiles or not. This is useful for ensuring that certain uses of our templates will NOT compile and thus prevent the users from misusing them by mistake.
-rw-r--r--cmake/modules/MacroCXXCompilationTest.cmake121
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/compilation/CMakeLists.txt10
-rw-r--r--tests/compilation/CompilationTests.cmake14
-rw-r--r--tests/compilation/CompilationTests_CMakeLists.txt17
-rw-r--r--tests/compilation/RunCompilationTests.cmake26
6 files changed, 189 insertions, 0 deletions
diff --git a/cmake/modules/MacroCXXCompilationTest.cmake b/cmake/modules/MacroCXXCompilationTest.cmake
new file mode 100644
index 0000000..f2e0184
--- /dev/null
+++ b/cmake/modules/MacroCXXCompilationTest.cmake
@@ -0,0 +1,121 @@
+# - Perform a C++ compilation test. The test passes if the given code compiles
+# or fails to compile, depending on the second argument given.
+#
+# CXX_COMPILATION_TEST(<name> <expect_fail> <source>)
+# <name> - name of the test & name of the variable to store if the test passes
+# <expect_fail> - indicates whether the given C++ code should compile or not
+# <source> - source code to try to compile
+#
+# The following variables may be set before calling this macro to
+# modify the way the check is run:
+#
+# CMAKE_REQUIRED_FLAGS = string of compile command line flags
+# CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
+# CMAKE_REQUIRED_INCLUDES = list of include directories
+# CMAKE_REQUIRED_LIBRARIES = list of libraries to link
+
+# Based on CheckCXXSourceCompiles.cmake
+#=============================================================================
+# Copyright 2005-2009 Kitware, Inc.
+# Copyright 2010 Collabora Ltd.
+# @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the names of Kitware, Inc., the Insight Software Consortium,
+# nor the names of their contributors may be used to endorse or promote
+# products derived from this software without specific prior written
+# permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#=============================================================================
+
+
+set(CXX_COMPILATION_TESTS_SUCCEEDED "")
+set(CXX_COMPILATION_TESTS_FAILED "")
+
+macro(cxx_compilation_test _name _expect_fail _source)
+ if("${_name}" MATCHES "^${_name}$")
+ if ("${_expect_fail}" STREQUAL "SHOULD_FAIL")
+ set(${_name}_SHOULD_FAIL 1)
+ elseif("${_expect_fail}" STREQUAL "SHOULD_COMPILE")
+ set(${_name}_SHOULD_FAIL 0)
+ else()
+ message(FATAL_ERROR "Invalid argument passed to cxx_compilation_test()")
+ endif()
+
+ if(CMAKE_REQUIRED_LIBRARIES)
+ set(CXX_COMPILATION_TEST_ADD_LIBRARIES
+ "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
+ else()
+ set(CXX_COMPILATION_TEST_ADD_LIBRARIES)
+ endif()
+
+ if(CMAKE_REQUIRED_INCLUDES)
+ set(CXX_COMPILATION_TEST_ADD_INCLUDES
+ "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
+ else()
+ set(CXX_COMPILATION_TEST_ADD_INCLUDES)
+ endif()
+
+ file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx" "${_source}\n")
+
+ try_compile(${_name}_COMPILES
+ ${CMAKE_BINARY_DIR}
+ ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx
+ COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+ CMAKE_FLAGS
+ "${CXX_COMPILATION_TEST_ADD_LIBRARIES}"
+ "${CXX_COMPILATION_TEST_ADD_INCLUDES}"
+ OUTPUT_VARIABLE OUTPUT)
+
+ if((${_name}_COMPILES AND NOT ${_name}_SHOULD_FAIL) OR (NOT ${_name}_COMPILES AND ${_name}_SHOULD_FAIL))
+ list(APPEND CXX_COMPILATION_TESTS_SUCCEEDED ${_name})
+ message("PASS : ${_name}")
+ file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilationTest.log
+ "C++ compilation test ${_name} succeded with the following output:\n"
+ "${OUTPUT}\n"
+ "Should fail: ${${_name}_SHOULD_FAIL}\n"
+ "Source file was:\n${_source}\n")
+ else()
+ message("FAIL : ${_name}")
+ list(APPEND CXX_COMPILATION_TESTS_FAILED ${_name})
+ set(CXX_COMPILATION_TESTS_HAVE_FAILURE 1)
+ file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilationTest.log
+ "C++ compilation test ${_name} failed with the following output:\n"
+ "${OUTPUT}\n"
+ "Should fail: ${${_name}_SHOULD_FAIL}\n"
+ "Source file was:\n${_source}\n")
+ endif()
+ endif()
+endmacro()
+
+macro(evaluate_cxx_compilation_test_results)
+ list(LENGTH CXX_COMPILATION_TESTS_SUCCEEDED CXX_COMPILATION_TESTS_SUCCEEDED_COUNT)
+ list(LENGTH CXX_COMPILATION_TESTS_FAILED CXX_COMPILATION_TESTS_FAILED_COUNT)
+
+ message("Totals: ${CXX_COMPILATION_TESTS_SUCCEEDED_COUNT} passed, ${CXX_COMPILATION_TESTS_FAILED_COUNT} failed")
+
+ if (CXX_COMPILATION_TESTS_FAILED_COUNT GREATER 0)
+ message(SEND_ERROR "Some tests have failed")
+ endif()
+endmacro()
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index ec45685..be35e1a 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,2 +1,3 @@
add_subdirectory(auto)
+add_subdirectory(compilation)
add_subdirectory(manual)
diff --git a/tests/compilation/CMakeLists.txt b/tests/compilation/CMakeLists.txt
new file mode 100644
index 0000000..5fc1e0e
--- /dev/null
+++ b/tests/compilation/CMakeLists.txt
@@ -0,0 +1,10 @@
+add_test(NAME compilation_tests
+ COMMAND ${CMAKE_COMMAND}
+ -DQTGSTREAMER_LIBRARY=$<TARGET_FILE:${QTGSTREAMER_LIBRARY}>
+ -DQTGSTREAMER_UI_LIBRARY=$<TARGET_FILE:${QTGSTREAMER_UI_LIBRARY}>
+ -DQTGSTREAMER_INCLUDE_DIR=${QTGSTREAMER_INCLUDE_DIR}
+ -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
+ -DBINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}/CompilationTests
+ -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
+ -P ${CMAKE_CURRENT_SOURCE_DIR}/RunCompilationTests.cmake
+)
diff --git a/tests/compilation/CompilationTests.cmake b/tests/compilation/CompilationTests.cmake
new file mode 100644
index 0000000..a55f802
--- /dev/null
+++ b/tests/compilation/CompilationTests.cmake
@@ -0,0 +1,14 @@
+# This file defines all the compilation tests
+
+##########
+# Tests for testing whether the compilation tests system works as expected.
+# If at least one of those tests fails, something is wrong with the cmake scripts.
+
+cxx_compilation_test(compilation_test_system_test_1 SHOULD_COMPILE "
+int main() { return 0; }
+")
+
+cxx_compilation_test(compilation_test_system_test_2 SHOULD_FAIL "
+int main(float i) { return foobar; }
+")
+#########
diff --git a/tests/compilation/CompilationTests_CMakeLists.txt b/tests/compilation/CompilationTests_CMakeLists.txt
new file mode 100644
index 0000000..fe58e5f
--- /dev/null
+++ b/tests/compilation/CompilationTests_CMakeLists.txt
@@ -0,0 +1,17 @@
+# This is a cmake project file that is used to run the compilation tests.
+# We misuse cmake here by running tests with the try_compile command and basing
+# the result of the whole test suite on the cmake exit status.
+
+cmake_minimum_required(VERSION 2.8)
+find_package(QtGstreamer REQUIRED)
+include(MacroCXXCompilationTest)
+
+set(CMAKE_REQUIRED_LIBRARIES ${QTGSTREAMER_LIBRARIES})
+set(CMAKE_REQUIRED_INCLUDES ${QTGSTREAMER_INCLUDES})
+set(CMAKE_REQUIRED_DEFINITIONS ${QTGSTREAMER_DEFINITIONS})
+set(CMAKE_REQUIRED_FLAGS "${QTGSTREAMER_FLAGS}")
+
+message("********* Begin running compilation tests *********")
+include(CompilationTests.cmake)
+evaluate_cxx_compilation_test_results()
+message("********* Finished running compilation tests *********")
diff --git a/tests/compilation/RunCompilationTests.cmake b/tests/compilation/RunCompilationTests.cmake
new file mode 100644
index 0000000..91a542b
--- /dev/null
+++ b/tests/compilation/RunCompilationTests.cmake
@@ -0,0 +1,26 @@
+# This script executes the compilation tests defined in CompilationTests.cmake
+
+# Clean up and re-create a build directory for the tests project
+execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR})
+execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${BINARY_DIR})
+
+# Copy the necessary files
+execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_DIR}/CompilationTests_CMakeLists.txt
+ ${BINARY_DIR}/CMakeLists.txt)
+execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_DIR}/CompilationTests.cmake
+ ${BINARY_DIR}/CompilationTests.cmake)
+
+# Run cmake to execute the tests
+execute_process(COMMAND ${CMAKE_COMMAND}
+ -DQTGSTREAMER_LIBRARY=${QTGSTREAMER_LIBRARY}
+ -DQTGSTREAMER_UI_LIBRARY=${QTGSTREAMER_UI_LIBRARY}
+ -DQTGSTREAMER_INCLUDE_DIR=${QTGSTREAMER_INCLUDE_DIR}
+ -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
+ WORKING_DIRECTORY ${BINARY_DIR}
+ OUTPUT_QUIET
+ RESULT_VARIABLE TESTS_RESULT
+)
+
+if (NOT (${TESTS_RESULT} EQUAL 0))
+ message(FATAL_ERROR "Compilation tests failed!")
+endif()