149 lines
3.7 KiB
Python
149 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Resolve ROS 2 package include directories through the ament index."""
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
import xml.etree.ElementTree as ET
|
|
from pathlib import Path
|
|
|
|
|
|
def get_prefixes(explicit_prefix: str) -> list[Path]:
|
|
"""Build an ordered list of ROS installation prefixes."""
|
|
raw = [explicit_prefix]
|
|
raw.extend(os.environ.get("AMENT_PREFIX_PATH", "").split(os.pathsep))
|
|
|
|
prefixes = []
|
|
seen = set()
|
|
|
|
for item in raw:
|
|
if not item:
|
|
continue
|
|
|
|
path = Path(item).resolve()
|
|
if path.is_dir() and path not in seen:
|
|
prefixes.append(path)
|
|
seen.add(path)
|
|
|
|
return prefixes
|
|
|
|
|
|
def find_package(name: str, prefixes: list[Path]):
|
|
"""Find a package through the ament resource index."""
|
|
for prefix in prefixes:
|
|
marker = (
|
|
prefix
|
|
/ "share/ament_index/resource_index/packages"
|
|
/ name
|
|
)
|
|
manifest = prefix / "share" / name / "package.xml"
|
|
|
|
if marker.is_file():
|
|
if not manifest.is_file():
|
|
raise RuntimeError(
|
|
f"Package '{name}' is indexed but package.xml "
|
|
f"is missing: {manifest}"
|
|
)
|
|
return prefix, manifest
|
|
|
|
return None
|
|
|
|
|
|
def get_dependencies(manifest: Path) -> list[str]:
|
|
"""Read compile-relevant dependencies from package.xml."""
|
|
root = ET.parse(manifest).getroot()
|
|
|
|
dependency_tags = {
|
|
"depend",
|
|
"build_depend",
|
|
"build_export_depend",
|
|
"buildtool_export_depend",
|
|
}
|
|
|
|
dependencies = set()
|
|
|
|
for element in root:
|
|
tag = element.tag.split("}")[-1]
|
|
|
|
if tag not in dependency_tags:
|
|
continue
|
|
|
|
name = (element.text or "").strip()
|
|
if name:
|
|
dependencies.add(name)
|
|
|
|
return sorted(dependencies)
|
|
|
|
|
|
def resolve(roots: list[str], prefixes: list[Path]) -> list[Path]:
|
|
"""Recursively resolve packages and collect include directories."""
|
|
visited = set()
|
|
include_dirs = []
|
|
include_seen = set()
|
|
|
|
def add_include(path: Path):
|
|
path = path.resolve()
|
|
if path.is_dir() and path not in include_seen:
|
|
include_dirs.append(path)
|
|
include_seen.add(path)
|
|
|
|
def visit(name: str, is_root: bool = False):
|
|
if name in visited:
|
|
return
|
|
|
|
visited.add(name)
|
|
result = find_package(name, prefixes)
|
|
|
|
if result is None:
|
|
message = f"Package '{name}' not found in ament index"
|
|
|
|
if is_root:
|
|
raise RuntimeError(message)
|
|
|
|
print(f"[WARN] {message}", file=sys.stderr)
|
|
return
|
|
|
|
prefix, manifest = result
|
|
|
|
# Common ROS installation include directory.
|
|
add_include(prefix / "include")
|
|
|
|
# ROS packages commonly install headers under include/<package>.
|
|
add_include(prefix / "include" / name)
|
|
|
|
print(f"[ROS DEP] {name}", file=sys.stderr)
|
|
|
|
for dependency in get_dependencies(manifest):
|
|
visit(dependency)
|
|
|
|
for root in roots:
|
|
visit(root, is_root=True)
|
|
|
|
return include_dirs
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--prefix", required=True)
|
|
parser.add_argument("--roots", required=True)
|
|
args = parser.parse_args()
|
|
|
|
roots = [item for item in args.roots.split(",") if item]
|
|
prefixes = get_prefixes(args.prefix)
|
|
|
|
if not prefixes:
|
|
raise RuntimeError("No valid ROS installation prefixes found")
|
|
|
|
include_dirs = resolve(roots, prefixes)
|
|
|
|
for directory in include_dirs:
|
|
print(directory)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"[ERROR] {error}", file=sys.stderr)
|
|
sys.exit(1)
|