blob: 9d16e0fb8fa07d4476024f1bd8bc027665caaa69 [file] [log] [blame]
# Part of the Crubit project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""Utility module for sharing logic between rules and aspects that generate Rust bindings from C++.
"""
def compile_cc(
ctx,
attr,
cc_toolchain,
feature_configuration,
src,
cc_infos,
extra_cc_compilation_action_inputs):
"""Compiles a C++ source file.
Args:
ctx: The rule context.
attr: The current rule's attributes.
cc_toolchain: A cc_toolchain.
feature_configuration: A feature configuration.
src: The source file to be compiled.
cc_infos: List[CcInfo]: A list of CcInfo dependencies.
extra_cc_compilation_action_inputs: A list of input files for the C++ compilation action.
Returns:
A CcInfo provider.
"""
cc_info = cc_common.merge_cc_infos(cc_infos = cc_infos)
user_copts = []
for copt in getattr(attr, "copts", []):
# ctx.expand_make_variables is deprecated, but its replacement ctx.var does not suffice.
user_copts.append(ctx.expand_make_variables("copts", copt, {}))
(compilation_context, compilation_outputs) = cc_common.compile(
name = src.basename,
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
srcs = [src],
additional_inputs = extra_cc_compilation_action_inputs,
user_compile_flags = user_copts,
compilation_contexts = [cc_info.compilation_context],
)
(linking_context, _) = cc_common.create_linking_context_from_compilation_outputs(
name = src.basename,
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
compilation_outputs = compilation_outputs,
linking_contexts = [cc_info.linking_context],
)
debug_context = cc_common.merge_debug_context([
cc_info.debug_context(),
cc_common.create_debug_context(compilation_outputs),
])
return CcInfo(
compilation_context = compilation_context,
linking_context = linking_context,
debug_context = debug_context,
)