Skip to content

Commit 51dbed3

Browse files
committed
Add tooling for generating OSS test CMakeLists.txt
Use runtime/core/test as example: python test/utils.generate_gtest_cmake_lists.py Then run sh test/run_oss_cpp_tests.sh Note: for generate_gtest_cmake_lists.py CONFIG, you can use buck ``` buck cquery "inputs(deps('//runtime/core/test/...'))" | grep test.cpp ``` to get test srcs. However, manual check is needed. After this, all runtime/core OSS tests are added.
1 parent 245434e commit 51dbed3

File tree

7 files changed

+303
-8
lines changed

7 files changed

+303
-8
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
# ### Editing this file ###
8+
# This file is generated by test/utils/generate_gtest_cmakelists.py
9+
#
10+
# This file should be formatted with
11+
# ~~~
12+
# cmake-format -i CMakeLists.txt
13+
# ~~~
14+
# It should also be cmake-lint clean.
15+
#
16+
17+
cmake_minimum_required(VERSION 3.19)
18+
project(runtime_core_exec_aten_testing_util_test)
19+
20+
# Use C++14 for test.
21+
set(CMAKE_CXX_STANDARD 14)
22+
23+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..)
24+
25+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
26+
27+
# Find prebuilt executorch library
28+
find_package(executorch CONFIG REQUIRED)
29+
30+
enable_testing()
31+
find_package(GTest CONFIG REQUIRED)
32+
33+
# Let files say "include <executorch/path/to/header.h>".
34+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
35+
target_include_directories(executorch INTERFACE ${_common_include_directories})
36+
37+
set(_test_srcs tensor_util_test.cpp tensor_factory_test.cpp ../tensor_util.cpp)
38+
39+
add_executable(runtime_core_exec_aten_testing_util_test ${_test_srcs})
40+
target_link_libraries(
41+
runtime_core_exec_aten_testing_util_test GTest::gtest GTest::gtest_main
42+
GTest::gmock executorch
43+
)
44+
add_test(ExecuTorchTest runtime_core_exec_aten_testing_util_test)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
# ### Editing this file ###
8+
# This file is generated by test/utils/generate_gtest_cmakelists.py
9+
#
10+
# This file should be formatted with
11+
# ~~~
12+
# cmake-format -i CMakeLists.txt
13+
# ~~~
14+
# It should also be cmake-lint clean.
15+
#
16+
17+
cmake_minimum_required(VERSION 3.19)
18+
project(runtime_core_exec_aten_util_test)
19+
20+
# Use C++14 for test.
21+
set(CMAKE_CXX_STANDARD 14)
22+
23+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..)
24+
25+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
26+
27+
# Find prebuilt executorch library
28+
find_package(executorch CONFIG REQUIRED)
29+
30+
enable_testing()
31+
find_package(GTest CONFIG REQUIRED)
32+
33+
# Let files say "include <executorch/path/to/header.h>".
34+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
35+
target_include_directories(executorch INTERFACE ${_common_include_directories})
36+
37+
set(_test_srcs
38+
tensor_util_test.cpp scalar_type_util_test.cpp
39+
operator_impl_example_test.cpp dim_order_util_test.cpp
40+
../../testing_util/tensor_util.cpp
41+
)
42+
43+
add_executable(runtime_core_exec_aten_util_test ${_test_srcs})
44+
target_link_libraries(
45+
runtime_core_exec_aten_util_test GTest::gtest GTest::gtest_main GTest::gmock
46+
executorch
47+
)
48+
add_test(ExecuTorchTest runtime_core_exec_aten_util_test)

runtime/core/portable_type/test/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# LICENSE file in the root directory of this source tree.
66

77
# ### Editing this file ###
8+
# This file is generated by test/utils/generate_gtest_cmakelists.py
89
#
910
# This file should be formatted with
1011
# ~~~
@@ -16,8 +17,8 @@
1617
cmake_minimum_required(VERSION 3.19)
1718
project(runtime_core_portable_type_test)
1819

19-
# Use C++11 for test.
20-
set(CMAKE_CXX_STANDARD 11)
20+
# Use C++14 for test.
21+
set(CMAKE_CXX_STANDARD 14)
2122

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

@@ -30,9 +31,7 @@ enable_testing()
3031
find_package(GTest CONFIG REQUIRED)
3132

