Skip to content

[pybind] Find portable_lib.so in pip package during cmake build #5961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 104 additions & 72 deletions build/executorch-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,117 @@
# is:
#
# find_package(executorch REQUIRED)

# -------
#
# Finds the ExecuTorch library
#
# This will define the following variables:
#
# EXECUTORCH_FOUND -- True if the system has the Torch library
# EXECUTORCH_INCLUDE_DIRS -- The include directories for torch
# EXECUTORCH_LIBRARIES -- Libraries to link against
#
cmake_minimum_required(VERSION 3.19)

set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
set(required_lib_list executorch executorch_core portable_kernels)
foreach(lib ${required_lib_list})
set(lib_var "LIB_${lib}")
add_library(${lib} STATIC IMPORTED)
find_library(
${lib_var} ${lib}
HINTS "${_root}"
CMAKE_FIND_ROOT_PATH_BOTH
# Find prebuilt libportable_lib.so. If found, assuming current file is inside
# a pip package:
# <site-packages>/executorch/executorch-config.cmake.
# If not found, assuming current file is inside cmake-out:
# <cmake-out>/cmake/ExecuTorch/executorch-config.cmake
find_library(_portable_lib_LIBRARY _portable_lib.so PATHS "${CMAKE_CURRENT_LIST_DIR}/extension/pybindings/")
message(WARNING "${CMAKE_CURRENT_LIST_DIR}/extension/pybindings/")
set(EXECUTORCH_LIBRARIES)
if(_portable_lib_LIBRARY)
# Assuming current file is <site-packages>/executorch/executorch-config.cmake
message(WARNING "portable library is found")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of these should use STATUS instead of WARNING since they're just informational

list(APPEND EXECUTORCH_LIBRARIES _portable_lib)
add_library(_portable_lib STATIC IMPORTED)
set(EXECUTORCH_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include)
set_target_properties(_portable_lib PROPERTIES
IMPORTED_LOCATION "${_portable_lib_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${EXECUTORCH_INCLUDE_DIRS}"
CXX_STANDARD 17
)
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
target_include_directories(${lib} INTERFACE ${_root})
endforeach()
else()
# Assuming current file is <cmake-out>/cmake/ExecuTorch/executorch-config.cmake
message(WARNING "portable library is not found")
set(_root "${CMAKE_CURRENT_LIST_DIR}/../..")
set(required_lib_list executorch executorch_core portable_kernels)
foreach(lib ${required_lib_list})
set(lib_var "LIB_${lib}")
add_library(${lib} STATIC IMPORTED)
find_library(
${lib_var} ${lib}
HINTS "${_root}"
CMAKE_FIND_ROOT_PATH_BOTH
)
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
target_include_directories(${lib} INTERFACE ${_root})
endforeach()

target_link_libraries(executorch INTERFACE executorch_core)
target_link_libraries(executorch INTERFACE executorch_core)

if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(FLATCCRT_LIB flatccrt_d)
else()
set(FLATCCRT_LIB flatccrt)
endif()
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(FLATCCRT_LIB flatccrt_d)
else()
set(FLATCCRT_LIB flatccrt)
endif()

set(lib_list
etdump
bundled_program
extension_data_loader
${FLATCCRT_LIB}
coremldelegate
mpsdelegate
qnn_executorch_backend
portable_ops_lib
extension_module
extension_module_static
extension_runner_util
extension_tensor
extension_threadpool
extension_training
xnnpack_backend
# Start XNNPACK Lib Deps
XNNPACK
microkernels-prod
kleidiai
# End XNNPACK Lib Deps
cpuinfo
pthreadpool
vulkan_backend
optimized_kernels
cpublas
eigen_blas
optimized_ops_lib
optimized_native_cpu_ops_lib
quantized_kernels
quantized_ops_lib
quantized_ops_aot_lib
)
foreach(lib ${lib_list})
# Name of the variable which stores result of the find_library search
set(lib_var "LIB_${lib}")
find_library(
${lib_var} ${lib}
HINTS "${_root}"
CMAKE_FIND_ROOT_PATH_BOTH
set(lib_list
etdump
bundled_program
extension_data_loader
${FLATCCRT_LIB}
coremldelegate
mpsdelegate
qnn_executorch_backend
portable_ops_lib
extension_module
extension_module_static
extension_runner_util
extension_tensor
extension_threadpool
extension_training
xnnpack_backend
# Start XNNPACK Lib Deps
XNNPACK
microkernels-prod
kleidiai
# End XNNPACK Lib Deps
cpuinfo
pthreadpool
vulkan_backend
optimized_kernels
cpublas
eigen_blas
optimized_ops_lib
optimized_native_cpu_ops_lib
quantized_kernels
quantized_ops_lib
quantized_ops_aot_lib
)
if(NOT ${lib_var})
message("${lib} library is not found.
If needed rebuild with the proper options in CMakeLists.txt"
foreach(lib ${lib_list})
# Name of the variable which stores result of the find_library search
set(lib_var "LIB_${lib}")
find_library(
${lib_var} ${lib}
HINTS "${_root}"
CMAKE_FIND_ROOT_PATH_BOTH
)
else()
if("${lib}" STREQUAL "extension_module" AND (NOT CMAKE_TOOLCHAIN_IOS))
add_library(${lib} SHARED IMPORTED)
if(NOT ${lib_var})
message("${lib} library is not found.
If needed rebuild with the proper options in CMakeLists.txt"
)
else()
# Building a share library on iOS requires code signing, so it's easier to
# keep all libs as static when CMAKE_TOOLCHAIN_IOS is used
add_library(${lib} STATIC IMPORTED)
if("${lib}" STREQUAL "extension_module" AND (NOT CMAKE_TOOLCHAIN_IOS))
add_library(${lib} SHARED IMPORTED)
else()
# Building a share library on iOS requires code signing, so it's easier to
# keep all libs as static when CMAKE_TOOLCHAIN_IOS is used
add_library(${lib} STATIC IMPORTED)
endif()
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
target_include_directories(${lib} INTERFACE ${_root})
endif()
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
target_include_directories(${lib} INTERFACE ${_root})
endif()
endforeach()
endforeach()
endif()
4 changes: 4 additions & 0 deletions build/packaging/env_var_script_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# Enable pybindings so that users can execute ExecuTorch programs from python.
export EXECUTORCH_BUILD_PYBIND=1

# Override extension suffix to be ".so", skipping package info such as
# "cpython-311-x86_64-linux-gnu"
export SETUPTOOLS_EXT_SUFFIX=".so"

# Ensure that CMAKE_ARGS is defined before referencing it. Defaults to empty
# if not defined.
export CMAKE_ARGS="${CMAKE_ARGS:-}"
Expand Down
4 changes: 4 additions & 0 deletions build/packaging/env_var_script_m1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# Enable pybindings so that users can execute ExecuTorch programs from python.
export EXECUTORCH_BUILD_PYBIND=1

# Override extension suffix to be ".so", skipping package info such as
# "cpython-311-darwin"
export SETUPTOOLS_EXT_SUFFIX=".so"

# Ensure that CMAKE_ARGS is defined before referencing it. Defaults to empty
# if not defined.
export CMAKE_ARGS="${CMAKE_ARGS:-}"
Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ def run(self):
"devtools/bundled_program/schema/scalar_type.fbs",
"devtools/bundled_program/serialize/scalar_type.fbs",
),
# Install executorch-config.cmake to the root of the package.
(
"build/executorch-config.cmake",
"executorch-config.cmake",
),
]
# Copy all the necessary headers into include/executorch/ so that they can
# be found in the pip package. This is the subset of headers that are
Expand Down
Loading