Skip to content

Commit 6efe44c

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Improvements to OSS Test CMake generation and add tests (#3649)
Summary: * Fix the bash script looping through exectubales * For the generator, add a way to add additional libs in generated CMakeLists.txt * Build extension/data_loader in main target as an example for the fix Pull Request resolved: #3649 Test Plan: `sh test/run_oss_cpp_tests.sh` Reviewed By: larryliu0820 Differential Revision: D57467885 Pulled By: kirklandsign fbshipit-source-id: 49f9b8dac9f091aca05a122aaf0cb9020d192b68
1 parent 0364d45 commit 6efe44c

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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(extension_data_loader_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 buffer_data_loader_test.cpp shared_ptr_data_loader_test.cpp
37+
file_data_loader_test.cpp mmap_data_loader_test.cpp
38+
)
39+
40+
add_executable(extension_data_loader_test ${_test_srcs})
41+
target_link_libraries(
42+
extension_data_loader_test GTest::gtest GTest::gtest_main GTest::gmock
43+
executorch extension_data_loader
44+
)
45+
add_test(ExecuTorchTest extension_data_loader_test)

test/run_oss_cpp_tests.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@
88
set -ex
99

1010
build_executorch() {
11-
cmake . -DCMAKE_INSTALL_PREFIX=cmake-out -DEXECUTORCH_BUILD_GTESTS=ON -Bcmake-out
11+
cmake . \
12+
-DCMAKE_INSTALL_PREFIX=cmake-out \
13+
-DEXECUTORCH_BUILD_GTESTS=ON \
14+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
15+
-Bcmake-out
1216
cmake --build cmake-out -j9 --target install
1317
}
1418

1519
build_and_run_test() {
1620
local test_dir=$1
1721
cmake "${test_dir}" -Bcmake-out/"${test_dir}" -DCMAKE_INSTALL_PREFIX=cmake-out
1822
cmake --build cmake-out/"${test_dir}" -j9
19-
for t in $(cmake-out/"${test_dir}"/*test); do ./"$t"; done
23+
for t in cmake-out/"${test_dir}"/*test; do ./"$t"; done
2024
}
2125

2226
build_executorch
27+
build_and_run_test extension/data_loader/test/
2328
build_and_run_test runtime/core/portable_type/test/
2429
build_and_run_test runtime/core/test/
2530
build_and_run_test runtime/core/exec_aten/util/test/

test/utils/OSSTest.cmake.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ set(_test_srcs {test_srcs})
4040
add_executable({project_name} ${{_test_srcs}})
4141
target_link_libraries(
4242
{project_name} GTest::gtest GTest::gtest_main GTest::gmock executorch
43+
{additional_libs}
4344
)
4445
add_test(ExecuTorchTest {project_name})

test/utils/OSSTestConfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
{ "tests": [
2+
{
3+
"directory": "extension/data_loader/test",
4+
"sources": [
5+
"buffer_data_loader_test.cpp",
6+
"shared_ptr_data_loader_test.cpp",
7+
"file_data_loader_test.cpp",
8+
"mmap_data_loader_test.cpp"
9+
],
10+
"additional_libs": [
11+
"extension_data_loader"
12+
]
13+
},
214
{
315
"directory": "runtime/core/portable_type/test",
416
"sources": [

test/utils/generate_gtest_cmakelists.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def calculate_relative_path(path_to_root):
2929
return os.path.relpath("/", "/" + path_to_root)
3030

3131

32-
def format_template(path_to_root, test_srcs):
32+
def format_template(path_to_root, test_srcs, additional_libs):
3333
"""
3434
Format the template with the given path_to_root and test_srcs.
3535
"""
@@ -39,30 +39,34 @@ def format_template(path_to_root, test_srcs):
3939
project_name=calculate_project_name(path_to_root),
4040
path_to_root=calculate_relative_path(path_to_root),
4141
test_srcs=" ".join(test_srcs),
42+
additional_libs=" ".join(additional_libs),
4243
)
4344

4445

45-
def write_template(path_to_root, test_srcs):
46+
def write_template(path_to_root, test_srcs, additional_libs):
4647
"""
4748
Write the template to the given path_to_root.
4849
"""
4950
with open(os.path.join(path_to_root, "CMakeLists.txt"), "w") as f:
50-
f.write(format_template(path_to_root, test_srcs))
51+
f.write(format_template(path_to_root, test_srcs, additional_libs))
5152

5253

5354
def read_config_json(json_path):
5455
"""
55-
Read the config.json file and return the list of (path_to_root, test_srcs)
56+
Read the config.json file
5657
"""
5758
with open(json_path) as f:
5859
config = json.load(f)
59-
return [(d["directory"], d["sources"]) for d in config["tests"]]
60+
return config["tests"]
6061

6162

6263
if __name__ == "__main__":
6364
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)
65+
for d in read_config_json(json_path):
66+
path_to_root = d["directory"]
67+
test_srcs = d["sources"]
68+
additional_libs = d.get("additional_libs", [])
69+
write_template(path_to_root, test_srcs, additional_libs)
6670
if shutil.which("cmake-format") is not None:
6771
subprocess.run(
6872
["cmake-format", "-i", path_to_root + "/CMakeLists.txt"], check=True

0 commit comments

Comments
 (0)