Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Bazel/skylark rule for embedding source files as a cc_library rule.

This take a files from the build environment a puts it directly into the data section of a binary, making them accessible as a library.

This allows large test or binary artifacts to come from files (or other build artifacts) rather than deal with escaping them into string literals.

MODULE.bazel

bazel_dep(
    name = "com_github_bcsgh_cc_embed_data",
    version = ...,
)

Example usage:

src/BUILD

load("//cc_embed_data:cc_embed_data.bzl", "cc_embed_data")

cc_embed_data(
    name = "cc_embed_example",
    srcs = [
      "foo.bin",
      #...
    ],
    namespace = "my_namespace",
)


cc_test(
    name = "cc_embed_example_usage",
    srcs = ["cc_embed_example_usage.cc"],
    data = SRCS,
    deps = [
        ":cc_embed_example",
        "@com_google_googletest//:gtest_main",
    ],
)

src/cc_embed_example_usage.cc

#include <string_view>

#include "cc_embed_example_data.h"

void Use(std::string_view);
void Use(std::string_view, std::string_view);

int main() {
  // find one
  Use(my_namespace::src_foo_bin());

  // find all
  for (const auto& i : my_namespace::EmbedIndex()) {
    Use(i.first, i.second);
  }

  return 0;
}

cc_embed_data

load("@com_github_bcsgh_cc_embed_data//cc_embed_data:cc_embed_data.bzl", "cc_embed_data")

cc_embed_data(name, deps, srcs, a, cc, h, json, namespace)

Generate a library containing the contents of srcs.

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
deps A map from build rules to embed to a path (e.g. "files") on the Target object that yeilds a depset of files. (Figuring out what this path should be more or less requiers mucking around with rule implementations. Sorry.) Dictionary: Label -> String optional {}
srcs The files to embed. List of labels optional []
a The generated cc_library archive. (This must be set for other rules to depend on individual output files.) Label optional None
cc The generated C++ source file. (This must be set for other rules to depend on individual output files.) Label optional None
h The generated C++ header file. (This must be set for other rules to depend on individual output files.) Label optional None
json - Label optional None
namespace If given, the C++ namespace to generate in. String optional ""

Setup (for development)

To configure the git hooks, run ./.git_hooks/setup.sh

About

Bazel build rules for embedding arbitraty files into a cc_library target.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages