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