Skip to content

Commit 5ff0169

Browse files
committed
[cmake] Install libexecutorch.a and libportable_kernels.a
Summary: This PR is the first one to decentralize options only used by examples, from root level CMakeLists.txt to examples directories. Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent 2570eec commit 5ff0169

File tree

5 files changed

+134
-41
lines changed

5 files changed

+134
-41
lines changed

CMakeLists.txt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ endif()
5454
if(NOT CMAKE_BUILD_TYPE)
5555
set(CMAKE_BUILD_TYPE Debug)
5656
endif()
57+
# ------------------------------ OPTIONS -------------------------------------
58+
# WARNING: Please don't add example specific options in this CMakeLists.txt.
59+
# Instead please use `find_package(Executorch REQUIRED)` in the example
60+
# directory and add a new executable in the example `CMakeLists.txt`.
5761

5862
# _default_release_disabled_options: default value for options that should be
5963
# disabled in Release mode by default. Users can still manually enable them,
@@ -111,16 +115,9 @@ option(BUILD_SELECTIVE_BUILD_TEST
111115

112116
option(EXECUTORCH_BUILD_SIZE_TEST "Whether to build size test" OFF)
113117

114-
# Selective build options.
115-
option(EXECUTORCH_SELECT_ALL_OPS
116-
"Whether to register all ops defined in portable kernel library." ON)
117-
118118
# Option to register op list
119119
option(EXECUTORCH_SELECT_OPS_LIST "Register the following list of ops" OFF)
120120

121-
# Option to register ops from yaml file
122-
option(EXECUTORCH_SELECT_OPS_YAML "Register all the ops from a given yaml file"
123-
OFF)
124121
# Do not enable select all ops if any of the other select options is on.
125122
if(EXECUTORCH_SELECT_OPS_LIST OR EXECUTORCH_SELECT_OPS_YAML)
126123
set(EXECUTORCH_SELECT_ALL_OPS OFF)
@@ -281,6 +278,16 @@ if(EXECUTORCH_BUILD_GFLAGS)
281278
add_subdirectory(third-party/gflags)
282279
endif()
283280

281+
282+
# Install `executorch` library as well as `ExecuTorchConfig.cmake`
283+
# under ${CMAKE_INSTALL_PREFIX}/
284+
install(
285+
TARGETS executorch
286+
DESTINATION lib
287+
INCLUDES DESTINATION ${_common_include_directories}
288+
)
289+
install(FILES build/ExecuTorchConfig.cmake DESTINATION lib/cmake/ExecuTorch)
290+
284291
#
285292
# executor_runner: Host tool that demonstrates program execution.
286293
#

build/ExecuTorchConfig.cmake

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
cmake_minimum_required(VERSION 3.19)
8+
9+
set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
10+
add_library(executorch STATIC IMPORTED)
11+
find_library(
12+
EXECUTORCH_LIBRARY_PATH executorch HINTS "${_root}"
13+
)
14+
set_target_properties(
15+
executorch PROPERTIES IMPORTED_LOCATION "${EXECUTORCH_LIBRARY_PATH}"
16+
)
17+
target_include_directories(executorch INTERFACE ${_root})
18+
19+
add_library(portable_kernels STATIC IMPORTED)
20+
find_library(
21+
PORTABLE_KERNELS_PATH portable_kernels HINTS "${_root}"
22+
)
23+
set_target_properties(
24+
portable_kernels PROPERTIES IMPORTED_LOCATION "${PORTABLE_KERNELS_PATH}"
25+
)
26+
target_include_directories(portable_kernels INTERFACE ${_root})

examples/selective_build/CMakeLists.txt

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,66 @@
1515
# ~~~
1616
# It should also be cmake-lint clean.
1717
#
18-
1918
cmake_minimum_required(VERSION 3.19)
19+
project(SelectiveBuildExample)
20+
21+
if(NOT PYTHON_EXECUTABLE)
22+
set(PYTHON_EXECUTABLE python3)
23+
endif()
24+
2025
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
2126
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
2227
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
2328
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
2429

30+
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
31+
32+
# Let files say "include <executorch/path/to/header.h>".
33+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
34+
35+
find_package(ExecuTorch CONFIG REQUIRED)
36+
find_package(
37+
gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../third-party
38+
)
39+
40+
target_include_directories(executorch INTERFACE ${_common_include_directories})
41+
42+
# ------------------------------ OPTIONS BEGIN -------------------------------
43+
44+
# Option to register ops from yaml file
45+
option(EXECUTORCH_SELECT_OPS_YAML "Register all the ops from a given yaml file"
46+
OFF)
47+
48+
# Option to register op list
49+
option(EXECUTORCH_SELECT_OPS_LIST "Register the following list of ops" OFF)
50+
51+
# Selective build options.
52+
option(EXECUTORCH_SELECT_ALL_OPS
53+
"Whether to register all ops defined in portable kernel library." OFF)
54+
# ------------------------------- OPTIONS END --------------------------------
55+
56+
#
57+
# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
58+
#
59+
set(
60+
EXECUTORCH_SRCS_FILE
61+
"${CMAKE_CURRENT_BINARY_DIR}/../../executorch_srcs.cmake"
62+
)
63+
if(NOT EXISTS "${EXECUTORCH_SRCS_FILE}")
64+
# A file wasn't generated. Run a script to extract the source lists from the
65+
# buck2 build system and write them to a file we can include.
66+
#
67+
# NOTE: This will only happen once during cmake setup, so it will not re-run
68+
# if the buck2 targets change.
69+
message(STATUS "executorch: Generating source lists")
70+
set(EXECUTORCH_SRCS_FILE "${CMAKE_CURRENT_BINARY_DIR}/executorch_srcs.cmake")
71+
extract_sources(${EXECUTORCH_SRCS_FILE})
72+
endif()
73+
74+
# This file defines the `_<target>__srcs` variables used below.
75+
message(STATUS "executorch: Using sources file ${EXECUTORCH_SRCS_FILE}")
76+
include(${EXECUTORCH_SRCS_FILE})
77+
2578
#
2679
# select_build_lib: C++ library to register selected ops in custom kernel
2780
# library
@@ -30,7 +83,6 @@ set(_kernel_lib)
3083
if(EXECUTORCH_SELECT_OPS_YAML)
3184
set(_custom_ops_yaml
3285
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops.yaml)
33-
gen_selected_ops("${_custom_ops_yaml}" "" "")
3486
set(kernel_sources
3587
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops_1_out.cpp
3688
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops_2_out.cpp)
@@ -55,16 +107,13 @@ generate_bindings_for_kernels(${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
55107
"${_custom_ops_yaml}")
56108
gen_operators_lib("select_build_lib" ${_kernel_lib} executorch)
57109

58-
set(_updated__srcs)
59-
foreach(_src ${_executor_runner__srcs})
60-
list(APPEND _updated__srcs "${EXECUTORCH_ROOT}/${_src}")
61-
endforeach()
110+
list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
62111

63112
#
64113
# selective_build_test: test binary to allow different operator libraries to
65114
# link to
66115
#
67-
add_executable(selective_build_test ${_updated__srcs})
116+
add_executable(selective_build_test ${_executor_runner__srcs})
68117
if(CMAKE_BUILD_TYPE EQUAL "RELEASE")
69118
target_link_options(selective_build_test PRIVATE "LINKER:--gc-sections")
70119
endif()

examples/selective_build/test_selective_build.sh

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ set -e
1515
# shellcheck source=/dev/null
1616
source "$(dirname "${BASH_SOURCE[0]}")/../../.ci/scripts/utils.sh"
1717

18+
cmake_install_executorch_lib() {
19+
echo "Installing libexecutorch.a and libportable_kernels.a"
20+
rm -rf cmake-out
21+
retry cmake -DBUCK2="$BUCK" \
22+
-DCMAKE_INSTALL_PREFIX=cmake-out \
23+
-DCMAKE_BUILD_TYPE=Release \
24+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
25+
-Bcmake-out .
26+
cmake --build cmake-out -j9 --target install --config Release
27+
}
28+
1829
test_buck2_select_all_ops() {
1930
echo "Exporting MobilenetV3"
2031
${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="mv3"
@@ -57,17 +68,17 @@ test_cmake_select_all_ops() {
5768
echo "Exporting MobilenetV3"
5869
${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="mv3"
5970

60-
(rm -rf cmake-out \
61-
&& mkdir cmake-out \
62-
&& cd cmake-out \
63-
&& retry cmake -DBUCK2="$BUCK" \
64-
-DBUILD_SELECTIVE_BUILD_TEST=ON \
71+
rm -rf cmake-out/examples/selective_build
72+
retry cmake -DBUCK2="$BUCK" \
6573
-DCMAKE_BUILD_TYPE=Release \
6674
-DEXECUTORCH_SELECT_ALL_OPS=ON \
67-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
75+
-DCMAKE_INSTALL_PREFIX=cmake-out \
76+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
77+
-Bcmake-out/examples/selective_build \
78+
examples/selective_build
6879

6980
echo "Build selective build test"
70-
cmake --build cmake-out -j9 --config Release
81+
cmake --build cmake-out/examples/selective_build -j9 --config Release
7182

7283
echo 'Running selective build test'
7384
cmake-out/examples/selective_build/selective_build_test --model_path="./mv3.pte"
@@ -81,21 +92,21 @@ test_cmake_select_ops_in_list() {
8192
${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="mv2"
8293

8394
# set MAX_KERNEL_NUM=17: 14 primops, add, mul
84-
(rm -rf cmake-out \
85-
&& mkdir cmake-out \
86-
&& cd cmake-out \
87-
&& retry cmake -DBUCK2="$BUCK" \
88-
-DMAX_KERNEL_NUM=17 \
89-
-DBUILD_SELECTIVE_BUILD_TEST=ON \
95+
rm -rf cmake-out/examples/selective_build
96+
retry cmake -DBUCK2="$BUCK" \
9097
-DCMAKE_BUILD_TYPE=Release \
98+
-DMAX_KERNEL_NUM=17 \
9199
-DEXECUTORCH_SELECT_OPS_LIST="aten::convolution.out,\
92100
aten::_native_batch_norm_legit_no_training.out,aten::hardtanh.out,aten::add.out,\
93101
aten::mean.out,aten::view_copy.out,aten::permute_copy.out,aten::addmm.out,\
94102
aten,aten::clone.out" \
95-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
103+
-DCMAKE_INSTALL_PREFIX=cmake-out \
104+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
105+
-Bcmake-out/examples/selective_build \
106+
examples/selective_build
96107

97108
echo "Build selective build test"
98-
cmake --build cmake-out -j9 --config Release
109+
cmake --build cmake-out/examples/selective_build -j9 --config Release
99110

100111
echo 'Running selective build test'
101112
cmake-out/examples/selective_build/selective_build_test --model_path="./mv2.pte"
@@ -108,17 +119,17 @@ test_cmake_select_ops_in_yaml() {
108119
echo "Exporting custom_op_1"
109120
${PYTHON_EXECUTABLE} -m examples.portable.custom_ops.custom_ops_1
110121

111-
(rm -rf cmake-out \
112-
&& mkdir cmake-out \
113-
&& cd cmake-out \
114-
&& retry cmake -DBUCK2="$BUCK" \
115-
-DBUILD_SELECTIVE_BUILD_TEST=ON \
122+
rm -rf cmake-out/examples/selective_build
123+
retry cmake -DBUCK2="$BUCK" \
116124
-DCMAKE_BUILD_TYPE=Release \
117125
-DEXECUTORCH_SELECT_OPS_YAML=ON \
118-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
126+
-DCMAKE_INSTALL_PREFIX=cmake-out \
127+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
128+
-Bcmake-out/examples/selective_build \
129+
examples/selective_build
119130

120131
echo "Build selective build test"
121-
cmake --build cmake-out -j9 --config Release
132+
cmake --build cmake-out/examples/selective_build -j9 --config Release
122133

123134
echo 'Running selective build test'
124135
cmake-out/examples/selective_build/selective_build_test --model_path="./custom_ops_1.pte"
@@ -139,6 +150,7 @@ fi
139150

140151
if [[ $1 == "cmake" ]];
141152
then
153+
cmake_install_executorch_lib
142154
test_cmake_select_all_ops
143155
test_cmake_select_ops_in_list
144156
test_cmake_select_ops_in_yaml

kernels/portable/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@ list(FILTER _portable_kernels__srcs EXCLUDE REGEX "test/*.cpp")
3939
list(FILTER _portable_kernels__srcs EXCLUDE REGEX "codegen")
4040
# Generate C++ bindings to register kernels into both PyTorch (for AOT) and
4141
# Executorch (for runtime). Here select all ops in functions.yaml
42-
if(EXECUTORCH_SELECT_OPS_YAML)
43-
set(_yaml "${CMAKE_CURRENT_LIST_DIR}/functions.yaml")
44-
endif()
45-
gen_selected_ops(
46-
"${_yaml}" "${EXECUTORCH_SELECT_OPS_LIST}" "${EXECUTORCH_SELECT_ALL_OPS}")
42+
set(_yaml "${CMAKE_CURRENT_LIST_DIR}/functions.yaml")
43+
gen_selected_ops("${_yaml}" "" "")
4744
# Expect gen_selected_ops output file to be selected_operators.yaml
4845
generate_bindings_for_kernels(${CMAKE_CURRENT_SOURCE_DIR}/functions.yaml "")
4946
message("Generated files ${gen_command_sources}")
@@ -62,3 +59,5 @@ target_compile_options(portable_kernels PUBLIC ${_common_compile_options})
6259
# portable_ops_lib: Register portable_ops_lib ops kernels into Executorch
6360
# runtime
6461
gen_operators_lib("portable_ops_lib" portable_kernels executorch)
62+
63+
install(TARGETS portable_kernels DESTINATION lib)

0 commit comments

Comments
 (0)