Skip to content

Commit

Permalink
Add CMake variable SHADERC_SKIP_TESTS.
Browse files Browse the repository at this point in the history
Configuring with -DSHADERC_SKIP_TESTS=ON will prevent building
any tests based on gtest or gmock.

Required for cross compiling from Linux to Windows with MinGW
since it seems MinGW can't compile googletest.
See google/googletest#606

To compile on Linux targeting Windows, do this:
  cmake -DCMAKE_SYSTEM_NAME=Windows \
        -DCMAKE_C_COMPILER=i686-w64-mingw32-gcc \
        -DCMAKE_CXX_COMPILER=i686-w64-mingw32-g++ \
        -DSHADERC_SKIP_TESTS=ON \
	$SHADERC_SRC_DIR

Change-Id: I31cb0568f335debb1767b93a5f9067df6848973b
  • Loading branch information
dneto0 committed Mar 16, 2016
1 parent 5aa3dbd commit a712018
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 35 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
54 changes: 28 additions & 26 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions libshaderc_util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
13 changes: 8 additions & 5 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit a712018

Please sign in to comment.