Skip to content

Commit 1f6abcd

Browse files
committed
WIP: Clean up custom ops usage of executorch-config.cmake
1 parent ed63fb1 commit 1f6abcd

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

build/executorch-config.cmake

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
2020
add_library(executorch STATIC IMPORTED)
2121
find_library(
2222
EXECUTORCH_LIBRARY_PATH executorch
23-
HINTS "${_root}"
24-
CMAKE_FIND_ROOT_PATH_BOTH
23+
PATHS "${_root}"
24+
REQUIRED
25+
# Look only under PATHS; do not search under other implicit roots.
26+
NO_CMAKE_FIND_ROOT_PATH
2527
)
2628
set_target_properties(
2729
executorch PROPERTIES IMPORTED_LOCATION "${EXECUTORCH_LIBRARY_PATH}"
@@ -31,8 +33,10 @@ target_include_directories(executorch INTERFACE ${_root})
3133
add_library(portable_kernels STATIC IMPORTED)
3234
find_library(
3335
PORTABLE_KERNELS_PATH portable_kernels
34-
HINTS "${_root}"
35-
CMAKE_FIND_ROOT_PATH_BOTH
36+
PATHS "${_root}"
37+
REQUIRED
38+
# Look only under PATHS; do not search under other implicit roots.
39+
NO_CMAKE_FIND_ROOT_PATH
3640
)
3741
set_target_properties(
3842
portable_kernels PROPERTIES IMPORTED_LOCATION "${PORTABLE_KERNELS_PATH}"

examples/portable/custom_ops/CMakeLists.txt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ set(_common_compile_options -Wno-deprecated-declarations -fPIC)
4242
# Let files say "include <executorch/path/to/header.h>".
4343
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
4444

45-
find_package(executorch CONFIG REQUIRED)
46-
find_package(
47-
gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party
48-
)
45+
# The path to executorch/cmake-out. We assume that the top-level project has
46+
# already been built, and then installed directly under the same cmake-out.
47+
set(_root_binary_dir "${EXECUTORCH_ROOT}/cmake-out")
48+
if(NOT EXISTS "${_root_binary_dir}")
49+
message(FATAL_ERROR "Root binary dir '${_root_binary_dir}' does not exist. "
50+
"Please build and install the top-level project first.")
51+
endif()
52+
53+
find_package(executorch CONFIG REQUIRED PATHS ${_root_binary_dir})
54+
find_package(gflags REQUIRED PATHS ${_root_binary_dir}/third-party)
4955

5056
target_include_directories(executorch INTERFACE ${_common_include_directories})
5157

@@ -63,10 +69,7 @@ option(
6369
#
6470
# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
6571
#
66-
set(
67-
EXECUTORCH_SRCS_FILE
68-
"${CMAKE_CURRENT_BINARY_DIR}/../../../executorch_srcs.cmake"
69-
)
72+
set(EXECUTORCH_SRCS_FILE "${_root_binary_dir}/executorch_srcs.cmake")
7073

7174
extract_sources(${EXECUTORCH_SRCS_FILE})
7275

examples/portable/custom_ops/test_custom_ops.sh

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
# EXIR to capture and export a model file. Then use `executor_runner` demo C++
1010
# binary to run the model.
1111

12-
set -e
12+
set -eu
13+
set -o pipefail
1314

1415
# shellcheck source=/dev/null
1516
source "$(dirname "${BASH_SOURCE[0]}")/../../../.ci/scripts/utils.sh"
1617

18+
# Allow overriding the number of build jobs. The `-` makes this safe to check
19+
# even if the variable isn't defined; it is not a negative value.
20+
readonly NUM_JOBS=${CMAKE_BUILD_PARALLEL_LEVEL:-9}
21+
1722
test_buck2_custom_op_1() {
1823
local model_name='custom_ops_1'
1924
echo "Exporting ${model_name}.pte"
@@ -39,13 +44,12 @@ test_cmake_custom_op_1() {
3944
rm -rf ${build_dir}
4045
retry cmake \
4146
-DREGISTER_EXAMPLE_CUSTOM_OP=1 \
42-
-DCMAKE_INSTALL_PREFIX=cmake-out \
4347
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
4448
-B${build_dir} \
4549
${example_dir}
4650

4751
echo "Building ${example_dir}"
48-
cmake --build ${build_dir} -j9 --config Release
52+
cmake --build ${build_dir} -j${NUM_JOBS} --config Release
4953

5054
echo 'Running custom_ops_executor_runner'
5155
${build_dir}/custom_ops_executor_runner --model_path="./${model_name}.pte"
@@ -85,21 +89,27 @@ get_shared_lib_ext() {
8589

8690
test_cmake_custom_op_2() {
8791
local model_name='custom_ops_2'
88-
SITE_PACKAGES="$(${PYTHON_EXECUTABLE} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')"
89-
CMAKE_PREFIX_PATH="$PWD/cmake-out/lib/cmake/ExecuTorch;${SITE_PACKAGES}/torch"
92+
# Declare as local separately so it doesn't hide errors when executing the
93+
# subcommand.
94+
local site_packages
95+
site_packages="$(${PYTHON_EXECUTABLE} -c \
96+
'from distutils.sysconfig import get_python_lib; print(get_python_lib())')"
97+
# executorch-config.cmake is installed under cmake-out. The torch config lives
98+
# inside its installed pip package.
99+
local cmake_prefix_path="$PWD/cmake-out;${site_packages}/torch"
90100

91101
local example_dir=examples/portable/custom_ops
92102
local build_dir=cmake-out/${example_dir}
93103
rm -rf ${build_dir}
94104
retry cmake \
95105
-DREGISTER_EXAMPLE_CUSTOM_OP=2 \
96-
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
97-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
106+
-DCMAKE_PREFIX_PATH="${cmake_prefix_path}" \
107+
-DPYTHON_EXECUTABLE="${PYTHON_EXECUTABLE}" \
98108
-B${build_dir} \
99109
${example_dir}
100110

101111
echo "Building ${example_dir}"
102-
cmake --build ${build_dir} -j9 --config Release
112+
cmake --build ${build_dir} -j${NUM_JOBS} --config Release
103113

104114
EXT=$(get_shared_lib_ext)
105115
echo "Exporting ${model_name}.pte"
@@ -110,12 +120,12 @@ test_cmake_custom_op_2() {
110120
${build_dir}/custom_ops_executor_runner --model_path="./${model_name}.pte"
111121
}
112122

113-
if [[ -z $PYTHON_EXECUTABLE ]];
123+
if [[ -z ${PYTHON_EXECUTABLE:-} ]];
114124
then
115125
PYTHON_EXECUTABLE=python3
116126
fi
117127

118-
if [[ -z $BUCK ]];
128+
if [[ -z ${BUCK:-} ]];
119129
then
120130
BUCK=buck2
121131
fi

0 commit comments

Comments
 (0)