Skip to content

Commit 7cd4bec

Browse files
committed
Improve pip package build (#5965)
Summary: Addressing comments in #5961. * Separate out `executorch-wheel-config.cmake` from `executorch-config.cmake`. * Hardcode the envrionment variable `SETUPTOOLS_EXT_SUFFIX` in `setup.py`. Pull Request resolved: #5965 Reviewed By: dbort Differential Revision: D64017947 Pulled By: larryliu0820 fbshipit-source-id: 0bdff5e2d2ec5873540d1b701595c7a316e84e80
1 parent 08a53e1 commit 7cd4bec

File tree

5 files changed

+114
-110
lines changed

5 files changed

+114
-110
lines changed

build/executorch-config.cmake

Lines changed: 67 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,112 +9,80 @@
99
# is:
1010
#
1111
# find_package(executorch REQUIRED)
12-
# -------
13-
#
14-
# Finds the ExecuTorch library
15-
#
16-
# This will define the following variables:
17-
#
18-
# EXECUTORCH_FOUND -- True if the system has the Torch library
19-
# EXECUTORCH_INCLUDE_DIRS -- The include directories for torch
20-
# EXECUTORCH_LIBRARIES -- Libraries to link against
21-
#
12+
2213
cmake_minimum_required(VERSION 3.19)
2314

24-
# Find prebuilt libportable_lib.so. If found, assuming current file is inside
25-
# a pip package:
26-
# <site-packages>/executorch/executorch-config.cmake.
27-
# If not found, assuming current file is inside cmake-out:
28-
# <cmake-out>/cmake/ExecuTorch/executorch-config.cmake
29-
find_library(_portable_lib_LIBRARY _portable_lib.so PATHS "${CMAKE_CURRENT_LIST_DIR}/extension/pybindings/")
30-
message(WARNING "${CMAKE_CURRENT_LIST_DIR}/extension/pybindings/")
31-
set(EXECUTORCH_LIBRARIES)
32-
if(_portable_lib_LIBRARY)
33-
# Assuming current file is <site-packages>/executorch/executorch-config.cmake
34-
message(WARNING "portable library is found")
35-
list(APPEND EXECUTORCH_LIBRARIES _portable_lib)
36-
add_library(_portable_lib STATIC IMPORTED)
37-
set(EXECUTORCH_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include)
38-
set_target_properties(_portable_lib PROPERTIES
39-
IMPORTED_LOCATION "${_portable_lib_LIBRARY}"
40-
INTERFACE_INCLUDE_DIRECTORIES "${EXECUTORCH_INCLUDE_DIRS}"
41-
CXX_STANDARD 17
15+
set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
16+
set(required_lib_list executorch executorch_no_prim_ops portable_kernels)
17+
foreach(lib ${required_lib_list})
18+
set(lib_var "LIB_${lib}")
19+
add_library(${lib} STATIC IMPORTED)
20+
find_library(
21+
${lib_var} ${lib}
22+
HINTS "${_root}"
23+
CMAKE_FIND_ROOT_PATH_BOTH
4224
)
43-
else()
44-
# Assuming current file is <cmake-out>/cmake/ExecuTorch/executorch-config.cmake
45-
message(WARNING "portable library is not found")
46-
set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
47-
set(required_lib_list executorch executorch_no_prim_ops portable_kernels)
48-
foreach(lib ${required_lib_list})
49-
set(lib_var "LIB_${lib}")
50-
add_library(${lib} STATIC IMPORTED)
51-
find_library(
52-
${lib_var} ${lib}
53-
HINTS "${_root}"
54-
CMAKE_FIND_ROOT_PATH_BOTH
55-
)
56-
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
57-
target_include_directories(${lib} INTERFACE ${_root})
58-
endforeach()
25+
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
26+
target_include_directories(${lib} INTERFACE ${_root})
27+
endforeach()
5928

60-
target_link_libraries(executorch INTERFACE executorch_no_prim_ops)
29+
target_link_libraries(executorch INTERFACE executorch_no_prim_ops)
6130

62-
if(CMAKE_BUILD_TYPE MATCHES "Debug")
63-
set(FLATCCRT_LIB flatccrt_d)
64-
else()
65-
set(FLATCCRT_LIB flatccrt)
66-
endif()
31+
if(CMAKE_BUILD_TYPE MATCHES "Debug")
32+
set(FLATCCRT_LIB flatccrt_d)
33+
else()
34+
set(FLATCCRT_LIB flatccrt)
35+
endif()
6736

68-
set(lib_list
69-
etdump
70-
bundled_program
71-
extension_data_loader
72-
${FLATCCRT_LIB}
73-
coremldelegate
74-
mpsdelegate
75-
qnn_executorch_backend
76-
portable_ops_lib
77-
extension_module
78-
extension_module_static
79-
extension_runner_util
80-
extension_tensor
81-
extension_threadpool
82-
xnnpack_backend
83-
XNNPACK
84-
cpuinfo
85-
pthreadpool
86-
vulkan_backend
87-
optimized_kernels
88-
cpublas
89-
eigen_blas
90-
optimized_ops_lib
91-
optimized_native_cpu_ops_lib
92-
quantized_kernels
93-
quantized_ops_lib
94-
quantized_ops_aot_lib
37+
set(lib_list
38+
etdump
39+
bundled_program
40+
extension_data_loader
41+
${FLATCCRT_LIB}
42+
coremldelegate
43+
mpsdelegate
44+
qnn_executorch_backend
45+
portable_ops_lib
46+
extension_module
47+
extension_module_static
48+
extension_runner_util
49+
extension_tensor
50+
extension_threadpool
51+
xnnpack_backend
52+
XNNPACK
53+
cpuinfo
54+
pthreadpool
55+
vulkan_backend
56+
optimized_kernels
57+
cpublas
58+
eigen_blas
59+
optimized_ops_lib
60+
optimized_native_cpu_ops_lib
61+
quantized_kernels
62+
quantized_ops_lib
63+
quantized_ops_aot_lib
64+
)
65+
foreach(lib ${lib_list})
66+
# Name of the variable which stores result of the find_library search
67+
set(lib_var "LIB_${lib}")
68+
find_library(
69+
${lib_var} ${lib}
70+
HINTS "${_root}"
71+
CMAKE_FIND_ROOT_PATH_BOTH
9572
)
96-
foreach(lib ${lib_list})
97-
# Name of the variable which stores result of the find_library search
98-
set(lib_var "LIB_${lib}")
99-
find_library(
100-
${lib_var} ${lib}
101-
HINTS "${_root}"
102-
CMAKE_FIND_ROOT_PATH_BOTH
73+
if(NOT ${lib_var})
74+
message("${lib} library is not found.
75+
If needed rebuild with the proper options in CMakeLists.txt"
10376
)
104-
if(NOT ${lib_var})
105-
message("${lib} library is not found.
106-
If needed rebuild with the proper options in CMakeLists.txt"
107-
)
77+
else()
78+
if("${lib}" STREQUAL "extension_module" AND (NOT CMAKE_TOOLCHAIN_IOS))
79+
add_library(${lib} SHARED IMPORTED)
10880
else()
109-
if("${lib}" STREQUAL "extension_module" AND (NOT CMAKE_TOOLCHAIN_IOS))
110-
add_library(${lib} SHARED IMPORTED)
111-
else()
112-
# Building a share library on iOS requires code signing, so it's easier to
113-
# keep all libs as static when CMAKE_TOOLCHAIN_IOS is used
114-
add_library(${lib} STATIC IMPORTED)
115-
endif()
116-
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
117-
target_include_directories(${lib} INTERFACE ${_root})
81+
# Building a share library on iOS requires code signing, so it's easier to
82+
# keep all libs as static when CMAKE_TOOLCHAIN_IOS is used
83+
add_library(${lib} STATIC IMPORTED)
11884
endif()
119-
endforeach()
120-
endif()
85+
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
86+
target_include_directories(${lib} INTERFACE ${_root})
87+
endif()
88+
endforeach()

build/executorch-wheel-config.cmake

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
# Config defining how CMake should find ExecuTorch package. CMake will search
8+
# for this file and find ExecuTorch package if it is installed. Typical usage
9+
# is:
10+
#
11+
# find_package(executorch REQUIRED)
12+
# -------
13+
#
14+
# Finds the ExecuTorch library
15+
#
16+
# This will define the following variables:
17+
#
18+
# EXECUTORCH_FOUND -- True if the system has the ExecuTorch library
19+
# EXECUTORCH_INCLUDE_DIRS -- The include directories for ExecuTorch
20+
# EXECUTORCH_LIBRARIES -- Libraries to link against
21+
#
22+
cmake_minimum_required(VERSION 3.19)
23+
24+
# Find prebuilt _portable_lib.so. This file should be installed under
25+
# <site-packages>/executorch/share/cmake
26+
find_library(_portable_lib_LIBRARY _portable_lib.so PATHS "${CMAKE_CURRENT_LIST_DIR}/../../extension/pybindings/")
27+
set(EXECUTORCH_LIBRARIES)
28+
set(EXECUTORCH_FOUND OFF)
29+
if(_portable_lib_LIBRARY)
30+
set(EXECUTORCH_FOUND ON)
31+
message(STATUS "ExecuTorch portable library is found at ${_portable_lib_LIBRARY}")
32+
list(APPEND EXECUTORCH_LIBRARIES _portable_lib)
33+
add_library(_portable_lib STATIC IMPORTED)
34+
set(EXECUTORCH_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include)
35+
set_target_properties(_portable_lib PROPERTIES
36+
IMPORTED_LOCATION "${_portable_lib_LIBRARY}"
37+
INTERFACE_INCLUDE_DIRECTORIES "${EXECUTORCH_INCLUDE_DIRS}"
38+
CXX_STANDARD 17
39+
)
40+
endif()

build/packaging/env_var_script_linux.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
# Enable pybindings so that users can execute ExecuTorch programs from python.
1212
export EXECUTORCH_BUILD_PYBIND=1
1313

14-
# Override extension suffix to be ".so", skipping package info such as
15-
# "cpython-311-x86_64-linux-gnu"
16-
export SETUPTOOLS_EXT_SUFFIX=".so"
17-
1814
# Ensure that CMAKE_ARGS is defined before referencing it. Defaults to empty
1915
# if not defined.
2016
export CMAKE_ARGS="${CMAKE_ARGS:-}"

build/packaging/env_var_script_m1.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
# Enable pybindings so that users can execute ExecuTorch programs from python.
1212
export EXECUTORCH_BUILD_PYBIND=1
1313

14-
# Override extension suffix to be ".so", skipping package info such as
15-
# "cpython-311-darwin"
16-
export SETUPTOOLS_EXT_SUFFIX=".so"
17-
1814
# Ensure that CMAKE_ARGS is defined before referencing it. Defaults to empty
1915
# if not defined.
2016
export CMAKE_ARGS="${CMAKE_ARGS:-}"

setup.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,10 @@ def run(self):
423423
"devtools/bundled_program/schema/scalar_type.fbs",
424424
"devtools/bundled_program/serialize/scalar_type.fbs",
425425
),
426-
# Install executorch-config.cmake to the root of the package.
426+
# Install executorch-wheel-config.cmake to pip package.
427427
(
428-
"build/executorch-config.cmake",
429-
"executorch-config.cmake",
428+
"build/executorch-wheel-config.cmake",
429+
"share/cmake/executorch-config.cmake",
430430
),
431431
]
432432
for src, dst in src_to_dst:
@@ -668,6 +668,10 @@ def get_ext_modules() -> List[Extension]:
668668
return ext_modules
669669

670670

671+
# Override extension suffix to be ".so", skipping package info such as
672+
# "cpython-311-darwin"
673+
os.environ["SETUPTOOLS_EXT_SUFFIX"] = ".so"
674+
671675
setup(
672676
version=Version.string(),
673677
# TODO(dbort): Could use py_modules to restrict the set of modules we

0 commit comments

Comments
 (0)