Marcel Hlopko | daf0703 | 2022-09-15 09:26:51 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | Disclaimer: This project is experimental, under heavy development, and should |
| 8 | not be used yet. |
| 9 | """ |
| 10 | |
| 11 | def 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 Lu | 0abff84 | 2023-10-18 07:28:26 -0700 | [diff] [blame] | 35 | 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 Hlopko | daf0703 | 2022-09-15 09:26:51 -0700 | [diff] [blame] | 40 | (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 Lu | 0abff84 | 2023-10-18 07:28:26 -0700 | [diff] [blame] | 47 | user_compile_flags = user_copts, |
Marcel Hlopko | daf0703 | 2022-09-15 09:26:51 -0700 | [diff] [blame] | 48 | 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 | ) |