• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of 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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16Rule that allows to embed binary files into a C++ program.
17
18The result is exposed as an absl::string_view global variable.
19"""
20
21def cc_data_blob(name, src, varname):
22    native.genrule(
23        name = name + "_sources",
24        srcs = [src],
25        outs = [name + ".cc", name + ".h"],
26        cmd = "; ".join([
27            "cat $< > $(@D)/datablob.tmp",
28            "pushd $(@D)",
29            "outname=%s" % name,
30            "echo 'namespace {' > $$outname.cc",
31            "xxd -i datablob.tmp >> $$outname.cc",
32            "echo '}' >> $$outname.cc",
33            "echo '#include \"absl/strings/string_view.h\"' >> $$outname.cc",
34            "echo 'absl::string_view %s(reinterpret_cast<const char*>(datablob_tmp), datablob_tmp_len);' >> $$outname.cc" % varname,
35            "echo '#include \"absl/strings/string_view.h\"' >> $$outname.h",
36            "echo 'extern absl::string_view %s;' >> $$outname.h" % varname,
37            "rm datablob.tmp",
38            "popd",
39        ]),
40    )
41    native.cc_library(
42        name = name,
43        srcs = [name + ".cc"],
44        hdrs = [name + ".h"],
45        deps = ["@com_google_absl//absl/strings"],
46    )
47