Update build rule application
Prior to this CL, the build rules for zephyr-bazel required a reference
to @zephyr//. This approach worked, but was limiting (we weren't able to
use globs). Instead, this new approach generates a patch on the fly and
applies it to Zephyr such that the BUILD.bazel and .bzl files in this
repo are copied to Zephyr's repo directly. The benefits:
1. The used can choose the name. @zephyr is not required.
2. We can reference files as though they were local.
3. We can check for merge conflicts with Zephyr in CI since we're
generating a patch and applying it directly to Zephyr.
BUG=b/346596832
Change-Id: Ifc024c1dec485f4b7d5140b4c1fb20cffeee68b5
Reviewed-on: https://pigweed-review.googlesource.com/c/zephyr/zephyr-bazel/+/223215
Reviewed-by: Sergio Soares <sergiosoares@google.com>
Commit-Queue: Yuval Peress <peress@google.com>
diff --git a/generate_diff.py b/generate_diff.py
new file mode 100644
index 0000000..288f920
--- /dev/null
+++ b/generate_diff.py
@@ -0,0 +1,81 @@
+import argparse
+import pathlib
+import io
+import sys
+from typing import List
+
+
+def get_bazel_files(root_dir: pathlib.Path) -> List[pathlib.Path]:
+ """Finds files matching "BUILD.bazel" or "*.bzl" under the given directory.
+
+ Args:
+ root_dir: The root directory to search in.
+
+ Returns:
+ A list of pathlib.Path objects representing the matching files.
+ """
+ return [
+ path
+ for path in root_dir.rglob("*")
+ if path.match("BUILD.bazel") or path.match("*.bzl")
+ ]
+
+def print_file_info(
+ file_path: pathlib.Path,
+ root_dir: pathlib.Path,
+ writer: io.TextIOWrapper,
+):
+ """Prints the relative path, line count, and prefixed content of a file.
+
+ Args:
+ file_path: The path to the file.
+ root_dir: The root directory to calculate the relative path from.
+ writer: Where to write the output
+ """
+ relative_path = file_path.relative_to(root_dir)
+ with open(file_path, "r", encoding="utf-8") as file:
+ content = file.readlines()
+ line_count = len(content)
+ writer.write("\n")
+ writer.write("--- /dev/null\n")
+ writer.write(f"+++ {relative_path}\n")
+ writer.write(f"@@ -0,0 +1,{line_count} @@\n")
+ for line in content:
+ writer.write(f"+{line.rstrip()}\n")
+
+def print_all(root_dir: pathlib.Path, writer: io.TextIOWrapper):
+ """Iterate through all BUILD.bazel and *.bzl files and write them to writer
+
+ Args:
+ root_dir: The root directory to calculate the relative path from.
+ writer: Where to write the output
+ """
+ for file_path in get_bazel_files(root_dir=root_dir):
+ print_file_info(file_path=file_path, root_dir=root_dir, writer=writer)
+
+def main() -> None:
+ """Finds all BUILD.bazel files and *.bzl files and generates a diff"""
+ parser = argparse.ArgumentParser()
+ parser.add_argument(
+ "--root-dir",
+ type=pathlib.Path,
+ required=True
+ )
+ parser.add_argument(
+ "-o",
+ dest="output",
+ type=pathlib.Path,
+ )
+ args = parser.parse_args()
+ if args.output:
+ args.output.parent.mkdir(parents=True, exist_ok=True)
+ with open(args.output, mode="w", encoding="utf-8") as o:
+ print_all(root_dir=args.root_dir, writer=o)
+ else:
+ print_all(
+ root_dir=args.root_dir,
+ writer=io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
+ )
+
+if __name__ == "__main__":
+ main()