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