3233
# Let files say "include <executorch/path/to/header.h>".
33-
set(_common_include_directories
34-
${EXECUTORCH_ROOT}/..
35-
)
34+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
3635
target_include_directories(executorch INTERFACE ${_common_include_directories})
3736

3837
set(_test_srcs optional_test.cpp executor_tensor_test.cpp half_test.cpp
@@ -41,6 +40,7 @@ set(_test_srcs optional_test.cpp executor_tensor_test.cpp half_test.cpp
4140

4241
add_executable(runtime_core_portable_type_test ${_test_srcs})
4342
target_link_libraries(
44-
runtime_core_portable_type_test GTest::gtest GTest::gtest_main executorch
43+
runtime_core_portable_type_test GTest::gtest GTest::gtest_main GTest::gmock
44+
executorch
4545
)
4646
add_test(ExecuTorchTest runtime_core_portable_type_test)

runtime/core/test/CMakeLists.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
# ### Editing this file ###
8+
# This file is generated by test/utils/generate_gtest_cmakelists.py
9+
#
10+
# This file should be formatted with
11+
# ~~~
12+
# cmake-format -i CMakeLists.txt
13+
# ~~~
14+
# It should also be cmake-lint clean.
15+
#
16+
17+
cmake_minimum_required(VERSION 3.19)
18+
project(runtime_core_test)
19+
20+
# Use C++14 for test.
21+
set(CMAKE_CXX_STANDARD 14)
22+
23+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
24+
25+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
26+
27+
# Find prebuilt executorch library
28+
find_package(executorch CONFIG REQUIRED)
29+
30+
enable_testing()
31+
find_package(GTest CONFIG REQUIRED)
32+
33+
# Let files say "include <executorch/path/to/header.h>".
34+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
35+
target_include_directories(executorch INTERFACE ${_common_include_directories})
36+
37+
set(_test_srcs
38+
span_test.cpp
39+
error_handling_test.cpp
40+
event_tracer_test.cpp
41+
freeable_buffer_test.cpp
42+
array_ref_test.cpp
43+
memory_allocator_test.cpp
44+
hierarchical_allocator_test.cpp
45+
evalue_test.cpp
46+
)
47+
48+
add_executable(runtime_core_test ${_test_srcs})
49+
target_link_libraries(
50+
runtime_core_test GTest::gtest GTest::gtest_main GTest::gmock executorch
51+
)
52+
add_test(ExecuTorchTest runtime_core_test)

runtime/core/test/span_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ TEST(SpanTest, MutableElements) {
4444
TEST(SpanTest, Empty) {
4545
int64_t x[2] = {1, 2};
4646
Span<int64_t> span_full = {x, 2};
47-
Span<int64_t> span_empty = {x, (int64_t)0};
47+
Span<int64_t> span_empty = {x, (size_t)0};
4848

4949
EXPECT_FALSE(span_full.empty());
5050
EXPECT_TRUE(span_empty.empty());

test/run_oss_cpp_tests.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ build_executorch() {
1515
build_and_run_test() {
1616
local test_dir=$1
1717
cmake "${test_dir}" -Bcmake-out/"${test_dir}" -DCMAKE_INSTALL_PREFIX=cmake-out
18-
cmake --build cmake-out/"${test_dir}"
18+
cmake --build cmake-out/"${test_dir}" -j9
1919
for t in $(cmake-out/"${test_dir}"/*test); do ./"$t"; done
2020
}
2121

2222
build_executorch
2323
build_and_run_test runtime/core/portable_type/test/
24+
build_and_run_test runtime/core/test/
25+
build_and_run_test runtime/core/exec_aten/util/test/
26+
build_and_run_test runtime/core/exec_aten/testing_util/test/
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
import os
9+
import shutil
10+
import subprocess
11+
12+
13+
TEMPLATE = """# Copyright (c) Meta Platforms, Inc. and affiliates.
14+
# All rights reserved.
15+
#
16+
# This source code is licensed under the BSD-style license found in the
17+
# LICENSE file in the root directory of this source tree.
18+
19+
# ### Editing this file ###
20+
# This file is generated by test/utils/generate_gtest_cmakelists.py
21+
#
22+
# This file should be formatted with
23+
# ~~~
24+
# cmake-format -i CMakeLists.txt
25+
# ~~~
26+
# It should also be cmake-lint clean.
27+
#
28+
29+
cmake_minimum_required(VERSION 3.19)
30+
project({project_name})
31+
32+
# Use C++14 for test.
33+
set(CMAKE_CXX_STANDARD 14)
34+
35+
set(EXECUTORCH_ROOT ${{CMAKE_CURRENT_SOURCE_DIR}}/{path_to_root})
36+
37+
include(${{EXECUTORCH_ROOT}}/build/Utils.cmake)
38+
39+
# Find prebuilt executorch library
40+
find_package(executorch CONFIG REQUIRED)
41+
42+
enable_testing()
43+
find_package(GTest CONFIG REQUIRED)
44+
45+
# Let files say "include <executorch/path/to/header.h>".
46+
set(_common_include_directories
47+
${{EXECUTORCH_ROOT}}/..
48+
)
49+
target_include_directories(executorch INTERFACE ${{_common_include_directories}})
50+
51+
set(_test_srcs {test_srcs})
52+
53+
add_executable({project_name} ${{_test_srcs}})
54+
target_link_libraries(
55+
{project_name} GTest::gtest GTest::gtest_main GTest::gmock executorch
56+
)
57+
add_test(ExecuTorchTest {project_name})
58+
"""
59+
60+
61+
def calculate_project_name(path_to_root):
62+
"""
63+
Get a cmake project name for that path using relative path.
64+
>>> calculate_project_name("runtime/core/portable_type/test")
65+
'runtime_core_portable_type_test'
66+
"""
67+
return path_to_root.replace("/", "_")
68+
69+
70+
def calculate_relative_path(path_to_root):
71+
"""
72+
Return the relative path from the path_to_root to root (i.e. "..")
73+
>>> calculate_relative_path("runtime/core/portable_type/test")
74+
'../../../..'
75+
"""
76+
return os.path.relpath("/", "/" + path_to_root)
77+
78+
79+
def format_template(path_to_root, test_srcs):
80+
"""
81+
Format the template with the given path_to_root and test_srcs.
82+
"""
83+
return TEMPLATE.format(
84+
project_name=calculate_project_name(path_to_root),
85+
path_to_root=calculate_relative_path(path_to_root),
86+
test_srcs=" ".join(test_srcs),
87+
)
88+
89+
90+
CONFIG = [
91+
(
92+
"runtime/core/portable_type/test",
93+
[
94+
"optional_test.cpp",
95+
"executor_tensor_test.cpp",
96+
"half_test.cpp",
97+
"scalar_test.cpp",
98+
"tensor_impl_test.cpp",
99+
],
100+
),
101+
(
102+
"runtime/core/test",
103+
[
104+
"span_test.cpp",
105+
"error_handling_test.cpp",
106+
"event_tracer_test.cpp",
107+
"freeable_buffer_test.cpp",
108+
"array_ref_test.cpp",
109+
"memory_allocator_test.cpp",
110+
"hierarchical_allocator_test.cpp",
111+
"evalue_test.cpp",
112+
],
113+
),
114+
(
115+
"runtime/core/exec_aten/util/test",
116+
[
117+
"tensor_util_test.cpp",
118+
"scalar_type_util_test.cpp",
119+
"operator_impl_example_test.cpp",
120+
"dim_order_util_test.cpp",
121+
"../../testing_util/tensor_util.cpp",
122+
],
123+
),
124+
(
125+
"runtime/core/exec_aten/testing_util/test",
126+
["tensor_util_test.cpp", "tensor_factory_test.cpp", "../tensor_util.cpp"],
127+
),
128+
]
129+
130+
131+
def write_template(path_to_root, test_srcs):
132+
"""
133+
Write the template to the given path_to_root.
134+
"""
135+
with open(os.path.join(path_to_root, "CMakeLists.txt"), "w") as f:
136+
f.write(format_template(path_to_root, test_srcs))
137+
138+
139+
if __name__ == "__main__":
140+
for path_to_root, test_srcs in CONFIG:
141+
write_template(path_to_root, test_srcs)
142+
if shutil.which("cmake-format") is not None:
143+
subprocess.run(
144+
["cmake-format", "-i", path_to_root + "/CMakeLists.txt"], check=True
145+
)
146+
else:
147+
print(f"Please run cmake-format -i {path_to_root}/CMakeLists.txt")
148+
print("Note: Please update test/run_oss_cpp_tests.sh")

0 commit comments

Comments
 (0)