1include(FetchContent) 2 3if(${CMAKE_VERSION} VERSION_LESS "3.24.0") 4 FetchContent_Declare( 5 googletest 6 URL https://github.com/google/googletest/archive/41fe6be7d738237d1ca53070bd6ddebb73190b58.zip 7 ) 8else() 9 FetchContent_Declare( 10 googletest 11 URL https://github.com/google/googletest/archive/41fe6be7d738237d1ca53070bd6ddebb73190b58.zip 12 FIND_PACKAGE_ARGS NAMES GTest 13 DOWNLOAD_EXTRACT_TIMESTAMP = TRUE 14 ) 15endif() 16 17# For Windows: Prevent overriding the parent project's compiler/linker settings 18set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 19 20FetchContent_MakeAvailable(googletest) 21 22enable_testing() 23 24# Find all the binary files used for testing and copy them into the build 25# directory. This allows the test to be run from the build directory 26 27# First, create an elf_examples folder under the current build directory 28file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/elf_examples) 29 30# Second, glob all files under elf_examples 31file(GLOB elf_examples 32 LIST_DIRECTORIES 33 false 34 CONFIGURE_DEPENDS 35 elf_examples/*) 36 37# Third, copy each file globbed to the elf_examples folder under the current 38# build directory 39foreach(example ${elf_examples}) 40 configure_file(${example} ${CMAKE_CURRENT_BINARY_DIR}/elf_examples COPYONLY) 41endforeach() 42 43# Lastly, copy the script to run the tests 44configure_file(runELFtests ${CMAKE_CURRENT_BINARY_DIR}/runELFtests COPYONLY) 45 46add_executable( 47 ELFIOTest 48 ELFIOTest.cpp 49 ELFIOTest1.cpp 50 ELFIOTest2.cpp) 51 52target_link_libraries( 53 ELFIOTest 54 PRIVATE 55 elfio::elfio 56 gtest_main 57 GTest::gtest_main) 58 59add_test( 60 NAME 61 ELFIOTest 62 COMMAND 63 ${CMAKE_CURRENT_BINARY_DIR}/ELFIOTest 64 WORKING_DIRECTORY 65 ${CMAKE_CURRENT_BINARY_DIR}) 66 67if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 68 add_executable( 69 elfio_fuzzer 70 elfio_fuzzer.cpp) 71 72 target_link_libraries( 73 elfio_fuzzer 74 PRIVATE 75 elfio::elfio) 76 77 target_compile_options(elfio_fuzzer 78 PRIVATE $<$<C_COMPILER_ID:Clang>:-g -O1 -fsanitize=fuzzer,address> 79 ) 80 81 target_link_libraries(elfio_fuzzer 82 PRIVATE $<$<C_COMPILER_ID:Clang>:-fsanitize=fuzzer,address> 83 ) 84endif() 85 86add_dependencies(check ELFIOTest) 87 88include(GoogleTest) 89gtest_discover_tests(ELFIOTest) 90