fork/rules_rust

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

Leopold Schabelcrate_universe: fix typo (#1314)aca4ec1a

0.5.0
8.9 KiB259 linesraw
1"""Utilities directly related to the `splicing` step of `cargo-bazel`."""
2
3load(":common_utils.bzl", "cargo_environ", "execute")
4
5CARGO_BAZEL_DEBUG = "CARGO_BAZEL_DEBUG"
6
7def splicing_config(resolver_version = "1"):
8    """Various settings used to configure Cargo manifest splicing behavior.
9
10    [rv]: https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
11
12    Args:
13        resolver_version (str, optional): The [resolver version][rv] to use in generated Cargo
14            manifests. This flag is **only** used when splicing a manifest from direct package
15            definitions. See `crates_repository::packages`.
16
17    Returns:
18        str: A json encoded string of the parameters provided
19    """
20    return json.encode(struct(
21        resolver_version = resolver_version,
22    ))
23
24def download_extra_workspace_members(repository_ctx, cache_dir, render_template_registry_url):
25    """Download additional workspace members for use in splicing.
26
27    Args:
28        repository_ctx (repository_ctx): The rule's context object.
29        cache_dir (path): A directory in which to download and extract extra workspace members
30        render_template_registry_url (str): The base template to use for determining the crate's registry URL.
31
32    Returns:
33        list: A list of information related to the downloaded crates
34            - manifest: The path of the manifest.
35            - url: The url the manifest came from.
36            - sha256: The sha256 checksum of the new manifest.
37    """
38    manifests = []
39    extra_workspace_members = repository_ctx.attr.extra_workspace_members
40    if extra_workspace_members:
41        repository_ctx.report_progress("Downloading extra workspace members.")
42
43    for name, spec in repository_ctx.attr.extra_workspace_members.items():
44        spec = struct(**json.decode(spec))
45
46        url = render_template_registry_url
47        url = url.replace("{name}", name)
48        url = url.replace("{version}", spec.version)
49
50        if spec.sha256:
51            result = repository_ctx.download_and_extract(
52                output = cache_dir,
53                url = url,
54                sha256 = spec.sha256,
55                type = "tar.gz",
56            )
57        else:
58            result = repository_ctx.download_and_extract(
59                output = cache_dir,
60                url = url,
61                type = "tar.gz",
62            )
63
64        manifest = repository_ctx.path("{}/{}-{}/Cargo.toml".format(
65            cache_dir,
66            name,
67            spec.version,
68        ))
69
70        if not manifest.exists:
71            fail("Extra workspace member '{}' has no root Cargo.toml file".format(name))
72
73        manifests.append(struct(
74            manifest = str(manifest),
75            url = url,
76            sha256 = result.sha256,
77        ))
78
79    return manifests
80
81def kebab_case_keys(data):
82    """Ensure the key value of the data given are kebab-case
83
84    Args:
85        data (dict): A deserialized json blob
86
87    Returns:
88        dict: The same `data` but with kebab-case keys
89    """
90    return {
91        key.lower().replace("_", "-"): val
92        for (key, val) in data.items()
93    }
94
95def compile_splicing_manifest(splicing_config, manifests, cargo_config_path, packages):
96    """Produce a manifest containing required components for splciing a new Cargo workspace
97
98    [cargo_config]: https://doc.rust-lang.org/cargo/reference/config.html
99    [cargo_toml]: https://doc.rust-lang.org/cargo/reference/manifest.html
100
101    Args:
102        splicing_config (dict): A deserialized `splicing_config`
103        manifests (dict): A mapping of paths to Bazel labels which represent [Cargo manifests][cargo_toml].
104        cargo_config_path (str): The absolute path to a [Cargo config][cargo_config].
105        packages (dict): A set of crates (packages) specifications to depend on
106
107    Returns:
108        dict: A dictionary representation of a `cargo_bazel::splicing::SplicingManifest`
109    """
110
111    # Deserialize information about direct packges
112    direct_packages_info = {
113        # Ensure the data is using kebab-case as that's what `cargo_toml::DependencyDetail` expects.
114        pkg: kebab_case_keys(dict(json.decode(data)))
115        for (pkg, data) in packages.items()
116    }
117
118    # Auto-generated splicier manifest values
119    splicing_manifest_content = {
120        "cargo_config": cargo_config_path,
121        "direct_packages": direct_packages_info,
122        "manifests": manifests,
123    }
124
125    return dict(splicing_config.items() + splicing_manifest_content.items())
126
127def create_splicing_manifest(repository_ctx):
128    """Produce a manifest containing required components for splciing a new Cargo workspace
129
130    Args:
131        repository_ctx (repository_ctx): The rule's context object.
132
133    Returns:
134        path: The path to a json encoded manifest
135    """
136
137    manifests = {str(repository_ctx.path(m)): str(m) for m in repository_ctx.attr.manifests}
138
139    if repository_ctx.attr.cargo_config:
140        cargo_config = str(repository_ctx.path(repository_ctx.attr.cargo_config))
141    else:
142        cargo_config = None
143
144    # Load user configurable splicing settings
145    config = json.decode(repository_ctx.attr.splicing_config or splicing_config())
146
147    repo_dir = repository_ctx.path(".")
148
149    splicing_manifest = repository_ctx.path("{}/splicing_manifest.json".format(repo_dir))
150
151    data = compile_splicing_manifest(
152        splicing_config = config,
153        manifests = manifests,
154        cargo_config_path = cargo_config,
155        packages = repository_ctx.attr.packages,
156    )
157
158    # Serialize information required for splicing
159    repository_ctx.file(
160        splicing_manifest,
161        json.encode_indent(
162            data,
163            indent = " " * 4,
164        ),
165    )
166
167    return splicing_manifest
168
169def splice_workspace_manifest(repository_ctx, generator, lockfile, splicing_manifest, cargo, rustc):
170    """Splice together a Cargo workspace from various other manifests and package definitions
171
172    Args:
173        repository_ctx (repository_ctx): The rule's context object.
174        generator (path): The `cargo-bazel` binary.
175        lockfile (path): The path to a "lock" file for reproducible `cargo-bazel` renderings.
176        splicing_manifest (path): The path to a splicing manifest.
177        cargo (path): The path to a Cargo binary.
178        rustc (path): The Path to a Rustc binary.
179
180    Returns:
181        path: The path to a Cargo metadata json file found in the spliced workspace root.
182    """
183    repository_ctx.report_progress("Splicing Cargo workspace.")
184    repo_dir = repository_ctx.path(".")
185
186    # Download extra workspace members
187    crates_cache_dir = repository_ctx.path("{}/.crates_cache".format(repo_dir))
188    extra_manifest_info = download_extra_workspace_members(
189        repository_ctx = repository_ctx,
190        cache_dir = crates_cache_dir,
191        render_template_registry_url = repository_ctx.attr.extra_workspace_member_url_template,
192    )
193
194    extra_manifests_manifest = repository_ctx.path("{}/extra_manifests_manifest.json".format(repo_dir))
195    repository_ctx.file(
196        extra_manifests_manifest,
197        json.encode_indent(struct(
198            manifests = extra_manifest_info,
199        ), indent = " " * 4),
200    )
201
202    splicing_output_dir = repository_ctx.path("splicing-output")
203
204    # Generate a workspace root which contains all workspace members
205    arguments = [
206        generator,
207        "splice",
208        "--output-dir",
209        splicing_output_dir,
210        "--splicing-manifest",
211        splicing_manifest,
212        "--extra-manifests-manifest",
213        extra_manifests_manifest,
214        "--cargo",
215        cargo,
216        "--rustc",
217        rustc,
218    ]
219
220    # Optionally set the splicing workspace directory to somewhere within the repository directory
221    # to improve the debugging experience.
222    if CARGO_BAZEL_DEBUG in repository_ctx.os.environ:
223        arguments.extend([
224            "--workspace-dir",
225            repository_ctx.path("{}/splicing-workspace".format(repo_dir)),
226        ])
227
228    # Splicing accepts a Cargo.lock file in some scenarios. Ensure it's passed
229    # if the lockfile is a actually a Cargo lockfile.
230    if lockfile.kind == "cargo":
231        arguments.extend([
232            "--cargo-lockfile",
233            lockfile.path,
234        ])
235
236    env = {
237        "CARGO": str(cargo),
238        "RUSTC": str(rustc),
239        "RUST_BACKTRACE": "full",
240    }
241
242    # Add any Cargo environment variables to the `cargo-bazel` execution
243    env.update(cargo_environ(repository_ctx))
244
245    execute(
246        repository_ctx = repository_ctx,
247        args = arguments,
248        env = env,
249    )
250
251    # This file must have been produced by the execution above.
252    spliced_lockfile = repository_ctx.path("{}/Cargo.lock".format(splicing_output_dir))
253    if not spliced_lockfile.exists:
254        fail("Lockfile file does not exist: {}".format(spliced_lockfile))
255    spliced_metadata = repository_ctx.path("{}/metadata.json".format(splicing_output_dir))
256    if not spliced_metadata.exists:
257        fail("Metadata file does not exist: {}".format(spliced_metadata))
258
259    return spliced_metadata