fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

Roman Kashitsynfeat: build script toolchains annotations (#1344)e48bec94

0.11.0
8.0 KiB178 linesraw
1"""Macros used for represeting crates or annotations for existing crates"""
2
3def _workspace_member(version, sha256 = None):
4    """Define information for extra workspace members
5
6    Args:
7        version (str): The semver of the crate to download. Must be an exact version.
8        sha256 (str, optional): The sha256 checksum of the `.crate` file.
9
10    Returns:
11        string: A json encoded string of all inputs
12    """
13    return json.encode(struct(
14        version = version,
15        sha256 = sha256,
16    ))
17
18def _spec(
19        package = None,
20        version = None,
21        default_features = True,
22        features = [],
23        git = None,
24        rev = None):
25    """A constructor for a crate dependency.
26
27    See [specifying dependencies][sd] in the Cargo book for more details.
28
29    [sd]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
30
31    Args:
32        package (str, optional): The explicit name of the package (used when attempting to alias a crate).
33        version (str, optional): The exact version of the crate. Cannot be used with `git`.
34        default_features (bool, optional): Maps to the `default-features` flag.
35        features (list, optional): A list of features to use for the crate
36        git (str, optional): The Git url to use for the crate. Cannot be used with `version`.
37        rev (str, optional): The git revision of the remote crate. Tied with the `git` param.
38
39    Returns:
40        string: A json encoded string of all inputs
41    """
42    return json.encode(struct(
43        package = package,
44        default_features = default_features,
45        features = features,
46        version = version,
47        git = git,
48        rev = rev,
49    ))
50
51def _assert_absolute(label):
52    """Ensure a given label is an absolute label
53
54    Args:
55        label (Label): The label to check
56    """
57    label_str = str(label)
58    if not label.startswith("@"):
59        fail("The labels must be absolute. Please update '{}'".format(
60            label_str,
61        ))
62
63def _annotation(
64        version = "*",
65        additive_build_file = None,
66        additive_build_file_content = None,
67        build_script_data = None,
68        build_script_tools = None,
69        build_script_data_glob = None,
70        build_script_deps = None,
71        build_script_env = None,
72        build_script_proc_macro_deps = None,
73        build_script_rustc_env = None,
74        build_script_toolchains = None,
75        compile_data = None,
76        compile_data_glob = None,
77        crate_features = None,
78        data = None,
79        data_glob = None,
80        deps = None,
81        gen_build_script = None,
82        patch_args = None,
83        patch_tool = None,
84        patches = None,
85        proc_macro_deps = None,
86        rustc_env = None,
87        rustc_env_files = None,
88        rustc_flags = None,
89        shallow_since = None):
90    """A collection of extra attributes and settings for a particular crate
91
92    Args:
93        version (str, optional): The version or semver-conditions to match with a crate.
94        additive_build_file_content (str, optional): Extra contents to write to the bottom of generated BUILD files.
95        additive_build_file (str, optional): A file containing extra contents to write to the bottom of
96            generated BUILD files.
97        build_script_data (list, optional): A list of labels to add to a crate's `cargo_build_script::data` attribute.
98        build_script_tools (list, optional): A list of labels to add to a crate's `cargo_build_script::tools` attribute.
99        build_script_data_glob (list, optional): A list of glob patterns to add to a crate's `cargo_build_script::data`
100            attribute.
101        build_script_deps (list, optional): A list of labels to add to a crate's `cargo_build_script::deps` attribute.
102        build_script_env (dict, optional): Additional environment variables to set on a crate's
103            `cargo_build_script::env` attribute.
104        build_script_proc_macro_deps (list, optional): A list of labels to add to a crate's
105            `cargo_build_script::proc_macro_deps` attribute.
106        build_script_rustc_env (dict, optional): Additional environment variables to set on a crate's
107            `cargo_build_script::env` attribute.
108        build_script_toolchains (list, optional): A list of labels to set on a crates's `cargo_build_script::toolchains` attribute.
109        compile_data (list, optional): A list of labels to add to a crate's `rust_library::compile_data` attribute.
110        compile_data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::compile_data`
111            attribute.
112        crate_features (list, optional): A list of strings to add to a crate's `rust_library::crate_features`
113            attribute.
114        data (list, optional): A list of labels to add to a crate's `rust_library::data` attribute.
115        data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::data` attribute.
116        deps (list, optional): A list of labels to add to a crate's `rust_library::deps` attribute.
117        gen_build_script (bool, optional): An authorative flag to determine whether or not to produce
118            `cargo_build_script` targets for the current crate.
119        patch_args (list, optional): The `patch_args` attribute of a Bazel repository rule. See
120            [http_archive.patch_args](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_args)
121        patch_tool (list, optional): The `patch_tool` attribute of a Bazel repository rule. See
122            [http_archive.patch_tool](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_tool)
123        patches (list, optional): The `patches` attribute of a Bazel repository rule. See
124            [http_archive.patches](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patches)
125        proc_macro_deps (list, optional): A list of labels to add to a crate's `rust_library::proc_macro_deps`
126            attribute.
127        rustc_env (dict, optional): Additional variables to set on a crate's `rust_library::rustc_env` attribute.
128        rustc_env_files (list, optional): A list of labels to set on a crate's `rust_library::rustc_env_files`
129            attribute.
130        rustc_flags (list, optional): A list of strings to set on a crate's `rust_library::rustc_flags` attribute.
131        shallow_since (str, optional): An optional timestamp used for crates originating from a git repository
132            instead of a crate registry. This flag optimizes fetching the source code.
133
134    Returns:
135        string: A json encoded string containing the specified version and separately all other inputs.
136    """
137    if additive_build_file:
138        _assert_absolute(additive_build_file)
139    if patches:
140        for patch in patches:
141            _assert_absolute(patch)
142
143    return json.encode((
144        version,
145        struct(
146            additive_build_file = additive_build_file,
147            additive_build_file_content = additive_build_file_content,
148            build_script_data = build_script_data,
149            build_script_tools = build_script_tools,
150            build_script_data_glob = build_script_data_glob,
151            build_script_deps = build_script_deps,
152            build_script_env = build_script_env,
153            build_script_proc_macro_deps = build_script_proc_macro_deps,
154            build_script_rustc_env = build_script_rustc_env,
155            build_script_toolchains = build_script_toolchains,
156            compile_data = compile_data,
157            compile_data_glob = compile_data_glob,
158            crate_features = crate_features,
159            data = data,
160            data_glob = data_glob,
161            deps = deps,
162            gen_build_script = gen_build_script,
163            patch_args = patch_args,
164            patch_tool = patch_tool,
165            patches = patches,
166            proc_macro_deps = proc_macro_deps,
167            rustc_env = rustc_env,
168            rustc_env_files = rustc_env_files,
169            rustc_flags = rustc_flags,
170            shallow_since = shallow_since,
171        ),
172    ))
173
174crate = struct(
175    spec = _spec,
176    annotation = _annotation,
177    workspace_member = _workspace_member,
178)