[build] MVP for building with bazel (#6994)

This commit is contained in:
PJ Reiniger
2024-10-19 12:54:49 -04:00
committed by GitHub
parent 95b9bd880b
commit 36e0c9d6db
31 changed files with 1544 additions and 1 deletions

View File

@@ -0,0 +1,8 @@
load("@rules_python//python:defs.bzl", "py_binary")
py_binary(
name = "gen_resources",
srcs = ["gen_resources.py"],
tags = ["manual"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,10 @@
def generate_resources(name, resource_files, prefix, namespace, visibility = None):
cmd = "$(locations //shared/bazel/rules/gen:gen_resources) --prefix={} --namespace={} --resources $(SRCS) --output_files $(OUTS)".format(prefix, namespace, resource_files)
native.genrule(
name = name,
srcs = resource_files,
outs = ["{}.cpp".format(input_file) for input_file in resource_files],
cmd = cmd,
tools = ["//shared/bazel/rules/gen:gen_resources"],
visibility = visibility,
)

View File

@@ -0,0 +1,19 @@
def _generate_version_file_impl(ctx):
out = ctx.actions.declare_file(ctx.attr.output_file)
ctx.actions.expand_template(
output = out,
template = ctx.file.template,
substitutions = {"${wpilib_version}": "TODO - Built with bazel"},
)
return [DefaultInfo(files = depset([out]))]
generate_version_file = rule(
implementation = _generate_version_file_impl,
attrs = {
"output_file": attr.string(mandatory = True),
"template": attr.label(
allow_single_file = True,
mandatory = True,
),
},
)

View File

@@ -0,0 +1,58 @@
import os
import argparse
import binascii
def generate_file(resource_file, output_file, prefix, namespace):
func_name = "GetResource_" + os.path.basename(resource_file).replace(
"-", "_"
).replace(".", "_")
with open(resource_file, "rb") as f:
raw_data = f.read()
hex = str(binascii.hexlify(raw_data), "ascii")
data = ", ".join("0x" + hex[i : i + 2] for i in range(0, len(hex), 2))
data_size = len(raw_data)
output = """#include <stddef.h>
#include <string_view>
extern "C" {{
static const unsigned char contents[] = {{ {data} }};
const unsigned char* {prefix}_{func_name}(size_t* len) {{
*len = {data_size};
return contents;
}}
}}
namespace {namespace} {{
std::string_view {func_name}() {{
return std::string_view(reinterpret_cast<const char*>(contents), {data_size});
}}
}}
""".format(
func_name=func_name,
data_size=data_size,
prefix=prefix,
data=data,
namespace=namespace,
)
with open(output_file, "w") as f:
f.write(output)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--namespace")
parser.add_argument("--prefix")
parser.add_argument("--resources", nargs="+")
parser.add_argument("--output_files", nargs="+")
args = parser.parse_args()
assert len(args.resources) == len(args.output_files)
for i, resource in enumerate(args.resources):
generate_file(resource, args.output_files[i], args.prefix, args.namespace)
if __name__ == "__main__":
main()