diff --git a/CMakeLists.txt b/CMakeLists.txt index 68ae4bf90..5038cfb4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,18 @@ enable_testing() message(STATUS "Shaderc: build type is \"${CMAKE_BUILD_TYPE}\".") +option(SHADERC_SKIP_TESTS + "Skip building unit tests based on googlemock and googletest" + ${SHADERC_SKIP_TESTS}) +if(NOT ${SHADERC_SKIP_TESTS}) + set(SHADERC_ENABLE_TESTS ON) +endif() +if(${SHADERC_ENABLE_TESTS}) + message(STATUS "Configuring Shaderc to build unit tests.") +else() + message(STATUS "Configuring Shaderc to avoid building unit tests.") +endif() + include(cmake/setup_build.cmake) include(cmake/utils.cmake) diff --git a/cmake/utils.cmake b/cmake/utils.cmake index 7af415016..329008c36 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -68,34 +68,36 @@ endfunction() # INCLUDE_DIRS: (optional) a list of include directories to be searched # for header files. function(shaderc_add_tests) - cmake_parse_arguments(PARSED_ARGS - "" - "TEST_PREFIX" - "TEST_NAMES;LINK_LIBS;INCLUDE_DIRS" - ${ARGN}) - if (NOT PARSED_ARGS_TEST_NAMES) - message(FATAL_ERROR "Tests must have a target") - endif() - if (NOT PARSED_ARGS_TEST_PREFIX) - message(FATAL_ERROR "Tests must have a prefix") - endif() - foreach(TARGET ${PARSED_ARGS_TEST_NAMES}) - set(TEST_NAME ${PARSED_ARGS_TEST_PREFIX}_${TARGET}_test) - add_executable(${TEST_NAME} src/${TARGET}_test.cc) - shaderc_default_compile_options(${TEST_NAME}) - if (PARSED_ARGS_LINK_LIBS) - target_link_libraries(${TEST_NAME} PRIVATE - ${PARSED_ARGS_LINK_LIBS}) + if(${SHADERC_ENABLE_TESTS}) + cmake_parse_arguments(PARSED_ARGS + "" + "TEST_PREFIX" + "TEST_NAMES;LINK_LIBS;INCLUDE_DIRS" + ${ARGN}) + if (NOT PARSED_ARGS_TEST_NAMES) + message(FATAL_ERROR "Tests must have a target") endif() - if (PARSED_ARGS_INCLUDE_DIRS) - target_include_directories(${TEST_NAME} PRIVATE - ${PARSED_ARGS_INCLUDE_DIRS}) + if (NOT PARSED_ARGS_TEST_PREFIX) + message(FATAL_ERROR "Tests must have a prefix") endif() - shaderc_use_gmock(${TEST_NAME}) - add_test( - NAME ${PARSED_ARGS_TEST_PREFIX}_${TARGET} - COMMAND ${TEST_NAME}) - endforeach() + foreach(TARGET ${PARSED_ARGS_TEST_NAMES}) + set(TEST_NAME ${PARSED_ARGS_TEST_PREFIX}_${TARGET}_test) + add_executable(${TEST_NAME} src/${TARGET}_test.cc) + shaderc_default_compile_options(${TEST_NAME}) + if (PARSED_ARGS_LINK_LIBS) + target_link_libraries(${TEST_NAME} PRIVATE + ${PARSED_ARGS_LINK_LIBS}) + endif() + if (PARSED_ARGS_INCLUDE_DIRS) + target_include_directories(${TEST_NAME} PRIVATE + ${PARSED_ARGS_INCLUDE_DIRS}) + endif() + shaderc_use_gmock(${TEST_NAME}) + add_test( + NAME ${PARSED_ARGS_TEST_PREFIX}_${TARGET} + COMMAND ${TEST_NAME}) + endforeach() + endif(${SHADERC_ENABLE_TESTS}) endfunction(shaderc_add_tests) # Finds all transitive static library dependencies of a given target diff --git a/libshaderc_util/CMakeLists.txt b/libshaderc_util/CMakeLists.txt index 7e7f7b805..c6a4cc222 100644 --- a/libshaderc_util/CMakeLists.txt +++ b/libshaderc_util/CMakeLists.txt @@ -38,8 +38,10 @@ shaderc_add_tests( io message) -target_include_directories(shaderc_util_counting_includer_test - PRIVATE ${glslang_SOURCE_DIR}) +if(${SHADERC_ENABLE_TESTS}) + target_include_directories(shaderc_util_counting_includer_test + PRIVATE ${glslang_SOURCE_DIR}) +endif() shaderc_add_tests( TEST_PREFIX shaderc_util @@ -52,7 +54,9 @@ shaderc_add_tests( add_custom_target(testdata COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/testdata/copy-to-build.cmake) -add_dependencies(shaderc_util_file_finder_test testdata) -add_dependencies(shaderc_util_io_test testdata) +if(${SHADERC_ENABLE_TESTS}) + add_dependencies(shaderc_util_file_finder_test testdata) + add_dependencies(shaderc_util_io_test testdata) +endif() add_definitions(-DCURRENT_DIR="${CMAKE_CURRENT_BINARY_DIR}") diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index 3fcc6f752..ead9d9a03 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -2,11 +2,14 @@ set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS -w) # Configure third party projects. -if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/googletest) - add_subdirectory(googletest) -endif() -if (NOT TARGET gmock) - message(FATAL_ERROR "gmock was not found - required for tests") + +if(${SHADERC_ENABLE_TESTS}) + if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/googletest) + add_subdirectory(googletest) + endif() + if (NOT TARGET gmock) + message(FATAL_ERROR "gmock was not found - required for tests") + endif() endif() set(OLD_PLATFORM_TOOLSET ${CMAKE_GENERATOR_TOOLSET})