Skip to content

Add a script for generating OSS gtest CMakeLists.txt #3608

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

Closed
wants to merge 3 commits into from
Closed
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
43 changes: 43 additions & 0 deletions runtime/core/exec_aten/testing_util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.

# @generated by test/utils/generate_gtest_cmakelists.py
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#

cmake_minimum_required(VERSION 3.19)
project(runtime_core_exec_aten_testing_util_test)

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

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

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

# Find prebuilt executorch library
find_package(executorch CONFIG REQUIRED)

enable_testing()
find_package(GTest CONFIG REQUIRED)

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

set(_test_srcs tensor_util_test.cpp tensor_factory_test.cpp ../tensor_util.cpp)

add_executable(runtime_core_exec_aten_testing_util_test ${_test_srcs})
target_link_libraries(
runtime_core_exec_aten_testing_util_test GTest::gtest GTest::gtest_main
GTest::gmock executorch
)
add_test(ExecuTorchTest runtime_core_exec_aten_testing_util_test)
47 changes: 47 additions & 0 deletions runtime/core/exec_aten/util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.

# @generated by test/utils/generate_gtest_cmakelists.py
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#

cmake_minimum_required(VERSION 3.19)
project(runtime_core_exec_aten_util_test)

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

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

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

# Find prebuilt executorch library
find_package(executorch CONFIG REQUIRED)

enable_testing()
find_package(GTest CONFIG REQUIRED)

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

set(_test_srcs
tensor_util_test.cpp scalar_type_util_test.cpp
operator_impl_example_test.cpp dim_order_util_test.cpp
../../testing_util/tensor_util.cpp
)

add_executable(runtime_core_exec_aten_util_test ${_test_srcs})
target_link_libraries(
runtime_core_exec_aten_util_test GTest::gtest GTest::gtest_main GTest::gmock
executorch
)
add_test(ExecuTorchTest runtime_core_exec_aten_util_test)
13 changes: 6 additions & 7 deletions runtime/core/portable_type/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# ### Editing this file ###
# @generated by test/utils/generate_gtest_cmakelists.py
#
# This file should be formatted with
# ~~~
Expand All @@ -16,8 +16,8 @@
cmake_minimum_required(VERSION 3.19)
project(runtime_core_portable_type_test)

# Use C++11 for test.
set(CMAKE_CXX_STANDARD 11)
# Use C++14 for test.
set(CMAKE_CXX_STANDARD 14)

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

Expand All @@ -30,9 +30,7 @@ enable_testing()
find_package(GTest CONFIG REQUIRED)

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

