Skip to content

[pybind] Improve pip package build #5965

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 2 commits 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
177 changes: 73 additions & 104 deletions build/executorch-config.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
Expand All @@ -9,117 +10,85 @@
# 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)

# 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")
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(_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
)
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()
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
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
)
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
if(NOT ${lib_var})
message("${lib} library is not found.
If needed rebuild with the proper options in CMakeLists.txt"
)
if(NOT ${lib_var})
message("${lib} library is not found.
If needed rebuild with the proper options in CMakeLists.txt"
)
else()
if("${lib}" STREQUAL "extension_module" AND (NOT CMAKE_TOOLCHAIN_IOS))
add_library(${lib} SHARED IMPORTED)
else()
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})
# 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()
endforeach()
endif()
set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}")
target_include_directories(${lib} INTERFACE ${_root})
endif()
endforeach()
40 changes: 40 additions & 0 deletions build/executorch-wheel-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# Config defining how CMake should find ExecuTorch package. CMake will search
# for this file and find ExecuTorch package if it is installed. Typical usage
# is:
#
# find_package(executorch REQUIRED)
# -------
#
# Finds the ExecuTorch library
#
# This will define the following variables:
#
# EXECUTORCH_FOUND -- True if the system has the ExecuTorch library
# EXECUTORCH_INCLUDE_DIRS -- The include directories for ExecuTorch
# EXECUTORCH_LIBRARIES -- Libraries to link against
#
cmake_minimum_required(VERSION 3.19)

# Find prebuilt _portable_lib.so. This file should be installed under
# <site-packages>/executorch/share/cmake
find_library(_portable_lib_LIBRARY _portable_lib.so PATHS "${CMAKE_CURRENT_LIST_DIR}/../../extension/pybindings/")
set(EXECUTORCH_LIBRARIES)
set(EXECUTORCH_FOUND OFF)
if(_portable_lib_LIBRARY)
set(EXECUTORCH_FOUND ON)
message(STATUS "ExecuTorch portable library is found at ${_portable_lib_LIBRARY}")
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
)
endif()
4 changes: 0 additions & 4 deletions build/packaging/env_var_script_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
# 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: 0 additions & 4 deletions build/packaging/env_var_script_m1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
# 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
10 changes: 7 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ 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.
# Install executorch-wheel-config.cmake to pip package.
(
"build/executorch-config.cmake",
"executorch-config.cmake",
"build/executorch-wheel-config.cmake",
"share/cmake/executorch-config.cmake",
),
]
# Copy all the necessary headers into include/executorch/ so that they can
Expand Down Expand Up @@ -687,6 +687,10 @@ def get_ext_modules() -> List[Extension]:
return ext_modules


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

setup(
version=Version.string(),
# TODO(dbort): Could use py_modules to restrict the set of modules we
Expand Down
Loading