blob: 2ec3cb396ef69828b310bc696a110377807cba50 [file] [log] [blame]
def _local_patched_repository_impl(rctx):
path = rctx.attr.path
# This path is a bit ugly to get the actual path if it is relative.
if path[0] != "/":
path = rctx.workspace_root.get_child(path)
# Copy the repository
rctx.execute(["mkdir", str(rctx.path(""))])
for child in path.readdir():
copy_args = ["cp", "-fr", str(child), str(rctx.path(""))]
if rctx.attr.debug:
print(copy_args)
result = rctx.execute(copy_args)
if result.return_code != 0:
fail("Failed to copy %s (%s):\n%s" % (rctx.attr.path, result.return_code, result.stderr))
# Now patch the repository
for patch_file in rctx.attr.patches:
patch_file_path_string = str(rctx.path(patch_file).realpath)
if rctx.attr.debug:
print("Applying patch file: %s / %s" % (patch_file, patch_file_path_string))
result = rctx.execute([
"bash",
"-c",
"patch -p0 < " + patch_file_path_string,
])
if result.return_code != 0:
fail("Failed to apply patch: %s\n%s" % (patch_file, result.stderr))
local_patched_repository = repository_rule(
implementation = _local_patched_repository_impl,
attrs = {
"debug": attr.bool(default = False),
"patches": attr.label_list(allow_files = True),
"path": attr.string(mandatory = True),
},
local = True,
)
def _create_zephyr_patch_file_impl(rctx):
script_path = Label("//:generate_diff.py")
output_file = rctx.attr.filename
output_file_path = rctx.path(output_file)
rctx.file(output_file)
rctx.file("BUILD")
args = [
rctx.which("python3"),
script_path,
"--root-dir",
rctx.path(script_path).dirname,
"-o",
output_file_path,
]
if rctx.attr.debug:
print(args)
result = rctx.execute(args)
if result.return_code != 0:
fail("Failed to generate zephyr-bazel diff file (%s):\n%s" % (result.return_code, result.stderr))
if rctx.attr.debug:
print("Generated %s" % (output_file_path))
return [
DefaultInfo(
files = depset([output_file_path, rctx.path("BUILD")]),
),
]
create_zephyr_patch_file = repository_rule(
implementation = _create_zephyr_patch_file_impl,
attrs = {
"debug": attr.bool(default = False),
"filename": attr.string(
default = "patch.diff",
mandatory = True,
),
},
local = True,
)
###############################################################################
def _print_file_impl(ctx):
# Get the input file
input_file = ctx.file.file
# Create a dummy output file
output_file = ctx.actions.declare_file(ctx.label.name + ".out")
# Create a shell action to print the file
ctx.actions.run_shell(
outputs = [output_file],
inputs = [input_file],
command = "cat {} > {}".format(input_file.path, output_file.path),
)
# Return DefaultInfo to signal dependencies on the input file
return DefaultInfo(files = depset([output_file]))
# Define the rule
print_file = rule(
implementation = _print_file_impl,
attrs = {
"file": attr.label(allow_single_file = True, mandatory = True),
},
)