1*d4726bddSHONG Yifan"""The rust_toolchain rule definition and implementation.""" 2*d4726bddSHONG Yifan 3*d4726bddSHONG Yifanload("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") 4*d4726bddSHONG Yifanload("//rust/platform:triple.bzl", "triple") 5*d4726bddSHONG Yifanload("//rust/private:common.bzl", "rust_common") 6*d4726bddSHONG Yifanload("//rust/private:rust_analyzer.bzl", _rust_analyzer_toolchain = "rust_analyzer_toolchain") 7*d4726bddSHONG Yifanload( 8*d4726bddSHONG Yifan "//rust/private:rustfmt.bzl", 9*d4726bddSHONG Yifan _current_rustfmt_toolchain = "current_rustfmt_toolchain", 10*d4726bddSHONG Yifan _rustfmt_toolchain = "rustfmt_toolchain", 11*d4726bddSHONG Yifan) 12*d4726bddSHONG Yifanload( 13*d4726bddSHONG Yifan "//rust/private:utils.bzl", 14*d4726bddSHONG Yifan "dedent", 15*d4726bddSHONG Yifan "dedup_expand_location", 16*d4726bddSHONG Yifan "find_cc_toolchain", 17*d4726bddSHONG Yifan "is_exec_configuration", 18*d4726bddSHONG Yifan "is_std_dylib", 19*d4726bddSHONG Yifan "make_static_lib_symlink", 20*d4726bddSHONG Yifan) 21*d4726bddSHONG Yifanload("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo") 22*d4726bddSHONG Yifan 23*d4726bddSHONG Yifanrust_analyzer_toolchain = _rust_analyzer_toolchain 24*d4726bddSHONG Yifanrustfmt_toolchain = _rustfmt_toolchain 25*d4726bddSHONG Yifancurrent_rustfmt_toolchain = _current_rustfmt_toolchain 26*d4726bddSHONG Yifan 27*d4726bddSHONG Yifandef _rust_stdlib_filegroup_impl(ctx): 28*d4726bddSHONG Yifan rust_std = ctx.files.srcs 29*d4726bddSHONG Yifan dot_a_files = [] 30*d4726bddSHONG Yifan between_alloc_and_core_files = [] 31*d4726bddSHONG Yifan core_files = [] 32*d4726bddSHONG Yifan between_core_and_std_files = [] 33*d4726bddSHONG Yifan std_files = [] 34*d4726bddSHONG Yifan test_files = [] 35*d4726bddSHONG Yifan memchr_files = [] 36*d4726bddSHONG Yifan alloc_files = [] 37*d4726bddSHONG Yifan self_contained_files = [ 38*d4726bddSHONG Yifan file 39*d4726bddSHONG Yifan for file in rust_std 40*d4726bddSHONG Yifan if file.basename.endswith(".o") and "self-contained" in file.path 41*d4726bddSHONG Yifan ] 42*d4726bddSHONG Yifan panic_files = [] 43*d4726bddSHONG Yifan 44*d4726bddSHONG Yifan std_rlibs = [f for f in rust_std if f.basename.endswith(".rlib")] 45*d4726bddSHONG Yifan if std_rlibs: 46*d4726bddSHONG Yifan # test depends on std 47*d4726bddSHONG Yifan # std depends on everything except test 48*d4726bddSHONG Yifan # 49*d4726bddSHONG Yifan # core only depends on alloc, but we poke adler in there 50*d4726bddSHONG Yifan # because that needs to be before miniz_oxide 51*d4726bddSHONG Yifan # 52*d4726bddSHONG Yifan # panic_unwind depends on unwind, alloc, cfg_if, compiler_builtins, core, libc 53*d4726bddSHONG Yifan # panic_abort depends on alloc, cfg_if, compiler_builtins, core, libc 54*d4726bddSHONG Yifan # 55*d4726bddSHONG Yifan # alloc depends on the allocator_library if it's configured, but we 56*d4726bddSHONG Yifan # do that later. 57*d4726bddSHONG Yifan dot_a_files = [make_static_lib_symlink(ctx.label.package, ctx.actions, f) for f in std_rlibs] 58*d4726bddSHONG Yifan 59*d4726bddSHONG Yifan alloc_files = [f for f in dot_a_files if "alloc" in f.basename and "std" not in f.basename] 60*d4726bddSHONG Yifan between_alloc_and_core_files = [f for f in dot_a_files if "compiler_builtins" in f.basename] 61*d4726bddSHONG Yifan core_files = [f for f in dot_a_files if ("core" in f.basename or "adler" in f.basename) and "std" not in f.basename] 62*d4726bddSHONG Yifan panic_files = [f for f in dot_a_files if f.basename in ["cfg_if", "libc", "panic_abort", "panic_unwind", "unwind"]] 63*d4726bddSHONG Yifan between_core_and_std_files = [ 64*d4726bddSHONG Yifan f 65*d4726bddSHONG Yifan for f in dot_a_files 66*d4726bddSHONG Yifan if "alloc" not in f.basename and "compiler_builtins" not in f.basename and "core" not in f.basename and "adler" not in f.basename and "std" not in f.basename and "memchr" not in f.basename and "test" not in f.basename 67*d4726bddSHONG Yifan ] 68*d4726bddSHONG Yifan memchr_files = [f for f in dot_a_files if "memchr" in f.basename] 69*d4726bddSHONG Yifan std_files = [f for f in dot_a_files if "std" in f.basename] 70*d4726bddSHONG Yifan test_files = [f for f in dot_a_files if "test" in f.basename] 71*d4726bddSHONG Yifan 72*d4726bddSHONG Yifan partitioned_files_len = len(alloc_files) + len(between_alloc_and_core_files) + len(core_files) + len(between_core_and_std_files) + len(memchr_files) + len(std_files) + len(test_files) 73*d4726bddSHONG Yifan if partitioned_files_len != len(dot_a_files): 74*d4726bddSHONG Yifan partitioned = alloc_files + between_alloc_and_core_files + core_files + between_core_and_std_files + memchr_files + std_files + test_files 75*d4726bddSHONG Yifan for f in sorted(partitioned): 76*d4726bddSHONG Yifan # buildifier: disable=print 77*d4726bddSHONG Yifan print("File partitioned: {}".format(f.basename)) 78*d4726bddSHONG Yifan fail("rust_toolchain couldn't properly partition rlibs in rust_std. Partitioned {} out of {} files. This is probably a bug in the rule implementation.".format(partitioned_files_len, len(dot_a_files))) 79*d4726bddSHONG Yifan 80*d4726bddSHONG Yifan std_dylib = None 81*d4726bddSHONG Yifan 82*d4726bddSHONG Yifan for file in rust_std: 83*d4726bddSHONG Yifan if is_std_dylib(file): 84*d4726bddSHONG Yifan std_dylib = file 85*d4726bddSHONG Yifan break 86*d4726bddSHONG Yifan 87*d4726bddSHONG Yifan return [ 88*d4726bddSHONG Yifan DefaultInfo( 89*d4726bddSHONG Yifan files = depset(ctx.files.srcs), 90*d4726bddSHONG Yifan runfiles = ctx.runfiles(ctx.files.srcs), 91*d4726bddSHONG Yifan ), 92*d4726bddSHONG Yifan rust_common.stdlib_info( 93*d4726bddSHONG Yifan std_rlibs = std_rlibs, 94*d4726bddSHONG Yifan dot_a_files = dot_a_files, 95*d4726bddSHONG Yifan between_alloc_and_core_files = between_alloc_and_core_files, 96*d4726bddSHONG Yifan core_files = core_files, 97*d4726bddSHONG Yifan between_core_and_std_files = between_core_and_std_files, 98*d4726bddSHONG Yifan std_files = std_files, 99*d4726bddSHONG Yifan std_dylib = std_dylib, 100*d4726bddSHONG Yifan test_files = test_files, 101*d4726bddSHONG Yifan memchr_files = memchr_files, 102*d4726bddSHONG Yifan alloc_files = alloc_files, 103*d4726bddSHONG Yifan self_contained_files = self_contained_files, 104*d4726bddSHONG Yifan panic_files = panic_files, 105*d4726bddSHONG Yifan srcs = ctx.attr.srcs, 106*d4726bddSHONG Yifan ), 107*d4726bddSHONG Yifan ] 108*d4726bddSHONG Yifan 109*d4726bddSHONG Yifanrust_stdlib_filegroup = rule( 110*d4726bddSHONG Yifan doc = "A dedicated filegroup-like rule for Rust stdlib artifacts.", 111*d4726bddSHONG Yifan implementation = _rust_stdlib_filegroup_impl, 112*d4726bddSHONG Yifan attrs = { 113*d4726bddSHONG Yifan "srcs": attr.label_list( 114*d4726bddSHONG Yifan allow_files = True, 115*d4726bddSHONG Yifan doc = "The list of targets/files that are components of the rust-stdlib file group", 116*d4726bddSHONG Yifan mandatory = True, 117*d4726bddSHONG Yifan ), 118*d4726bddSHONG Yifan }, 119*d4726bddSHONG Yifan) 120*d4726bddSHONG Yifan 121*d4726bddSHONG Yifandef _ltl(library, ctx, cc_toolchain, feature_configuration): 122*d4726bddSHONG Yifan """A helper to generate `LibraryToLink` objects 123*d4726bddSHONG Yifan 124*d4726bddSHONG Yifan Args: 125*d4726bddSHONG Yifan library (File): A rust library file to link. 126*d4726bddSHONG Yifan ctx (ctx): The rule's context object. 127*d4726bddSHONG Yifan cc_toolchain (CcToolchainInfo): A cc toolchain provider to be used. 128*d4726bddSHONG Yifan feature_configuration (feature_configuration): feature_configuration to be queried. 129*d4726bddSHONG Yifan 130*d4726bddSHONG Yifan Returns: 131*d4726bddSHONG Yifan LibraryToLink: A provider containing information about libraries to link. 132*d4726bddSHONG Yifan """ 133*d4726bddSHONG Yifan return cc_common.create_library_to_link( 134*d4726bddSHONG Yifan actions = ctx.actions, 135*d4726bddSHONG Yifan feature_configuration = feature_configuration, 136*d4726bddSHONG Yifan cc_toolchain = cc_toolchain, 137*d4726bddSHONG Yifan static_library = library, 138*d4726bddSHONG Yifan pic_static_library = library, 139*d4726bddSHONG Yifan ) 140*d4726bddSHONG Yifan 141*d4726bddSHONG Yifandef _make_libstd_and_allocator_ccinfo(ctx, rust_std, allocator_library, std = "std"): 142*d4726bddSHONG Yifan """Make the CcInfo (if possible) for libstd and allocator libraries. 143*d4726bddSHONG Yifan 144*d4726bddSHONG Yifan Args: 145*d4726bddSHONG Yifan ctx (ctx): The rule's context object. 146*d4726bddSHONG Yifan rust_std: The Rust standard library. 147*d4726bddSHONG Yifan allocator_library: The target to use for providing allocator functions. 148*d4726bddSHONG Yifan std: Standard library flavor. Currently only "std" and "no_std_with_alloc" are supported, 149*d4726bddSHONG Yifan accompanied with the default panic behavior. 150*d4726bddSHONG Yifan 151*d4726bddSHONG Yifan 152*d4726bddSHONG Yifan Returns: 153*d4726bddSHONG Yifan A CcInfo object for the required libraries, or None if no such libraries are available. 154*d4726bddSHONG Yifan """ 155*d4726bddSHONG Yifan cc_toolchain, feature_configuration = find_cc_toolchain(ctx) 156*d4726bddSHONG Yifan cc_infos = [] 157*d4726bddSHONG Yifan 158*d4726bddSHONG Yifan if not rust_common.stdlib_info in rust_std: 159*d4726bddSHONG Yifan fail(dedent("""\ 160*d4726bddSHONG Yifan {} -- 161*d4726bddSHONG Yifan The `rust_lib` ({}) must be a target providing `rust_common.stdlib_info` 162*d4726bddSHONG Yifan (typically `rust_stdlib_filegroup` rule from @rules_rust//rust:defs.bzl). 163*d4726bddSHONG Yifan See https://github.com/bazelbuild/rules_rust/pull/802 for more information. 164*d4726bddSHONG Yifan """).format(ctx.label, rust_std)) 165*d4726bddSHONG Yifan rust_stdlib_info = rust_std[rust_common.stdlib_info] 166*d4726bddSHONG Yifan 167*d4726bddSHONG Yifan if rust_stdlib_info.self_contained_files: 168*d4726bddSHONG Yifan compilation_outputs = cc_common.create_compilation_outputs( 169*d4726bddSHONG Yifan objects = depset(rust_stdlib_info.self_contained_files), 170*d4726bddSHONG Yifan ) 171*d4726bddSHONG Yifan 172*d4726bddSHONG Yifan linking_context, _linking_outputs = cc_common.create_linking_context_from_compilation_outputs( 173*d4726bddSHONG Yifan name = ctx.label.name, 174*d4726bddSHONG Yifan actions = ctx.actions, 175*d4726bddSHONG Yifan feature_configuration = feature_configuration, 176*d4726bddSHONG Yifan cc_toolchain = cc_toolchain, 177*d4726bddSHONG Yifan compilation_outputs = compilation_outputs, 178*d4726bddSHONG Yifan ) 179*d4726bddSHONG Yifan 180*d4726bddSHONG Yifan cc_infos.append(CcInfo( 181*d4726bddSHONG Yifan linking_context = linking_context, 182*d4726bddSHONG Yifan )) 183*d4726bddSHONG Yifan 184*d4726bddSHONG Yifan if rust_stdlib_info.std_rlibs: 185*d4726bddSHONG Yifan alloc_inputs = depset( 186*d4726bddSHONG Yifan [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.alloc_files], 187*d4726bddSHONG Yifan ) 188*d4726bddSHONG Yifan between_alloc_and_core_inputs = depset( 189*d4726bddSHONG Yifan [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.between_alloc_and_core_files], 190*d4726bddSHONG Yifan transitive = [alloc_inputs], 191*d4726bddSHONG Yifan order = "topological", 192*d4726bddSHONG Yifan ) 193*d4726bddSHONG Yifan core_inputs = depset( 194*d4726bddSHONG Yifan [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.core_files], 195*d4726bddSHONG Yifan transitive = [between_alloc_and_core_inputs], 196*d4726bddSHONG Yifan order = "topological", 197*d4726bddSHONG Yifan ) 198*d4726bddSHONG Yifan 199*d4726bddSHONG Yifan # The libraries panic_abort and panic_unwind are alternatives. 200*d4726bddSHONG Yifan # The std by default requires panic_unwind. 201*d4726bddSHONG Yifan # Exclude panic_abort if panic_unwind is present. 202*d4726bddSHONG Yifan # TODO: Provide a setting to choose between panic_abort and panic_unwind. 203*d4726bddSHONG Yifan filtered_between_core_and_std_files = rust_stdlib_info.between_core_and_std_files 204*d4726bddSHONG Yifan has_panic_unwind = [ 205*d4726bddSHONG Yifan f 206*d4726bddSHONG Yifan for f in filtered_between_core_and_std_files 207*d4726bddSHONG Yifan if "panic_unwind" in f.basename 208*d4726bddSHONG Yifan ] 209*d4726bddSHONG Yifan if has_panic_unwind: 210*d4726bddSHONG Yifan filtered_between_core_and_std_files = [ 211*d4726bddSHONG Yifan f 212*d4726bddSHONG Yifan for f in filtered_between_core_and_std_files 213*d4726bddSHONG Yifan if "abort" not in f.basename 214*d4726bddSHONG Yifan ] 215*d4726bddSHONG Yifan core_alloc_and_panic_inputs = depset( 216*d4726bddSHONG Yifan [ 217*d4726bddSHONG Yifan _ltl(f, ctx, cc_toolchain, feature_configuration) 218*d4726bddSHONG Yifan for f in rust_stdlib_info.panic_files 219*d4726bddSHONG Yifan if "unwind" not in f.basename 220*d4726bddSHONG Yifan ], 221*d4726bddSHONG Yifan transitive = [core_inputs], 222*d4726bddSHONG Yifan order = "topological", 223*d4726bddSHONG Yifan ) 224*d4726bddSHONG Yifan else: 225*d4726bddSHONG Yifan core_alloc_and_panic_inputs = depset( 226*d4726bddSHONG Yifan [ 227*d4726bddSHONG Yifan _ltl(f, ctx, cc_toolchain, feature_configuration) 228*d4726bddSHONG Yifan for f in rust_stdlib_info.panic_files 229*d4726bddSHONG Yifan if "unwind" not in f.basename 230*d4726bddSHONG Yifan ], 231*d4726bddSHONG Yifan transitive = [core_inputs], 232*d4726bddSHONG Yifan order = "topological", 233*d4726bddSHONG Yifan ) 234*d4726bddSHONG Yifan memchr_inputs = depset( 235*d4726bddSHONG Yifan [ 236*d4726bddSHONG Yifan _ltl(f, ctx, cc_toolchain, feature_configuration) 237*d4726bddSHONG Yifan for f in rust_stdlib_info.memchr_files 238*d4726bddSHONG Yifan ], 239*d4726bddSHONG Yifan transitive = [core_inputs], 240*d4726bddSHONG Yifan order = "topological", 241*d4726bddSHONG Yifan ) 242*d4726bddSHONG Yifan between_core_and_std_inputs = depset( 243*d4726bddSHONG Yifan [ 244*d4726bddSHONG Yifan _ltl(f, ctx, cc_toolchain, feature_configuration) 245*d4726bddSHONG Yifan for f in filtered_between_core_and_std_files 246*d4726bddSHONG Yifan ], 247*d4726bddSHONG Yifan transitive = [memchr_inputs], 248*d4726bddSHONG Yifan order = "topological", 249*d4726bddSHONG Yifan ) 250*d4726bddSHONG Yifan 251*d4726bddSHONG Yifan if _experimental_link_std_dylib(ctx): 252*d4726bddSHONG Yifan # std dylib has everything so that we do not need to include all std_files 253*d4726bddSHONG Yifan std_inputs = depset( 254*d4726bddSHONG Yifan [cc_common.create_library_to_link( 255*d4726bddSHONG Yifan actions = ctx.actions, 256*d4726bddSHONG Yifan feature_configuration = feature_configuration, 257*d4726bddSHONG Yifan cc_toolchain = cc_toolchain, 258*d4726bddSHONG Yifan dynamic_library = rust_stdlib_info.std_dylib, 259*d4726bddSHONG Yifan )], 260*d4726bddSHONG Yifan ) 261*d4726bddSHONG Yifan else: 262*d4726bddSHONG Yifan std_inputs = depset( 263*d4726bddSHONG Yifan [ 264*d4726bddSHONG Yifan _ltl(f, ctx, cc_toolchain, feature_configuration) 265*d4726bddSHONG Yifan for f in rust_stdlib_info.std_files 266*d4726bddSHONG Yifan ], 267*d4726bddSHONG Yifan transitive = [between_core_and_std_inputs], 268*d4726bddSHONG Yifan order = "topological", 269*d4726bddSHONG Yifan ) 270*d4726bddSHONG Yifan 271*d4726bddSHONG Yifan test_inputs = depset( 272*d4726bddSHONG Yifan [ 273*d4726bddSHONG Yifan _ltl(f, ctx, cc_toolchain, feature_configuration) 274*d4726bddSHONG Yifan for f in rust_stdlib_info.test_files 275*d4726bddSHONG Yifan ], 276*d4726bddSHONG Yifan transitive = [std_inputs], 277*d4726bddSHONG Yifan order = "topological", 278*d4726bddSHONG Yifan ) 279*d4726bddSHONG Yifan 280*d4726bddSHONG Yifan if std == "std": 281*d4726bddSHONG Yifan link_inputs = cc_common.create_linker_input( 282*d4726bddSHONG Yifan owner = rust_std.label, 283*d4726bddSHONG Yifan libraries = test_inputs, 284*d4726bddSHONG Yifan ) 285*d4726bddSHONG Yifan elif std == "no_std_with_alloc": 286*d4726bddSHONG Yifan link_inputs = cc_common.create_linker_input( 287*d4726bddSHONG Yifan owner = rust_std.label, 288*d4726bddSHONG Yifan libraries = core_alloc_and_panic_inputs, 289*d4726bddSHONG Yifan ) 290*d4726bddSHONG Yifan else: 291*d4726bddSHONG Yifan fail("Requested '{}' std mode is currently not supported.".format(std)) 292*d4726bddSHONG Yifan 293*d4726bddSHONG Yifan allocator_inputs = None 294*d4726bddSHONG Yifan if allocator_library: 295*d4726bddSHONG Yifan allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs] 296*d4726bddSHONG Yifan 297*d4726bddSHONG Yifan cc_infos.append(CcInfo( 298*d4726bddSHONG Yifan linking_context = cc_common.create_linking_context( 299*d4726bddSHONG Yifan linker_inputs = depset( 300*d4726bddSHONG Yifan [link_inputs], 301*d4726bddSHONG Yifan transitive = allocator_inputs, 302*d4726bddSHONG Yifan order = "topological", 303*d4726bddSHONG Yifan ), 304*d4726bddSHONG Yifan ), 305*d4726bddSHONG Yifan )) 306*d4726bddSHONG Yifan 307*d4726bddSHONG Yifan if cc_infos: 308*d4726bddSHONG Yifan return cc_common.merge_cc_infos( 309*d4726bddSHONG Yifan direct_cc_infos = cc_infos, 310*d4726bddSHONG Yifan ) 311*d4726bddSHONG Yifan return None 312*d4726bddSHONG Yifan 313*d4726bddSHONG Yifandef _symlink_sysroot_tree(ctx, name, target): 314*d4726bddSHONG Yifan """Generate a set of symlinks to files from another target 315*d4726bddSHONG Yifan 316*d4726bddSHONG Yifan Args: 317*d4726bddSHONG Yifan ctx (ctx): The toolchain's context object 318*d4726bddSHONG Yifan name (str): The name of the sysroot directory (typically `ctx.label.name`) 319*d4726bddSHONG Yifan target (Target): A target owning files to symlink 320*d4726bddSHONG Yifan 321*d4726bddSHONG Yifan Returns: 322*d4726bddSHONG Yifan depset[File]: A depset of the generated symlink files 323*d4726bddSHONG Yifan """ 324*d4726bddSHONG Yifan tree_files = [] 325*d4726bddSHONG Yifan for file in target.files.to_list(): 326*d4726bddSHONG Yifan # Parse the path to the file relative to the workspace root so a 327*d4726bddSHONG Yifan # symlink matching this path can be created within the sysroot. 328*d4726bddSHONG Yifan 329*d4726bddSHONG Yifan # The code blow attempts to parse any workspace names out of the 330*d4726bddSHONG Yifan # path. For local targets, this code is a noop. 331*d4726bddSHONG Yifan if target.label.workspace_root: 332*d4726bddSHONG Yifan file_path = file.path.split(target.label.workspace_root, 1)[-1] 333*d4726bddSHONG Yifan else: 334*d4726bddSHONG Yifan file_path = file.path 335*d4726bddSHONG Yifan 336*d4726bddSHONG Yifan symlink = ctx.actions.declare_file("{}/{}".format(name, file_path.lstrip("/"))) 337*d4726bddSHONG Yifan 338*d4726bddSHONG Yifan ctx.actions.symlink( 339*d4726bddSHONG Yifan output = symlink, 340*d4726bddSHONG Yifan target_file = file, 341*d4726bddSHONG Yifan ) 342*d4726bddSHONG Yifan 343*d4726bddSHONG Yifan tree_files.append(symlink) 344*d4726bddSHONG Yifan 345*d4726bddSHONG Yifan return depset(tree_files) 346*d4726bddSHONG Yifan 347*d4726bddSHONG Yifandef _symlink_sysroot_bin(ctx, name, directory, target): 348*d4726bddSHONG Yifan """Crete a symlink to a target file. 349*d4726bddSHONG Yifan 350*d4726bddSHONG Yifan Args: 351*d4726bddSHONG Yifan ctx (ctx): The rule's context object 352*d4726bddSHONG Yifan name (str): A common name for the output directory 353*d4726bddSHONG Yifan directory (str): The directory under `name` to put the file in 354*d4726bddSHONG Yifan target (File): A File object to symlink to 355*d4726bddSHONG Yifan 356*d4726bddSHONG Yifan Returns: 357*d4726bddSHONG Yifan File: A newly generated symlink file 358*d4726bddSHONG Yifan """ 359*d4726bddSHONG Yifan symlink = ctx.actions.declare_file("{}/{}/{}".format( 360*d4726bddSHONG Yifan name, 361*d4726bddSHONG Yifan directory, 362*d4726bddSHONG Yifan target.basename, 363*d4726bddSHONG Yifan )) 364*d4726bddSHONG Yifan 365*d4726bddSHONG Yifan ctx.actions.symlink( 366*d4726bddSHONG Yifan output = symlink, 367*d4726bddSHONG Yifan target_file = target, 368*d4726bddSHONG Yifan is_executable = True, 369*d4726bddSHONG Yifan ) 370*d4726bddSHONG Yifan 371*d4726bddSHONG Yifan return symlink 372*d4726bddSHONG Yifan 373*d4726bddSHONG Yifandef _generate_sysroot( 374*d4726bddSHONG Yifan ctx, 375*d4726bddSHONG Yifan rustc, 376*d4726bddSHONG Yifan rustdoc, 377*d4726bddSHONG Yifan rustc_lib, 378*d4726bddSHONG Yifan cargo = None, 379*d4726bddSHONG Yifan clippy = None, 380*d4726bddSHONG Yifan cargo_clippy = None, 381*d4726bddSHONG Yifan llvm_tools = None, 382*d4726bddSHONG Yifan rust_std = None, 383*d4726bddSHONG Yifan rustfmt = None): 384*d4726bddSHONG Yifan """Generate a rust sysroot from collection of toolchain components 385*d4726bddSHONG Yifan 386*d4726bddSHONG Yifan Args: 387*d4726bddSHONG Yifan ctx (ctx): A context object from a `rust_toolchain` rule. 388*d4726bddSHONG Yifan rustc (File): The path to a `rustc` executable. 389*d4726bddSHONG Yifan rustdoc (File): The path to a `rustdoc` executable. 390*d4726bddSHONG Yifan rustc_lib (Target): A collection of Files containing dependencies of `rustc`. 391*d4726bddSHONG Yifan cargo (File, optional): The path to a `cargo` executable. 392*d4726bddSHONG Yifan cargo_clippy (File, optional): The path to a `cargo-clippy` executable. 393*d4726bddSHONG Yifan clippy (File, optional): The path to a `clippy-driver` executable. 394*d4726bddSHONG Yifan llvm_tools (Target, optional): A collection of llvm tools used by `rustc`. 395*d4726bddSHONG Yifan rust_std (Target, optional): A collection of Files containing Rust standard library components. 396*d4726bddSHONG Yifan rustfmt (File, optional): The path to a `rustfmt` executable. 397*d4726bddSHONG Yifan 398*d4726bddSHONG Yifan Returns: 399*d4726bddSHONG Yifan struct: A struct of generated files representing the new sysroot 400*d4726bddSHONG Yifan """ 401*d4726bddSHONG Yifan name = ctx.label.name 402*d4726bddSHONG Yifan 403*d4726bddSHONG Yifan # Define runfiles 404*d4726bddSHONG Yifan direct_files = [] 405*d4726bddSHONG Yifan transitive_file_sets = [] 406*d4726bddSHONG Yifan 407*d4726bddSHONG Yifan # Rustc 408*d4726bddSHONG Yifan sysroot_rustc = _symlink_sysroot_bin(ctx, name, "bin", rustc) 409*d4726bddSHONG Yifan direct_files.extend([sysroot_rustc]) 410*d4726bddSHONG Yifan 411*d4726bddSHONG Yifan # Rustc dependencies 412*d4726bddSHONG Yifan sysroot_rustc_lib = None 413*d4726bddSHONG Yifan if rustc_lib: 414*d4726bddSHONG Yifan sysroot_rustc_lib = _symlink_sysroot_tree(ctx, name, rustc_lib) 415*d4726bddSHONG Yifan transitive_file_sets.extend([sysroot_rustc_lib]) 416*d4726bddSHONG Yifan 417*d4726bddSHONG Yifan # Rustdoc 418*d4726bddSHONG Yifan sysroot_rustdoc = _symlink_sysroot_bin(ctx, name, "bin", rustdoc) 419*d4726bddSHONG Yifan direct_files.extend([sysroot_rustdoc]) 420*d4726bddSHONG Yifan 421*d4726bddSHONG Yifan # Clippy 422*d4726bddSHONG Yifan sysroot_clippy = None 423*d4726bddSHONG Yifan if clippy: 424*d4726bddSHONG Yifan sysroot_clippy = _symlink_sysroot_bin(ctx, name, "bin", clippy) 425*d4726bddSHONG Yifan direct_files.extend([sysroot_clippy]) 426*d4726bddSHONG Yifan 427*d4726bddSHONG Yifan # Cargo 428*d4726bddSHONG Yifan sysroot_cargo = None 429*d4726bddSHONG Yifan if cargo: 430*d4726bddSHONG Yifan sysroot_cargo = _symlink_sysroot_bin(ctx, name, "bin", cargo) 431*d4726bddSHONG Yifan direct_files.extend([sysroot_cargo]) 432*d4726bddSHONG Yifan 433*d4726bddSHONG Yifan # Cargo-clippy 434*d4726bddSHONG Yifan sysroot_cargo_clippy = None 435*d4726bddSHONG Yifan if cargo_clippy: 436*d4726bddSHONG Yifan sysroot_cargo_clippy = _symlink_sysroot_bin(ctx, name, "bin", cargo_clippy) 437*d4726bddSHONG Yifan direct_files.extend([sysroot_cargo_clippy]) 438*d4726bddSHONG Yifan 439*d4726bddSHONG Yifan # Rustfmt 440*d4726bddSHONG Yifan sysroot_rustfmt = None 441*d4726bddSHONG Yifan if rustfmt: 442*d4726bddSHONG Yifan sysroot_rustfmt = _symlink_sysroot_bin(ctx, name, "bin", rustfmt) 443*d4726bddSHONG Yifan direct_files.extend([sysroot_rustfmt]) 444*d4726bddSHONG Yifan 445*d4726bddSHONG Yifan # Llvm tools 446*d4726bddSHONG Yifan sysroot_llvm_tools = None 447*d4726bddSHONG Yifan if llvm_tools: 448*d4726bddSHONG Yifan sysroot_llvm_tools = _symlink_sysroot_tree(ctx, name, llvm_tools) 449*d4726bddSHONG Yifan transitive_file_sets.extend([sysroot_llvm_tools]) 450*d4726bddSHONG Yifan 451*d4726bddSHONG Yifan # Rust standard library 452*d4726bddSHONG Yifan sysroot_rust_std = None 453*d4726bddSHONG Yifan if rust_std: 454*d4726bddSHONG Yifan sysroot_rust_std = _symlink_sysroot_tree(ctx, name, rust_std) 455*d4726bddSHONG Yifan transitive_file_sets.extend([sysroot_rust_std]) 456*d4726bddSHONG Yifan 457*d4726bddSHONG Yifan # Made available to support $(location) expansion in stdlib_linkflags and extra_rustc_flags. 458*d4726bddSHONG Yifan transitive_file_sets.append(depset(ctx.files.rust_std)) 459*d4726bddSHONG Yifan 460*d4726bddSHONG Yifan # Declare a file in the root of the sysroot to make locating the sysroot easy 461*d4726bddSHONG Yifan sysroot_anchor = ctx.actions.declare_file("{}/rust.sysroot".format(name)) 462*d4726bddSHONG Yifan ctx.actions.write( 463*d4726bddSHONG Yifan output = sysroot_anchor, 464*d4726bddSHONG Yifan content = "\n".join([ 465*d4726bddSHONG Yifan "cargo: {}".format(cargo), 466*d4726bddSHONG Yifan "clippy: {}".format(clippy), 467*d4726bddSHONG Yifan "cargo-clippy: {}".format(cargo_clippy), 468*d4726bddSHONG Yifan "llvm_tools: {}".format(llvm_tools), 469*d4726bddSHONG Yifan "rust_std: {}".format(rust_std), 470*d4726bddSHONG Yifan "rustc_lib: {}".format(rustc_lib), 471*d4726bddSHONG Yifan "rustc: {}".format(rustc), 472*d4726bddSHONG Yifan "rustdoc: {}".format(rustdoc), 473*d4726bddSHONG Yifan "rustfmt: {}".format(rustfmt), 474*d4726bddSHONG Yifan ]), 475*d4726bddSHONG Yifan ) 476*d4726bddSHONG Yifan 477*d4726bddSHONG Yifan # Create a depset of all sysroot files (symlinks and their real paths) 478*d4726bddSHONG Yifan all_files = depset(direct_files, transitive = transitive_file_sets) 479*d4726bddSHONG Yifan 480*d4726bddSHONG Yifan return struct( 481*d4726bddSHONG Yifan all_files = all_files, 482*d4726bddSHONG Yifan cargo = sysroot_cargo, 483*d4726bddSHONG Yifan clippy = sysroot_clippy, 484*d4726bddSHONG Yifan cargo_clippy = sysroot_cargo_clippy, 485*d4726bddSHONG Yifan rust_std = sysroot_rust_std, 486*d4726bddSHONG Yifan rustc = sysroot_rustc, 487*d4726bddSHONG Yifan rustc_lib = sysroot_rustc_lib, 488*d4726bddSHONG Yifan rustdoc = sysroot_rustdoc, 489*d4726bddSHONG Yifan rustfmt = sysroot_rustfmt, 490*d4726bddSHONG Yifan sysroot_anchor = sysroot_anchor, 491*d4726bddSHONG Yifan ) 492*d4726bddSHONG Yifan 493*d4726bddSHONG Yifandef _experimental_use_cc_common_link(ctx): 494*d4726bddSHONG Yifan return ctx.attr.experimental_use_cc_common_link[BuildSettingInfo].value 495*d4726bddSHONG Yifan 496*d4726bddSHONG Yifandef _rust_toolchain_impl(ctx): 497*d4726bddSHONG Yifan """The rust_toolchain implementation 498*d4726bddSHONG Yifan 499*d4726bddSHONG Yifan Args: 500*d4726bddSHONG Yifan ctx (ctx): The rule's context object 501*d4726bddSHONG Yifan 502*d4726bddSHONG Yifan Returns: 503*d4726bddSHONG Yifan list: A list containing the target's toolchain Provider info 504*d4726bddSHONG Yifan """ 505*d4726bddSHONG Yifan compilation_mode_opts = {} 506*d4726bddSHONG Yifan for k, opt_level in ctx.attr.opt_level.items(): 507*d4726bddSHONG Yifan if not k in ctx.attr.debug_info: 508*d4726bddSHONG Yifan fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k)) 509*d4726bddSHONG Yifan if not k in ctx.attr.strip_level: 510*d4726bddSHONG Yifan fail("Compilation mode {} is not defined in strip_level but is defined opt_level".format(k)) 511*d4726bddSHONG Yifan compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = opt_level, strip_level = ctx.attr.strip_level[k]) 512*d4726bddSHONG Yifan for k in ctx.attr.debug_info.keys(): 513*d4726bddSHONG Yifan if not k in ctx.attr.opt_level: 514*d4726bddSHONG Yifan fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k)) 515*d4726bddSHONG Yifan 516*d4726bddSHONG Yifan rename_first_party_crates = ctx.attr._rename_first_party_crates[BuildSettingInfo].value 517*d4726bddSHONG Yifan third_party_dir = ctx.attr._third_party_dir[BuildSettingInfo].value 518*d4726bddSHONG Yifan pipelined_compilation = ctx.attr._pipelined_compilation[BuildSettingInfo].value 519*d4726bddSHONG Yifan no_std = ctx.attr._no_std[BuildSettingInfo].value 520*d4726bddSHONG Yifan 521*d4726bddSHONG Yifan experimental_use_global_allocator = ctx.attr._experimental_use_global_allocator[BuildSettingInfo].value 522*d4726bddSHONG Yifan if _experimental_use_cc_common_link(ctx): 523*d4726bddSHONG Yifan if experimental_use_global_allocator and not ctx.attr.global_allocator_library: 524*d4726bddSHONG Yifan fail("rust_toolchain.experimental_use_cc_common_link with --@rules_rust//rust/settings:experimental_use_global_allocator " + 525*d4726bddSHONG Yifan "requires rust_toolchain.global_allocator_library to be set") 526*d4726bddSHONG Yifan if not ctx.attr.allocator_library: 527*d4726bddSHONG Yifan fail("rust_toolchain.experimental_use_cc_common_link requires rust_toolchain.allocator_library to be set") 528*d4726bddSHONG Yifan if experimental_use_global_allocator and not _experimental_use_cc_common_link(ctx): 529*d4726bddSHONG Yifan fail( 530*d4726bddSHONG Yifan "Using @rules_rust//rust/settings:experimental_use_global_allocator requires" + 531*d4726bddSHONG Yifan "--@rules_rust//rust/settings:experimental_use_cc_common_link to be set", 532*d4726bddSHONG Yifan ) 533*d4726bddSHONG Yifan 534*d4726bddSHONG Yifan rust_std = ctx.attr.rust_std 535*d4726bddSHONG Yifan 536*d4726bddSHONG Yifan sysroot = _generate_sysroot( 537*d4726bddSHONG Yifan ctx = ctx, 538*d4726bddSHONG Yifan rustc = ctx.file.rustc, 539*d4726bddSHONG Yifan rustdoc = ctx.file.rust_doc, 540*d4726bddSHONG Yifan rustc_lib = ctx.attr.rustc_lib, 541*d4726bddSHONG Yifan rust_std = rust_std, 542*d4726bddSHONG Yifan rustfmt = ctx.file.rustfmt, 543*d4726bddSHONG Yifan clippy = ctx.file.clippy_driver, 544*d4726bddSHONG Yifan cargo = ctx.file.cargo, 545*d4726bddSHONG Yifan cargo_clippy = ctx.file.cargo_clippy, 546*d4726bddSHONG Yifan llvm_tools = ctx.attr.llvm_tools, 547*d4726bddSHONG Yifan ) 548*d4726bddSHONG Yifan 549*d4726bddSHONG Yifan expanded_stdlib_linkflags = [] 550*d4726bddSHONG Yifan for flag in ctx.attr.stdlib_linkflags: 551*d4726bddSHONG Yifan expanded_stdlib_linkflags.append( 552*d4726bddSHONG Yifan dedup_expand_location( 553*d4726bddSHONG Yifan ctx, 554*d4726bddSHONG Yifan flag, 555*d4726bddSHONG Yifan targets = rust_std[rust_common.stdlib_info].srcs, 556*d4726bddSHONG Yifan ), 557*d4726bddSHONG Yifan ) 558*d4726bddSHONG Yifan 559*d4726bddSHONG Yifan expanded_extra_rustc_flags = [] 560*d4726bddSHONG Yifan for flag in ctx.attr.extra_rustc_flags: 561*d4726bddSHONG Yifan expanded_extra_rustc_flags.append( 562*d4726bddSHONG Yifan dedup_expand_location( 563*d4726bddSHONG Yifan ctx, 564*d4726bddSHONG Yifan flag, 565*d4726bddSHONG Yifan targets = rust_std[rust_common.stdlib_info].srcs, 566*d4726bddSHONG Yifan ), 567*d4726bddSHONG Yifan ) 568*d4726bddSHONG Yifan 569*d4726bddSHONG Yifan linking_context = cc_common.create_linking_context( 570*d4726bddSHONG Yifan linker_inputs = depset([ 571*d4726bddSHONG Yifan cc_common.create_linker_input( 572*d4726bddSHONG Yifan owner = ctx.label, 573*d4726bddSHONG Yifan user_link_flags = depset(expanded_stdlib_linkflags), 574*d4726bddSHONG Yifan ), 575*d4726bddSHONG Yifan ]), 576*d4726bddSHONG Yifan ) 577*d4726bddSHONG Yifan 578*d4726bddSHONG Yifan # Contains linker flags needed to link Rust standard library. 579*d4726bddSHONG Yifan # These need to be added to linker command lines when the linker is not rustc 580*d4726bddSHONG Yifan # (rustc does this automatically). Linker flags wrapped in an otherwise empty 581*d4726bddSHONG Yifan # `CcInfo` to provide the flags in a way that doesn't duplicate them per target 582*d4726bddSHONG Yifan # providing a `CcInfo`. 583*d4726bddSHONG Yifan stdlib_linkflags_cc_info = CcInfo( 584*d4726bddSHONG Yifan compilation_context = cc_common.create_compilation_context(), 585*d4726bddSHONG Yifan linking_context = linking_context, 586*d4726bddSHONG Yifan ) 587*d4726bddSHONG Yifan 588*d4726bddSHONG Yifan # Determine the path and short_path of the sysroot 589*d4726bddSHONG Yifan sysroot_path = sysroot.sysroot_anchor.dirname 590*d4726bddSHONG Yifan sysroot_short_path, _, _ = sysroot.sysroot_anchor.short_path.rpartition("/") 591*d4726bddSHONG Yifan 592*d4726bddSHONG Yifan # Variables for make variable expansion 593*d4726bddSHONG Yifan make_variables = { 594*d4726bddSHONG Yifan "RUSTC": sysroot.rustc.path, 595*d4726bddSHONG Yifan "RUSTDOC": sysroot.rustdoc.path, 596*d4726bddSHONG Yifan "RUST_DEFAULT_EDITION": ctx.attr.default_edition or "", 597*d4726bddSHONG Yifan "RUST_SYSROOT": sysroot_path, 598*d4726bddSHONG Yifan "RUST_SYSROOT_SHORT": sysroot_short_path, 599*d4726bddSHONG Yifan } 600*d4726bddSHONG Yifan 601*d4726bddSHONG Yifan if sysroot.cargo: 602*d4726bddSHONG Yifan make_variables.update({ 603*d4726bddSHONG Yifan "CARGO": sysroot.cargo.path, 604*d4726bddSHONG Yifan }) 605*d4726bddSHONG Yifan 606*d4726bddSHONG Yifan if sysroot.rustfmt: 607*d4726bddSHONG Yifan make_variables.update({ 608*d4726bddSHONG Yifan "RUSTFMT": sysroot.rustfmt.path, 609*d4726bddSHONG Yifan }) 610*d4726bddSHONG Yifan 611*d4726bddSHONG Yifan make_variable_info = platform_common.TemplateVariableInfo(make_variables) 612*d4726bddSHONG Yifan 613*d4726bddSHONG Yifan exec_triple = triple(ctx.attr.exec_triple) 614*d4726bddSHONG Yifan 615*d4726bddSHONG Yifan if not exec_triple.system: 616*d4726bddSHONG Yifan fail("No system was provided for the execution platform. Please update {}".format( 617*d4726bddSHONG Yifan ctx.label, 618*d4726bddSHONG Yifan )) 619*d4726bddSHONG Yifan 620*d4726bddSHONG Yifan if ctx.attr.target_triple and ctx.attr.target_json: 621*d4726bddSHONG Yifan fail("Do not specify both target_triple and target_json, either use a builtin triple or provide a custom specification file. Please update {}".format( 622*d4726bddSHONG Yifan ctx.label, 623*d4726bddSHONG Yifan )) 624*d4726bddSHONG Yifan 625*d4726bddSHONG Yifan target_triple = None 626*d4726bddSHONG Yifan target_json = None 627*d4726bddSHONG Yifan target_arch = None 628*d4726bddSHONG Yifan target_os = None 629*d4726bddSHONG Yifan 630*d4726bddSHONG Yifan if ctx.attr.target_triple: 631*d4726bddSHONG Yifan target_triple = triple(ctx.attr.target_triple) 632*d4726bddSHONG Yifan target_arch = target_triple.arch 633*d4726bddSHONG Yifan target_os = target_triple.system 634*d4726bddSHONG Yifan 635*d4726bddSHONG Yifan elif ctx.attr.target_json: 636*d4726bddSHONG Yifan # Ensure the data provided is valid json 637*d4726bddSHONG Yifan target_json_content = json.decode(ctx.attr.target_json) 638*d4726bddSHONG Yifan target_json = ctx.actions.declare_file("{}.target.json".format(ctx.label.name)) 639*d4726bddSHONG Yifan 640*d4726bddSHONG Yifan ctx.actions.write( 641*d4726bddSHONG Yifan output = target_json, 642*d4726bddSHONG Yifan content = json.encode_indent(target_json_content, indent = " " * 4), 643*d4726bddSHONG Yifan ) 644*d4726bddSHONG Yifan 645*d4726bddSHONG Yifan if "arch" in target_json_content: 646*d4726bddSHONG Yifan target_arch = target_json_content["arch"] 647*d4726bddSHONG Yifan if "os" in target_json_content: 648*d4726bddSHONG Yifan target_os = target_json_content["os"] 649*d4726bddSHONG Yifan else: 650*d4726bddSHONG Yifan fail("Either `target_triple` or `target_json` must be provided. Please update {}".format( 651*d4726bddSHONG Yifan ctx.label, 652*d4726bddSHONG Yifan )) 653*d4726bddSHONG Yifan 654*d4726bddSHONG Yifan toolchain = platform_common.ToolchainInfo( 655*d4726bddSHONG Yifan all_files = sysroot.all_files, 656*d4726bddSHONG Yifan binary_ext = ctx.attr.binary_ext, 657*d4726bddSHONG Yifan cargo = sysroot.cargo, 658*d4726bddSHONG Yifan clippy_driver = sysroot.clippy, 659*d4726bddSHONG Yifan cargo_clippy = sysroot.cargo_clippy, 660*d4726bddSHONG Yifan compilation_mode_opts = compilation_mode_opts, 661*d4726bddSHONG Yifan crosstool_files = ctx.files._cc_toolchain, 662*d4726bddSHONG Yifan default_edition = ctx.attr.default_edition, 663*d4726bddSHONG Yifan dylib_ext = ctx.attr.dylib_ext, 664*d4726bddSHONG Yifan env = ctx.attr.env, 665*d4726bddSHONG Yifan exec_triple = exec_triple, 666*d4726bddSHONG Yifan libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.allocator_library, "std"), 667*d4726bddSHONG Yifan libstd_and_global_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.global_allocator_library, "std"), 668*d4726bddSHONG Yifan nostd_and_global_allocator_cc_info = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.global_allocator_library, "no_std_with_alloc"), 669*d4726bddSHONG Yifan llvm_cov = ctx.file.llvm_cov, 670*d4726bddSHONG Yifan llvm_profdata = ctx.file.llvm_profdata, 671*d4726bddSHONG Yifan make_variables = make_variable_info, 672*d4726bddSHONG Yifan rust_doc = sysroot.rustdoc, 673*d4726bddSHONG Yifan rust_std = sysroot.rust_std, 674*d4726bddSHONG Yifan rust_std_paths = depset([file.dirname for file in sysroot.rust_std.to_list()]), 675*d4726bddSHONG Yifan rustc = sysroot.rustc, 676*d4726bddSHONG Yifan rustc_lib = sysroot.rustc_lib, 677*d4726bddSHONG Yifan rustfmt = sysroot.rustfmt, 678*d4726bddSHONG Yifan staticlib_ext = ctx.attr.staticlib_ext, 679*d4726bddSHONG Yifan stdlib_linkflags = stdlib_linkflags_cc_info, 680*d4726bddSHONG Yifan extra_rustc_flags = expanded_extra_rustc_flags, 681*d4726bddSHONG Yifan extra_rustc_flags_for_crate_types = ctx.attr.extra_rustc_flags_for_crate_types, 682*d4726bddSHONG Yifan extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags, 683*d4726bddSHONG Yifan per_crate_rustc_flags = ctx.attr.per_crate_rustc_flags, 684*d4726bddSHONG Yifan sysroot = sysroot_path, 685*d4726bddSHONG Yifan sysroot_short_path = sysroot_short_path, 686*d4726bddSHONG Yifan target_arch = target_arch, 687*d4726bddSHONG Yifan target_flag_value = target_json.path if target_json else target_triple.str, 688*d4726bddSHONG Yifan target_json = target_json, 689*d4726bddSHONG Yifan target_os = target_os, 690*d4726bddSHONG Yifan target_triple = target_triple, 691*d4726bddSHONG Yifan 692*d4726bddSHONG Yifan # Experimental and incompatible flags 693*d4726bddSHONG Yifan _rename_first_party_crates = rename_first_party_crates, 694*d4726bddSHONG Yifan _third_party_dir = third_party_dir, 695*d4726bddSHONG Yifan _pipelined_compilation = pipelined_compilation, 696*d4726bddSHONG Yifan _experimental_link_std_dylib = _experimental_link_std_dylib(ctx), 697*d4726bddSHONG Yifan _experimental_use_cc_common_link = _experimental_use_cc_common_link(ctx), 698*d4726bddSHONG Yifan _experimental_use_global_allocator = experimental_use_global_allocator, 699*d4726bddSHONG Yifan _experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value, 700*d4726bddSHONG Yifan _experimental_toolchain_generated_sysroot = ctx.attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled, 701*d4726bddSHONG Yifan _incompatible_no_rustc_sysroot_env = ctx.attr._incompatible_no_rustc_sysroot_env[IncompatibleFlagInfo].enabled, 702*d4726bddSHONG Yifan _no_std = no_std, 703*d4726bddSHONG Yifan ) 704*d4726bddSHONG Yifan return [ 705*d4726bddSHONG Yifan toolchain, 706*d4726bddSHONG Yifan make_variable_info, 707*d4726bddSHONG Yifan ] 708*d4726bddSHONG Yifan 709*d4726bddSHONG Yifandef _experimental_link_std_dylib(ctx): 710*d4726bddSHONG Yifan return not is_exec_configuration(ctx) and \ 711*d4726bddSHONG Yifan ctx.attr.experimental_link_std_dylib[BuildSettingInfo].value and \ 712*d4726bddSHONG Yifan ctx.attr.rust_std[rust_common.stdlib_info].std_dylib != None 713*d4726bddSHONG Yifan 714*d4726bddSHONG Yifanrust_toolchain = rule( 715*d4726bddSHONG Yifan implementation = _rust_toolchain_impl, 716*d4726bddSHONG Yifan fragments = ["cpp"], 717*d4726bddSHONG Yifan attrs = { 718*d4726bddSHONG Yifan "allocator_library": attr.label( 719*d4726bddSHONG Yifan doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.", 720*d4726bddSHONG Yifan default = "@rules_rust//ffi/cc/allocator_library", 721*d4726bddSHONG Yifan ), 722*d4726bddSHONG Yifan "binary_ext": attr.string( 723*d4726bddSHONG Yifan doc = "The extension for binaries created from rustc.", 724*d4726bddSHONG Yifan mandatory = True, 725*d4726bddSHONG Yifan ), 726*d4726bddSHONG Yifan "cargo": attr.label( 727*d4726bddSHONG Yifan doc = "The location of the `cargo` binary. Can be a direct source or a filegroup containing one item.", 728*d4726bddSHONG Yifan allow_single_file = True, 729*d4726bddSHONG Yifan cfg = "exec", 730*d4726bddSHONG Yifan ), 731*d4726bddSHONG Yifan "cargo_clippy": attr.label( 732*d4726bddSHONG Yifan doc = "The location of the `cargo_clippy` binary. Can be a direct source or a filegroup containing one item.", 733*d4726bddSHONG Yifan allow_single_file = True, 734*d4726bddSHONG Yifan cfg = "exec", 735*d4726bddSHONG Yifan ), 736*d4726bddSHONG Yifan "clippy_driver": attr.label( 737*d4726bddSHONG Yifan doc = "The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item.", 738*d4726bddSHONG Yifan allow_single_file = True, 739*d4726bddSHONG Yifan cfg = "exec", 740*d4726bddSHONG Yifan ), 741*d4726bddSHONG Yifan "debug_info": attr.string_dict( 742*d4726bddSHONG Yifan doc = "Rustc debug info levels per opt level", 743*d4726bddSHONG Yifan default = { 744*d4726bddSHONG Yifan "dbg": "2", 745*d4726bddSHONG Yifan "fastbuild": "0", 746*d4726bddSHONG Yifan "opt": "0", 747*d4726bddSHONG Yifan }, 748*d4726bddSHONG Yifan ), 749*d4726bddSHONG Yifan "default_edition": attr.string( 750*d4726bddSHONG Yifan doc = ( 751*d4726bddSHONG Yifan "The edition to use for rust_* rules that don't specify an edition. " + 752*d4726bddSHONG Yifan "If absent, every rule is required to specify its `edition` attribute." 753*d4726bddSHONG Yifan ), 754*d4726bddSHONG Yifan ), 755*d4726bddSHONG Yifan "dylib_ext": attr.string( 756*d4726bddSHONG Yifan doc = "The extension for dynamic libraries created from rustc.", 757*d4726bddSHONG Yifan mandatory = True, 758*d4726bddSHONG Yifan ), 759*d4726bddSHONG Yifan "env": attr.string_dict( 760*d4726bddSHONG Yifan doc = "Environment variables to set in actions.", 761*d4726bddSHONG Yifan ), 762*d4726bddSHONG Yifan "exec_triple": attr.string( 763*d4726bddSHONG Yifan doc = ( 764*d4726bddSHONG Yifan "The platform triple for the toolchains execution environment. " + 765*d4726bddSHONG Yifan "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations" 766*d4726bddSHONG Yifan ), 767*d4726bddSHONG Yifan mandatory = True, 768*d4726bddSHONG Yifan ), 769*d4726bddSHONG Yifan "experimental_link_std_dylib": attr.label( 770*d4726bddSHONG Yifan default = Label("@rules_rust//rust/settings:experimental_link_std_dylib"), 771*d4726bddSHONG Yifan doc = "Label to a boolean build setting that controls whether whether to link libstd dynamically.", 772*d4726bddSHONG Yifan ), 773*d4726bddSHONG Yifan "experimental_use_cc_common_link": attr.label( 774*d4726bddSHONG Yifan default = Label("//rust/settings:experimental_use_cc_common_link"), 775*d4726bddSHONG Yifan doc = "Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries.", 776*d4726bddSHONG Yifan ), 777*d4726bddSHONG Yifan "extra_exec_rustc_flags": attr.string_list( 778*d4726bddSHONG Yifan doc = "Extra flags to pass to rustc in exec configuration", 779*d4726bddSHONG Yifan ), 780*d4726bddSHONG Yifan "extra_rustc_flags": attr.string_list( 781*d4726bddSHONG Yifan doc = "Extra flags to pass to rustc in non-exec configuration. Subject to location expansion with respect to the srcs of the `rust_std` attribute.", 782*d4726bddSHONG Yifan ), 783*d4726bddSHONG Yifan "extra_rustc_flags_for_crate_types": attr.string_list_dict( 784*d4726bddSHONG Yifan doc = "Extra flags to pass to rustc based on crate type", 785*d4726bddSHONG Yifan ), 786*d4726bddSHONG Yifan "global_allocator_library": attr.label( 787*d4726bddSHONG Yifan doc = "Target that provides allocator functions for when a global allocator is present.", 788*d4726bddSHONG Yifan default = "@rules_rust//ffi/cc/global_allocator_library", 789*d4726bddSHONG Yifan ), 790*d4726bddSHONG Yifan "llvm_cov": attr.label( 791*d4726bddSHONG Yifan doc = "The location of the `llvm-cov` binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage.", 792*d4726bddSHONG Yifan allow_single_file = True, 793*d4726bddSHONG Yifan cfg = "exec", 794*d4726bddSHONG Yifan ), 795*d4726bddSHONG Yifan "llvm_profdata": attr.label( 796*d4726bddSHONG Yifan doc = "The location of the `llvm-profdata` binary. Can be a direct source or a filegroup containing one item. If `llvm_cov` is None, this can be None as well and rust code is not instrumented for coverage.", 797*d4726bddSHONG Yifan allow_single_file = True, 798*d4726bddSHONG Yifan cfg = "exec", 799*d4726bddSHONG Yifan ), 800*d4726bddSHONG Yifan "llvm_tools": attr.label( 801*d4726bddSHONG Yifan doc = "LLVM tools that are shipped with the Rust toolchain.", 802*d4726bddSHONG Yifan allow_files = True, 803*d4726bddSHONG Yifan ), 804*d4726bddSHONG Yifan "opt_level": attr.string_dict( 805*d4726bddSHONG Yifan doc = "Rustc optimization levels.", 806*d4726bddSHONG Yifan default = { 807*d4726bddSHONG Yifan "dbg": "0", 808*d4726bddSHONG Yifan "fastbuild": "0", 809*d4726bddSHONG Yifan "opt": "3", 810*d4726bddSHONG Yifan }, 811*d4726bddSHONG Yifan ), 812*d4726bddSHONG Yifan "per_crate_rustc_flags": attr.string_list( 813*d4726bddSHONG Yifan doc = "Extra flags to pass to rustc in non-exec configuration", 814*d4726bddSHONG Yifan ), 815*d4726bddSHONG Yifan "rust_doc": attr.label( 816*d4726bddSHONG Yifan doc = "The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item.", 817*d4726bddSHONG Yifan allow_single_file = True, 818*d4726bddSHONG Yifan cfg = "exec", 819*d4726bddSHONG Yifan mandatory = True, 820*d4726bddSHONG Yifan ), 821*d4726bddSHONG Yifan "rust_std": attr.label( 822*d4726bddSHONG Yifan doc = "The Rust standard library.", 823*d4726bddSHONG Yifan mandatory = True, 824*d4726bddSHONG Yifan ), 825*d4726bddSHONG Yifan "rustc": attr.label( 826*d4726bddSHONG Yifan doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item.", 827*d4726bddSHONG Yifan allow_single_file = True, 828*d4726bddSHONG Yifan cfg = "exec", 829*d4726bddSHONG Yifan mandatory = True, 830*d4726bddSHONG Yifan ), 831*d4726bddSHONG Yifan "rustc_lib": attr.label( 832*d4726bddSHONG Yifan doc = "The libraries used by rustc during compilation.", 833*d4726bddSHONG Yifan cfg = "exec", 834*d4726bddSHONG Yifan ), 835*d4726bddSHONG Yifan "rustfmt": attr.label( 836*d4726bddSHONG Yifan doc = "**Deprecated**: Instead see [rustfmt_toolchain](#rustfmt_toolchain)", 837*d4726bddSHONG Yifan allow_single_file = True, 838*d4726bddSHONG Yifan cfg = "exec", 839*d4726bddSHONG Yifan ), 840*d4726bddSHONG Yifan "staticlib_ext": attr.string( 841*d4726bddSHONG Yifan doc = "The extension for static libraries created from rustc.", 842*d4726bddSHONG Yifan mandatory = True, 843*d4726bddSHONG Yifan ), 844*d4726bddSHONG Yifan "stdlib_linkflags": attr.string_list( 845*d4726bddSHONG Yifan doc = ( 846*d4726bddSHONG Yifan "Additional linker flags to use when Rust standard library is linked by a C++ linker " + 847*d4726bddSHONG Yifan "(rustc will deal with these automatically). Subject to location expansion with respect " + 848*d4726bddSHONG Yifan "to the srcs of the `rust_std` attribute." 849*d4726bddSHONG Yifan ), 850*d4726bddSHONG Yifan mandatory = True, 851*d4726bddSHONG Yifan ), 852*d4726bddSHONG Yifan "strip_level": attr.string_dict( 853*d4726bddSHONG Yifan doc = ( 854*d4726bddSHONG Yifan "Rustc strip levels. For all potential options, see " + 855*d4726bddSHONG Yifan "https://doc.rust-lang.org/rustc/codegen-options/index.html#strip" 856*d4726bddSHONG Yifan ), 857*d4726bddSHONG Yifan default = { 858*d4726bddSHONG Yifan "dbg": "none", 859*d4726bddSHONG Yifan "fastbuild": "none", 860*d4726bddSHONG Yifan "opt": "debuginfo", 861*d4726bddSHONG Yifan }, 862*d4726bddSHONG Yifan ), 863*d4726bddSHONG Yifan "target_json": attr.string( 864*d4726bddSHONG Yifan doc = ("Override the target_triple with a custom target specification. " + 865*d4726bddSHONG Yifan "For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"), 866*d4726bddSHONG Yifan ), 867*d4726bddSHONG Yifan "target_triple": attr.string( 868*d4726bddSHONG Yifan doc = ( 869*d4726bddSHONG Yifan "The platform triple for the toolchains target environment. " + 870*d4726bddSHONG Yifan "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations" 871*d4726bddSHONG Yifan ), 872*d4726bddSHONG Yifan ), 873*d4726bddSHONG Yifan "_cc_toolchain": attr.label( 874*d4726bddSHONG Yifan default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"), 875*d4726bddSHONG Yifan ), 876*d4726bddSHONG Yifan "_experimental_toolchain_generated_sysroot": attr.label( 877*d4726bddSHONG Yifan default = Label("//rust/settings:experimental_toolchain_generated_sysroot"), 878*d4726bddSHONG Yifan doc = ( 879*d4726bddSHONG Yifan "Label to a boolean build setting that lets the rule knows wheter to set --sysroot to rustc" + 880*d4726bddSHONG Yifan "This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot." 881*d4726bddSHONG Yifan ), 882*d4726bddSHONG Yifan ), 883*d4726bddSHONG Yifan "_experimental_use_coverage_metadata_files": attr.label( 884*d4726bddSHONG Yifan default = Label("//rust/settings:experimental_use_coverage_metadata_files"), 885*d4726bddSHONG Yifan ), 886*d4726bddSHONG Yifan "_experimental_use_global_allocator": attr.label( 887*d4726bddSHONG Yifan default = Label("//rust/settings:experimental_use_global_allocator"), 888*d4726bddSHONG Yifan doc = ( 889*d4726bddSHONG Yifan "Label to a boolean build setting that informs the target build whether a global allocator is being used." + 890*d4726bddSHONG Yifan "This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_use_global_allocator." 891*d4726bddSHONG Yifan ), 892*d4726bddSHONG Yifan ), 893*d4726bddSHONG Yifan "_incompatible_no_rustc_sysroot_env": attr.label( 894*d4726bddSHONG Yifan default = Label("//rust/settings:incompatible_no_rustc_sysroot_env"), 895*d4726bddSHONG Yifan ), 896*d4726bddSHONG Yifan "_no_std": attr.label( 897*d4726bddSHONG Yifan default = Label("//:no_std"), 898*d4726bddSHONG Yifan ), 899*d4726bddSHONG Yifan "_pipelined_compilation": attr.label( 900*d4726bddSHONG Yifan default = Label("//rust/settings:pipelined_compilation"), 901*d4726bddSHONG Yifan ), 902*d4726bddSHONG Yifan "_rename_first_party_crates": attr.label( 903*d4726bddSHONG Yifan default = Label("//rust/settings:rename_first_party_crates"), 904*d4726bddSHONG Yifan ), 905*d4726bddSHONG Yifan "_third_party_dir": attr.label( 906*d4726bddSHONG Yifan default = Label("//rust/settings:third_party_dir"), 907*d4726bddSHONG Yifan ), 908*d4726bddSHONG Yifan }, 909*d4726bddSHONG Yifan toolchains = [ 910*d4726bddSHONG Yifan "@bazel_tools//tools/cpp:toolchain_type", 911*d4726bddSHONG Yifan ], 912*d4726bddSHONG Yifan doc = """Declares a Rust toolchain for use. 913*d4726bddSHONG Yifan 914*d4726bddSHONG YifanThis is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform. 915*d4726bddSHONG Yifan 916*d4726bddSHONG YifanExample: 917*d4726bddSHONG Yifan 918*d4726bddSHONG YifanSuppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This \ 919*d4726bddSHONG Yifansupport can be used in Bazel by defining a new toolchain definition and declaration: 920*d4726bddSHONG Yifan 921*d4726bddSHONG Yifan```python 922*d4726bddSHONG Yifanload('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') 923*d4726bddSHONG Yifan 924*d4726bddSHONG Yifanrust_toolchain( 925*d4726bddSHONG Yifan name = "rust_cpuX_impl", 926*d4726bddSHONG Yifan binary_ext = "", 927*d4726bddSHONG Yifan dylib_ext = ".so", 928*d4726bddSHONG Yifan exec_triple = "cpuX-unknown-linux-gnu", 929*d4726bddSHONG Yifan rust_doc = "@rust_cpuX//:rustdoc", 930*d4726bddSHONG Yifan rust_std = "@rust_cpuX//:rust_std", 931*d4726bddSHONG Yifan rustc = "@rust_cpuX//:rustc", 932*d4726bddSHONG Yifan rustc_lib = "@rust_cpuX//:rustc_lib", 933*d4726bddSHONG Yifan staticlib_ext = ".a", 934*d4726bddSHONG Yifan stdlib_linkflags = ["-lpthread", "-ldl"], 935*d4726bddSHONG Yifan target_triple = "cpuX-unknown-linux-gnu", 936*d4726bddSHONG Yifan) 937*d4726bddSHONG Yifan 938*d4726bddSHONG Yifantoolchain( 939*d4726bddSHONG Yifan name = "rust_cpuX", 940*d4726bddSHONG Yifan exec_compatible_with = [ 941*d4726bddSHONG Yifan "@platforms//cpu:cpuX", 942*d4726bddSHONG Yifan "@platforms//os:linux", 943*d4726bddSHONG Yifan ], 944*d4726bddSHONG Yifan target_compatible_with = [ 945*d4726bddSHONG Yifan "@platforms//cpu:cpuX", 946*d4726bddSHONG Yifan "@platforms//os:linux", 947*d4726bddSHONG Yifan ], 948*d4726bddSHONG Yifan toolchain = ":rust_cpuX_impl", 949*d4726bddSHONG Yifan) 950*d4726bddSHONG Yifan``` 951*d4726bddSHONG Yifan 952*d4726bddSHONG YifanThen, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass \ 953*d4726bddSHONG Yifanit to the `"--extra_toolchains"` flag for Bazel, and it will be used. 954*d4726bddSHONG Yifan 955*d4726bddSHONG YifanSee `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository \ 956*d4726bddSHONG Yifanwith the actual binaries and libraries. 957*d4726bddSHONG Yifan""", 958*d4726bddSHONG Yifan) 959