xref: /aosp_15_r20/external/bazelbuild-rules_rust/rust/private/common.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan# Copyright 2021 The Bazel Authors. All rights reserved.
2*d4726bddSHONG Yifan#
3*d4726bddSHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License");
4*d4726bddSHONG Yifan# you may not use this file except in compliance with the License.
5*d4726bddSHONG Yifan# You may obtain a copy of the License at
6*d4726bddSHONG Yifan#
7*d4726bddSHONG Yifan#    http://www.apache.org/licenses/LICENSE-2.0
8*d4726bddSHONG Yifan#
9*d4726bddSHONG Yifan# Unless required by applicable law or agreed to in writing, software
10*d4726bddSHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS,
11*d4726bddSHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d4726bddSHONG Yifan# See the License for the specific language governing permissions and
13*d4726bddSHONG Yifan# limitations under the License.
14*d4726bddSHONG Yifan
15*d4726bddSHONG Yifan"""A resilient API layer wrapping compilation and other logic for Rust rules.
16*d4726bddSHONG Yifan
17*d4726bddSHONG YifanThis module is meant to be used by custom rules that need to compile Rust code
18*d4726bddSHONG Yifanand cannot simply rely on writing a macro that wraps `rust_library`. This module
19*d4726bddSHONG Yifanprovides the lower-level interface to Rust providers, actions, and functions.
20*d4726bddSHONG YifanDo not load this file directly; instead, load the top-level `defs.bzl` file,
21*d4726bddSHONG Yifanwhich exports the `rust_common` struct.
22*d4726bddSHONG Yifan
23*d4726bddSHONG YifanIn the Bazel lingo, `rust_common` gives the access to the Rust Sandwich API.
24*d4726bddSHONG Yifan"""
25*d4726bddSHONG Yifan
26*d4726bddSHONG Yifanload(":providers.bzl", "CrateGroupInfo", "CrateInfo", "DepInfo", "DepVariantInfo", "StdLibInfo", "TestCrateInfo")
27*d4726bddSHONG Yifan
28*d4726bddSHONG Yifan# This constant only represents the default value for attributes and macros
29*d4726bddSHONG Yifan# defined in `rules_rust`. Like any attribute public attribute, it can be
30*d4726bddSHONG Yifan# overwritten by the user on the rules they're defined on.
31*d4726bddSHONG Yifan#
32*d4726bddSHONG Yifan# Note: Code in `.github/workflows/crate_universe.yaml` looks for this line, if
33*d4726bddSHONG Yifan# you remove it or change its format, you will also need to update that code.
34*d4726bddSHONG YifanDEFAULT_RUST_VERSION = "1.80.0"
35*d4726bddSHONG Yifan
36*d4726bddSHONG YifanDEFAULT_NIGHTLY_ISO_DATE = "2024-07-25"
37*d4726bddSHONG Yifan
38*d4726bddSHONG Yifandef _create_crate_info(**kwargs):
39*d4726bddSHONG Yifan    """A constructor for a `CrateInfo` provider
40*d4726bddSHONG Yifan
41*d4726bddSHONG Yifan    This function should be used in place of directly creating a `CrateInfo`
42*d4726bddSHONG Yifan    provider to improve API stability.
43*d4726bddSHONG Yifan
44*d4726bddSHONG Yifan    Args:
45*d4726bddSHONG Yifan        **kwargs: An inital set of keyword arguments.
46*d4726bddSHONG Yifan
47*d4726bddSHONG Yifan    Returns:
48*d4726bddSHONG Yifan        CrateInfo: A provider
49*d4726bddSHONG Yifan    """
50*d4726bddSHONG Yifan    if not "wrapped_crate_type" in kwargs:
51*d4726bddSHONG Yifan        kwargs.update({"wrapped_crate_type": None})
52*d4726bddSHONG Yifan    if not "metadata" in kwargs:
53*d4726bddSHONG Yifan        kwargs.update({"metadata": None})
54*d4726bddSHONG Yifan    if not "rustc_rmeta_output" in kwargs:
55*d4726bddSHONG Yifan        kwargs.update({"rustc_rmeta_output": None})
56*d4726bddSHONG Yifan    if not "rustc_output" in kwargs:
57*d4726bddSHONG Yifan        kwargs.update({"rustc_output": None})
58*d4726bddSHONG Yifan    if not "rustc_env_files" in kwargs:
59*d4726bddSHONG Yifan        kwargs.update({"rustc_env_files": []})
60*d4726bddSHONG Yifan    if not "data" in kwargs:
61*d4726bddSHONG Yifan        kwargs.update({"data": depset([])})
62*d4726bddSHONG Yifan    return CrateInfo(**kwargs)
63*d4726bddSHONG Yifan
64*d4726bddSHONG Yifanrust_common = struct(
65*d4726bddSHONG Yifan    create_crate_info = _create_crate_info,
66*d4726bddSHONG Yifan    crate_info = CrateInfo,
67*d4726bddSHONG Yifan    dep_info = DepInfo,
68*d4726bddSHONG Yifan    dep_variant_info = DepVariantInfo,
69*d4726bddSHONG Yifan    stdlib_info = StdLibInfo,
70*d4726bddSHONG Yifan    test_crate_info = TestCrateInfo,
71*d4726bddSHONG Yifan    crate_group_info = CrateGroupInfo,
72*d4726bddSHONG Yifan    default_version = DEFAULT_RUST_VERSION,
73*d4726bddSHONG Yifan)
74*d4726bddSHONG Yifan
75*d4726bddSHONG YifanCOMMON_PROVIDERS = [
76*d4726bddSHONG Yifan    CrateInfo,
77*d4726bddSHONG Yifan    DepInfo,
78*d4726bddSHONG Yifan    DefaultInfo,
79*d4726bddSHONG Yifan]
80