Skip to content

Commit 1327090

Browse files
larryliu0820facebook-github-bot
authored andcommitted
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 f663ba6 commit 1327090

File tree

5 files changed

+120
-115
lines changed

5 files changed

+120
-115
lines changed

build/executorch-config.cmake

Lines changed: 73 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# Copyright (c) Meta Platforms, Inc. and affiliates.
23
# All rights reserved.
34
#
@@ -9,117 +10,85 @@
910
# is:
1011
#
1112
# 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-
#
13+
2214
cmake_minimum_required(VERSION 3.19)
2315

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
16+
set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
17+
set(required_lib_list executorch executorch_core portable_kernels)
18+
foreach(lib ${required_lib_list})
19+
set(lib_var "LIB_${lib}")
20+
add_library(${lib} STATIC IMPORTED)
21+
find_library(
22+
${lib_var} ${lib}
23+
HINTS "${_root}"
24+
CMAKE_FIND_ROOT_PATH_BOTH
4225
)
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_core 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()
26+
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
27+
target_include_directories(${lib} INTERFACE ${_root})
28+
endforeach()
5929

60-
target_link_libraries(executorch INTERFACE executorch_core)
30+
target_link_libraries(executorch INTERFACE executorch_core)
6131

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

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-
extension_training
83-
xnnpack_backend
84-
# Start XNNPACK Lib Deps
85-
XNNPACK
86-
microkernels-prod
87-
kleidiai
88-
# End XNNPACK Lib Deps
89-
cpuinfo
90-
pthreadpool
91-
vulkan_backend
92-
optimized_kernels
93-
cpublas
94-
eigen_blas
95-
optimized_ops_lib
96-
optimized_native_cpu_ops_lib
97-
quantized_kernels
98-
quantized_ops_lib
99-
quantized_ops_aot_lib
38+
set(lib_list
39+
etdump
40+
bundled_program
41+
extension_data_loader
42+
${FLATCCRT_LIB}
43+
coremldelegate
44+
mpsdelegate
45+
qnn_executorch_backend
46+
portable_ops_lib
47+
extension_module
48+
extension_module_static
49+
extension_runner_util
50+
extension_tensor
51+
extension_threadpool
52+
extension_training
53+
xnnpack_backend
54+
# Start XNNPACK Lib Deps
55+
XNNPACK
56+
microkernels-prod
57+
kleidiai
58+
# End XNNPACK Lib Deps
59+
cpuinfo
60+
pthreadpool
61+
vulkan_backend
62+
optimized_kernels
63+
cpublas
64+
eigen_blas
65+
optimized_ops_lib
66+
optimized_native_cpu_ops_lib
67+
quantized_kernels
68+
quantized_ops_lib
69+
quantized_ops_aot_lib
70+
)
71+
foreach(lib ${lib_list})
72+
# Name of the variable which stores result of the find_library search
73+
set(lib_var "LIB_${lib}")
74+
find_library(
75+
${lib_var} ${lib}
76+
HINTS "${_root}"
77+
CMAKE_FIND_ROOT_PATH_BOTH
10078
)
101-
foreach(lib ${lib_list})
102-
# Name of the variable which stores result of the find_library search
103-
set(lib_var "LIB_${lib}")
104-
find_library(
105-
${lib_var} ${lib}
106-
HINTS "${_root}"
107-
CMAKE_FIND_ROOT_PATH_BOTH
79+
if(NOT ${lib_var})
80+
message("${lib} library is not found.
81+
If needed rebuild with the proper options in CMakeLists.txt"
10882
)
109-
if(NOT ${lib_var})
110-
message("${lib} library is not found.
111-
If needed rebuild with the proper options in CMakeLists.txt"
112-
)
83+
else()
84+
if("${lib}" STREQUAL "extension_module" AND (NOT CMAKE_TOOLCHAIN_IOS))
85+
add_library(${lib} SHARED IMPORTED)
11386
else()
114-
if("${lib}" STREQUAL "extension_module" AND (NOT CMAKE_TOOLCHAIN_IOS))
115-
add_library(${lib} SHARED IMPORTED)
116-
else()
117-
# Building a share library on iOS requires code signing, so it's easier to
118-
# keep all libs as static when CMAKE_TOOLCHAIN_IOS is used
119-
add_library(${lib} STATIC IMPORTED)
120-
endif()
121-
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
122-
target_include_directories(${lib} INTERFACE ${_root})
87+
# Building a share library on iOS requires code signing, so it's easier to
88+
# keep all libs as static when CMAKE_TOOLCHAIN_IOS is used
89+
add_library(${lib} STATIC IMPORTED)
12390
endif()
124-
endforeach()
125-
endif()
91+
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
92+
target_include_directories(${lib} INTERFACE ${_root})
93+
endif()
94+
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
# Copy all the necessary headers into include/executorch/ so that they can
@@ -687,6 +687,10 @@ def get_ext_modules() -> List[Extension]:
687687
return ext_modules
688688

689689

690+
# Override extension suffix to be ".so", skipping package info such as
691+
# "cpython-311-darwin"
692+
os.environ["SETUPTOOLS_EXT_SUFFIX"] = ".so"
693+
690694
setup(
691695
version=Version.string(),
692696
# TODO(dbort): Could use py_modules to restrict the set of modules we

0 commit comments

Comments
 (0)