blob: 025c4d76a1c075c026903a46e9605b071b8e516a [file] [log] [blame]
Brian Silvermancc09f182022-03-09 15:40:20 -08001# 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
17This module is meant to be used by custom rules that need to compile Rust code
18and cannot simply rely on writing a macro that wraps `rust_library`. This module
19provides the lower-level interface to Rust providers, actions, and functions.
20Do not load this file directly; instead, load the top-level `defs.bzl` file,
21which exports the `rust_common` struct.
22
23In the Bazel lingo, `rust_common` gives the access to the Rust Sandwich API.
24"""
25
Brian Silverman5f6f2762022-08-13 19:30:05 -070026load(":providers.bzl", "CrateInfo", "DepInfo", "StdLibInfo", "TestCrateInfo")
Brian Silvermancc09f182022-03-09 15:40:20 -080027
Brian Silverman5f6f2762022-08-13 19:30:05 -070028# 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 Silvermancc09f182022-03-09 15:40:20 -080031#
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 Silverman5f6f2762022-08-13 19:30:05 -070034DEFAULT_RUST_VERSION = "1.62.1"
Brian Silvermancc09f182022-03-09 15:40:20 -080035
36def _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 Silverman5f6f2762022-08-13 19:30:05 -070050 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 Silvermancc09f182022-03-09 15:40:20 -080054 return CrateInfo(**kwargs)
55
56rust_common = struct(
57 create_crate_info = _create_crate_info,
58 crate_info = CrateInfo,
59 dep_info = DepInfo,
60 stdlib_info = StdLibInfo,
Brian Silverman5f6f2762022-08-13 19:30:05 -070061 test_crate_info = TestCrateInfo,
Brian Silvermancc09f182022-03-09 15:40:20 -080062 default_version = DEFAULT_RUST_VERSION,
63)