Skip to content

Commit 12c2dd5

Browse files
committed
New pip build system
Summary: Compatible with `pythom -m build` instead of using the deprecated `python setup.py bdist_wheel`. A new custom build command builds all CMake targets, and a new custom build_ext command installs built files into the wheel. A new custom build_py command copies platform-independent files into the wheel instead of copying them into the source tree. Puts all generated files under pip-out instead of mixing with files in the tree. Uses Command methods like `mkpath` and `copy_file` to benefit from setuptools concepts like dry_run, and to get logging for free. Adds `flatc` to the wheel, but doesn't yet make it available on the path. Test Plan: Iteration command to test wheel building without completely rebuilding the system: ``` rm -rf \ /tmp/wheel-out \ pip-out/lib* \ pip-out/bdist.* \ pip-out/temp.*/cmake-out/CMake* \ executorch.egg-info \ third-party/flatcc/{bin,lib} \ ; \ python -m build \ --wheel --outdir /tmp/wheel-out --no-isolation ``` To fully rebuild the system, also `rm -rf pip-out`. The wheel file is written to (e.g.) ``` /tmp/wheel-out/executorch-0.1.0-cp310-cp310-linux_x86_64.whl ``` Examine it with `unzip -l`, or install it with `pip install /tmp/wheel-out/executorch*.whl`.
1 parent 9dfe0dd commit 12c2dd5

File tree

6 files changed

+421
-166
lines changed

6 files changed

+421
-166
lines changed

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ cmake-android-out/
55
cmake-ios-out/
66
ethos-u-scratch/
77
executorch.egg-info
8+
pip-out/
89
__pycache__/
9-
build/lib/
10-
exir/_serialize/scalar_type.fbs
11-
exir/_serialize/program.fbs
12-
sdk/bundled_program/serialize/bundled_program_schema.fbs
13-
sdk/bundled_program/serialize/scalar_type.fbs
1410

1511
# Any exported models and profiling outputs
1612
*.pte

build/pip_data_bin_init.py.in

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This file should be written to the wheel package as
2+
# `executorch/data/bin/__init__.py`.
3+
#
4+
# Setuptools will expect to be able to say something like `from
5+
# executorch.data.bin import mybin; mybin()` for each entry listed in the
6+
# [project.scripts] section of pyproject.toml. This file makes the `mybin()`
7+
# function execute the binary at `executorch/data/bin/mybin` and exit with that
8+
# binary's exit status.
9+
10+
import subprocess
11+
import os
12+
import sys
13+
import types
14+
15+
# This file should live in the target `bin` directory.
16+
_bin_dir = os.path.join(os.path.dirname(__file__))
17+
18+
def _find_executable_files_under(dir):
19+
"""Lists all executable files in the given directory."""
20+
bin_names = []
21+
for filename in os.listdir(dir):
22+
filepath = os.path.join(dir, filename)
23+
if os.path.isfile(filepath) and os.access(filepath, os.X_OK):
24+
bin_names.append(filename)
25+
return bin_names
26+
27+
# The list of binaries to create wrapper functions for. An executable should
28+
# exist for each name under `data/bin/<bin-name>`.
29+
# @nocommit: see if we can just scrape this from the filesystem to avoid the template
30+
_bin_names = _find_executable_files_under(_bin_dir)
31+
32+
# We'll define functions named after each binary. Make them importable.
33+
__all__ = _bin_names
34+
35+
def _run(name):
36+
"""Runs the named binary, which should live under _bin_dir.
37+
38+
Exits the current process with the return code of the subprocess.
39+
"""
40+
raise SystemExit(subprocess.call([os.path.join(_bin_dir, name)] + sys.argv[1:], close_fds=False))
41+
42+
# Define a function named after each of the binaries.
43+
for bin_name in _bin_names:
44+
exec(f"def {bin_name}(): _run('{bin_name}')")

extension/aot_util/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ find_library(TORCH_PYTHON_LIBRARY torch_python
6363
unset(CMAKE_CXX_FLAGS_RELEASE)
6464

6565
list(TRANSFORM _extension_aot_util__srcs PREPEND "${EXECUTORCH_ROOT}/")
66-
add_library(aot_util ${_extension_aot_util__srcs})
66+
add_library(aot_util SHARED ${_extension_aot_util__srcs})
6767
target_include_directories(aot_util PUBLIC ${TORCH_INCLUDE_DIRS})
6868
target_link_libraries(aot_util torch)

pyproject.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,25 @@ dependencies=[
2424
"zstd",
2525
]
2626

27+
# Tell setuptools to generate commandline wrappers for tools that we install
28+
# under data/bin in the pip package. This will put these commands on the user's
29+
# path.
30+
[project.scripts]
31+
flatc = "executorch.data.bin:flatc"
32+
2733
[tool.setuptools.package-data]
28-
"*" = ["*.fbs", "*.yaml"]
34+
# TODO(dbort): Prune /test[s]/ dirs, /third-party/ dirs, yaml files that we
35+
# don't need.
36+
"*" = [
37+
# Some backends like XNNPACK need their .fbs files.
38+
"*.fbs",
39+
# Some kernel libraries need their .yaml files.
40+
"*.yaml",
41+
]
2942

3043
[tool.setuptools.exclude-package-data]
3144
"*" = ["*.pyc"]
3245

3346
[tool.usort]
47+
# Do not try to put "first-party" imports in their own section.
3448
first_party_detection = false

sdk/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ ExternalProject_Add(
7575
SOURCE_DIR ${CMAKE_SOURCE_DIR}/third-party/flatcc
7676
BINARY_DIR ${CMAKE_BINARY_DIR}/_host_build
7777
CMAKE_CACHE_ARGS -DFLATCC_TEST:BOOL=OFF -DFLATCC_REFLECTION:BOOL=OFF
78+
# See above comment about POSITION_INDEPENDENT_CODE.
79+
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
7880
INSTALL_COMMAND "" # Prevent the install step, modify as needed
7981
)
8082

0 commit comments

Comments
 (0)