Skip to content

Commit

Permalink
Remove bazel-specific semantics
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Jan 14, 2025
1 parent 747b833 commit 5ae92bb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 31 deletions.
3 changes: 1 addition & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ py_library(
name = "xacro_lib",
srcs = [
"xacro/__init__.py",
"xacro/bazel_support.py",
"xacro/cli.py",
"xacro/color.py",
"xacro/substitution_args.py",
Expand All @@ -38,8 +37,8 @@ py_library(
imports = ["."],
visibility = ["//visibility:public"],
deps = [
"@xacro_python_dependencies//pyyaml:pkg",
"@rules_python//python/runfiles",
"@xacro_python_dependencies//pyyaml:pkg",
],
)

Expand Down
59 changes: 34 additions & 25 deletions bazel/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,52 @@

XacroInfo = provider(
"Provider holding the result of a xacro generation step.",
fields = ["result", "data"],
fields = ["result"],
)

XACRO_EXTENSION = ".xacro"

def _xacro_impl(ctx):
if ctx.outputs.out:
out = ctx.outputs.out
else:
src = ctx.file.src.basename
if not src.endswith(XACRO_EXTENSION):
fail("xacro_file src should be named *.xacro not {}".format(src))
out = ctx.actions.declare_file(src[:-len(XACRO_EXTENSION)])
# Use declared output or derive from source name
out = ctx.outputs.out or ctx.actions.declare_file(ctx.file.src.basename[:-len(XACRO_EXTENSION)])

# Gather inputs for the xacro command
direct_inputs = [ctx.file.src] + ctx.files.data
dep_inputs = [dep[XacroInfo].result for dep in ctx.attr.deps]
all_inputs = direct_inputs + dep_inputs

# Create a temporary directory
temp_dir = "TMP_XACRO/" + ctx.label.name

# The list of arguments we pass to the script.
args = [ctx.file.src.path, "-o", out.path] + ctx.attr.extra_args
symlink_paths = []
for input in all_inputs:
symlink_path = ctx.actions.declare_file(temp_dir + "/" + input.basename)
ctx.actions.symlink(
output = symlink_path,
target_file = input,
)
symlink_paths.append(symlink_path)

arguments = [
"-o",
out.path,
"--root-dir",
ctx.bin_dir.path + "/" + temp_dir,
ctx.file.src.basename,
]
arguments += ["{}:={}".format(arg, val) for arg, val in ctx.attr.arguments.items()]

# Action to call the script.
all_inputs = [ctx.file.src] + ctx.files.data + [dep[XacroInfo].result for dep in ctx.attr.deps]
ctx.actions.run(
inputs = all_inputs,
inputs = symlink_paths,
outputs = [out],
arguments = args,
env = {"XACRO_INPUTS": "\n".join([file.path for file in all_inputs])},
progress_message = "Running xacro: %s -> %s" % (ctx.file.src.short_path, out.short_path),
arguments = arguments,
executable = ctx.executable._xacro,
progress_message = "Running xacro: %s -> %s" % (ctx.file.src.short_path, out.short_path),
mnemonic = "Xacro",
)

xacro_data = depset(
direct = [out] + ctx.files.data + [d[XacroInfo].result for d in ctx.attr.deps],
transitive = [d[XacroInfo].data for d in ctx.attr.deps],
)

runfiles = ctx.runfiles(files = xacro_data.to_list())

return [
XacroInfo(result = out, data = xacro_data),
XacroInfo(result = out),
DefaultInfo(
files = depset([out]),
data_runfiles = ctx.runfiles(files = [out]),
Expand All @@ -55,7 +64,7 @@ xacro_file = rule(
"data": attr.label_list(
allow_files = True,
),
"extra_args": attr.string_list(),
"arguments": attr.string_dict(),
"deps": attr.label_list(providers = [XacroInfo]),
"_xacro": attr.label(
default = "@xacro//:xacro",
Expand Down
4 changes: 2 additions & 2 deletions bazel/integration_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ xacro_file(
name = "complex_example",
src = "inputs/complex.xml.xacro",
out = "my_complex_model.xml",
extra_args = ["special_argument:=foo"],
arguments = {"special_argument": "foo"},
deps = [":sample1"],
)

Expand All @@ -30,7 +30,7 @@ xacro_file(
name = "conditional_false",
src = "inputs/conditional.xml.xacro",
out = "conditional_false.xml",
extra_args = ["myarg:=false"],
arguments = {"myarg": "false"},
)

diff_test(
Expand Down
2 changes: 1 addition & 1 deletion bazel/integration_test/inputs/complex.xml.xacro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<root xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- include a file from a bazel path -->
<xacro:include filename="//sample1.xml"/>
<xacro:include filename="sample1.xml"/>

<!-- populate an argument from command line -->
<sphere name="$(arg special_argument)" radius="1"/>
Expand Down
10 changes: 9 additions & 1 deletion xacro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
filestack = None
macrostack = None

root_dir = os.curdir

def init_stacks(file):
global filestack
Expand Down Expand Up @@ -523,6 +524,7 @@ def process_include(elt, macros, symbols, func):
try:
# extend filestack
filestack.append(filename)
print(filename)
include = parse(None, filename).documentElement

# recursive call to func
Expand Down Expand Up @@ -1013,10 +1015,11 @@ def parse(inp, filename=None):
:return:xml.dom.minidom.Document
:raise: xml.parsers.expat.ExpatError
"""
global root_dir
f = None
if inp is None:
try:
inp = f = open(filename)
inp = f = open(os.path.join(root_dir, filename))
except IOError as e:
# do not report currently processed file as "in file ..."
filestack.pop()
Expand Down Expand Up @@ -1099,6 +1102,11 @@ def process_file(input_file_name, **kwargs):
"""main processing pipeline"""
# initialize file stack for error-reporting
init_stacks(input_file_name)

global root_dir
if 'root_dir' in kwargs:
root_dir = kwargs['root_dir']

# parse the document into a xml.dom tree
doc = parse(None, input_file_name)
# perform macro replacement
Expand Down
2 changes: 2 additions & 0 deletions xacro/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def process_args(argv, require_input=True):
help="print file dependencies")
parser.add_option("--inorder", "-i", action="store_true", dest="in_order",
help="processing in read order (default, can be omitted)")
parser.add_option("--root-dir", dest="root_dir", metavar="DIR",
help="set the root directory to resolve relative paths to")

# verbosity options
parser.add_option("-q", action="store_const", dest="verbosity", const=0,
Expand Down

0 comments on commit 5ae92bb

Please sign in to comment.