Skip to content

Simplify cmake test invocation #7043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ option(EXECUTORCH_BUILD_EXTENSION_TENSOR "Build the Tensor extension" OFF)

option(EXECUTORCH_BUILD_EXTENSION_TRAINING "Build the training extension" OFF)

option(EXECUTORCH_BUILD_GTESTS "Build googletest based test binaries" OFF)

option(EXECUTORCH_BUILD_MPS "Build the MPS backend" OFF)

option(EXECUTORCH_BUILD_NEURON "Build the backends/mediatek directory" OFF)
Expand All @@ -216,6 +214,8 @@ option(EXECUTORCH_BUILD_KERNELS_QUANTIZED "Build the quantized kernels" OFF)

option(EXECUTORCH_BUILD_DEVTOOLS "Build the ExecuTorch Developer Tools")

option(EXECUTORCH_BUILD_TESTS "Build CMake-based unit tests" OFF)

option(EXECUTORCH_NNLIB_OPT "Build Cadence backend Hifi nnlib kernel" OFF)

option(EXECUTORCH_CADENCE_CPU_RUNNER "Build Cadence backend CPU runner" OFF)
Expand Down Expand Up @@ -330,6 +330,10 @@ if(EXECUTORCH_BUILD_PTHREADPOOL)
)
endif()

if(EXECUTORCH_BUILD_TESTS)
include(CTest)
endif()

if(NOT PYTHON_EXECUTABLE)
resolve_python_executable()
endif()
Expand Down Expand Up @@ -625,7 +629,7 @@ cmake_dependent_option(
)

# Add googletest if any test targets should be built
if(EXECUTORCH_BUILD_GTESTS)
if(BUILD_TESTING)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not EXECUTORCH_BUILD_TESTS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUILD_TESTING is from cmake system to recognize whether test config is on or not. It will be enabled when we include ctest (see https://github.com/pytorch/executorch/pull/7043/files#diff-1e7de1ae2d059d21e1dd75d5812d5a34b0222cef273b7c3a2af62eb747f9d20aR318-R320)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/googletest)
endif()

Expand Down Expand Up @@ -829,5 +833,7 @@ if(EXECUTORCH_BUILD_VULKAN)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan)
endif()

include(Test.cmake)

# Print all summary
executorch_print_configuration_summary()
29 changes: 29 additions & 0 deletions Test.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

#
# A helper CMake file to trigger C++ unit tests.
#

if(BUILD_TESTING)
# This contains the list of tests which are always built
add_subdirectory(extension/evalue_util/test)
add_subdirectory(extension/kernel_util/test)
add_subdirectory(extension/memory_allocator/test)
add_subdirectory(extension/parallel/test)
add_subdirectory(extension/pytree/test)
add_subdirectory(kernels/portable/cpu/util/test)
add_subdirectory(kernels/prim_ops/test)
add_subdirectory(kernels/test)
add_subdirectory(runtime/core/exec_aten/testing_util/test)
add_subdirectory(runtime/core/exec_aten/util/test)
add_subdirectory(runtime/core/portable_type/test)
add_subdirectory(runtime/core/test)
add_subdirectory(runtime/executor/test)
add_subdirectory(runtime/kernel/test)
add_subdirectory(runtime/platform/test)
add_subdirectory(test/utils)
endif()
4 changes: 4 additions & 0 deletions backends/xnnpack/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ install(
INCLUDES
DESTINATION ${_common_include_directories}
)

if(BUILD_TESTING)
add_subdirectory(test)
endif()
9 changes: 1 addition & 8 deletions backends/xnnpack/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@
#

