From 2ea7449c9c14e392c37694736d65957821657d81 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Sun, 22 May 2016 13:48:08 -0400 Subject: [PATCH] Create a function for adding unittests in CMake. Also remove unnecessary main() function for unittests. --- test/CMakeLists.txt | 44 +++++++++++++++++++++++++------------------- test/main.cpp | 32 -------------------------------- 2 files changed, 25 insertions(+), 51 deletions(-) delete mode 100644 test/main.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 630b015..3e43fd3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,6 +24,29 @@ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. +function(add_spvtools_unittest target srcs libs) + add_executable(${target} ${srcs}) + spvtools_default_compile_options(${target}) + if(${COMPILER_IS_LIKE_GNU}) + target_compile_options(${target} PRIVATE -Wno-undef) + endif() + if(${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC") + # Disable C4503 "decorated name length exceeded" warning, + # triggered by some heavily templated types. + # We don't care much about that in test code. + # Important to do since we have warnings-as-errors. + target_compile_options(${target} PRIVATE /wd4503) + endif() + target_include_directories(${target} PRIVATE + ${spirv-tools_SOURCE_DIR} + ${gtest_SOURCE_DIR}/include + ${gmock_SOURCE_DIR}/include + ) + target_link_libraries(${target} PRIVATE ${libs}) + target_link_libraries(${target} PRIVATE gmock_main) + add_test(NAME spirv-tools-${target} COMMAND ${target}) +endfunction() + if (NOT ${SPIRV_SKIP_EXECUTABLES}) if (TARGET gmock) message(STATUS "Found Google Mock, building tests.") @@ -91,25 +114,8 @@ if (NOT ${SPIRV_SKIP_EXECUTABLES}) ${CMAKE_CURRENT_SOURCE_DIR}/Validate.SSA.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ValidateID.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ValidationState.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) - - add_executable(UnitSPIRV ${TEST_SOURCES}) - spvtools_default_compile_options(UnitSPIRV) - if(${COMPILER_IS_LIKE_GNU}) - target_compile_options(UnitSPIRV PRIVATE -Wno-undef) - endif() - if(${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC") - # Disable C4503 "decorated name length exceeded" warning, triggered - # by some heavily templated types. - # We don't care much about that in test code. - # Important to do since we have warnings-as-errors. - target_compile_options(UnitSPIRV PRIVATE /wd4503) - endif() - target_include_directories(UnitSPIRV PRIVATE - ${spirv-tools_SOURCE_DIR} - ${gmock_SOURCE_DIR}/include ${gtest_SOURCE_DIR}/include) - target_link_libraries(UnitSPIRV PRIVATE ${SPIRV_TOOLS} gmock) - add_test(NAME spirv-tools-testsuite COMMAND UnitSPIRV) + ) + add_spvtools_unittest(UnitSPIRV "${TEST_SOURCES}" ${SPIRV_TOOLS}) else() message(STATUS "Did not find googletest, tests will not be built." "To enable tests place googletest in '/external/googletest'.") diff --git a/test/main.cpp b/test/main.cpp deleted file mode 100644 index 54bdca0..0000000 --- a/test/main.cpp +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2015-2016 The Khronos Group Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and/or associated documentation files (the -// "Materials"), to deal in the Materials without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Materials, and to -// permit persons to whom the Materials are furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Materials. -// -// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS -// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS -// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT -// https://www.khronos.org/registry/ -// -// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. - -#include - -int main(int argc, char** argv) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} -- 2.7.4