fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
86dc561f
0.11.0
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 226def _expand_location ( ctx , env , data ): 227"""A trivial helper for `expand_dict_value_locations` and `expand_list_element_locations` 228 229Args: 230ctx (ctx): The rule's context object 231env (str): The value possibly containing location macros to expand. 232data (sequence of Targets): See one of the parent functions. 233 234Returns: 235string: The location-macro expanded version of the string. 236""" 237for directive in ( "$(execpath " , "$(location " ): 238if directive in env : 239# build script runner will expand pwd to execroot for us 240env = env . replace ( directive , "$${pwd}/" + directive ) 241return ctx . expand_make_variables ( 242env , 243ctx . expand_location ( env , data ), 244{}, 245) 246 247def expand_dict_value_locations ( ctx , env , data ): 248"""Performs location-macro expansion on string values. 249 250$(execroot ...) and $(location ...) are prefixed with ${pwd}, 251which process_wrapper and build_script_runner will expand at run time 252to the absolute path. This is necessary because include_str!() is relative 253to the currently compiled file, and build scripts run relative to the 254manifest dir, so we can not use execroot-relative paths. 255 256$(rootpath ...) is unmodified, and is useful for passing in paths via 257rustc_env that are encoded in the binary with env!(), but utilized at 258runtime, such as in tests. The absolute paths are not usable in this case, 259as compilation happens in a separate sandbox folder, so when it comes time 260to read the file at runtime, the path is no longer valid. 261 262For detailed documentation, see: 263- [`expand_location`](https://bazel.build/rules/lib/ctx#expand_location) 264- [`expand_make_variables`](https://bazel.build/rules/lib/ctx#expand_make_variables) 265 266Args: 267ctx (ctx): The rule's context object 268env (dict): A dict whose values we iterate over 269data (sequence of Targets): The targets which may be referenced by 270location macros. This is expected to be the `data` attribute of 271the target, though may have other targets or attributes mixed in. 272 273Returns: 274dict: A dict of environment variables with expanded location macros 275""" 276return dict ([( k , _expand_location ( ctx , v , data )) for ( k , v ) in env . items ()]) 277 278def expand_list_element_locations ( ctx , args , data ): 279"""Performs location-macro expansion on a list of string values. 280 281$(execroot ...) and $(location ...) are prefixed with ${pwd}, 282which process_wrapper and build_script_runner will expand at run time 283to the absolute path. 284 285For detailed documentation, see: 286- [`expand_location`](https://bazel.build/rules/lib/ctx#expand_location) 287- [`expand_make_variables`](https://bazel.build/rules/lib/ctx#expand_make_variables) 288 289Args: 290ctx (ctx): The rule's context object 291args (list): A list we iterate over 292data (sequence of Targets): The targets which may be referenced by 293location macros. This is expected to be the `data` attribute of 294the target, though may have other targets or attributes mixed in. 295 296Returns: 297list: A list of arguments with expanded location macros 298""" 299return [ _expand_location ( ctx , arg , data ) for arg in args ] 300 301def name_to_crate_name ( name ): 302"""Converts a build target's name into the name of its associated crate. 303 304Crate names cannot contain certain characters, such as -, which are allowed 305in build target names. All illegal characters will be converted to 306underscores. 307 308This is a similar conversion as that which cargo does, taking a 309`Cargo.toml`'s `package.name` and canonicalizing it 310 311Note that targets can specify the `crate_name` attribute to customize their 312crate name; in situations where this is important, use the 313compute_crate_name() function instead. 314 315Args: 316name (str): The name of the target. 317 318Returns: 319str: The name of the crate for this target. 320""" 321for illegal in ( "-" , "/" ): 322name = name . replace ( illegal , "_" ) 323return name 324 325def _invalid_chars_in_crate_name ( name ): 326"""Returns any invalid chars in the given crate name. 327 328Args: 329name (str): Name to test. 330 331Returns: 332list: List of invalid characters in the crate name. 333""" 334 335return dict ([( c , ( )) for c in name . elems () if not ( c . isalnum () or c == "_" )]). keys () 336 337def compute_crate_name ( workspace_name , label , toolchain , name_override = None ): 338"""Returns the crate name to use for the current target. 339 340Args: 341workspace_name (string): The current workspace name. 342label (struct): The label of the current target. 343toolchain (struct): The toolchain in use for the target. 344name_override (String): An optional name to use (as an override of label.name). 345 346Returns: 347str: The crate name to use for this target. 348""" 349if name_override : 350invalid_chars = _invalid_chars_in_crate_name ( name_override ) 351if invalid_chars : 352fail ( "Crate name '{}' contains invalid character(s): {}" . format ( 353name_override , 354" " . join ( invalid_chars ), 355)) 356return name_override 357 358if ( toolchain and label and toolchain . _rename_first_party_crates and 359should_encode_label_in_crate_name ( workspace_name , label , toolchain . _third_party_dir )): 360crate_name = encode_label_as_crate_name ( label . package , label . name ) 361else : 362crate_name = name_to_crate_name ( label . name ) 363 364invalid_chars = _invalid_chars_in_crate_name ( crate_name ) 365if invalid_chars : 366fail ( 367"Crate name '{}' " . format ( crate_name ) + 368"derived from Bazel target name '{}' " . format ( label . name ) + 369"contains invalid character(s): {}\n ".format(" " . join ( invalid_chars )) + 370"Consider adding a crate_name attribute to set a valid crate name" , 371) 372return crate_name 373 374def dedent ( doc_string ): 375"""Remove any common leading whitespace from every line in text. 376 377This functionality is similar to python's `textwrap.dedent` functionality 378https://docs.python.org/3/library/textwrap.html#textwrap.dedent 379 380Args: 381doc_string (str): A docstring style string 382 383Returns: 384str: A string optimized for stardoc rendering 385""" 386lines = doc_string . splitlines () 387if not lines : 388return doc_string 389 390# If the first line is empty, use the second line 391first_line = lines [ 0 ] 392if not first_line : 393first_line = lines [ 1 ] 394 395# Detect how much space prepends the first line and subtract that from all lines 396space_count = len ( first_line ) - len ( first_line . lstrip ()) 397 398# If there are no leading spaces, do not alter the docstring 399if space_count == 0 : 400return doc_string 401else : 402# Remove the leading block of spaces from the current line 403block = " " * space_count 404return "\n ".join([line.replace(block, " " , 1 ). rstrip () for line in lines ]) 405 406def make_static_lib_symlink ( actions , rlib_file ): 407"""Add a .a symlink to an .rlib file. 408 409The name of the symlink is derived from the <name> of the <name>.rlib file as follows: 410* `<name>.a`, if <name> starts with `lib` 411* `lib<name>.a`, otherwise. 412 413For example, the name of the symlink for 414* `libcratea.rlib` is `libcratea.a` 415* `crateb.rlib` is `libcrateb.a`. 416 417Args: 418actions (actions): The rule's context actions object. 419rlib_file (File): The file to symlink, which must end in .rlib. 420 421Returns: 422The symlink's File. 423""" 424if not rlib_file . basename . endswith ( ".rlib" ): 425fail ( "file is not an .rlib: " , rlib_file . basename ) 426basename = rlib_file . basename [: - 5 ] 427if not basename . startswith ( "lib" ): 428basename = "lib" + basename 429dot_a = actions . declare_file ( basename + ".a" , sibling = rlib_file ) 430actions . symlink ( output = dot_a , target_file = rlib_file ) 431return dot_a 432 433def is_exec_configuration ( ctx ): 434"""Determine if a context is building for the exec configuration. 435 436This is helpful when processing command line flags that should apply 437to the target configuration but not the exec configuration. 438 439Args: 440ctx (ctx): The ctx object for the current target. 441 442Returns: 443True if the exec configuration is detected, False otherwise. 444""" 445 446# TODO(djmarcin): Is there any better way to determine cfg=exec? 447return ctx . genfiles_dir . path . find ( "-exec-" ) != - 1 448 449def transform_deps ( deps ): 450"""Transforms a [Target] into [DepVariantInfo]. 451 452This helper function is used to transform ctx.attr.deps and ctx.attr.proc_macro_deps into 453[DepVariantInfo]. 454 455Args: 456deps (list of Targets): Dependencies coming from ctx.attr.deps or ctx.attr.proc_macro_deps 457 458Returns: 459list of DepVariantInfos. 460""" 461return [ DepVariantInfo ( 462crate_info = dep [ CrateInfo ] if CrateInfo in dep else None , 463dep_info = dep [ DepInfo ] if DepInfo in dep else None , 464build_info = dep [ BuildInfo ] if BuildInfo in dep else None , 465cc_info = dep [ CcInfo ] if CcInfo in dep else None , 466) for dep in deps ] 467 468def get_import_macro_deps ( ctx ): 469"""Returns a list of targets to be added to proc_macro_deps. 470 471Args: 472ctx (struct): the ctx of the current target. 473 474Returns: 475list of Targets. Either empty (if the fake import macro implementation 476is being used), or a singleton list with the real implementation. 477""" 478if ctx . attr . _import_macro_dep . label . name == "fake_import_macro_impl" : 479return [] 480 481return [ ctx . attr . _import_macro_dep ] 482 483def should_encode_label_in_crate_name ( workspace_name , label , third_party_dir ): 484"""Determines if the crate's name should include the Bazel label, encoded. 485 486Crate names may only encode the label if the target is in the current repo, 487the target is not in the third_party_dir, and the current repo is not 488rules_rust. 489 490Args: 491workspace_name (string): The name of the current workspace. 492label (Label): The package in question. 493third_party_dir (string): The directory in which third-party packages are kept. 494 495Returns: 496True if the crate name should encode the label, False otherwise. 497""" 498 499# TODO(hlopko): This code assumes a monorepo; make it work with external 500# repositories as well. 501return ( 502workspace_name != "rules_rust" and 503not label . workspace_root and 504not ( "//" + label . package + "/" ). startswith ( third_party_dir + "/" ) 505) 506 507# This is a list of pairs, where the first element of the pair is a character 508# that is allowed in Bazel package or target names but not in crate names; and 509# the second element is an encoding of that char suitable for use in a crate 510# name. 511_encodings = ( 512( ":" , "x" ), 513( "!" , "excl" ), 514( "%" , "prc" ), 515( "@" , "ao" ), 516( "^" , "caret" ), 517( "`" , "bt" ), 518( " " , "sp" ), 519( "\ "" , "dq" ), 520( " #", "octo"), 521(" $ ", " dllr " ), 522( "&" , "amp" ), 523( "'" , "sq" ), 524( "(" , "lp" ), 525( ")" , "rp" ), 526( "*" , "astr" ), 527( "-" , "d" ), 528( "+" , "pl" ), 529( "," , "cm" ), 530( ";" , "sm" ), 531( "<" , "la" ), 532( "=" , "eq" ), 533( ">" , "ra" ), 534( "?" , "qm" ), 535( "[" , "lbk" ), 536( "]" , "rbk" ), 537( "{" , "lbe" ), 538( "|" , "pp" ), 539( "}" , "rbe" ), 540( "~" , "td" ), 541( "/" , "y" ), 542( "." , "pd" ), 543) 544 545# For each of the above encodings, we generate two substitution rules: one that 546# ensures any occurrences of the encodings themselves in the package/target 547# aren't clobbered by this translation, and one that does the encoding itself. 548# We also include a rule that protects the clobbering-protection rules from 549# getting clobbered. 550_substitutions = [( "_z" , "_zz_" )] + [ 551subst 552for ( pattern , replacement ) in _encodings 553for subst in ( 554( "_{}_" . format ( replacement ), "_z{}_" . format ( replacement )), 555( pattern , "_{}_" . format ( replacement )), 556) 557] 558 559# Expose the substitutions for testing only. 560substitutions_for_testing = _substitutions 561 562def encode_label_as_crate_name ( package , name ): 563"""Encodes the package and target names in a format suitable for a crate name. 564 565Args: 566package (string): The package of the target in question. 567name (string): The name of the target in question. 568 569Returns: 570A string that encodes the package and target name, to be used as the crate's name. 571""" 572return _encode_raw_string ( package + ":" + name ) 573 574def _encode_raw_string ( str ): 575"""Encodes a string using the above encoding format. 576 577Args: 578str (string): The string to be encoded. 579 580Returns: 581An encoded version of the input string. 582""" 583return _replace_all ( str , _substitutions ) 584 585# Expose the underlying encoding function for testing only. 586encode_raw_string_for_testing = _encode_raw_string 587 588def decode_crate_name_as_label_for_testing ( crate_name ): 589"""Decodes a crate_name that was encoded by encode_label_as_crate_name. 590 591This is used to check that the encoding is bijective; it is expected to only 592be used in tests. 593 594Args: 595crate_name (string): The name of the crate. 596 597Returns: 598A string representing the Bazel label (package and target). 599""" 600return _replace_all ( crate_name , [( t [ 1 ], t [ 0 ]) for t in _substitutions ]) 601 602def _replace_all ( string , substitutions ): 603"""Replaces occurrences of the given patterns in `string`. 604 605There are a few reasons this looks complicated: 606* The substitutions are performed with some priority, i.e. patterns that are 607listed first in `substitutions` are higher priority than patterns that are 608listed later. 609* We also take pains to avoid doing replacements that overlap with each 610other, since overlaps invalidate pattern matches. 611* To avoid hairy offset invalidation, we apply the substitutions 612right-to-left. 613* To avoid the "_quote" -> "_quotequote_" rule introducing new pattern 614matches later in the string during decoding, we take the leftmost 615replacement, in cases of overlap. (Note that no rule can induce new 616pattern matches *earlier* in the string.) (E.g. "_quotedot_" encodes to 617"_quotequote_dot_". Note that "_quotequote_" and "_dot_" both occur in 618this string, and overlap.). 619 620Args: 621string (string): the string in which the replacements should be performed. 622substitutions: the list of patterns and replacements to apply. 623 624Returns: 625A string with the appropriate substitutions performed. 626""" 627 628# Find the highest-priority pattern matches for each string index, going 629# left-to-right and skipping indices that are already involved in a 630# pattern match. 631plan = {} 632matched_indices_set = {} 633for pattern_start in range ( len ( string )): 634if pattern_start in matched_indices_set : 635continue 636for ( pattern , replacement ) in substitutions : 637if not string . startswith ( pattern , pattern_start ): 638continue 639length = len ( pattern ) 640plan [ pattern_start ] = ( length , replacement ) 641matched_indices_set . update ([( pattern_start + i , True ) for i in range ( length )]) 642break 643 644# Execute the replacement plan, working from right to left. 645for pattern_start in sorted ( plan . keys (), reverse = True ): 646length , replacement = plan [ pattern_start ] 647after_pattern = pattern_start + length 648string = string [: pattern_start ] + replacement + string [ after_pattern :] 649 650return string 651 652def can_build_metadata ( toolchain , ctx , crate_type ): 653"""Can we build metadata for this rust_library? 654 655Args: 656toolchain (toolchain): The rust toolchain 657ctx (ctx): The rule's context object 658crate_type (String): one of lib|rlib|dylib|staticlib|cdylib|proc-macro 659 660Returns: 661bool: whether we can build metadata for this rule. 662""" 663 664# In order to enable pipelined compilation we require that: 665# 1) The _pipelined_compilation flag is enabled, 666# 2) the OS running the rule is something other than windows as we require sandboxing (for now), 667# 3) process_wrapper is enabled (this is disabled when compiling process_wrapper itself), 668# 4) the crate_type is rlib or lib. 669return toolchain . _pipelined_compilation and \ 670toolchain . os != "windows" and \ 671ctx . attr . _process_wrapper and \ 672crate_type in ( "rlib" , "lib" ) 673 674def crate_root_src ( name , srcs , crate_type ): 675"""Determines the source file for the crate root, should it not be specified in `attr.crate_root`. 676 677Args: 678name (str): The name of the target. 679srcs (list): A list of all sources for the target Crate. 680crate_type (str): The type of this crate ("bin", "lib", "rlib", "cdylib", etc). 681 682Returns: 683File: The root File object for a given crate. See the following links for more details: 684- https://doc.rust-lang.org/cargo/reference/cargo-targets.html#library 685- https://doc.rust-lang.org/cargo/reference/cargo-targets.html#binaries 686""" 687default_crate_root_filename = "main.rs" if crate_type == "bin" else "lib.rs" 688 689crate_root = ( 690( srcs [ 0 ] if len ( srcs ) == 1 else None ) or 691_shortest_src_with_basename ( srcs , default_crate_root_filename ) or 692_shortest_src_with_basename ( srcs , name + ".rs" ) 693) 694if not crate_root : 695file_names = [ default_crate_root_filename , name + ".rs" ] 696fail ( "No {} source file found." . format ( " or " . join ( file_names )), "srcs" ) 697return crate_root 698 699def _shortest_src_with_basename ( srcs , basename ): 700"""Finds the shortest among the paths in srcs that match the desired basename. 701 702Args: 703srcs (list): A list of File objects 704basename (str): The target basename to match against. 705 706Returns: 707File: The File object with the shortest path that matches `basename` 708""" 709shortest = None 710for f in srcs : 711if f . basename == basename : 712if not shortest or len ( f . dirname ) < len ( shortest . dirname ): 713shortest = f 714return shortest