pw_build: Allow cc_blob_library to handle relative paths

cc_blob_library currently sets every path to the absolute
path but for RBE all paths must come from the build root
and not absolute paths.  This change will allow for
everything to be based off the build root.

Bug: b/239560853
Test: Verified this generated the build files correct and built
      correctly with RBE and regular builds.
Change-Id: I224e60e3c68a6018de0dc747f2620a8897f78613
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/98441
Reviewed-by: Tom Turney <tturney@google.com>
Reviewed-by: Joe Brennan <jmbrenna@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Pigweed-Auto-Submit: Joe Brennan <jmbrenna@google.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
diff --git a/pw_build/cc_blob_library.cmake b/pw_build/cc_blob_library.cmake
index f34cb64..63c650f 100644
--- a/pw_build/cc_blob_library.cmake
+++ b/pw_build/cc_blob_library.cmake
@@ -134,7 +134,6 @@
       python3
       "$ENV{PW_ROOT}/pw_build/py/pw_build/generate_cc_blob_library.py"
       --blob-file "${blob_json_file}"
-      --header-include "${arg_HEADER}"
       --out-header "${generated_header}"
       --out-source "${generated_source}"
       --namespace "${arg_NAMESPACE}"
diff --git a/pw_build/cc_blob_library.gni b/pw_build/cc_blob_library.gni
index 259f273..79e355d 100644
--- a/pw_build/cc_blob_library.gni
+++ b/pw_build/cc_blob_library.gni
@@ -53,9 +53,9 @@
   foreach(blob, invoker.blobs) {
     assert(defined(blob.symbol_name), "Each 'blob' requires a 'symbol_name'")
     assert(defined(blob.file_path), "Each 'blob' requires a 'file_path'")
-    blob.file_path = rebase_path(blob.file_path)
-    _blobs += [ blob ]
     _blob_files += [ blob.file_path ]
+    blob.file_path = rebase_path(blob.file_path, root_build_dir)
+    _blobs += [ blob ]
   }
 
   _out_dir = "$target_gen_dir/$target_name"
@@ -78,7 +78,6 @@
       "--blob-file",
       rebase_path(_blob_json_file, root_build_dir),
       "--namespace=${invoker.namespace}",
-      "--header-include=${invoker.out_header}",
       "--out-header",
       rebase_path(_header, root_build_dir),
       "--out-source",
diff --git a/pw_build/py/generate_cc_blob_library_test.py b/pw_build/py/generate_cc_blob_library_test.py
index 4bd1781..f43e7e6 100644
--- a/pw_build/py/generate_cc_blob_library_test.py
+++ b/pw_build/py/generate_cc_blob_library_test.py
@@ -40,7 +40,7 @@
 
 COMMON_SOURCE_START = COMMENT + """\
 
-#include "path/to/header.h"
+#include "header.h"
 
 #include <array>
 #include <cstddef>
@@ -166,7 +166,7 @@
         ]
 
         source = generate_cc_blob_library.source_from_blobs(
-            blobs, 'path/to/header.h')
+            blobs, 'path/to/header.h', 'path/to/source.cc')
         expected_source = (f'{COMMON_SOURCE_START}'
                            '\n'
                            '\n'
@@ -183,7 +183,7 @@
         blobs = [generate_cc_blob_library.Blob('fooBlob', foo_blob, None)]
 
         source = generate_cc_blob_library.source_from_blobs(
-            blobs, 'path/to/header.h', 'pw::foo')
+            blobs, 'path/to/header.h', 'path/to/source.cc', 'pw::foo')
         expected_source = (f'{COMMON_SOURCE_START}'
                            '\n'
                            'namespace pw::foo {\n'
@@ -206,7 +206,7 @@
         ]
 
         source = generate_cc_blob_library.source_from_blobs(
-            blobs, 'path/to/header.h')
+            blobs, 'path/to/header.h', 'path/to/source.cc')
         expected_source = (f'{COMMON_SOURCE_START}'
                            '\n'
                            '\n'
@@ -230,7 +230,7 @@
         ]
 
         source = generate_cc_blob_library.source_from_blobs(
-            blobs, 'path/to/header.h')
+            blobs, 'path/to/header.h', 'path/to/source.cc')
         expected_source = (f'{COMMON_SOURCE_START}'
                            '\n'
                            '\n'
diff --git a/pw_build/py/pw_build/generate_cc_blob_library.py b/pw_build/py/pw_build/generate_cc_blob_library.py
index 64bf050..52cb2b0 100644
--- a/pw_build/py/pw_build/generate_cc_blob_library.py
+++ b/pw_build/py/pw_build/generate_cc_blob_library.py
@@ -16,6 +16,7 @@
 import argparse
 import itertools
 import json
+import os
 from pathlib import Path
 from string import Template
 import textwrap
@@ -90,9 +91,6 @@
                         required=True,
                         help=('Path to json file containing the list of blobs '
                               'to generate.'))
-    parser.add_argument('--header-include',
-                        required=True,
-                        help='Path to use in #includes for the header')
     parser.add_argument('--out-source',
                         type=Path,
                         required=True,
@@ -147,7 +145,6 @@
         section_attr = ''
 
     byte_strs = ['std::byte{{0x{:02X}}}'.format(b) for b in blob_data]
-
     lines = []
     for byte_strs_for_line in split_into_chunks(byte_strs, BYTES_PER_LINE):
         bytes_segment = ', '.join(byte_strs_for_line)
@@ -162,9 +159,13 @@
 
 
 def source_from_blobs(blobs: Iterable[Blob],
-                      header_path: str,
+                      header_path: Path,
+                      source_path: Path,
                       namespace: Optional[str] = None) -> str:
     """Generate the contents of a C++ source file from blobs."""
+    header_path = Path(
+        os.path.relpath(header_path,
+                        os.path.commonprefix([header_path, source_path])))
     lines = [SOURCE_PREFIX_TEMPLATE.substitute(header_path=header_path)]
     if namespace:
         lines.append(NAMESPACE_OPEN_TEMPLATE.substitute(namespace=namespace))
@@ -183,7 +184,6 @@
 
 
 def main(blob_file: Path,
-         header_include: str,
          out_source: Path,
          out_header: Path,
          namespace: Optional[str] = None) -> None:
@@ -191,9 +191,9 @@
 
     out_header.parent.mkdir(parents=True, exist_ok=True)
     out_header.write_text(header_from_blobs(blobs, namespace))
-
     out_source.parent.mkdir(parents=True, exist_ok=True)
-    out_source.write_text(source_from_blobs(blobs, header_include, namespace))
+    out_source.write_text(
+        source_from_blobs(blobs, out_header, out_source, namespace))
 
 
 if __name__ == '__main__':