Skip to content

Commit

Permalink
Drop cc_embed_data.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottTodd committed May 11, 2021
1 parent 62f1500 commit 6af6162
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 308 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ include(iree_cc_library)
include(iree_cc_test)
include(iree_tablegen_library)
include(iree_tablegen_doc)
include(iree_cc_embed_data)
include(iree_c_embed_data)
include(iree_bytecode_module)
include(iree_c_module)
Expand Down
34 changes: 0 additions & 34 deletions build_tools/bazel_to_cmake/bazel_to_cmake_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,40 +425,6 @@ def cc_binary(self,
# Effectively an alias in IREE code.
iree_cc_binary = cc_binary

def cc_embed_data(self,
name,
srcs,
cc_file_output,
h_file_output,
testonly=None,
cpp_namespace=None,
strip_prefix=None,
flatten=None,
identifier=None,
**kwargs):
if identifier:
self._convert_unimplemented_function("cc_embed_data",
name + " has identifier")
name_block = _convert_string_arg_block("NAME", name, quote=False)
srcs_block = _convert_srcs_block(srcs)
cc_file_output_block = _convert_string_arg_block("CC_FILE_OUTPUT",
cc_file_output)
h_file_output_block = _convert_string_arg_block("H_FILE_OUTPUT",
h_file_output)
testonly_block = _convert_option_block("TESTONLY", testonly)
namespace_block = _convert_string_arg_block("CPP_NAMESPACE", cpp_namespace)
flatten_block = _convert_option_block("FLATTEN", flatten)

self.converter.body += (f"iree_cc_embed_data(\n"
f"{name_block}"
f"{srcs_block}"
f"{cc_file_output_block}"
f"{h_file_output_block}"
f"{testonly_block}"
f"{namespace_block}"
f"{flatten_block}"
f" PUBLIC\n)\n\n")

def c_embed_data(self,
name,
srcs,
Expand Down
105 changes: 0 additions & 105 deletions build_tools/cmake/iree_cc_embed_data.cmake

This file was deleted.

38 changes: 7 additions & 31 deletions build_tools/embed_data/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# Generates source files with embedded file contents.

load(":build_defs.bzl", "c_embed_data", "cc_embed_data")
load(":build_defs.bzl", "c_embed_data")

package(
default_visibility = ["//visibility:public"],
Expand All @@ -32,58 +32,34 @@ cc_binary(
],
)

cc_embed_data(
c_embed_data(
name = "testembed1",
# do not sort
srcs = [
"file1.bin",
"data/file2.bin",
],
cc_file_output = "testembed1.cc",
cpp_namespace = "foobar",
c_file_output = "testembed1.c",
flatten = True,
h_file_output = "testembed1.h",
)

cc_embed_data(
c_embed_data(
name = "testembed2",
srcs = [
"data/file3.bin",
],
cc_file_output = "testembed2.cc",
cpp_namespace = "foobar",
c_file_output = "testembed2.c",
flatten = True,
h_file_output = "testembed2.h",
)

cc_test(
name = "cc_embed_data_test",
srcs = ["cc_embed_data_test.cc"],
deps = [
":testembed1",
":testembed2",
"//iree/testing:gtest",
"//iree/testing:gtest_main",
],
)

c_embed_data(
name = "testembed1_c",
# do not sort
srcs = [
"file1.bin",
"data/file2.bin",
],
c_file_output = "testembed1_c.c",
flatten = True,
h_file_output = "testembed1_c.h",
)

cc_test(
name = "c_embed_data_test",
srcs = ["c_embed_data_test.cc"],
deps = [
":testembed1_c",
":testembed1",
":testembed2",
"//iree/testing:gtest",
"//iree/testing:gtest_main",
],
Expand Down
81 changes: 1 addition & 80 deletions build_tools/embed_data/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Embeds data files into a C or C++ module."""

def cc_embed_data(
name,
srcs,
cc_file_output,
h_file_output,
testonly = False,
cpp_namespace = None,
strip_prefix = None,
flatten = False,
identifier = None,
**kwargs):
"""Embeds 'srcs' into a C++ module.
Generates a header like:
namespace iree {
struct iree_file_toc_t {
const char* name; // the file's original name
const char* data; // beginning of the file
size_t size; // length of the file
};
}
namespace foo {
extern const struct ::iree::iree_file_toc_t* this_rule_name_create();
}
The 'this_rule_name()' function will return an array of iree_file_toc_t
structs terminated by one that has nullptr 'name' and 'data' fields.
The 'data' field always has an extra null terminator at the end (which
is not included in the size).
Args:
name: The rule name, which will also be the identifier of the generated
code symbol.
srcs: List of files to embed.
cc_file_output: The CC implementation file to output.
h_file_output: The H header file to output.
testonly: If True, only testonly targets can depend on this target.
cpp_namespace: Wraps everything in a C++ namespace.
strip_prefix: Strips this verbatim prefix from filenames (in the TOC).
flatten: Removes all directory components from filenames (in the TOC).
identifier: The identifier to use in generated names (defaults to name).
**kwargs: Args to pass to the cc_library.
"""
generator = "//build_tools/embed_data:generate_embed_data"
generator_location = "$(location %s)" % generator
if identifier == None:
identifier = name
flags = "--output_header='$(location %s)' --output_impl='$(location %s)'" % (
h_file_output,
cc_file_output,
)
flags += " --identifier='%s'" % (identifier,)
flags += " --c_output=false"
if cpp_namespace != None:
flags += " --cpp_namespace='%s'" % (cpp_namespace,)
if strip_prefix != None:
flags += " --strip_prefix='%s'" % (strip_prefix,)
if flatten:
flags += " --flatten"

native.genrule(
name = name + "__generator",
srcs = srcs,
outs = [
cc_file_output,
h_file_output,
],
tools = [generator],
cmd = "%s $(SRCS) %s" % (generator_location, flags),
testonly = testonly,
)
native.cc_library(
name = name,
hdrs = [h_file_output],
srcs = [cc_file_output],
testonly = testonly,
**kwargs
)
"""Embeds data files into a C module."""

def c_embed_data(
name,
Expand Down
21 changes: 15 additions & 6 deletions build_tools/embed_data/c_embed_data_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,40 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "build_tools/embed_data/testembed1_c.h"
#include "build_tools/embed_data/testembed1.h"
#include "build_tools/embed_data/testembed2.h"
#include "iree/testing/gtest.h"

namespace {

TEST(Generator, TestContents) {
auto* toc1 = testembed1_c_create();
auto* toc1 = testembed1_create();
ASSERT_EQ("file1.bin", std::string(toc1->name));
ASSERT_EQ(R"(Are you '"Still"' here?)"
EXPECT_EQ(R"(Are you '"Still"' here?)"
"\n",
std::string(toc1->data));
ASSERT_EQ(24, toc1->size);
EXPECT_EQ(24, toc1->size);
ASSERT_EQ(0, *(toc1->data + toc1->size));

++toc1;
ASSERT_EQ("file2.bin", std::string(toc1->name));
ASSERT_EQ(R"(¯\_(ツ)_/¯)"
EXPECT_EQ(R"(¯\_(ツ)_/¯)"
"\n",
std::string(toc1->data));
ASSERT_EQ(14, toc1->size);
ASSERT_EQ(0, *(toc1->data + toc1->size));

++toc1;
ASSERT_EQ(nullptr, toc1->name);
EXPECT_EQ(nullptr, toc1->name);
ASSERT_EQ(nullptr, toc1->data);

auto* toc2 = testembed2_create();
ASSERT_EQ("file3.bin", std::string(toc2->name));
EXPECT_EQ(R"(ᕕ( ᐛ )ᕗ)"
"\n",
std::string(toc2->data));
EXPECT_EQ(14, toc2->size);
ASSERT_EQ(0, *(toc2->data + toc2->size));
}

} // namespace
Loading

0 comments on commit 6af6162

Please sign in to comment.