Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame^] | 1 | # Copyright 2021 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """A resilient API layer wrapping compilation and other logic for Rust rules. |
| 16 | |
| 17 | This module is meant to be used by custom rules that need to compile Rust code |
| 18 | and cannot simply rely on writing a macro that wraps `rust_library`. This module |
| 19 | provides the lower-level interface to Rust providers, actions, and functions. |
| 20 | Do not load this file directly; instead, load the top-level `defs.bzl` file, |
| 21 | which exports the `rust_common` struct. |
| 22 | |
| 23 | In the Bazel lingo, `rust_common` gives the access to the Rust Sandwich API. |
| 24 | """ |
| 25 | |
| 26 | load(":providers.bzl", "CrateInfo", "DepInfo", "StdLibInfo") |
| 27 | |
| 28 | # These constants only represent the default value for attributes and macros |
| 29 | # defiend in `rules_rust`. Like any attribute public attribute, they can be |
| 30 | # overwritten by the user on the rules they're defiend on. |
| 31 | # |
| 32 | # Note: Code in `.github/workflows/crate_universe.yaml` looks for this line, if |
| 33 | # you remove it or change its format, you will also need to update that code. |
| 34 | DEFAULT_RUST_VERSION = "1.59.0" |
| 35 | DEFAULT_RUST_EDITION = "2018" |
| 36 | |
| 37 | def _create_crate_info(**kwargs): |
| 38 | """A constructor for a `CrateInfo` provider |
| 39 | |
| 40 | This function should be used in place of directly creating a `CrateInfo` |
| 41 | provider to improve API stability. |
| 42 | |
| 43 | Args: |
| 44 | **kwargs: An inital set of keyword arguments. |
| 45 | |
| 46 | Returns: |
| 47 | CrateInfo: A provider |
| 48 | """ |
| 49 | if not "wrapped_crate_type" in kwargs: |
| 50 | kwargs.update({"wrapped_crate_type": None}) |
| 51 | return CrateInfo(**kwargs) |
| 52 | |
| 53 | rust_common = struct( |
| 54 | create_crate_info = _create_crate_info, |
| 55 | crate_info = CrateInfo, |
| 56 | dep_info = DepInfo, |
| 57 | stdlib_info = StdLibInfo, |
| 58 | default_edition = DEFAULT_RUST_EDITION, |
| 59 | default_version = DEFAULT_RUST_VERSION, |
| 60 | ) |