Add bazel build scripts
This commit is contained in:
parent
09f0382e56
commit
e907023423
1269
BUILD.bazel
Normal file
1269
BUILD.bazel
Normal file
File diff suppressed because it is too large
Load Diff
4
MODULE.bazel
Normal file
4
MODULE.bazel
Normal file
@ -0,0 +1,4 @@
|
||||
module(
|
||||
name = "esp_open_rtos",
|
||||
version = "0.1",
|
||||
)
|
||||
133
bazel/cc_static_library.bzl
Normal file
133
bazel/cc_static_library.bzl
Normal file
@ -0,0 +1,133 @@
|
||||
"""Provides a rule that outputs a monolithic static library."""
|
||||
|
||||
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
|
||||
|
||||
TOOLS_CPP_REPO = "@bazel_tools"
|
||||
|
||||
def _cc_static_library_impl(ctx):
|
||||
output_lib = ctx.actions.declare_file("{}.a".format(ctx.attr.name))
|
||||
output_flags = ctx.actions.declare_file("{}.link".format(ctx.attr.name))
|
||||
|
||||
cc_toolchain = find_cpp_toolchain(ctx)
|
||||
|
||||
# Populate the feature configuration. This is used by the various cc library
|
||||
# functions to generate contexts.
|
||||
feature_configuration = cc_common.configure_features(
|
||||
ctx = ctx,
|
||||
cc_toolchain = cc_toolchain,
|
||||
requested_features = ctx.features,
|
||||
unsupported_features = ctx.disabled_features
|
||||
)
|
||||
|
||||
lib_sets = []
|
||||
lib_inputs = []
|
||||
unique_flags = {}
|
||||
|
||||
merged_compilation_context = cc_common.merge_compilation_contexts(
|
||||
compilation_contexts = [dep[CcInfo].compilation_context for dep in ctx.attr.deps]
|
||||
)
|
||||
|
||||
for dep in ctx.attr.deps:
|
||||
if hasattr(dep[CcInfo].linking_context.linker_inputs, "to_list"):
|
||||
lib_inputs.append(dep[CcInfo].linking_context.linker_inputs.to_list())
|
||||
else:
|
||||
lib_inputs.append(dep[CcInfo].linking_context.linker_inputs)
|
||||
|
||||
for lib in dep[CcInfo].linking_context.linker_inputs.to_list():
|
||||
if hasattr(lib.libraries, "to_list"):
|
||||
lib_sets.append(lib.libraries)
|
||||
else:
|
||||
lib_sets.append(depset(direct = lib.libraries))
|
||||
unique_flags.update({
|
||||
flag: None
|
||||
for flag in lib.user_link_flags
|
||||
})
|
||||
|
||||
libraries_to_link = depset(transitive = lib_sets)
|
||||
link_flags = unique_flags.keys()
|
||||
|
||||
libs = []
|
||||
libs.extend([lib.pic_static_library for lib in libraries_to_link.to_list() if lib.pic_static_library])
|
||||
libs.extend([
|
||||
lib.static_library
|
||||
for lib in libraries_to_link.to_list()
|
||||
if lib.static_library and not lib.pic_static_library
|
||||
])
|
||||
|
||||
script_file = ctx.actions.declare_file("{}.mri".format(ctx.attr.name))
|
||||
commands = ["create {}".format(output_lib.path)]
|
||||
for lib in libs:
|
||||
commands.append("addlib {}".format(lib.path))
|
||||
commands.append("save")
|
||||
commands.append("end")
|
||||
ctx.actions.write(
|
||||
output = script_file,
|
||||
content = "\n".join(commands) + "\n",
|
||||
)
|
||||
|
||||
ar_tool = cc_common.get_tool_for_action(
|
||||
feature_configuration = feature_configuration,
|
||||
action_name = "ar",
|
||||
)
|
||||
|
||||
ctx.actions.run_shell(
|
||||
command = "{} -M < {}".format(ar_tool, script_file.path),
|
||||
inputs = [script_file] + libs + cc_toolchain.all_files.to_list(),
|
||||
outputs = [output_lib],
|
||||
mnemonic = "ArMerge",
|
||||
progress_message = "Merging static library {}".format(output_lib.path),
|
||||
)
|
||||
ctx.actions.write(
|
||||
output = output_flags,
|
||||
content = "\n".join(link_flags) + "\n",
|
||||
)
|
||||
|
||||
# With the library in hand, the next step is to set up information for
|
||||
# Bazel's C/C++ library to utilize the newly-created library.
|
||||
library_to_link = cc_common.create_library_to_link(
|
||||
actions = ctx.actions,
|
||||
feature_configuration = feature_configuration,
|
||||
static_library = output_lib,
|
||||
#user_link_flags = ['fooxbar']
|
||||
)
|
||||
|
||||
linker_input = cc_common.create_linker_input(
|
||||
libraries = depset([library_to_link]),
|
||||
owner = ctx.label,
|
||||
)
|
||||
|
||||
linking_context = cc_common.create_linking_context(
|
||||
linker_inputs = depset([linker_input])
|
||||
)
|
||||
|
||||
# CcInfo is what is actually passed to future targets. We may need to merge
|
||||
# this with the dependency's original info in order for builds to complete.
|
||||
# Not sure yet.
|
||||
this_cc_info = CcInfo(compilation_context = merged_compilation_context, linking_context = linking_context)
|
||||
cc_infos = [this_cc_info]
|
||||
# print(this_cc_info.linking_context)
|
||||
# print(ctx.attr.library[CcInfo].compilation_context)
|
||||
# print(ctx.attr.library[CcInfo].linking_context)
|
||||
# cc_infos.append(ctx.attr.library[CcInfo])
|
||||
|
||||
merged_cc_info = cc_common.merge_cc_infos(direct_cc_infos = [this_cc_info], cc_infos = cc_infos)
|
||||
# print(merged_cc_info.linking_context)
|
||||
|
||||
return [
|
||||
DefaultInfo(files = depset([output_lib])),
|
||||
merged_cc_info
|
||||
]
|
||||
|
||||
cc_static_library = rule(
|
||||
implementation = _cc_static_library_impl,
|
||||
attrs = {
|
||||
"deps": attr.label_list(),
|
||||
"_cc_toolchain": attr.label(
|
||||
default = TOOLS_CPP_REPO + "//tools/cpp:current_cc_toolchain",
|
||||
),
|
||||
},
|
||||
toolchains = [TOOLS_CPP_REPO + "//tools/cpp:toolchain_type"],
|
||||
fragments = ["cpp"],
|
||||
incompatible_use_toolchain_transition = True,
|
||||
)
|
||||
|
||||
73
bazel/postprocess_archive.sh
Executable file
73
bazel/postprocess_archive.sh
Executable file
@ -0,0 +1,73 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This wrapper script takes a given archive (static library) and processes it,
|
||||
# either removing/renaming symbols according to the provided arguments.
|
||||
|
||||
BAZEL_EXECROOT=
|
||||
ARCHIVE_TOOL=
|
||||
OBJCOPY_TOOL=
|
||||
INPUT_FILE=
|
||||
OUTPUT_FILE=
|
||||
STAGE_NUMBER=0
|
||||
|
||||
RENAME_FILE=
|
||||
|
||||
ARGS=()
|
||||
|
||||
set -e
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--archive)
|
||||
ARCHIVE_TOOL="$2"
|
||||
shift; shift; ;;
|
||||
--objcopy)
|
||||
OBJCOPY_TOOL="$2"
|
||||
shift; shift; ;;
|
||||
--input)
|
||||
INPUT_FILE="$2"
|
||||
shift; shift; ;;
|
||||
--output)
|
||||
OUTPUT_FILE="$2"
|
||||
shift; shift; ;;
|
||||
--remove|--redefine)
|
||||
ARGS+=("$1")
|
||||
shift ; ;;
|
||||
-*)
|
||||
echo "Unknown option $1"
|
||||
exit 1 ; ;;
|
||||
*)
|
||||
ARGS+=("$1")
|
||||
shift ; ;;
|
||||
esac
|
||||
done
|
||||
|
||||
set -- "${ARGS[@]}"
|
||||
|
||||
if [[ $# -eq 0 ]]; then
|
||||
echo "No modification to be performed."
|
||||
exit 2;
|
||||
fi
|
||||
|
||||
IN=${INPUT_FILE}
|
||||
while [[ $# -gt 0 ]]; do
|
||||
OUT=${OUTPUT_FILE}.stage${STAGE_NUMBER}
|
||||
case $1 in
|
||||
--remove)
|
||||
# Avoid read-only input file.
|
||||
cat $IN > $OUT && $ARCHIVE_TOOL d $OUT @$2
|
||||
|
||||
shift; shift; ;;
|
||||
--redefine)
|
||||
$OBJCOPY_TOOL --redefine-syms $2 --weaken $IN $OUT
|
||||
shift; shift; ;;
|
||||
*)
|
||||
echo "Bad argument: $1"
|
||||
exit 3 ; ;;
|
||||
esac
|
||||
STAGE_NUMBER=$((STAGE_NUMBER+1))
|
||||
IN=$OUT
|
||||
done
|
||||
|
||||
|
||||
mv $IN $OUTPUT_FILE
|
||||
149
bazel/postprocess_static_library.bzl
Normal file
149
bazel/postprocess_static_library.bzl
Normal file
@ -0,0 +1,149 @@
|
||||
# postprocess_static_library takes a prebuilt archive (static library) and
|
||||
# filters out unnecessary objects / redefines symbols files.
|
||||
# In ESP8266's case, this is because the ROM already provides their
|
||||
# implementations.
|
||||
|
||||
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
|
||||
load("@bazel_tools//tools/cpp:toolchain_utils.bzl",
|
||||
"find_cpp_toolchain", "use_cpp_toolchain")
|
||||
|
||||
def _impl(ctx):
|
||||
# Find the toolchain configuration for the given platform. This is _not_
|
||||
# the same way that other languages do this, as ctx.toolchains is empty
|
||||
# for C/C++ (??)
|
||||
cc_toolchain = find_cpp_toolchain(ctx)
|
||||
|
||||
# Populate the feature configuration. This is used by the various cc library
|
||||
# functions to generate contexts.
|
||||
feature_configuration = cc_common.configure_features(
|
||||
ctx = ctx,
|
||||
cc_toolchain = cc_toolchain,
|
||||
requested_features = ctx.features,
|
||||
unsupported_features = ctx.disabled_features
|
||||
)
|
||||
|
||||
# We need these toolchain executables in order to process the library.
|
||||
archive_tool = cc_common.get_tool_for_action(
|
||||
feature_configuration = feature_configuration,
|
||||
action_name = ACTION_NAMES.cpp_link_static_library,
|
||||
)
|
||||
|
||||
objcopy_tool = cc_common.get_tool_for_action(
|
||||
feature_configuration = feature_configuration,
|
||||
action_name = "objcopy"
|
||||
)
|
||||
|
||||
# Library label (target) for processing.
|
||||
input_lib = ctx.file.library
|
||||
|
||||
# The output library will be named after the target of this rule.
|
||||
output_lib = ctx.actions.declare_file(ctx.label.name + ".a")
|
||||
|
||||
# Bazel needs to know what files are inputs to a given action.
|
||||
input_files = [input_lib]
|
||||
|
||||
# Construct the argument list to pass to the postprocessor script
|
||||
args = ctx.actions.args()
|
||||
args.add_all([
|
||||
'--input', input_lib,
|
||||
'--output', output_lib,
|
||||
'--archive', archive_tool,
|
||||
'--objcopy', objcopy_tool
|
||||
])
|
||||
|
||||
# If removing objects from the library, create a file, write the objects
|
||||
# to remove into it, and add it to the list of inputs for the action.
|
||||
if len(ctx.attr.remove) > 0:
|
||||
rmobjs = ctx.actions.declare_file(ctx.file.library.basename + ".remove")
|
||||
ctx.actions.write(rmobjs, "\n".join(ctx.attr.remove))
|
||||
input_files.append(rmobjs)
|
||||
args.add_all(['--remove', rmobjs])
|
||||
|
||||
# Similar for redefining symbols.
|
||||
if len(ctx.attr.redefine) > 0:
|
||||
rdobjs = ctx.actions.declare_file(ctx.file.library.basename + ".redefine")
|
||||
kv_pairs = [k + " " + v for k, v in ctx.attr.redefine.items()]
|
||||
ctx.actions.write(rdobjs, "\n".join(kv_pairs) + "\n")
|
||||
input_files.append(rdobjs)
|
||||
args.add_all(['--redefine', rdobjs])
|
||||
|
||||
# TODO: Handle other ways to process a library.
|
||||
|
||||
# This runs the actual script to generate the processed library.
|
||||
ctx.actions.run(
|
||||
inputs = input_files,
|
||||
outputs = [output_lib],
|
||||
tools = cc_toolchain.all_files,
|
||||
arguments = [args],
|
||||
executable = ctx.executable.postprocessor,
|
||||
)
|
||||
|
||||
# With the library in hand, the next step is to set up information for
|
||||
# Bazel's C/C++ library to utilize the newly-created library.
|
||||
library_to_link = cc_common.create_library_to_link(
|
||||
actions = ctx.actions,
|
||||
feature_configuration = feature_configuration,
|
||||
static_library = output_lib,
|
||||
)
|
||||
|
||||
linker_input = cc_common.create_linker_input(
|
||||
libraries = depset([library_to_link]),
|
||||
owner = ctx.label,
|
||||
)
|
||||
|
||||
linking_context = cc_common.create_linking_context(
|
||||
linker_inputs = depset([linker_input])
|
||||
)
|
||||
|
||||
# Not yet complete: This part needs us to forward the library and its
|
||||
# associated headers for targets to use without forwarding the original
|
||||
# target's library (or you'll get a bunch of linking errors.
|
||||
(compilation_context, compilation_outputs) = cc_common.compile(
|
||||
actions = ctx.actions,
|
||||
feature_configuration = feature_configuration,
|
||||
cc_toolchain = cc_toolchain,
|
||||
name = ctx.label.name,
|
||||
)
|
||||
|
||||
# CcInfo is what is actually passed to future targets. We may need to merge
|
||||
# this with the dependency's original info in order for builds to complete.
|
||||
# Not sure yet.
|
||||
this_cc_info = CcInfo(compilation_context = compilation_context, linking_context = linking_context)
|
||||
cc_infos = [this_cc_info]
|
||||
# print(this_cc_info.linking_context)
|
||||
# print(ctx.attr.library[CcInfo].compilation_context)
|
||||
# print(ctx.attr.library[CcInfo].linking_context)
|
||||
# cc_infos.append(ctx.attr.library[CcInfo])
|
||||
|
||||
merged_cc_info = cc_common.merge_cc_infos(direct_cc_infos = [this_cc_info], cc_infos = cc_infos)
|
||||
# print(merged_cc_info.linking_context)
|
||||
|
||||
return [
|
||||
DefaultInfo(files = depset([output_lib])),
|
||||
merged_cc_info
|
||||
]
|
||||
|
||||
postprocess_static_library = rule(
|
||||
implementation = _impl,
|
||||
attrs = {
|
||||
"library": attr.label(
|
||||
allow_single_file = True,
|
||||
),
|
||||
"remove": attr.string_list(
|
||||
doc = "A list of modules (objects) to remove from the library.",
|
||||
),
|
||||
"redefine": attr.string_dict(
|
||||
doc = "A list of symbols to redefine in the library.",
|
||||
),
|
||||
"postprocessor": attr.label(
|
||||
default = Label("//toolchain:postprocess_archive"),
|
||||
executable = True,
|
||||
cfg = "exec",
|
||||
),
|
||||
"_cc_toolchain": attr.label(
|
||||
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
|
||||
)
|
||||
},
|
||||
fragments = ["cpp"],
|
||||
toolchains = use_cpp_toolchain(),
|
||||
)
|
||||
Loading…
Reference in New Issue
Block a user