Skip to content

Commit

Permalink
pw_build: Allow cc_blob_library to handle relative paths
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Joe Brennan authored and CQ Bot Account committed Jul 26, 2022
1 parent 0117408 commit 63271a2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
1 change: 0 additions & 1 deletion pw_build/cc_blob_library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ function(pw_cc_blob_library NAME)
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}"
Expand Down
5 changes: 2 additions & 3 deletions pw_build/cc_blob_library.gni
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ template("pw_cc_blob_library") {
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"
Expand All @@ -78,7 +78,6 @@ template("pw_cc_blob_library") {
"--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",
Expand Down
10 changes: 5 additions & 5 deletions pw_build/py/generate_cc_blob_library_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

COMMON_SOURCE_START = COMMENT + """\
#include "path/to/header.h"
#include "header.h"
#include <array>
#include <cstddef>
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_source_with_mixed_blobs(self):
]

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'
Expand All @@ -183,7 +183,7 @@ def test_source_with_namespace(self):
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'
Expand All @@ -206,7 +206,7 @@ def test_source_with_linker_sections(self):
]

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'
Expand All @@ -230,7 +230,7 @@ def test_source_with_alignas(self):
]

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'
Expand Down
16 changes: 8 additions & 8 deletions pw_build/py/pw_build/generate_cc_blob_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import argparse
import itertools
import json
import os
from pathlib import Path
from string import Template
import textwrap
Expand Down Expand Up @@ -90,9 +91,6 @@ def parse_args() -> dict:
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,
Expand Down Expand Up @@ -147,7 +145,6 @@ def array_def_from_blob_data(blob: Blob, blob_data: bytes) -> str:
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)
Expand All @@ -162,9 +159,13 @@ def array_def_from_blob_data(blob: Blob, blob_data: bytes) -> str:


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))
Expand All @@ -183,17 +184,16 @@ def load_blobs(blob_file: Path) -> Sequence[Blob]:


def main(blob_file: Path,
header_include: str,
out_source: Path,
out_header: Path,
namespace: Optional[str] = None) -> None:
blobs = load_blobs(blob_file)

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__':
Expand Down

0 comments on commit 63271a2

Please sign in to comment.