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