fork/rules_rust

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

UebelAndrecrate_universe support for individually updating packages. (#1426)3d65214d

0.7.0
16.2 KiB445 linesraw
1"""Utilities directly related to the `generate` step of `cargo-bazel`."""
2
3load(":common_utils.bzl", "CARGO_BAZEL_ISOLATED", "REPIN_ENV_VARS", "cargo_environ", "execute")
4
5CARGO_BAZEL_GENERATOR_SHA256 = "CARGO_BAZEL_GENERATOR_SHA256"
6CARGO_BAZEL_GENERATOR_URL = "CARGO_BAZEL_GENERATOR_URL"
7
8GENERATOR_ENV_VARS = [
9    CARGO_BAZEL_GENERATOR_URL,
10    CARGO_BAZEL_GENERATOR_SHA256,
11]
12
13CRATES_REPOSITORY_ENVIRON = GENERATOR_ENV_VARS + REPIN_ENV_VARS + [
14    CARGO_BAZEL_ISOLATED,
15]
16
17def get_generator(repository_ctx, host_triple):
18    """Query network resources to locate a `cargo-bazel` binary
19
20    Args:
21        repository_ctx (repository_ctx): The rule's context object.
22        host_triple (string): A string representing the host triple
23
24    Returns:
25        tuple(path, dict): The path to a `cargo-bazel` binary and the host sha256 pairing.
26            The pairing (dict) may be `None` if there is no need to update the attribute
27    """
28    use_environ = False
29    for var in GENERATOR_ENV_VARS:
30        if var in repository_ctx.os.environ:
31            use_environ = True
32
33    output = repository_ctx.path("cargo-bazel.exe" if "win" in repository_ctx.os.name else "cargo-bazel")
34
35    # The `generator` attribute is the next highest priority behind
36    # environment variables. We check those first before deciding to
37    # use an explicitly provided variable.
38    if not use_environ and repository_ctx.attr.generator:
39        generator = repository_ctx.path(Label(repository_ctx.attr.generator))
40
41        # Resolve a few levels of symlinks to ensure we're accessing the direct binary
42        for _ in range(1, 100):
43            real_generator = generator.realpath
44            if real_generator == generator:
45                break
46            generator = real_generator
47        return generator, None
48
49    # The environment variable will take precedence if set
50    if use_environ:
51        generator_sha256 = repository_ctx.os.environ.get(CARGO_BAZEL_GENERATOR_SHA256)
52        generator_url = repository_ctx.os.environ.get(CARGO_BAZEL_GENERATOR_URL)
53    else:
54        generator_sha256 = repository_ctx.attr.generator_sha256s.get(host_triple)
55        generator_url = repository_ctx.attr.generator_urls.get(host_triple)
56
57    if not generator_url:
58        fail((
59            "No generator URL was found either in the `CARGO_BAZEL_GENERATOR_URL` " +
60            "environment variable or for the `{}` triple in the `generator_urls` attribute"
61        ).format(host_triple))
62
63    # Download the file into place
64    if generator_sha256:
65        repository_ctx.download(
66            output = output,
67            url = generator_url,
68            sha256 = generator_sha256,
69            executable = True,
70        )
71        return output, None
72
73    result = repository_ctx.download(
74        output = output,
75        url = generator_url,
76        executable = True,
77    )
78
79    return output, {host_triple: result.sha256}
80
81def render_config(
82        build_file_template = "//:BUILD.{name}-{version}.bazel",
83        crate_label_template = "@{repository}__{name}-{version}//:{target}",
84        crate_repository_template = "{repository}__{name}-{version}",
85        crates_module_template = "//:{file}",
86        default_package_name = None,
87        platforms_template = "@rules_rust//rust/platform:{triple}",
88        vendor_mode = None):
89    """Various settings used to configure rendered outputs
90
91    The template parameters each support a select number of format keys. A description of each key
92    can be found below where the supported keys for each template can be found in the parameter docs
93
94    | key | definition |
95    | --- | --- |
96    | `name` | The name of the crate. Eg `tokio` |
97    | `repository` | The rendered repository name for the crate. Directly relates to `crate_repository_template`. |
98    | `triple` | A platform triple. Eg `x86_64-unknown-linux-gnu` |
99    | `version` | The crate version. Eg `1.2.3` |
100    | `target` | The library or binary target of the crate |
101    | `file` | The basename of a file |
102
103    Args:
104        build_file_template (str, optional): The base template to use for BUILD file names. The available format keys
105            are [`{name}`, {version}`].
106        crate_label_template (str, optional): The base template to use for crate labels. The available format keys
107            are [`{repository}`, `{name}`, `{version}`, `{target}`].
108        crate_repository_template (str, optional): The base template to use for Crate label repository names. The
109            available format keys are [`{repository}`, `{name}`, `{version}`].
110        crates_module_template (str, optional): The pattern to use for the `defs.bzl` and `BUILD.bazel`
111            file names used for the crates module. The available format keys are [`{file}`].
112        default_package_name (str, optional): The default package name to use in the rendered macros. This affects the
113            auto package detection of things like `all_crate_deps`.
114        platforms_template (str, optional): The base template to use for platform names.
115            See [platforms documentation](https://docs.bazel.build/versions/main/platforms.html). The available format
116            keys are [`{triple}`].
117        vendor_mode (str, optional): An optional configuration for rendirng content to be rendered into repositories.
118
119    Returns:
120        string: A json encoded struct to match the Rust `config::RenderConfig` struct
121    """
122    return json.encode(struct(
123        build_file_template = build_file_template,
124        crate_label_template = crate_label_template,
125        crate_repository_template = crate_repository_template,
126        crates_module_template = crates_module_template,
127        default_package_name = default_package_name,
128        platforms_template = platforms_template,
129        vendor_mode = vendor_mode,
130    ))
131
132def _crate_id(name, version):
133    """Creates a `cargo_bazel::config::CrateId`.
134
135    Args:
136        name (str): The name of the crate
137        version (str): The crate's version
138
139    Returns:
140        str: A serialized representation of a CrateId
141    """
142    return "{} {}".format(name, version)
143
144def collect_crate_annotations(annotations, repository_name):
145    """Deserialize and sanitize crate annotations.
146
147    Args:
148        annotations (dict): A mapping of crate names to lists of serialized annotations
149        repository_name (str): The name of the repository that owns the annotations
150
151    Returns:
152        dict: A mapping of `cargo_bazel::config::CrateId` to sets of annotations
153    """
154    annotations = {name: [json.decode(a) for a in annotation] for name, annotation in annotations.items()}
155    crate_annotations = {}
156    for name, annotation in annotations.items():
157        for (version, data) in annotation:
158            if name == "*" and version != "*":
159                fail(
160                    "Wildcard crate names must have wildcard crate versions. " +
161                    "Please update the `annotations` attribute of the {} crates_repository".format(
162                        repository_name,
163                    ),
164                )
165            id = _crate_id(name, version)
166            if id in crate_annotations:
167                fail("Found duplicate entries for {}".format(id))
168
169            crate_annotations.update({id: data})
170    return crate_annotations
171
172def _read_cargo_config(repository_ctx):
173    if repository_ctx.attr.cargo_config:
174        config = repository_ctx.path(repository_ctx.attr.cargo_config)
175        return repository_ctx.read(config)
176    return None
177
178def _update_render_config(config, repository_name):
179    """Add the repository name to the render config
180
181    Args:
182        config (dict): A `render_config` struct
183        repository_name (str): The name of the repository that owns the config
184
185    Returns:
186        struct: An updated `render_config`.
187    """
188
189    # Add the repository name as it's very relevant to rendering.
190    config.update({"repository_name": repository_name})
191
192    return struct(**config)
193
194def _get_render_config(repository_ctx):
195    if repository_ctx.attr.render_config:
196        config = dict(json.decode(repository_ctx.attr.render_config))
197    else:
198        config = dict(json.decode(render_config()))
199
200    return config
201
202def compile_config(crate_annotations, generate_build_scripts, cargo_config, render_config, supported_platform_triples, repository_name, repository_ctx = None):
203    """Create a config file for generating crate targets
204
205    [cargo_config]: https://doc.rust-lang.org/cargo/reference/config.html
206
207    Args:
208        crate_annotations (dict): Extra settings to apply to crates. See
209            `crates_repository.annotations` or `crates_vendor.annotations`.
210        generate_build_scripts (bool): Whether or not to globally disable build scripts.
211        cargo_config (str): The optional contents of a [Cargo config][cargo_config].
212        render_config (dict): The deserialized dict of the `render_config` function.
213        supported_platform_triples (list): A list of platform triples
214        repository_name (str): The name of the repository being generated
215        repository_ctx (repository_ctx, optional): A repository context object used for enabling
216            certain functionality.
217
218    Returns:
219        struct: A struct matching a `cargo_bazel::config::Config`.
220    """
221    annotations = collect_crate_annotations(crate_annotations, repository_name)
222
223    # Load additive build files if any have been provided.
224    unexpected = []
225    for name, data in annotations.items():
226        f = data.pop("additive_build_file", None)
227        if f and not repository_ctx:
228            unexpected.append(name)
229            f = None
230        content = [x for x in [
231            data.pop("additive_build_file_content", None),
232            repository_ctx.read(Label(f)) if f else None,
233        ] if x]
234        if content:
235            data.update({"additive_build_file_content": "\n".join(content)})
236
237    if unexpected:
238        fail("The following annotations use `additive_build_file` which is not supported for {}: {}".format(repository_name, unexpected))
239
240    config = struct(
241        generate_build_scripts = generate_build_scripts,
242        annotations = annotations,
243        cargo_config = cargo_config,
244        rendering = _update_render_config(
245            config = render_config,
246            repository_name = repository_name,
247        ),
248        supported_platform_triples = supported_platform_triples,
249    )
250
251    return config
252
253def generate_config(repository_ctx):
254    """Generate a config file from various attributes passed to the rule.
255
256    Args:
257        repository_ctx (repository_ctx): The rule's context object.
258
259    Returns:
260        struct: A struct containing the path to a config and it's contents
261    """
262
263    config = compile_config(
264        crate_annotations = repository_ctx.attr.annotations,
265        generate_build_scripts = repository_ctx.attr.generate_build_scripts,
266        cargo_config = _read_cargo_config(repository_ctx),
267        render_config = _get_render_config(repository_ctx),
268        supported_platform_triples = repository_ctx.attr.supported_platform_triples,
269        repository_name = repository_ctx.name,
270        repository_ctx = repository_ctx,
271    )
272
273    config_path = repository_ctx.path("cargo-bazel.json")
274    repository_ctx.file(
275        config_path,
276        json.encode_indent(config, indent = " " * 4),
277    )
278
279    return config_path
280
281def get_lockfiles(repository_ctx):
282    """_summary_
283
284    Args:
285        repository_ctx (repository_ctx): The rule's context object.
286
287    Returns:
288        struct: _description_
289    """
290    return struct(
291        cargo = repository_ctx.path(repository_ctx.attr.cargo_lockfile),
292        bazel = repository_ctx.path(repository_ctx.attr.lockfile) if repository_ctx.attr.lockfile else None,
293    )
294
295def determine_repin(repository_ctx, generator, lockfile_path, config, splicing_manifest, cargo, rustc):
296    """Use the `cargo-bazel` binary to determine whether or not dpeendencies need to be re-pinned
297
298    Args:
299        repository_ctx (repository_ctx): The rule's context object.
300        generator (path): The path to a `cargo-bazel` binary.
301        config (path): The path to a `cargo-bazel` config file. See `generate_config`.
302        splicing_manifest (path): The path to a `cargo-bazel` splicing manifest. See `create_splicing_manifest`
303        lockfile_path (path): The path to a "lock" file for reproducible outputs.
304        cargo (path): The path to a Cargo binary.
305        rustc (path): The path to a Rustc binary.
306
307    Returns:
308        bool: True if dependencies need to be re-pinned
309    """
310
311    # If a repin environment variable is set, always repin
312    for var in REPIN_ENV_VARS:
313        if var in repository_ctx.os.environ and repository_ctx.os.environ[var].lower() not in ["false", "no", "0", "off"]:
314            return True
315
316    # If a deterministic lockfile was not added then always repin
317    if not lockfile_path or lockfile_path.exists:
318        return True
319
320    # Run the binary to check if a repin is needed
321    args = [
322        generator,
323        "query",
324        "--lockfile",
325        lockfile_path,
326        "--config",
327        config,
328        "--splicing-manifest",
329        splicing_manifest,
330        "--cargo",
331        cargo,
332        "--rustc",
333        rustc,
334    ]
335
336    env = {
337        "CARGO": str(cargo),
338        "RUSTC": str(rustc),
339        "RUST_BACKTRACE": "full",
340    }
341
342    # Add any Cargo environment variables to the `cargo-bazel` execution
343    env.update(cargo_environ(repository_ctx))
344
345    result = execute(
346        repository_ctx = repository_ctx,
347        args = args,
348        env = env,
349    )
350
351    # If it was determined repinning should occur but there was no
352    # flag indicating repinning was requested, an error is raised
353    # since repinning should be an explicit action
354    if result.stdout.strip().lower() == "repin":
355        # buildifier: disable=print
356        print(result.stderr)
357        fail((
358            "The current `lockfile` is out of date for '{}'. Please re-run " +
359            "bazel using `CARGO_BAZEL_REPIN=true` if this is expected " +
360            "and the lockfile should be updated."
361        ).format(repository_ctx.name))
362
363    return False
364
365def execute_generator(
366        repository_ctx,
367        lockfile_path,
368        cargo_lockfile_path,
369        generator,
370        config,
371        splicing_manifest,
372        repository_dir,
373        cargo,
374        rustc,
375        metadata = None):
376    """Execute the `cargo-bazel` binary to produce `BUILD` and `.bzl` files.
377
378    Args:
379        repository_ctx (repository_ctx): The rule's context object.
380        lockfile_path (path): The path to a "lock" file (file used for reproducible renderings).
381        cargo_lockfile_path (path): The path to a "Cargo.lock" file within the root workspace.
382        generator (path): The path to a `cargo-bazel` binary.
383        config (path): The path to a `cargo-bazel` config file.
384        splicing_manifest (path): The path to a `cargo-bazel` splicing manifest. See `create_splicing_manifest`
385        repository_dir (path): The output path for the Bazel module and BUILD files.
386        cargo (path): The path of a Cargo binary.
387        rustc (path): The path of a Rustc binary.
388        metadata (path, optional): The path to a Cargo metadata json file. If this is set, it indicates to
389            the generator that repinning is required. This file must be adjacent to a `Cargo.toml` and
390            `Cargo.lock` file.
391
392    Returns:
393        struct: The results of `repository_ctx.execute`.
394    """
395    repository_ctx.report_progress("Generating crate BUILD files.")
396
397    args = [
398        generator,
399        "generate",
400        "--cargo-lockfile",
401        cargo_lockfile_path,
402        "--config",
403        config,
404        "--splicing-manifest",
405        splicing_manifest,
406        "--repository-dir",
407        repository_dir,
408        "--cargo",
409        cargo,
410        "--rustc",
411        rustc,
412    ]
413
414    if lockfile_path:
415        args.extend([
416            "--lockfile",
417            lockfile_path,
418        ])
419
420    env = {
421        "RUST_BACKTRACE": "full",
422    }
423
424    # Some components are not required unless re-pinning is enabled
425    if metadata:
426        args.extend([
427            "--repin",
428            "--metadata",
429            metadata,
430        ])
431        env.update({
432            "CARGO": str(cargo),
433            "RUSTC": str(rustc),
434        })
435
436    # Add any Cargo environment variables to the `cargo-bazel` execution
437    env.update(cargo_environ(repository_ctx))
438
439    result = execute(
440        repository_ctx = repository_ctx,
441        args = args,
442        env = env,
443    )
444
445    return result