fork/rules_rust

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

UebelAndreAdded TemplateVariableInfo to rust_toolchain (#1377)e765719e

0.7.0
20.9 KiB612 linesraw
1# Copyright 2015 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"""Utility functions not specific to the rust toolchain."""
16
17load("@bazel_tools//tools/cpp:toolchain_utils.bzl", find_rules_cc_toolchain = "find_cpp_toolchain")
18load(":providers.bzl", "BuildInfo", "CrateInfo", "DepInfo", "DepVariantInfo")
19
20UNSUPPORTED_FEATURES = [
21    "thin_lto",
22    "module_maps",
23    "use_header_modules",
24    "fdo_instrument",
25    "fdo_optimize",
26]
27
28def find_toolchain(ctx):
29    """Finds the first rust toolchain that is configured.
30
31    Args:
32        ctx (ctx): The ctx object for the current target.
33
34    Returns:
35        rust_toolchain: A Rust toolchain context.
36    """
37    return ctx.toolchains[Label("//rust:toolchain")]
38
39def find_cc_toolchain(ctx):
40    """Extracts a CcToolchain from the current target's context
41
42    Args:
43        ctx (ctx): The current target's rule context object
44
45    Returns:
46        tuple: A tuple of (CcToolchain, FeatureConfiguration)
47    """
48    cc_toolchain = find_rules_cc_toolchain(ctx)
49
50    feature_configuration = cc_common.configure_features(
51        ctx = ctx,
52        cc_toolchain = cc_toolchain,
53        requested_features = ctx.features,
54        unsupported_features = UNSUPPORTED_FEATURES + ctx.disabled_features,
55    )
56    return cc_toolchain, feature_configuration
57
58# TODO: Replace with bazel-skylib's `path.dirname`. This requires addressing some
59# dependency issues or generating docs will break.
60def relativize(path, start):
61    """Returns the relative path from start to path.
62
63    Args:
64        path (str): The path to relativize.
65        start (str): The ancestor path against which to relativize.
66
67    Returns:
68        str: The portion of `path` that is relative to `start`.
69    """
70    src_parts = _path_parts(start)
71    dest_parts = _path_parts(path)
72    n = 0
73    for src_part, dest_part in zip(src_parts, dest_parts):
74        if src_part != dest_part:
75            break
76        n += 1
77
78    relative_path = ""
79    for _ in range(n, len(src_parts)):
80        relative_path += "../"
81    relative_path += "/".join(dest_parts[n:])
82
83    return relative_path
84
85def _path_parts(path):
86    """Takes a path and returns a list of its parts with all "." elements removed.
87
88    The main use case of this function is if one of the inputs to relativize()
89    is a relative path, such as "./foo".
90
91    Args:
92      path (str): A string representing a unix path
93
94    Returns:
95      list: A list containing the path parts with all "." elements removed.
96    """
97    path_parts = path.split("/")
98    return [part for part in path_parts if part != "."]
99
100def get_lib_name(lib):
101    """Returns the name of a library artifact, eg. libabc.a -> abc
102
103    Args:
104        lib (File): A library file
105
106    Returns:
107        str: The name of the library
108    """
109    # On macos and windows, dynamic/static libraries always end with the
110    # extension and potential versions will be before the extension, and should
111    # be part of the library name.
112    # On linux, the version usually comes after the extension.
113    # So regardless of the platform we want to find the extension and make
114    # everything left to it the library name.
115
116    # Search for the extension - starting from the right - by removing any
117    # trailing digit.
118    comps = lib.basename.split(".")
119    for comp in reversed(comps):
120        if comp.isdigit():
121            comps.pop()
122        else:
123            break
124
125    # The library name is now everything minus the extension.
126    libname = ".".join(comps[:-1])
127
128    if libname.startswith("lib"):
129        return libname[3:]
130    else:
131        return libname
132
133def abs(value):
134    """Returns the absolute value of a number.
135
136    Args:
137      value (int): A number.
138
139    Returns:
140      int: The absolute value of the number.
141    """
142    if value < 0:
143        return -value
144    return value
145
146def determine_output_hash(crate_root, label):
147    """Generates a hash of the crate root file's path.
148
149    Args:
150        crate_root (File): The crate's root file (typically `lib.rs`).
151        label (Label): The label of the target.
152
153    Returns:
154        str: A string representation of the hash.
155    """
156
157    # Take the absolute value of hash() since it could be negative.
158    h = abs(hash(crate_root.path) + hash(repr(label)))
159    return repr(h)
160
161def get_preferred_artifact(library_to_link, use_pic):
162    """Get the first available library to link from a LibraryToLink object.
163
164    Args:
165        library_to_link (LibraryToLink): See the followg links for additional details:
166            https://docs.bazel.build/versions/master/skylark/lib/LibraryToLink.html
167        use_pic: If set, prefers pic_static_library over static_library.
168
169    Returns:
170        File: Returns the first valid library type (only one is expected)
171    """
172    if use_pic:
173        # Order consistent with https://github.com/bazelbuild/bazel/blob/815dfdabb7df31d4e99b6fc7616ff8e2f9385d98/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingContext.java#L437.
174        return (
175            library_to_link.pic_static_library or
176            library_to_link.static_library or
177            library_to_link.interface_library or
178            library_to_link.dynamic_library
179        )
180    else:
181        return (
182            library_to_link.static_library or
183            library_to_link.pic_static_library or
184            library_to_link.interface_library or
185            library_to_link.dynamic_library
186        )
187
188def _expand_location(ctx, env, data):
189    """A trivial helper for `expand_dict_value_locations` and `expand_list_element_locations`
190
191    Args:
192        ctx (ctx): The rule's context object
193        env (str): The value possibly containing location macros to expand.
194        data (sequence of Targets): See one of the parent functions.
195
196    Returns:
197        string: The location-macro expanded version of the string.
198    """
199    for directive in ("$(execpath ", "$(location "):
200        if directive in env:
201            # build script runner will expand pwd to execroot for us
202            env = env.replace(directive, "$${pwd}/" + directive)
203    return ctx.expand_make_variables(
204        env,
205        ctx.expand_location(env, data),
206        {},
207    )
208
209def expand_dict_value_locations(ctx, env, data):
210    """Performs location-macro expansion on string values.
211
212    $(execroot ...) and $(location ...) are prefixed with ${pwd},
213    which process_wrapper and build_script_runner will expand at run time
214    to the absolute path. This is necessary because include_str!() is relative
215    to the currently compiled file, and build scripts run relative to the
216    manifest dir, so we can not use execroot-relative paths.
217
218    $(rootpath ...) is unmodified, and is useful for passing in paths via
219    rustc_env that are encoded in the binary with env!(), but utilized at
220    runtime, such as in tests. The absolute paths are not usable in this case,
221    as compilation happens in a separate sandbox folder, so when it comes time
222    to read the file at runtime, the path is no longer valid.
223
224    For detailed documentation, see:
225    - [`expand_location`](https://bazel.build/rules/lib/ctx#expand_location)
226    - [`expand_make_variables`](https://bazel.build/rules/lib/ctx#expand_make_variables)
227
228    Args:
229        ctx (ctx): The rule's context object
230        env (dict): A dict whose values we iterate over
231        data (sequence of Targets): The targets which may be referenced by
232            location macros. This is expected to be the `data` attribute of
233            the target, though may have other targets or attributes mixed in.
234
235    Returns:
236        dict: A dict of environment variables with expanded location macros
237    """
238    return dict([(k, _expand_location(ctx, v, data)) for (k, v) in env.items()])
239
240def expand_list_element_locations(ctx, args, data):
241    """Performs location-macro expansion on a list of string values.
242
243    $(execroot ...) and $(location ...) are prefixed with ${pwd},
244    which process_wrapper and build_script_runner will expand at run time
245    to the absolute path.
246
247    For detailed documentation, see:
248    - [`expand_location`](https://bazel.build/rules/lib/ctx#expand_location)
249    - [`expand_make_variables`](https://bazel.build/rules/lib/ctx#expand_make_variables)
250
251    Args:
252        ctx (ctx): The rule's context object
253        args (list): A list we iterate over
254        data (sequence of Targets): The targets which may be referenced by
255            location macros. This is expected to be the `data` attribute of
256            the target, though may have other targets or attributes mixed in.
257
258    Returns:
259        list: A list of arguments with expanded location macros
260    """
261    return [_expand_location(ctx, arg, data) for arg in args]
262
263def name_to_crate_name(name):
264    """Converts a build target's name into the name of its associated crate.
265
266    Crate names cannot contain certain characters, such as -, which are allowed
267    in build target names. All illegal characters will be converted to
268    underscores.
269
270    This is a similar conversion as that which cargo does, taking a
271    `Cargo.toml`'s `package.name` and canonicalizing it
272
273    Note that targets can specify the `crate_name` attribute to customize their
274    crate name; in situations where this is important, use the
275    compute_crate_name() function instead.
276
277    Args:
278        name (str): The name of the target.
279
280    Returns:
281        str: The name of the crate for this target.
282    """
283    for illegal in ("-", "/"):
284        name = name.replace(illegal, "_")
285    return name
286
287def _invalid_chars_in_crate_name(name):
288    """Returns any invalid chars in the given crate name.
289
290    Args:
291        name (str): Name to test.
292
293    Returns:
294        list: List of invalid characters in the crate name.
295    """
296
297    return dict([(c, ()) for c in name.elems() if not (c.isalnum() or c == "_")]).keys()
298
299def compute_crate_name(workspace_name, label, toolchain, name_override = None):
300    """Returns the crate name to use for the current target.
301
302    Args:
303        workspace_name (string): The current workspace name.
304        label (struct): The label of the current target.
305        toolchain (struct): The toolchain in use for the target.
306        name_override (String): An optional name to use (as an override of label.name).
307
308    Returns:
309        str: The crate name to use for this target.
310    """
311    if name_override:
312        invalid_chars = _invalid_chars_in_crate_name(name_override)
313        if invalid_chars:
314            fail("Crate name '{}' contains invalid character(s): {}".format(
315                name_override,
316                " ".join(invalid_chars),
317            ))
318        return name_override
319
320    if (toolchain and label and toolchain._rename_first_party_crates and
321        should_encode_label_in_crate_name(workspace_name, label, toolchain._third_party_dir)):
322        crate_name = encode_label_as_crate_name(label.package, label.name)
323    else:
324        crate_name = name_to_crate_name(label.name)
325
326    invalid_chars = _invalid_chars_in_crate_name(crate_name)
327    if invalid_chars:
328        fail(
329            "Crate name '{}' ".format(crate_name) +
330            "derived from Bazel target name '{}' ".format(label.name) +
331            "contains invalid character(s): {}\n".format(" ".join(invalid_chars)) +
332            "Consider adding a crate_name attribute to set a valid crate name",
333        )
334    return crate_name
335
336def dedent(doc_string):
337    """Remove any common leading whitespace from every line in text.
338
339    This functionality is similar to python's `textwrap.dedent` functionality
340    https://docs.python.org/3/library/textwrap.html#textwrap.dedent
341
342    Args:
343        doc_string (str): A docstring style string
344
345    Returns:
346        str: A string optimized for stardoc rendering
347    """
348    lines = doc_string.splitlines()
349    if not lines:
350        return doc_string
351
352    # If the first line is empty, use the second line
353    first_line = lines[0]
354    if not first_line:
355        first_line = lines[1]
356
357    # Detect how much space prepends the first line and subtract that from all lines
358    space_count = len(first_line) - len(first_line.lstrip())
359
360    # If there are no leading spaces, do not alter the docstring
361    if space_count == 0:
362        return doc_string
363    else:
364        # Remove the leading block of spaces from the current line
365        block = " " * space_count
366        return "\n".join([line.replace(block, "", 1).rstrip() for line in lines])
367
368def make_static_lib_symlink(actions, rlib_file):
369    """Add a .a symlink to an .rlib file.
370
371    The name of the symlink is derived from the <name> of the <name>.rlib file as follows:
372    * `<name>.a`, if <name> starts with `lib`
373    * `lib<name>.a`, otherwise.
374
375    For example, the name of the symlink for
376    * `libcratea.rlib` is `libcratea.a`
377    * `crateb.rlib` is `libcrateb.a`.
378
379    Args:
380        actions (actions): The rule's context actions object.
381        rlib_file (File): The file to symlink, which must end in .rlib.
382
383    Returns:
384        The symlink's File.
385    """
386    if not rlib_file.basename.endswith(".rlib"):
387        fail("file is not an .rlib: ", rlib_file.basename)
388    basename = rlib_file.basename[:-5]
389    if not basename.startswith("lib"):
390        basename = "lib" + basename
391    dot_a = actions.declare_file(basename + ".a", sibling = rlib_file)
392    actions.symlink(output = dot_a, target_file = rlib_file)
393    return dot_a
394
395def is_exec_configuration(ctx):
396    """Determine if a context is building for the exec configuration.
397
398    This is helpful when processing command line flags that should apply
399    to the target configuration but not the exec configuration.
400
401    Args:
402        ctx (ctx): The ctx object for the current target.
403
404    Returns:
405        True if the exec configuration is detected, False otherwise.
406    """
407
408    # TODO(djmarcin): Is there any better way to determine cfg=exec?
409    return ctx.genfiles_dir.path.find("-exec-") != -1
410
411def transform_deps(deps):
412    """Transforms a [Target] into [DepVariantInfo].
413
414    This helper function is used to transform ctx.attr.deps and ctx.attr.proc_macro_deps into
415    [DepVariantInfo].
416
417    Args:
418        deps (list of Targets): Dependencies coming from ctx.attr.deps or ctx.attr.proc_macro_deps
419
420    Returns:
421        list of DepVariantInfos.
422    """
423    return [DepVariantInfo(
424        crate_info = dep[CrateInfo] if CrateInfo in dep else None,
425        dep_info = dep[DepInfo] if DepInfo in dep else None,
426        build_info = dep[BuildInfo] if BuildInfo in dep else None,
427        cc_info = dep[CcInfo] if CcInfo in dep else None,
428    ) for dep in deps]
429
430def get_import_macro_deps(ctx):
431    """Returns a list of targets to be added to proc_macro_deps.
432
433    Args:
434        ctx (struct): the ctx of the current target.
435
436    Returns:
437        list of Targets. Either empty (if the fake import macro implementation
438        is being used), or a singleton list with the real implementation.
439    """
440    if ctx.attr._import_macro_dep.label.name == "fake_import_macro_impl":
441        return []
442
443    return [ctx.attr._import_macro_dep]
444
445def should_encode_label_in_crate_name(workspace_name, label, third_party_dir):
446    """Determines if the crate's name should include the Bazel label, encoded.
447
448    Crate names may only encode the label if the target is in the current repo,
449    the target is not in the third_party_dir, and the current repo is not
450    rules_rust.
451
452    Args:
453        workspace_name (string): The name of the current workspace.
454        label (Label): The package in question.
455        third_party_dir (string): The directory in which third-party packages are kept.
456
457    Returns:
458        True if the crate name should encode the label, False otherwise.
459    """
460
461    # TODO(hlopko): This code assumes a monorepo; make it work with external
462    # repositories as well.
463    return (
464        workspace_name != "rules_rust" and
465        not label.workspace_root and
466        not ("//" + label.package + "/").startswith(third_party_dir + "/")
467    )
468
469# This is a list of pairs, where the first element of the pair is a character
470# that is allowed in Bazel package or target names but not in crate names; and
471# the second element is an encoding of that char suitable for use in a crate
472# name.
473_encodings = (
474    (":", "x"),
475    ("!", "excl"),
476    ("%", "prc"),
477    ("@", "ao"),
478    ("^", "caret"),
479    ("`", "bt"),
480    (" ", "sp"),
481    ("\"", "dq"),
482    ("#", "octo"),
483    ("$", "dllr"),
484    ("&", "amp"),
485    ("'", "sq"),
486    ("(", "lp"),
487    (")", "rp"),
488    ("*", "astr"),
489    ("-", "d"),
490    ("+", "pl"),
491    (",", "cm"),
492    (";", "sm"),
493    ("<", "la"),
494    ("=", "eq"),
495    (">", "ra"),
496    ("?", "qm"),
497    ("[", "lbk"),
498    ("]", "rbk"),
499    ("{", "lbe"),
500    ("|", "pp"),
501    ("}", "rbe"),
502    ("~", "td"),
503    ("/", "y"),
504    (".", "pd"),
505)
506
507# For each of the above encodings, we generate two substitution rules: one that
508# ensures any occurrences of the encodings themselves in the package/target
509# aren't clobbered by this translation, and one that does the encoding itself.
510# We also include a rule that protects the clobbering-protection rules from
511# getting clobbered.
512_substitutions = [("_z", "_zz_")] + [
513    subst
514    for (pattern, replacement) in _encodings
515    for subst in (
516        ("_{}_".format(replacement), "_z{}_".format(replacement)),
517        (pattern, "_{}_".format(replacement)),
518    )
519]
520
521# Expose the substitutions for testing only.
522substitutions_for_testing = _substitutions
523
524def encode_label_as_crate_name(package, name):
525    """Encodes the package and target names in a format suitable for a crate name.
526
527    Args:
528        package (string): The package of the target in question.
529        name (string): The name of the target in question.
530
531    Returns:
532        A string that encodes the package and target name, to be used as the crate's name.
533    """
534    return _encode_raw_string(package + ":" + name)
535
536def _encode_raw_string(str):
537    """Encodes a string using the above encoding format.
538
539    Args:
540        str (string): The string to be encoded.
541
542    Returns:
543        An encoded version of the input string.
544    """
545    return _replace_all(str, _substitutions)
546
547# Expose the underlying encoding function for testing only.
548encode_raw_string_for_testing = _encode_raw_string
549
550def decode_crate_name_as_label_for_testing(crate_name):
551    """Decodes a crate_name that was encoded by encode_label_as_crate_name.
552
553    This is used to check that the encoding is bijective; it is expected to only
554    be used in tests.
555
556    Args:
557        crate_name (string): The name of the crate.
558
559    Returns:
560        A string representing the Bazel label (package and target).
561    """
562    return _replace_all(crate_name, [(t[1], t[0]) for t in _substitutions])
563
564def _replace_all(string, substitutions):
565    """Replaces occurrences of the given patterns in `string`.
566
567    There are a few reasons this looks complicated:
568    * The substitutions are performed with some priority, i.e. patterns that are
569      listed first in `substitutions` are higher priority than patterns that are
570      listed later.
571    * We also take pains to avoid doing replacements that overlap with each
572      other, since overlaps invalidate pattern matches.
573    * To avoid hairy offset invalidation, we apply the substitutions
574      right-to-left.
575    * To avoid the "_quote" -> "_quotequote_" rule introducing new pattern
576      matches later in the string during decoding, we take the leftmost
577      replacement, in cases of overlap.  (Note that no rule can induce new
578      pattern matches *earlier* in the string.) (E.g. "_quotedot_" encodes to
579      "_quotequote_dot_". Note that "_quotequote_" and "_dot_" both occur in
580      this string, and overlap.).
581
582    Args:
583        string (string): the string in which the replacements should be performed.
584        substitutions: the list of patterns and replacements to apply.
585
586    Returns:
587        A string with the appropriate substitutions performed.
588    """
589
590    # Find the highest-priority pattern matches for each string index, going
591    # left-to-right and skipping indices that are already involved in a
592    # pattern match.
593    plan = {}
594    matched_indices_set = {}
595    for pattern_start in range(len(string)):
596        if pattern_start in matched_indices_set:
597            continue
598        for (pattern, replacement) in substitutions:
599            if not string.startswith(pattern, pattern_start):
600                continue
601            length = len(pattern)
602            plan[pattern_start] = (length, replacement)
603            matched_indices_set.update([(pattern_start + i, True) for i in range(length)])
604            break
605
606    # Execute the replacement plan, working from right to left.
607    for pattern_start in sorted(plan.keys(), reverse = True):
608        length, replacement = plan[pattern_start]
609        after_pattern = pattern_start + length
610        string = string[:pattern_start] + replacement + string[after_pattern:]
611
612    return string