1# Copyright (c) Meta Platforms, Inc. and affiliates. 2# All rights reserved. 3# 4# This source code is licensed under the BSD-style license found in the 5# LICENSE file in the root directory of this source tree. 6 7# 8# This file is intended to have helper functions for test-related CMakeLists.txt 9# files. 10# 11# ### Editing this file ### 12# 13# This file should be formatted with 14# ~~~ 15# cmake-format -i Test.cmake 16# ~~~ 17# It should also be cmake-lint clean. 18# 19 20include(${EXECUTORCH_ROOT}/build/Utils.cmake) 21 22# Find prebuilt executorch library 23find_package(executorch CONFIG REQUIRED) 24 25enable_testing() 26find_package(GTest CONFIG REQUIRED) 27 28target_link_options_shared_lib(cpuinfo) 29target_link_options_shared_lib(extension_data_loader) 30target_link_options_shared_lib(portable_kernels) 31target_link_options_shared_lib(portable_ops_lib) 32target_link_options_shared_lib(pthreadpool) 33target_link_options_shared_lib(quantized_ops_lib) 34 35# Add code coverage flags to supported compilers 36if(EXECUTORCH_USE_CPP_CODE_COVERAGE) 37 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 38 string(APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path") 39 string(APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path") 40 elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 41 string(APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping") 42 string(APPEND CMAKE_CXX_FLAGS 43 " -fprofile-instr-generate -fcoverage-mapping" 44 ) 45 else() 46 message(ERROR 47 "Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported" 48 ) 49 endif() 50endif() 51 52# A helper function to generate a gtest cxx executable target @param 53# target_name: name for the executable @param SOURCES <list_of_sources>: test 54# sources to be compiled. Sometimes util sources are used as well @param EXTRA 55# LIBS <list_of_libs>: additional libraries to be linked against the target. 56# gtest, gmock, executorch are linked by default, but Sometimes user may need 57# additional libraries like kernels. We use CMake package executorch in this 58# helper, so user can easily add installed libraries. 59# 60# Example: et_cxx_test(my_test SOURCES my_test.cpp EXTRA_LIBS portable_kernels) 61# 62# This defines a gtest executable my_test, compiling my_test.cpp, and linking 63# against libportable_kernels.a. 64# 65function(et_cxx_test target_name) 66 67 set(multi_arg_names SOURCES EXTRA_LIBS) 68 cmake_parse_arguments(ET_CXX_TEST "" "" "${multi_arg_names}" ${ARGN}) 69 70 # Let files say "include <executorch/path/to/header.h>". 71 target_include_directories(executorch INTERFACE ${EXECUTORCH_ROOT}/..) 72 73 set(ET_TEST_UTIL_SOURCES 74 ${EXECUTORCH_ROOT}/runtime/core/exec_aten/testing_util/tensor_util.cpp 75 ) 76 77 add_executable(${target_name} ${ET_CXX_TEST_SOURCES} ${ET_TEST_UTIL_SOURCES}) 78 # Includes gtest, gmock, executorch by default 79 target_link_libraries( 80 ${target_name} GTest::gtest GTest::gtest_main GTest::gmock executorch 81 ${ET_CXX_TEST_EXTRA_LIBS} 82 ) 83 84 # add_test adds a test target to be used by ctest. We use `ExecuTorchTest` as 85 # the ctest target name for the test executable Usage: cd 86 # cmake-out/path/to/test/; ctest Note: currently we directly invoke the test 87 # target, without using ctest 88 add_test(ExecuTorchTest ${target_name}) 89 90endfunction() 91