mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
[build] MVP for building with bazel (#6994)
This commit is contained in:
8
shared/bazel/rules/gen/BUILD.bazel
Normal file
8
shared/bazel/rules/gen/BUILD.bazel
Normal 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"],
|
||||
)
|
||||
10
shared/bazel/rules/gen/gen-resources.bzl
Normal file
10
shared/bazel/rules/gen/gen-resources.bzl
Normal 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,
|
||||
)
|
||||
19
shared/bazel/rules/gen/gen-version-file.bzl
Normal file
19
shared/bazel/rules/gen/gen-version-file.bzl
Normal 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,
|
||||
),
|
||||
},
|
||||
)
|
||||
58
shared/bazel/rules/gen/gen_resources.py
Normal file
58
shared/bazel/rules/gen/gen_resources.py
Normal 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()
|
||||
Reference in New Issue
Block a user