Skip to content

Commit 7a35b6f

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Add a script for generating OSS gtest CMakeLists.txt (#3608)
Summary: Pull Request resolved: #3608 Reviewed By: larryliu0820 Differential Revision: D57351108 Pulled By: kirklandsign fbshipit-source-id: 312644cd52e1198623c287883088c79232aa14f9
1 parent 4c2ee9b commit 7a35b6f

File tree

8 files changed

+310
-8
lines changed

8 files changed

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

runtime/core/portable_type/test/CMakeLists.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
# ### Editing this file ###
7+
# @generated by test/utils/generate_gtest_cmakelists.py
88
#
99
# This file should be formatted with
1010
# ~~~
@@ -16,8 +16,8 @@
1616
cmake_minimum_required(VERSION 3.19)
1717
project(runtime_core_portable_type_test)
1818

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

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

@@ -30,9 +30,7 @@ enable_testing()
3030
find_package(GTest CONFIG REQUIRED)
3131

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

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

4240
add_executable(runtime_core_portable_type_test ${_test_srcs})
4341
target_link_libraries(
44-
runtime_core_portable_type_test GTest::gtest GTest::gtest_main executorch
42+
runtime_core_portable_type_test GTest::gtest GTest::gtest_main GTest::gmock
43+
executorch
4544
)
4645
add_test(ExecuTorchTest runtime_core_portable_type_test)

runtime/core/test/CMakeLists.txt

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

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/

test/utils/OSSTest.cmake.in

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+
# @generated by test/utils/generate_gtest_cmakelists.py
8+
#
9+
# This file should be formatted with
10+
# ~~~
11+
# cmake-format -i CMakeLists.txt
12+
# ~~~
13+
# It should also be cmake-lint clean.
14+
#
15+
16+
cmake_minimum_required(VERSION 3.19)
17+
project({project_name})
18+
19+
# Use C++14 for test.
20+
set(CMAKE_CXX_STANDARD 14)
21+
22+
set(EXECUTORCH_ROOT ${{CMAKE_CURRENT_SOURCE_DIR}}/{path_to_root})
23+
24+
include(${{EXECUTORCH_ROOT}}/build/Utils.cmake)
25+
26+
# Find prebuilt executorch library
27+
find_package(executorch CONFIG REQUIRED)
28+
29+
enable_testing()
30+
find_package(GTest CONFIG REQUIRED)
31+
32+
# Let files say "include <executorch/path/to/header.h>".
33+
set(_common_include_directories
34+
${{EXECUTORCH_ROOT}}/..
35+
)
36+
target_include_directories(executorch INTERFACE ${{_common_include_directories}})
37+
38+
set(_test_srcs {test_srcs})
39+
40+
add_executable({project_name} ${{_test_srcs}})
41+
target_link_libraries(
42+
{project_name} GTest::gtest GTest::gtest_main GTest::gmock executorch
43+
)
44+
add_test(ExecuTorchTest {project_name})

test/utils/OSSTestConfig.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{ "tests": [
2+
{
3+
"directory": "runtime/core/portable_type/test",
4+
"sources": [
5+
"optional_test.cpp",
6+
"executor_tensor_test.cpp",
7+
"half_test.cpp",
8+
"scalar_test.cpp",
9+
"tensor_impl_test.cpp"
10+
]
11+
},
12+
{
13+
"directory": "runtime/core/test",
14+
"sources": [
15+
"span_test.cpp",
16+
"error_handling_test.cpp",
17+
"event_tracer_test.cpp",
18+
"freeable_buffer_test.cpp",
19+
"array_ref_test.cpp",
20+
"memory_allocator_test.cpp",
21+
"hierarchical_allocator_test.cpp",
22+
"evalue_test.cpp"
23+
]
24+
},
25+
{
26+
"directory": "runtime/core/exec_aten/util/test",
27+
"sources": [
28+
"tensor_util_test.cpp",
29+
"scalar_type_util_test.cpp",
30+
"operator_impl_example_test.cpp",
31+
"dim_order_util_test.cpp",
32+
"../../testing_util/tensor_util.cpp"
33+
]
34+
},
35+
{
36+
"directory": "runtime/core/exec_aten/testing_util/test",
37+
"sources": [
38+
"tensor_util_test.cpp",
39+
"tensor_factory_test.cpp",
40+
"../tensor_util.cpp"
41+
]
42+
}
43+
] }
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 json
9+
import os
10+
import shutil
11+
import subprocess
12+
13+
14+
def calculate_project_name(path_to_root):
15+
"""
16+
Get a cmake project name for that path using relative path.
17+
>>> calculate_project_name("runtime/core/portable_type/test")
18+
'runtime_core_portable_type_test'
19+
"""
20+
return path_to_root.replace("/", "_")
21+
22+
23+
def calculate_relative_path(path_to_root):
24+
"""
25+
Return the relative path from the path_to_root to root (i.e. "..")
26+
>>> calculate_relative_path("runtime/core/portable_type/test")
27+
'../../../..'
28+
"""
29+
return os.path.relpath("/", "/" + path_to_root)
30+
31+
32+
def format_template(path_to_root, test_srcs):
33+
"""
34+
Format the template with the given path_to_root and test_srcs.
35+
"""
36+
with open(os.path.dirname(os.path.abspath(__file__)) + "/OSSTest.cmake.in") as f:
37+
template = f.read()
38+
return template.format(
39+
project_name=calculate_project_name(path_to_root),
40+
path_to_root=calculate_relative_path(path_to_root),
41+
test_srcs=" ".join(test_srcs),
42+
)
43+
44+
45+
def write_template(path_to_root, test_srcs):
46+
"""
47+
Write the template to the given path_to_root.
48+
"""
49+
with open(os.path.join(path_to_root, "CMakeLists.txt"), "w") as f:
50+
f.write(format_template(path_to_root, test_srcs))
51+
52+
53+
def read_config_json(json_path):
54+
"""
55+
Read the config.json file and return the list of (path_to_root, test_srcs)
56+
"""
57+
with open(json_path) as f:
58+
config = json.load(f)
59+
return [(d["directory"], d["sources"]) for d in config["tests"]]
60+
61+
62+
if __name__ == "__main__":
63+
json_path = os.path.dirname(os.path.abspath(__file__)) + "/OSSTestConfig.json"
64+
for path_to_root, test_srcs in read_config_json(json_path):
65+
write_template(path_to_root, test_srcs)
66+
if shutil.which("cmake-format") is not None:
67+
subprocess.run(
68+
["cmake-format", "-i", path_to_root + "/CMakeLists.txt"], check=True
69+
)
70+
else:
71+
print(f"Please run cmake-format -i {path_to_root}/CMakeLists.txt")
72+
print("Note: Please update test/run_oss_cpp_tests.sh")

0 commit comments

Comments
 (0)