|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +"""Checks that the installed wheel is usable as a C++ SDK. |
| 8 | +
|
| 9 | +The wheel ships a prebuilt runtime library plus a CMake package config, so a |
| 10 | +standalone application can find_package(executorch) and link |
| 11 | +executorch::runtime without building ExecuTorch from source. These checks run |
| 12 | +against the installed wheel only; they never look at the source tree's build |
| 13 | +directory. |
| 14 | +
|
| 15 | +Two properties are verified: |
| 16 | +
|
| 17 | +1. Exactly one shipped library defines the backend registry. Backends register |
| 18 | + into a process-wide table owned by the runtime, so a second definition would |
| 19 | + silently give the process two tables and let a backend register into the one |
| 20 | + nobody reads. |
| 21 | +2. A C++ consumer builds and runs against the wheel, and records a dependency |
| 22 | + on the shipped runtime with a relocatable RUNPATH. |
| 23 | +""" |
| 24 | + |
| 25 | +import os |
| 26 | +import re |
| 27 | +import shutil |
| 28 | +import subprocess |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | +# Registry entry points. A second definer of any of these means a second |
| 32 | +# process-wide registry. |
| 33 | +_REGISTRY_SYMBOLS = ( |
| 34 | + "executorch::runtime::register_backend", |
| 35 | + "executorch::runtime::get_num_registered_backends", |
| 36 | + "executorch::runtime::get_backend_class", |
| 37 | +) |
| 38 | + |
| 39 | +# `nm -DC` prints "<hexaddr> <kind> <name>" for a definition and |
| 40 | +# " U <name>" for an undefined reference. |
| 41 | +_DEFINED = re.compile(r"^[0-9a-fA-F]+\s+(?P<kind>[A-Za-z])\s+(?P<name>.+)$") |
| 42 | + |
| 43 | +# Symbol kinds that mean the object owns the code or storage. |
| 44 | +_OWNING_KINDS = frozenset("TtBbDdGgSsRrWV") |
| 45 | + |
| 46 | +_CONSUMER_SOURCE = """\ |
| 47 | +#include <executorch/runtime/backend/interface.h> |
| 48 | +#include <executorch/runtime/platform/runtime.h> |
| 49 | +
|
| 50 | +#include <cstdio> |
| 51 | +
|
| 52 | +int main() { |
| 53 | + executorch::runtime::runtime_init(); |
| 54 | + std::printf( |
| 55 | + "registered backends: %zu\\n", |
| 56 | + (size_t)executorch::runtime::get_num_registered_backends()); |
| 57 | + return 0; |
| 58 | +} |
| 59 | +""" |
| 60 | + |
| 61 | +_CONSUMER_CMAKE = """\ |
| 62 | +cmake_minimum_required(VERSION 3.24) |
| 63 | +project(executorch_wheel_consumer CXX) |
| 64 | +find_package(executorch REQUIRED) |
| 65 | +add_executable(consumer consumer.cpp) |
| 66 | +target_link_libraries(consumer PRIVATE executorch::runtime) |
| 67 | +""" |
| 68 | + |
| 69 | + |
| 70 | +def _installed_package_dir() -> Path: |
| 71 | + """The installed executorch package, never the source checkout.""" |
| 72 | + import executorch |
| 73 | + |
| 74 | + return Path(list(executorch.__path__)[0]).resolve() |
| 75 | + |
| 76 | + |
| 77 | +def _shipped_shared_objects(package_dir: Path): |
| 78 | + return [ |
| 79 | + path |
| 80 | + for path in sorted(package_dir.rglob("*.so*")) |
| 81 | + if path.is_file() and not path.is_symlink() |
| 82 | + ] |
| 83 | + |
| 84 | + |
| 85 | +def _defines_symbol(library: Path, symbol: str) -> bool: |
| 86 | + result = subprocess.run( |
| 87 | + ["nm", "-DC", str(library)], capture_output=True, text=True, check=False |
| 88 | + ) |
| 89 | + for line in result.stdout.splitlines(): |
| 90 | + if symbol not in line: |
| 91 | + continue |
| 92 | + match = _DEFINED.match(line) |
| 93 | + if ( |
| 94 | + match |
| 95 | + and match.group("name").startswith(symbol) |
| 96 | + and match.group("kind") in _OWNING_KINDS |
| 97 | + ): |
| 98 | + return True |
| 99 | + return False |
| 100 | + |
| 101 | + |
| 102 | +def test_single_backend_registry() -> None: |
| 103 | + """Exactly one shipped library may define the backend registry.""" |
| 104 | + assert shutil.which("nm") is not None, "nm is required to inspect the wheel" |
| 105 | + |
| 106 | + package_dir = _installed_package_dir() |
| 107 | + libraries = _shipped_shared_objects(package_dir) |
| 108 | + assert libraries, f"no shared libraries found under {package_dir}" |
| 109 | + |
| 110 | + for symbol in _REGISTRY_SYMBOLS: |
| 111 | + definers = [lib for lib in libraries if _defines_symbol(lib, symbol)] |
| 112 | + pretty = [str(lib.relative_to(package_dir)) for lib in definers] |
| 113 | + assert len(definers) == 1, ( |
| 114 | + f"expected exactly one library to define {symbol}, found " |
| 115 | + f"{len(definers)}: {pretty}. More than one definition means the " |
| 116 | + f"process has more than one backend registry." |
| 117 | + ) |
| 118 | + print(f"✓ single backend registry across {len(libraries)} shipped libraries") |
| 119 | + |
| 120 | + |
| 121 | +def test_cpp_consumer(work_dir: Path) -> None: |
| 122 | + """A standalone C++ app builds and runs against the installed wheel.""" |
| 123 | + assert shutil.which("cmake") is not None, "cmake is required to build a consumer" |
| 124 | + |
| 125 | + package_dir = _installed_package_dir() |
| 126 | + config = package_dir / "share" / "cmake" / "executorch-config.cmake" |
| 127 | + assert config.is_file(), f"wheel is missing its CMake package config: {config}" |
| 128 | + |
| 129 | + source_dir = work_dir / "consumer" |
| 130 | + build_dir = work_dir / "consumer-build" |
| 131 | + source_dir.mkdir(parents=True, exist_ok=True) |
| 132 | + (source_dir / "consumer.cpp").write_text(_CONSUMER_SOURCE) |
| 133 | + (source_dir / "CMakeLists.txt").write_text(_CONSUMER_CMAKE) |
| 134 | + |
| 135 | + subprocess.run( |
| 136 | + [ |
| 137 | + "cmake", |
| 138 | + "-S", |
| 139 | + str(source_dir), |
| 140 | + "-B", |
| 141 | + str(build_dir), |
| 142 | + f"-DCMAKE_PREFIX_PATH={config.parent}", |
| 143 | + ], |
| 144 | + check=True, |
| 145 | + ) |
| 146 | + subprocess.run(["cmake", "--build", str(build_dir)], check=True) |
| 147 | + |
| 148 | + consumer = build_dir / "consumer" |
| 149 | + # No LD_LIBRARY_PATH: the imported target is responsible for making the |
| 150 | + # shipped runtime findable. |
| 151 | + environment = { |
| 152 | + key: value for key, value in os.environ.items() if key != "LD_LIBRARY_PATH" |
| 153 | + } |
| 154 | + subprocess.run([str(consumer)], check=True, env=environment) |
| 155 | + print("✓ C++ consumer builds and runs against the installed wheel") |
| 156 | + |
| 157 | + assert shutil.which("readelf") is not None, "readelf is required to check the ELF" |
| 158 | + |
| 159 | + dynamic = subprocess.run( |
| 160 | + ["readelf", "-d", str(consumer)], capture_output=True, text=True, check=True |
| 161 | + ).stdout |
| 162 | + assert "libexecutorch.so" in dynamic, ( |
| 163 | + "the consumer does not depend on the shipped runtime; " |
| 164 | + f"dynamic section was:\n{dynamic}" |
| 165 | + ) |
| 166 | + assert "$ORIGIN" in dynamic, ( |
| 167 | + "the consumer has no $ORIGIN-relative RUNPATH, so it is not " |
| 168 | + f"relocatable; dynamic section was:\n{dynamic}" |
| 169 | + ) |
| 170 | + print("✓ consumer depends on the shipped runtime with a relocatable RUNPATH") |
| 171 | + |
| 172 | + |
| 173 | +def run_tests(work_dir: Path) -> None: |
| 174 | + test_single_backend_registry() |
| 175 | + test_cpp_consumer(work_dir) |
0 commit comments