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