rules_cc
rules_cc copied to clipboard
FR: capability to embed binary data in CC object file.
It is often extremely useful to embed a generated (or static) binary object directly in a C++ library. For example, the ICU library build compiles text files describing character attributes into compressed binary structures that can be evaluated at run time. The canonical technique is to
- generate a data structure that is appropriate for the target platform (in byte alignment and endian-ness)
- wrap it up just enough so that it becomes a linkable object (.o, .obj) file
Typically, the object file defines at least 2 symbols unit8_t _DATA_OBJECT_NAME_start[] unit32_t _DATA_OBJECT_NAME_size Those are declared in a generated .h file and provided by the object module.
Problem scope
- needs to support at least COFF & ELF object formats
- needs to work for *nix and Windows
- there may be a need to specify what section the data is in
- it may be useful to pack multiple blobs into a single .o file
- generating a large C or Assembler file to compile with the CC toolchain does not work. Large blobs can easily generate text files that hit compiler limits.
There is an interesting overview of issues and techniques here: https://www.devever.net/~hl/incbin
Any suggestions on this?
A partial solution (hits about half the bullets under "Scope"): https://github.com/bcsgh/bazel_rules/tree/master/cc_embed_data
I've only tested that on the limited environments I've needed it in (Linux/ELF) so YMMV. If anyone wants to add Win/COFF I'd accept pull requests.
Related: https://github.com/bazelbuild/rules_cc/issues/41.