blob: 468d7938c0e5b3d9ad32e5d24dd60269b2dff588 [file] [log] [blame]
Marcel Hlopkodaf07032022-09-15 09:26:51 -07001# Part of the Crubit project, under the Apache License v2.0 with LLVM
2# Exceptions. See /LICENSE for license information.
3# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5"""Utility module for sharing logic between rules and aspects that generate Rust bindings from C++.
6
7Disclaimer: This project is experimental, under heavy development, and should
8not be used yet.
9"""
10
11def compile_cc(
12 ctx,
13 attr,
14 cc_toolchain,
15 feature_configuration,
16 src,
17 cc_infos,
18 extra_cc_compilation_action_inputs):
19 """Compiles a C++ source file.
20
21 Args:
22 ctx: The rule context.
23 attr: The current rule's attributes.
24 cc_toolchain: A cc_toolchain.
25 feature_configuration: A feature configuration.
26 src: The source file to be compiled.
27 cc_infos: List[CcInfo]: A list of CcInfo dependencies.
28 extra_cc_compilation_action_inputs: A list of input files for the C++ compilation action.
29
30 Returns:
31 A CcInfo provider.
32 """
33 cc_info = cc_common.merge_cc_infos(cc_infos = cc_infos)
34
Jing Lu0abff842023-10-18 07:28:26 -070035 user_copts = []
36 for copt in getattr(attr, "copts", []):
37 # ctx.expand_make_variables is deprecated, but its replacement ctx.var does not suffice.
38 user_copts.append(ctx.expand_make_variables("copts", copt, {}))
39
Marcel Hlopkodaf07032022-09-15 09:26:51 -070040 (compilation_context, compilation_outputs) = cc_common.compile(
41 name = src.basename,
42 actions = ctx.actions,
43 feature_configuration = feature_configuration,
44 cc_toolchain = cc_toolchain,
45 srcs = [src],
46 additional_inputs = extra_cc_compilation_action_inputs,
Jing Lu0abff842023-10-18 07:28:26 -070047 user_compile_flags = user_copts,
Marcel Hlopkodaf07032022-09-15 09:26:51 -070048 compilation_contexts = [cc_info.compilation_context],
49 )
50
51 (linking_context, _) = cc_common.create_linking_context_from_compilation_outputs(
52 name = src.basename,
53 actions = ctx.actions,
54 feature_configuration = feature_configuration,
55 cc_toolchain = cc_toolchain,
56 compilation_outputs = compilation_outputs,
57 linking_contexts = [cc_info.linking_context],
58 )
59
60 return CcInfo(
61 compilation_context = compilation_context,
62 linking_context = linking_context,
63 )