From b14e4dd653ba5fc12e71b70de3690e803848738f Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sun, 15 Sep 2024 14:49:04 -0400 Subject: [PATCH] [Build] Support windows for pybindings install in setup.py Set the src directory based on build configuration for windows. Rename _portable_lib.* to _portable_lib.cp* to avoid _portable_lib.lib is selected on windows. For #4661 --- setup.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index 5645708a4ca..b92ae8adec5 100644 --- a/setup.py +++ b/setup.py @@ -243,7 +243,7 @@ def src_path(self, installer: "InstallerBuildExt") -> Path: src_path = src_path.replace("%BUILD_TYPE%", cfg) else: # Remove %BUILD_TYPE% from the path. - src_path = src_path.replace("/%BUILD_TYPE%", "") + src_path = src_path.replace("%BUILD_TYPE%/", "") # Construct the full source path, resolving globs. If there are no glob # pattern characters, this will just ensure that the source file exists. @@ -291,7 +291,7 @@ def __init__( output is in a subdirectory named after the build type. For single- config generators (like Makefile Generators or Ninja), this placeholder will be removed. - src_name: The name of the file to install + src_name: The name of the file to install. dst: The path to install to, relative to the root of the pip package. If dst ends in "/", it is treated as a directory. Otherwise it is treated as a filename. @@ -345,13 +345,17 @@ def inplace_dir(self, installer: "InstallerBuildExt") -> Path: class BuiltExtension(_BaseExtension): """An extension that installs a python extension that was built by cmake.""" - def __init__(self, src: str, modpath: str): + def __init__(self, src_dir: str, src_name: str, modpath: str): """Initializes a BuiltExtension. Args: - src: The path to the file to install (typically a shared library), - relative to the cmake-out directory. May be an fnmatch-style - glob that matches exactly one file. If the path ends in `.so`, + src_dir: The directory of the file to install, relative to the cmake-out + directory. A placeholder %BUILD_TYPE% will be replaced with the build + type for multi-config generators (like Visual Studio) where the build + output is in a subdirectory named after the build type. For single- + config generators (like Makefile Generators or Ninja), this placeholder + will be removed. + src_name: The name of the file to install. If the path ends in `.so`, this class will also look for similarly-named `.dylib` files. modpath: The dotted path of the python module that maps to the extension. @@ -359,6 +363,7 @@ def __init__(self, src: str, modpath: str): assert ( "/" not in modpath ), f"modpath must be a dotted python module path: saw '{modpath}'" + src = os.path.join(src_dir, src_name) # This is a real extension, so use the modpath as the name. super().__init__(src=f"%CMAKE_CACHE_DIR%/{src}", dst=modpath, name=modpath) @@ -783,7 +788,9 @@ def get_ext_modules() -> List[Extension]: # portable kernels, and a selection of backends. This lets users # load and execute .pte files from python. BuiltExtension( - "_portable_lib.*", "executorch.extension.pybindings._portable_lib" + src_dir="%BUILD_TYPE%/", + src_name="_portable_lib.cp*", + modpath="executorch.extension.pybindings._portable_lib", ) ) if ShouldBuild.training():