xref: /aosp_15_r20/external/pigweed/pw_build/cc_blob_library.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/python_action.gni")
18import("$dir_pw_build/target_types.gni")
19
20# Turns binary blobs into a C++ source_set library of hard-coded byte arrays.
21# The byte arrays are constant initialized and are safe to access at any time,
22# including before main().
23#
24#   blobs           A list of scopes, where each scope corresponds to a binary
25#                   blob to be transformed from file to byte array. This is a
26#                   required field. Blob fields include:
27#
28#                   symbol_name [required]: The C++ symbol for the byte array.
29#
30#                   file_path [required]: The file path for the binary blob.
31#
32#                   linker_section [optional]: If present, places the byte array
33#                     in the specified linker section.
34#
35#                   alignas [optional]: If present, the byte array is aligned as
36#                     specified. The value of this argument is used verbatim
37#                     in an alignas() specifier for the blob byte array.
38#
39#   out_header      The header file to generate. Users will include this file
40#                   exactly as it is written here to reference the byte arrays.
41#
42#   namespace       The C++ namespace in which to place the generated blobs.
43#
44template("pw_cc_blob_library") {
45  assert(defined(invoker.blobs), "pw_cc_blob_library requires 'blobs'")
46  assert(defined(invoker.out_header),
47         "pw_cc_blob_library requires an 'out_header'")
48  assert(defined(invoker.namespace),
49         "pw_cc_blob_library requires a 'namespace'")
50
51  _blobs = []
52  _blob_files = []
53  foreach(blob, invoker.blobs) {
54    assert(defined(blob.symbol_name), "Each 'blob' requires a 'symbol_name'")
55    assert(defined(blob.file_path), "Each 'blob' requires a 'file_path'")
56    _blob_files += [ blob.file_path ]
57    blob.file_path = rebase_path(blob.file_path, root_build_dir)
58    _blobs += [ blob ]
59  }
60
61  _out_dir = "$target_gen_dir/$target_name"
62  _blob_json_file = "$_out_dir/blobs.json"
63  write_file(_blob_json_file, _blobs, "json")
64
65  _header = "$_out_dir/public/${invoker.out_header}"
66  _source = "$_out_dir/" + get_path_info(invoker.out_header, "name") + ".cc"
67
68  pw_python_action("$target_name._gen") {
69    forward_variables_from(invoker,
70                           [
71                             "deps",
72                             "public_deps",
73                           ])
74    module = "pw_build.generate_cc_blob_library"
75    python_deps = [ "$dir_pw_build/py" ]
76
77    args = [
78      "--blob-file",
79      rebase_path(_blob_json_file, root_build_dir),
80      "--namespace=${invoker.namespace}",
81      "--header-include=${invoker.out_header}",
82      "--out-header",
83      rebase_path(_header, root_build_dir),
84      "--out-source",
85      rebase_path(_source, root_build_dir),
86    ]
87
88    inputs = [ _blob_json_file ] + _blob_files
89    outputs = [
90      _header,
91      _source,
92    ]
93  }
94
95  config("$target_name._include_path") {
96    include_dirs = [ "$_out_dir/public" ]
97    visibility = [ ":*" ]
98  }
99
100  pw_source_set(target_name) {
101    public = [ _header ]
102    sources = [ _source ]
103    public_configs = [ ":$target_name._include_path" ]
104    deps = [
105      ":$target_name._gen",
106      dir_pw_preprocessor,
107    ]
108    forward_variables_from(invoker, [ "visibility" ])
109  }
110}
111