Skip to content

Commit a0bacb1

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 necessary 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 and installs a wrapper to 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 setup.py bdist_wheel --dist-dir /tmp/wheel-out ``` 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 79a4ba0 commit a0bacb1

File tree

6 files changed

+428
-169
lines changed

6 files changed

+428
-169
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.
28+
_bin_names = _find_executable_files_under(_bin_dir)
29+
30+
# We'll define functions named after each binary. Make them importable.
31+
__all__ = _bin_names
32+
33+
def _run(name):
34+
"""Runs the named binary, which should live under _bin_dir.
35+
36+
Exits the current process with the return code of the subprocess.
37+
"""
38+
raise SystemExit(subprocess.call([os.path.join(_bin_dir, name)] + sys.argv[1:], close_fds=False))
39+
40+
# Define a function named after each of the binaries.
41+
for bin_name in _bin_names:
42+
exec(f"def {bin_name}(): _run('{bin_name}')")

install_requirements.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ for arg in "$@"; do
4141
esac
4242
done
4343

44+
4445
# Install pytorch dependencies
4546
#
4647
# Note:
@@ -69,11 +70,15 @@ pip install --force-reinstall --pre transformers==${TRANSFORMERS_VERSION}
6970
TORCHSR_VERSION=1.0.4
7071
pip install --pre torchsr==${TORCHSR_VERSION}
7172

72-
# Install ExecuTorch after dependencies are installed.
73+
74+
#
75+
# Install executorch pip package. This also makes `flatc` available on the path.
76+
#
77+
78+
# Deps needed by pip install and cmake.
79+
# TODO(dbort): Remove tomli once min python version is 3.11.
80+
pip install zstd tomli setuptools wheel
81+
7382
EXECUTORCH_BUILD_PYBIND="$EXECUTORCH_BUILD_PYBIND" \
7483
CMAKE_ARGS="$CMAKE_ARGS" \
75-
CMAKE_BUILD_PARALLEL_LEVEL=9 \
7684
pip install . --no-build-isolation -v
77-
78-
# Install flatc dependency
79-
bash build/install_flatc.sh

pyproject.toml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,27 @@ dependencies=[
2020
"ruamel.yaml",
2121
"sympy",
2222
"tabulate",
23-
"tomli",
24-
"zstd",
2523
]
2624

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

3041
[tool.setuptools.exclude-package-data]
3142
"*" = ["*.pyc"]
3243

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

sdk/CMakeLists.txt

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

0 commit comments

Comments
 (0)