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 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame^] | 26 | load(":providers.bzl", "CrateInfo", "DepInfo", "StdLibInfo", "TestCrateInfo") |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 27 | |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame^] | 28 | # This constant only represents the default value for attributes and macros |
| 29 | # defined in `rules_rust`. Like any attribute public attribute, it can be |
| 30 | # overwritten by the user on the rules they're defined on. |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 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. |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame^] | 34 | DEFAULT_RUST_VERSION = "1.62.1" |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 35 | |
| 36 | def _create_crate_info(**kwargs): |
| 37 | """A constructor for a `CrateInfo` provider |
| 38 | |
| 39 | This function should be used in place of directly creating a `CrateInfo` |
| 40 | provider to improve API stability. |
| 41 | |
| 42 | Args: |
| 43 | **kwargs: An inital set of keyword arguments. |
| 44 | |
| 45 | Returns: |
| 46 | CrateInfo: A provider |
| 47 | """ |
| 48 | if not "wrapped_crate_type" in kwargs: |
| 49 | kwargs.update({"wrapped_crate_type": None}) |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame^] | 50 | if not "metadata" in kwargs: |
| 51 | kwargs.update({"metadata": None}) |
| 52 | if not "rustc_env_files" in kwargs: |
| 53 | kwargs.update({"rustc_env_files": []}) |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 54 | return CrateInfo(**kwargs) |
| 55 | |
| 56 | rust_common = struct( |
| 57 | create_crate_info = _create_crate_info, |
| 58 | crate_info = CrateInfo, |
| 59 | dep_info = DepInfo, |
| 60 | stdlib_info = StdLibInfo, |
Brian Silverman | 5f6f276 | 2022-08-13 19:30:05 -0700 | [diff] [blame^] | 61 | test_crate_info = TestCrateInfo, |
Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 62 | default_version = DEFAULT_RUST_VERSION, |
| 63 | ) |