Split major functions out of rust_bindings_from_cc_utils to separate files
This is going to be necessary to avoid cycles once we instantiate C++ templates
from rustc.bzl.
PiperOrigin-RevId: 474581372
diff --git a/rs_bindings_from_cc/bazel_support/compile_cc.bzl b/rs_bindings_from_cc/bazel_support/compile_cc.bzl
new file mode 100644
index 0000000..f6b5a9b
--- /dev/null
+++ b/rs_bindings_from_cc/bazel_support/compile_cc.bzl
@@ -0,0 +1,59 @@
+# 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++.
+
+Disclaimer: This project is experimental, under heavy development, and should
+not be used yet.
+"""
+
+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)
+
+ (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,
+ grep_includes = ctx.file._grep_includes,
+ user_compile_flags = attr.copts if hasattr(attr, "copts") else [],
+ 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],
+ )
+
+ return CcInfo(
+ compilation_context = compilation_context,
+ linking_context = linking_context,
+ )