cmake_minimum_required(VERSION 3.19)
project(backends_xnnpack_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

include(${EXECUTORCH_ROOT}/build/Test.cmake)

set(_test_srcs # We can't put runtime/test_runtime_utils.cpp because we don't
# build aten
set(_test_srcs
runtime/test_xnnexecutor.cpp
${EXECUTORCH_ROOT}/extension/threadpool/threadpool.cpp
${EXECUTORCH_ROOT}/extension/threadpool/threadpool_guard.cpp
${EXECUTORCH_ROOT}/extension/threadpool/test/threadpool_test.cpp
)

Expand Down
48 changes: 3 additions & 45 deletions build/Test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,6 @@
# It should also be cmake-lint clean.
#

include(${EXECUTORCH_ROOT}/build/Utils.cmake)

# Find prebuilt executorch library
find_package(executorch CONFIG REQUIRED)

enable_testing()
find_package(GTest CONFIG REQUIRED)

target_link_options_shared_lib(cpuinfo)
target_link_options_shared_lib(extension_data_loader)
target_link_options_shared_lib(portable_kernels)
target_link_options_shared_lib(portable_ops_lib)
target_link_options_shared_lib(pthreadpool)
target_link_options_shared_lib(quantized_ops_lib)

# Add code coverage flags to supported compilers
if(EXECUTORCH_USE_CPP_CODE_COVERAGE)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
string(APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path")
string(APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path")
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
string(APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
string(APPEND CMAKE_CXX_FLAGS
" -fprofile-instr-generate -fcoverage-mapping"
)
else()
message(ERROR
"Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported"
)
endif()
endif()

# A helper function to generate a gtest cxx executable target @param
# target_name: name for the executable @param SOURCES <list_of_sources>: test
# sources to be compiled. Sometimes util sources are used as well @param EXTRA
Expand All @@ -67,24 +35,14 @@ function(et_cxx_test target_name)
set(multi_arg_names SOURCES EXTRA_LIBS)
cmake_parse_arguments(ET_CXX_TEST "" "" "${multi_arg_names}" ${ARGN})

# Let files say "include <executorch/path/to/header.h>".
target_include_directories(executorch INTERFACE ${EXECUTORCH_ROOT}/..)

set(ET_TEST_UTIL_SOURCES
${EXECUTORCH_ROOT}/runtime/core/exec_aten/testing_util/tensor_util.cpp
)

add_executable(${target_name} ${ET_CXX_TEST_SOURCES} ${ET_TEST_UTIL_SOURCES})
add_executable(${target_name} ${ET_CXX_TEST_SOURCES} ${EXECUTORCH_ROOT}/runtime/core/exec_aten/testing_util/tensor_util.cpp)
# Includes gtest, gmock, executorch by default
target_link_libraries(
${target_name} GTest::gtest GTest::gtest_main GTest::gmock executorch
${ET_CXX_TEST_EXTRA_LIBS}
)

# add_test adds a test target to be used by ctest. We use `ExecuTorchTest` as
# the ctest target name for the test executable Usage: cd
# cmake-out/path/to/test/; ctest Note: currently we directly invoke the test
# target, without using ctest
add_test(ExecuTorchTest ${target_name})
# add_test adds a test target to be used by ctest
add_test(NAME ${target_name} COMMAND ${target_name})

endfunction()
5 changes: 5 additions & 0 deletions devtools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,8 @@ install(
INCLUDES
DESTINATION ${_common_include_directories}
)

if(BUILD_TESTING)
# TODO: This is currently not working!
# add_subdirectory(etdump/tests)
endif()
6 changes: 1 addition & 5 deletions devtools/etdump/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(sdk_etdump_tests)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand All @@ -30,7 +26,7 @@ et_cxx_test(
EXTRA_LIBS
bundled_program
etdump
flatccrt_d
flatccrt
)
target_include_directories(
sdk_etdump_tests PRIVATE ${CMAKE_INSTALL_PREFIX}/sdk/include
Expand Down
4 changes: 0 additions & 4 deletions examples/models/llama/tokenizer/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(tokenizer_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..)

Expand Down
4 changes: 4 additions & 0 deletions extension/data_loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ install(
INCLUDES
DESTINATION ${_common_include_directories}
)

if(BUILD_TESTING)
add_subdirectory(test)
endif()
4 changes: 0 additions & 4 deletions extension/data_loader/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(extension_data_loader_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand Down
4 changes: 0 additions & 4 deletions extension/evalue_util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(extension_evalue_util_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand Down
4 changes: 0 additions & 4 deletions extension/kernel_util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(extension_kernel_util_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand Down
4 changes: 0 additions & 4 deletions extension/memory_allocator/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(extension_memory_allocator_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand Down
4 changes: 4 additions & 0 deletions extension/module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ install(
INCLUDES
DESTINATION ${_common_include_directories}
)

if(BUILD_TESTING)
add_subdirectory(test)
endif()
14 changes: 8 additions & 6 deletions extension/module/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# @generated by test/utils/generate_gtest_cmakelists.py
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
Expand All @@ -14,10 +12,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(extension_module_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand All @@ -36,3 +30,11 @@ et_cxx_test(
portable_kernels
portable_ops_lib
)

set(test_env "RESOURCES_PATH=${EXECUTORCH_ROOT}/extension/module/test/resources")

set_property(
TEST extension_module_test
PROPERTY ENVIRONMENT
"${test_env}"
)
4 changes: 0 additions & 4 deletions extension/pytree/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(extension_pytree_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand Down
4 changes: 4 additions & 0 deletions extension/runner_util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ install(
INCLUDES
DESTINATION ${_common_include_directories}
)

if(BUILD_TESTING)
add_subdirectory(test)
endif()
10 changes: 4 additions & 6 deletions extension/runner_util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# @generated by test/utils/generate_gtest_cmakelists.py
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
Expand All @@ -14,10 +12,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(extension_runner_util_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand All @@ -35,3 +29,7 @@ et_cxx_test(
portable_kernels
portable_ops_lib
)
set_property(
TEST extension_runner_util_test
PROPERTY ENVIRONMENT "ET_MODULE_ADD_PATH=${CMAKE_BINARY_DIR}/ModuleAdd.pte"
)
4 changes: 4 additions & 0 deletions extension/tensor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ install(
INCLUDES
DESTINATION ${_common_include_directories}
)

if(BUILD_TESTING)
add_subdirectory(test)
endif()
4 changes: 0 additions & 4 deletions extension/tensor/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(extension_tensor_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

Expand Down
4 changes: 0 additions & 4 deletions kernels/portable/cpu/util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#

cmake_minimum_required(VERSION 3.19)
project(kernels_portable_cpu_util_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..)

Expand Down
Loading
Loading