set(_test_srcs optional_test.cpp executor_tensor_test.cpp half_test.cpp
Expand All @@ -41,6 +39,7 @@ set(_test_srcs optional_test.cpp executor_tensor_test.cpp half_test.cpp

add_executable(runtime_core_portable_type_test ${_test_srcs})
target_link_libraries(
runtime_core_portable_type_test GTest::gtest GTest::gtest_main executorch
runtime_core_portable_type_test GTest::gtest GTest::gtest_main GTest::gmock
executorch
)
add_test(ExecuTorchTest runtime_core_portable_type_test)
51 changes: 51 additions & 0 deletions runtime/core/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.

# @generated by test/utils/generate_gtest_cmakelists.py
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#

cmake_minimum_required(VERSION 3.19)
project(runtime_core_test)

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

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

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

# Find prebuilt executorch library
find_package(executorch CONFIG REQUIRED)

enable_testing()
find_package(GTest CONFIG REQUIRED)

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

set(_test_srcs
span_test.cpp
error_handling_test.cpp
event_tracer_test.cpp
freeable_buffer_test.cpp
array_ref_test.cpp
memory_allocator_test.cpp
hierarchical_allocator_test.cpp
evalue_test.cpp
)

add_executable(runtime_core_test ${_test_srcs})
target_link_libraries(
runtime_core_test GTest::gtest GTest::gtest_main GTest::gmock executorch
)
add_test(ExecuTorchTest runtime_core_test)
5 changes: 4 additions & 1 deletion test/run_oss_cpp_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ build_executorch() {
build_and_run_test() {
local test_dir=$1
cmake "${test_dir}" -Bcmake-out/"${test_dir}" -DCMAKE_INSTALL_PREFIX=cmake-out
cmake --build cmake-out/"${test_dir}"
cmake --build cmake-out/"${test_dir}" -j9
for t in $(cmake-out/"${test_dir}"/*test); do ./"$t"; done
}

build_executorch
build_and_run_test runtime/core/portable_type/test/
build_and_run_test runtime/core/test/
build_and_run_test runtime/core/exec_aten/util/test/
build_and_run_test runtime/core/exec_aten/testing_util/test/
44 changes: 44 additions & 0 deletions test/utils/OSSTest.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.

# @generated by test/utils/generate_gtest_cmakelists.py
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#

cmake_minimum_required(VERSION 3.19)
project({project_name})

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

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

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

# Find prebuilt executorch library
find_package(executorch CONFIG REQUIRED)

enable_testing()
find_package(GTest CONFIG REQUIRED)

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

set(_test_srcs {test_srcs})

add_executable({project_name} ${{_test_srcs}})
target_link_libraries(
{project_name} GTest::gtest GTest::gtest_main GTest::gmock executorch
)
add_test(ExecuTorchTest {project_name})
43 changes: 43 additions & 0 deletions test/utils/OSSTestConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ "tests": [
{
"directory": "runtime/core/portable_type/test",
"sources": [
"optional_test.cpp",
"executor_tensor_test.cpp",
"half_test.cpp",
"scalar_test.cpp",
"tensor_impl_test.cpp"
]
},
{
"directory": "runtime/core/test",
"sources": [
"span_test.cpp",
"error_handling_test.cpp",
"event_tracer_test.cpp",
"freeable_buffer_test.cpp",
"array_ref_test.cpp",
"memory_allocator_test.cpp",
"hierarchical_allocator_test.cpp",
"evalue_test.cpp"
]
},
{
"directory": "runtime/core/exec_aten/util/test",
"sources": [
"tensor_util_test.cpp",
"scalar_type_util_test.cpp",
"operator_impl_example_test.cpp",
"dim_order_util_test.cpp",
"../../testing_util/tensor_util.cpp"
]
},
{
"directory": "runtime/core/exec_aten/testing_util/test",
"sources": [
"tensor_util_test.cpp",
"tensor_factory_test.cpp",
"../tensor_util.cpp"
]
}
] }
72 changes: 72 additions & 0 deletions test/utils/generate_gtest_cmakelists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python3
# 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.

import json
import os
import shutil
import subprocess


def calculate_project_name(path_to_root):
"""
Get a cmake project name for that path using relative path.
>>> calculate_project_name("runtime/core/portable_type/test")
'runtime_core_portable_type_test'
"""
return path_to_root.replace("/", "_")


def calculate_relative_path(path_to_root):
"""
Return the relative path from the path_to_root to root (i.e. "..")
>>> calculate_relative_path("runtime/core/portable_type/test")
'../../../..'
"""
return os.path.relpath("/", "/" + path_to_root)


def format_template(path_to_root, test_srcs):
"""
Format the template with the given path_to_root and test_srcs.
"""
with open(os.path.dirname(os.path.abspath(__file__)) + "/OSSTest.cmake.in") as f:
template = f.read()
return template.format(
project_name=calculate_project_name(path_to_root),
path_to_root=calculate_relative_path(path_to_root),
test_srcs=" ".join(test_srcs),
)


def write_template(path_to_root, test_srcs):
"""
Write the template to the given path_to_root.
"""
with open(os.path.join(path_to_root, "CMakeLists.txt"), "w") as f:
f.write(format_template(path_to_root, test_srcs))


def read_config_json(json_path):
"""
Read the config.json file and return the list of (path_to_root, test_srcs)
"""
with open(json_path) as f:
config = json.load(f)
return [(d["directory"], d["sources"]) for d in config["tests"]]


if __name__ == "__main__":
json_path = os.path.dirname(os.path.abspath(__file__)) + "/OSSTestConfig.json"
for path_to_root, test_srcs in read_config_json(json_path):
write_template(path_to_root, test_srcs)
if shutil.which("cmake-format") is not None:
subprocess.run(
["cmake-format", "-i", path_to_root + "/CMakeLists.txt"], check=True
)
else:
print(f"Please run cmake-format -i {path_to_root}/CMakeLists.txt")
print("Note: Please update test/run_oss_cpp_tests.sh")