|
| 1 | +""" Defines a rule and macro for transforming xacro files to a URDF. |
| 2 | +""" |
| 3 | + |
| 4 | +load("@bazel_ros2_rules//lib:ament_index.bzl", "ament_index_share_files") |
| 5 | +load(":common.bzl", "generate_file") |
| 6 | +load(":distro.bzl", "REPOSITORY_ROOT") |
| 7 | +load(":ros_py.bzl", "ros_py_binary") |
| 8 | + |
| 9 | +# Derive the plain workspace name for use in Rlocation paths ("name/target"). |
| 10 | +# Label.workspace_name handles both Bzlmod ("@@name//") and WORKSPACE ("@name//") |
| 11 | +# formats correctly without manual string manipulation. |
| 12 | +_WORKSPACE_NAME = Label(REPOSITORY_ROOT + ":xacro").workspace_name |
| 13 | + |
| 14 | +# Runner script template. The outer dload shim (from ros_py_binary) has |
| 15 | +# already set AMENT_PREFIX_PATH to include both system and user-package |
| 16 | +# prefixes (all absolute via $RUNFILES_DIR) before this script runs. What is |
| 17 | +# needed then, is to locate and exec the inner @ros2//:xacro shim. |
| 18 | +_XACRO_RUNNER_TEMPLATE = """\ |
| 19 | +import os |
| 20 | +import sys |
| 21 | +
|
| 22 | +from python.runfiles import runfiles as runfiles_api |
| 23 | +
|
| 24 | +assert __name__ == "__main__" |
| 25 | +runfiles = runfiles_api.Create() |
| 26 | +xacro_bin = runfiles.Rlocation("{xacro_rlocation}") |
| 27 | +os.execv(xacro_bin, [xacro_bin] + sys.argv[1:]) |
| 28 | +""" |
| 29 | + |
| 30 | +def _ros_xacro_impl(ctx): |
| 31 | + output = ctx.actions.declare_file(ctx.label.name + ".urdf") |
| 32 | + args = ctx.actions.args() |
| 33 | + args.add(ctx.file.src) |
| 34 | + args.add("-o", output) |
| 35 | + args.add_all(ctx.attr.xacro_args) |
| 36 | + ctx.actions.run( |
| 37 | + inputs = [ctx.file.src] + ctx.files.data, |
| 38 | + outputs = [output], |
| 39 | + executable = ctx.executable.xacro_tool, |
| 40 | + arguments = [args], |
| 41 | + ) |
| 42 | + return [DefaultInfo(files = depset([output]))] |
| 43 | + |
| 44 | +_ros_xacro_rule = rule( |
| 45 | + attrs = { |
| 46 | + "src": attr.label( |
| 47 | + allow_single_file = [".xacro"], |
| 48 | + mandatory = True, |
| 49 | + doc = "The main .urdf.xacro file to process.", |
| 50 | + ), |
| 51 | + "data": attr.label_list( |
| 52 | + allow_files = True, |
| 53 | + default = [], |
| 54 | + doc = "Additional files included via relative paths (e.g. .xacro, .yaml).", |
| 55 | + ), |
| 56 | + "xacro_args": attr.string_list( |
| 57 | + default = [], |
| 58 | + doc = "Extra key:=value arguments forwarded to xacro.", |
| 59 | + ), |
| 60 | + "xacro_tool": attr.label( |
| 61 | + executable = True, |
| 62 | + cfg = "exec", |
| 63 | + mandatory = True, |
| 64 | + doc = "The per-invocation xacro runner binary.", |
| 65 | + ), |
| 66 | + }, |
| 67 | + implementation = _ros_xacro_impl, |
| 68 | +) |
| 69 | + |
| 70 | +def ros_xacro(name, src, data = [], ros_packages = {}, xacro_args = [], visibility = None): |
| 71 | + """Transforms a .urdf.xacro file into a .urdf file. |
| 72 | +
|
| 73 | + User-defined packages are declared inline via the ros_packages dict. Each |
| 74 | + entry maps a ROS package name to the list of files to place under |
| 75 | + share/<package_name>/. The strip_prefix is derived automatically from the |
| 76 | + calling BUILD file's package path, so files are placed relative to that |
| 77 | + package directory. |
| 78 | +
|
| 79 | + The dload shim from ros_py_binary extends AMENT_PREFIX_PATH with all |
| 80 | + registered package prefixes (absolute, via $RUNFILES_DIR) before xacro |
| 81 | + runs, making $(find <pkg>) work for both local and system packages. |
| 82 | +
|
| 83 | + Example: |
| 84 | + ros_xacro( |
| 85 | + name = "example", |
| 86 | + src = "robot.urdf.xacro", |
| 87 | + data = ["base.xacro", "arm.xacro"], |
| 88 | + ros_packages = { |
| 89 | + "my_robot": glob(["urdf/**"]), |
| 90 | + }, |
| 91 | + xacro_args = ["sim:=false"], |
| 92 | + ) |
| 93 | +
|
| 94 | + Args: |
| 95 | + name: target name; the output file is named <name>.urdf |
| 96 | + src: the .urdf.xacro source file |
| 97 | + data: additional files included via relative paths |
| 98 | + (e.g. plain <xacro:include filename="other.xacro"/> |
| 99 | + or referenced .yaml configs); must be listed here so |
| 100 | + Bazel sandboxes them and tracks them as dependencies |
| 101 | + for incremental rebuilds |
| 102 | + ros_packages: dict mapping ROS package name to list of share files; |
| 103 | + files are stripped of the calling package's path prefix |
| 104 | + automatically before being placed under share/<pkg>/ |
| 105 | + xacro_args: list of key:=value arguments forwarded to xacro |
| 106 | + visibility: target visibility |
| 107 | + """ |
| 108 | + pkg_targets = [] |
| 109 | + strip_prefix = native.package_name() + "/" if native.package_name() else "" |
| 110 | + for pkg_name, srcs in ros_packages.items(): |
| 111 | + index_name = "_{}_pkg_{}".format(name, pkg_name) |
| 112 | + ament_index_share_files( |
| 113 | + name = index_name, |
| 114 | + package_name = pkg_name, |
| 115 | + srcs = srcs, |
| 116 | + strip_prefix = strip_prefix, |
| 117 | + visibility = ["//visibility:private"], |
| 118 | + ) |
| 119 | + pkg_targets.append(":" + index_name) |
| 120 | + |
| 121 | + runner_data = [REPOSITORY_ROOT + ":xacro"] + pkg_targets |
| 122 | + |
| 123 | + runner_main = "_{}_runner_main.py".format(name) |
| 124 | + generate_file( |
| 125 | + name = runner_main, |
| 126 | + content = _XACRO_RUNNER_TEMPLATE.format( |
| 127 | + xacro_rlocation = _WORKSPACE_NAME + "/xacro", |
| 128 | + ), |
| 129 | + visibility = ["//visibility:private"], |
| 130 | + ) |
| 131 | + |
| 132 | + runner_name = "_{}_runner".format(name) |
| 133 | + ros_py_binary( |
| 134 | + name = runner_name, |
| 135 | + main = runner_main, |
| 136 | + srcs = [runner_main], |
| 137 | + data = runner_data, |
| 138 | + deps = ["@bazel_ros2_rules//deps/python/runfiles"], |
| 139 | + visibility = ["//visibility:private"], |
| 140 | + ) |
| 141 | + |
| 142 | + _ros_xacro_rule( |
| 143 | + name = name, |
| 144 | + src = src, |
| 145 | + data = data, |
| 146 | + xacro_args = xacro_args, |
| 147 | + xacro_tool = ":" + runner_name, |
| 148 | + visibility = visibility, |
| 149 | + ) |
0 commit comments