Skip to content

Commit 59d84ee

Browse files
larryliu0820facebook-github-bot
authored andcommitted
Add a test for building size_test with no ops (#375)
Summary: Pull Request resolved: #375 As titled, adding a new CMakeLists.txt for building size_test Reviewed By: dbort Differential Revision: D49336922 fbshipit-source-id: 231340961894c1e0b2f7c56be0edb3ac7d6e2f2e
1 parent 9383453 commit 59d84ee

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ endif()
5656
# -Os: Optimize for size. -ffunction-sections -fdata-sections: breaks function
5757
# and data into sections so they can be properly gc'd. -s: strip symbols
5858
set(CMAKE_CXX_FLAGS_RELEASE
59-
"-Os -ffunction-sections -fdata-sections -s -fno-exceptions -fno-rtti")
59+
"-Os -ffunction-sections -fdata-sections -fno-exceptions -fno-rtti")
60+
if(NOT APPLE)
61+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")
62+
endif()
6063
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
6164

6265
# Option to register custom operator `my_ops::mul3` or `my_ops::mul4` or no
@@ -76,6 +79,9 @@ option(REGISTER_QUANTIZED_OPS
7679
option(BUILD_SELECTIVE_BUILD_TEST
7780
"Whether to build binary for demo selective build" OFF)
7881

82+
option(EXECUTORCH_BUILD_SIZE_TEST
83+
"Whether to build size test" OFF)
84+
7985
if(BUILD_SELECTIVE_BUILD_TEST)
8086
option(SELECT_ALL_OPS
8187
"Whether to register all ops defined in portable kernel library." OFF)
@@ -280,5 +286,10 @@ endif()
280286
if(BUILD_SELECTIVE_BUILD_TEST)
281287
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/selective_build)
282288
endif()
289+
290+
# Add size test subdirectory
291+
if(EXECUTORCH_BUILD_SIZE_TEST)
292+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test)
293+
endif()
283294
# Print all summary
284295
executorch_print_configuration_summary()

build/Codegen.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function(gen_selected_ops ops_schema_yaml root_ops include_all_ops)
2525
if(include_all_ops)
2626
list(APPEND _gen_oplist_command --include_all_operators)
2727
endif()
28+
2829
message("Command - ${_gen_oplist_command}")
2930
add_custom_command(
3031
COMMENT "Generating selected_operators.yaml for custom ops"
@@ -113,7 +114,9 @@ function(gen_operators_lib lib_name kernel_lib deps)
113114
${CMAKE_CURRENT_BINARY_DIR}/Functions.h
114115
${CMAKE_CURRENT_BINARY_DIR}/NativeFunctions.h)
115116
target_link_libraries(${lib_name} PRIVATE ${deps})
116-
target_link_libraries(${lib_name} INTERFACE ${kernel_lib})
117+
if(kernel_lib)
118+
target_link_libraries(${lib_name} INTERFACE ${kernel_lib})
119+
endif()
117120

118121
target_link_options_shared_lib(${lib_name})
119122
endfunction()

build/cmake_deps.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ deps = [
6363
"portable_kernels",
6464
]
6565

66+
[targets.size_test]
67+
buck_targets = [
68+
"//test:size_test",
69+
]
70+
filters = [
71+
".cpp$",
72+
]
73+
excludes = [
74+
"^codegen",
75+
]
76+
deps = [
77+
"executorch",
78+
]
79+
6680
[targets.xnnpack_schema]
6781
buck_targets = [
6882
"//backends/xnnpack:xnnpack_schema"

test/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#
8+
# Simple CMake build system for size_test demo.
9+
#
10+
# ### Editing this file ###
11+
#
12+
# This file should be formatted with
13+
# ~~~
14+
# cmake-format --first-comment-is-literal=True CMakeLists.txt
15+
# ~~~
16+
# It should also be cmake-lint clean.
17+
#
18+
19+
cmake_minimum_required(VERSION 3.19)
20+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
21+
22+
# Since extract_sources.py is not returning absolute values, we need to patch
23+
# the source paths. TODO(larryliu0820): Fix this
24+
set(_updated_size_test__srcs)
25+
foreach(_src ${_size_test__srcs})
26+
list(APPEND _updated_size_test__srcs "${EXECUTORCH_ROOT}/${_src}")
27+
endforeach()
28+
29+
#
30+
# size_test: minimal binary with no ops and no delegate backend
31+
#
32+
# TODO(larryliu0820): Add EXECUTORCH_BUILD_EXECUTABLES to not build executable
33+
# when we cross compile to ios
34+
add_executable(size_test ${_updated_size_test__srcs})
35+
target_link_libraries(size_test executorch)
36+
if(CMAKE_BUILD_TYPE EQUAL "Release")
37+
target_link_options(size_test PRIVATE "LINKER:--gc-sections")
38+
endif()
39+
40+
# Print all summary
41+
executorch_print_configuration_summary()

test/build_size_test.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
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+
# Build size_test and show the size of it
9+
set -e
10+
11+
# shellcheck source=/dev/null
12+
source "$(dirname "${BASH_SOURCE[0]}")/../.ci/scripts/utils.sh"
13+
14+
test_cmake_size_test() {
15+
(rm -rf cmake-out \
16+
&& mkdir cmake-out \
17+
&& cd cmake-out \
18+
&& retry cmake -DBUCK2="$BUCK2" \
19+
-DBUILD_SIZE_TEST=ON \
20+
-DCMAKE_BUILD_TYPE=Release \
21+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
22+
23+
echo "Build selective build test"
24+
cmake --build cmake-out -j9 --config Release
25+
26+
echo 'Size of the binary:'
27+
ls -al cmake-out/test/size_test
28+
}
29+
30+
if [[ -z $BUCK2 ]]; then
31+
BUCK2=buck2
32+
fi
33+
34+
if [[ -z $PYTHON_EXECUTABLE ]]; then
35+
PYTHON_EXECUTABLE=python3
36+
fi
37+
38+
test_cmake_size_test

0 commit comments

Comments
 (0)