Skip to content

Commit

Permalink
Merge pull request #2323 from johnplatts:hwy_test_enh_090524
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 681467921
  • Loading branch information
copybara-github committed Oct 2, 2024
2 parents 15dccd5 + 350a811 commit b1682dd
Show file tree
Hide file tree
Showing 3 changed files with 448 additions and 40 deletions.
39 changes: 32 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ set(HWY_ENABLE_EXAMPLES ON CACHE BOOL "Build examples")
set(HWY_ENABLE_INSTALL ON CACHE BOOL "Install library")
set(HWY_ENABLE_TESTS ON CACHE BOOL "Enable HWY tests")

if (MSVC)
set(HWY_TEST_STANDALONE ON CACHE BOOL "Disable use of googletest")
else()
set(HWY_TEST_STANDALONE OFF CACHE BOOL "Disable use of googletest")
endif()

if (NOT DEFINED CMAKE_CXX_STANDARD)
if ("cxx_std_17" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
set(HWY_CXX_STD_TGT_COMPILE_FEATURE cxx_std_17)
Expand Down Expand Up @@ -579,6 +585,15 @@ if (HWY_ENABLE_CONTRIB)
list(APPEND HWY_PC_FILES libhwy-contrib.pc)
endif() # HWY_ENABLE_CONTRIB
if (HWY_ENABLE_TESTS)

if (HWY_TEST_STANDALONE)
set(HWY_PC_HWY_TEST_REQUIRES "")
set(HWY_PC_HWY_TEST_CFLAGS "-DHWY_TEST_STANDALONE=1")
else()
set(HWY_PC_HWY_TEST_REQUIRES "gtest")
set(HWY_PC_HWY_TEST_CFLAGS "")
endif()

list(APPEND HWY_PC_FILES libhwy-test.pc)
endif() # HWY_ENABLE_TESTS
foreach (pc ${HWY_PC_FILES})
Expand Down Expand Up @@ -628,6 +643,8 @@ enable_testing()
include(GoogleTest)

set(HWY_SYSTEM_GTEST OFF CACHE BOOL "Use pre-installed googletest?")

if(NOT HWY_TEST_STANDALONE)
if(HWY_SYSTEM_GTEST)
find_package(GTest REQUIRED)
else()
Expand Down Expand Up @@ -656,6 +673,7 @@ add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
endif() # HWY_SYSTEM_GTEST
endif() # HWY_TEST_STANDALONE

set(HWY_TEST_FILES
hwy/contrib/algo/copy_test.cc
Expand Down Expand Up @@ -745,15 +763,19 @@ list(APPEND HWY_TEST_FILES
)
endif() # HWY_ENABLE_CONTRIB

if(HWY_SYSTEM_GTEST)
if (CMAKE_VERSION VERSION_LESS 3.20)
set(HWY_GTEST_LIBS GTest::GTest GTest::Main)
if(HWY_TEST_STANDALONE)
set(HWY_GTEST_LIBS "")
else()
if(HWY_SYSTEM_GTEST)
if (CMAKE_VERSION VERSION_LESS 3.20)
set(HWY_GTEST_LIBS GTest::GTest GTest::Main)
else()
set(HWY_GTEST_LIBS GTest::gtest GTest::gtest_main)
endif()
else()
set(HWY_GTEST_LIBS GTest::gtest GTest::gtest_main)
set(HWY_GTEST_LIBS gtest gtest_main)
endif()
else()
set(HWY_GTEST_LIBS gtest gtest_main)
endif()
endif() # HWY_TEST_STANDALONE

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests)
foreach (TESTFILE IN LISTS HWY_TEST_FILES)
Expand All @@ -766,6 +788,9 @@ foreach (TESTFILE IN LISTS HWY_TEST_FILES)
# cause compile errors because only one may be set, and other CMakeLists.txt
# that include us may set them.
target_compile_options(${TESTNAME} PRIVATE -DHWY_IS_TEST=1)
if(HWY_TEST_STANDALONE)
target_compile_options(${TESTNAME} PRIVATE -DHWY_TEST_STANDALONE=1)
endif()
target_compile_features(${TESTNAME} PRIVATE ${HWY_CXX_STD_TGT_COMPILE_FEATURE})

target_link_libraries(${TESTNAME} PRIVATE ${HWY_TEST_LIBS} ${HWY_GTEST_LIBS})
Expand Down
Loading

0 comments on commit b1682dd

Please sign in to comment.