Skip to content

Commit e4a18ec

Browse files
committed
WIP: Put flatc on the path
1 parent 595c610 commit e4a18ec

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

build/pip_data_bin_init.py.in

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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}')")
45+
### globals()[bin_name] = types.FunctionType(
46+
### code=f"def {bin_name}(): _run('{bin_name}')",
47+
### globals=globals(),
48+
### )

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ dependencies=[
3333
"zstd",
3434
]
3535

36+
# Tell setuptools to generate commandline wrappers for tools that we install
37+
# under data/bin in the pip package. This will put these commands on the user's
38+
# path.
39+
[project.scripts]
40+
flatc = "executorch.data.bin:flatc"
41+
3642
[tool.setuptools.package-data]
3743
# @nocommit: prune /test[s]/ dirs, /third-party/ dirs
3844
# @nocommit: yaml only for kernels?

setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ def run(self):
359359
data_root = os.path.join(self.build_lib, "executorch", "data")
360360
self.copy_tree(cmake_install_dir, data_root)
361361

362+
# Copy the bin wrapper so that users can run any executables under
363+
# data/bin, as long as they are listed in the [project.scripts] section
364+
# of pyproject.toml.
365+
self.copy_file(
366+
"build/pip_data_bin_init.py.in",
367+
os.path.join(data_root, "bin", "__init__.py"),
368+
)
369+
362370
# Finally, run the underlying subcommands like build_py, build_ext.
363371
build.run(self)
364372

0 commit comments

Comments
 (0)