fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
443b089c
main
1# Copyright 2020 Google 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""" 16Rust Analyzer Bazel rules. 17 18rust_analyzer will generate a rust-project.json file for the 19given targets. This file can be consumed by rust-analyzer as an alternative 20to Cargo.toml files. 21""" 22 23load ( "//rust/platform:triple_mappings.bzl" , "system_to_dylib_ext" , "triple_to_system" ) 24load ( "//rust/private:common.bzl" , "rust_common" ) 25load ( "//rust/private:providers.bzl" , "RustAnalyzerGroupInfo" , "RustAnalyzerInfo" ) 26load ( "//rust/private:rustc.bzl" , "BuildInfo" ) 27load ( 28"//rust/private:utils.bzl" , 29"concat" , 30"dedent" , 31"dedup_expand_location" , 32"find_toolchain" , 33) 34 35def write_rust_analyzer_spec_file ( ctx , attrs , owner , base_info ): 36"""Write a rust-analyzer spec info file. 37 38Args: 39ctx (ctx): The current rule's context object. 40attrs (dict): A mapping of attributes. 41owner (Label): The label of the owner of the spec info. 42base_info (RustAnalyzerInfo): The data the resulting RustAnalyzerInfo is based on. 43 44Returns: 45RustAnalyzerInfo: Info with the embedded spec file. 46""" 47crate_spec = ctx . actions . declare_file ( "{}.rust_analyzer_crate_spec.json" . format ( owner . name )) 48 49# Recreate the provider with the spec file embedded in it. 50rust_analyzer_info = RustAnalyzerInfo ( 51aliases = base_info . aliases , 52crate = base_info . crate , 53cfgs = base_info . cfgs , 54env = base_info . env , 55deps = base_info . deps , 56id = base_info . id , 57crate_specs = depset ( direct = [ crate_spec ], transitive = [ base_info . crate_specs ]), 58proc_macro_dylib_path = base_info . proc_macro_dylib_path , 59build_info = base_info . build_info , 60) 61 62ctx . actions . write ( 63output = crate_spec , 64content = json . encode_indent ( 65_create_single_crate ( 66ctx , 67attrs , 68rust_analyzer_info , 69), 70indent = " " * 4 , 71), 72) 73 74return rust_analyzer_info 75 76def _rust_analyzer_aspect_impl ( target , ctx ): 77if ( rust_common . crate_info not in target and 78rust_common . test_crate_info not in target and 79rust_common . crate_group_info not in target ): 80return [] 81 82if RustAnalyzerInfo in target or RustAnalyzerGroupInfo in target : 83return [] 84 85toolchain = find_toolchain ( ctx ) 86 87# Always add `test` & `debug_assertions`. See rust-analyzer source code: 88# https://github.com/rust-analyzer/rust-analyzer/blob/2021-11-15/crates/project_model/src/workspace.rs#L529-L531 89cfgs = [ "test" , "debug_assertions" ] 90if hasattr ( ctx . rule . attr , "crate_features" ): 91cfgs += [ 'feature="{}"' . format ( f ) for f in ctx . rule . attr . crate_features ] 92if hasattr ( ctx . rule . attr , "rustc_flags" ): 93cfgs += [ f [ 6 :] for f in ctx . rule . attr . rustc_flags if f . startswith ( "--cfg " ) or f . startswith ( "--cfg=" )] 94 95build_info = None 96for dep in getattr ( ctx . rule . attr , "deps" , []): 97# Save BuildInfo if we find any (for build script output) 98if BuildInfo in dep : 99build_info = dep [ BuildInfo ] 100 101# Gather required info from dependencies. 102label_to_id = {} #{ Label of dependency => crate_id } 103crate_specs = [] #[ depset of File - transitive crate_spec . json files ] 104attrs = ctx . rule . attr 105all_deps = getattr ( attrs , "deps" , []) + getattr ( attrs , "proc_macro_deps" , []) + \ 106[ dep for dep in [ getattr ( attrs , "crate" , None ), getattr ( attrs , "actual" , None )] if dep != None ] 107for dep in all_deps : 108if RustAnalyzerInfo in dep : 109label_to_id [ dep . label ] = dep [ RustAnalyzerInfo ]. id 110crate_specs . append ( dep [ RustAnalyzerInfo ]. crate_specs ) 111if RustAnalyzerGroupInfo in dep : 112for expanded_dep in dep [ RustAnalyzerGroupInfo ]. deps : 113label_to_id [ expanded_dep ] = expanded_dep 114crate_specs . append ( dep [ RustAnalyzerGroupInfo ]. crate_specs ) 115 116deps = label_to_id . values () 117crate_specs = depset ( transitive = crate_specs ) 118 119if rust_common . crate_group_info in target : 120return [ RustAnalyzerGroupInfo ( deps = deps , crate_specs = crate_specs )] 121elif rust_common . crate_info in target : 122crate_info = target [ rust_common . crate_info ] 123elif rust_common . test_crate_info in target : 124crate_info = target [ rust_common . test_crate_info ]. crate 125else : 126fail ( "Unexpected target type: {}" . format ( target )) 127 128aliases = { 129label_to_id [ target . label ]: name 130for ( target , name ) in getattr ( attrs , "aliases" , {}). items () 131if target . label in label_to_id 132} 133 134# An arbitrary unique and stable identifier. 135crate_id = "ID-" + crate_info . root . path 136 137rust_analyzer_info = write_rust_analyzer_spec_file ( ctx , ctx . rule . attr , ctx . label , RustAnalyzerInfo ( 138id = crate_id , 139aliases = aliases , 140crate = crate_info , 141cfgs = cfgs , 142env = crate_info . rustc_env , 143deps = deps , 144crate_specs = crate_specs , 145proc_macro_dylib_path = find_proc_macro_dylib_path ( toolchain , target ), 146build_info = build_info , 147)) 148 149return [ 150rust_analyzer_info , 151OutputGroupInfo ( rust_analyzer_crate_spec = rust_analyzer_info . crate_specs ), 152] 153 154def find_proc_macro_dylib_path ( toolchain , target ): 155"""Find the proc_macro_dylib_path of target. Returns None if target crate is not type proc-macro. 156 157Args: 158toolchain: The current rust toolchain. 159target: The current target. 160Returns: 161(path): The path to the proc macro dylib, or None if this crate is not a proc-macro. 162""" 163if rust_common . crate_info in target : 164crate_info = target [ rust_common . crate_info ] 165elif rust_common . test_crate_info in target : 166crate_info = target [ rust_common . test_crate_info ]. crate 167else : 168return None 169 170if crate_info . type != "proc-macro" : 171return None 172 173dylib_ext = system_to_dylib_ext ( triple_to_system ( toolchain . target_triple )) 174for action in target . actions : 175for output in action . outputs . to_list (): 176if output . extension == dylib_ext [ 1 :]: 177return output . path 178 179# Failed to find the dylib path inside a proc-macro crate. 180# TODO: Should this be an error? 181return None 182 183rust_analyzer_aspect = aspect ( 184attr_aspects = [ "deps" , "proc_macro_deps" , "crate" , "actual" , "proto" ], 185implementation = _rust_analyzer_aspect_impl , 186toolchains = [ str ( Label ( "//rust:toolchain_type" ))], 187doc = "Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.json" , 188) 189 190# Paths in the generated JSON file begin with one of these placeholders. 191# The gen_rust_project driver will replace them with absolute paths. 192_WORKSPACE_TEMPLATE = "__WORKSPACE__/" 193_EXEC_ROOT_TEMPLATE = "__EXEC_ROOT__/" 194_OUTPUT_BASE_TEMPLATE = "__OUTPUT_BASE__/" 195 196def _create_single_crate ( ctx , attrs , info ): 197"""Creates a crate in the rust-project.json format. 198 199Args: 200ctx (ctx): The rule context. 201attrs (dict): A mapping of attributes. 202info (RustAnalyzerInfo): RustAnalyzerInfo for the current crate. 203 204Returns: 205(dict) The crate rust-project.json representation 206""" 207crate_name = info . crate . name 208crate = dict () 209crate [ "crate_id" ] = info . id 210crate [ "display_name" ] = crate_name 211crate [ "edition" ] = info . crate . edition 212crate [ "env" ] = {} 213crate [ "crate_type" ] = info . crate . type 214 215# Switch on external/ to determine if crates are in the workspace or remote. 216# TODO: Some folks may want to override this for vendored dependencies. 217is_external = info . crate . root . path . startswith ( "external/" ) 218is_generated = not info . crate . root . is_source 219path_prefix = _EXEC_ROOT_TEMPLATE if is_external or is_generated else _WORKSPACE_TEMPLATE 220crate [ "is_workspace_member" ] = not is_external 221crate [ "root_module" ] = path_prefix + info . crate . root . path 222crate [ "source" ] = { "exclude_dirs" : [], "include_dirs" : []} 223 224if is_generated : 225srcs = getattr ( ctx . rule . files , "srcs" , []) 226src_map = { src . short_path : src for src in srcs if src . is_source } 227if info . crate . root . short_path in src_map : 228crate [ "root_module" ] = _WORKSPACE_TEMPLATE + src_map [ info . crate . root . short_path ]. path 229crate [ "source" ][ "include_dirs" ]. append ( path_prefix + info . crate . root . dirname ) 230 231if info . build_info != None and info . build_info . out_dir != None : 232out_dir_path = info . build_info . out_dir . path 233crate [ "env" ]. update ({ "OUT_DIR" : _EXEC_ROOT_TEMPLATE + out_dir_path }) 234 235# We have to tell rust-analyzer about our out_dir since it's not under the crate root. 236crate [ "source" ][ "include_dirs" ]. extend ([ 237path_prefix + info . crate . root . dirname , 238_EXEC_ROOT_TEMPLATE + out_dir_path , 239]) 240 241# TODO: The only imagined use case is an env var holding a filename in the workspace passed to a 242# macro like include_bytes!. Other use cases might exist that require more complex logic. 243expand_targets = concat ([ getattr ( attrs , attr , []) for attr in [ "data" , "compile_data" ]]) 244 245crate [ "env" ]. update ({ k : dedup_expand_location ( ctx , v , expand_targets ) for k , v in info . env . items ()}) 246 247# Omit when a crate appears to depend on itself (e.g. foo_test crates). 248# It can happen a single source file is present in multiple crates - there can 249# be a `rust_library` with a `lib.rs` file, and a `rust_test` for the `test` 250# module in that file. Tests can declare more dependencies than what library 251# had. Therefore we had to collect all RustAnalyzerInfos for a given crate 252# and take deps from all of them. 253 254# There's one exception - if the dependency is the same crate name as the 255# the crate being processed, we don't add it as a dependency to itself. This is 256# common and expected - `rust_test.crate` pointing to the `rust_library`. 257crate [ "deps" ] = [ id for id in info . deps if id != info . id ] 258crate [ "aliases" ] = info . aliases 259crate [ "cfg" ] = info . cfgs 260toolchain = find_toolchain ( ctx ) 261crate [ "target" ] = ( _EXEC_ROOT_TEMPLATE + toolchain . target_json . path ) if toolchain . target_json else toolchain . target_flag_value 262if info . proc_macro_dylib_path != None : 263crate [ "proc_macro_dylib_path" ] = _EXEC_ROOT_TEMPLATE + info . proc_macro_dylib_path 264return crate 265 266def _rust_analyzer_toolchain_impl ( ctx ): 267toolchain = platform_common . ToolchainInfo ( 268proc_macro_srv = ctx . executable . proc_macro_srv , 269rustc = ctx . executable . rustc , 270rustc_srcs = ctx . attr . rustc_srcs , 271) 272 273return [ toolchain ] 274 275rust_analyzer_toolchain = rule ( 276implementation = _rust_analyzer_toolchain_impl , 277doc = "A toolchain for [rust-analyzer](https://rust-analyzer.github.io/)." , 278attrs = { 279"proc_macro_srv" : attr . label ( 280doc = "The path to a `rust_analyzer_proc_macro_srv` binary." , 281cfg = "exec" , 282executable = True , 283allow_single_file = True , 284), 285"rustc" : attr . label ( 286doc = "The path to a `rustc` binary." , 287cfg = "exec" , 288executable = True , 289allow_single_file = True , 290mandatory = True , 291), 292"rustc_srcs" : attr . label ( 293doc = "The source code of rustc." , 294mandatory = True , 295), 296}, 297) 298 299def _rust_analyzer_detect_sysroot_impl ( ctx ): 300rust_analyzer_toolchain = ctx . toolchains [ Label ( "@rules_rust//rust/rust_analyzer:toolchain_type" )] 301 302if not rust_analyzer_toolchain . rustc_srcs : 303fail ( 304"Current Rust-Analyzer toolchain doesn't contain rustc sources in `rustc_srcs` attribute." , 305"These are needed by rust-analyzer. If you are using the default Rust toolchain, add `rust_repositories(include_rustc_srcs = True, ...).` to your WORKSPACE file." , 306) 307 308rustc_srcs = rust_analyzer_toolchain . rustc_srcs 309 310sysroot_src = rustc_srcs . label . package + "/library" 311if rustc_srcs . label . workspace_root : 312sysroot_src = _OUTPUT_BASE_TEMPLATE + rustc_srcs . label . workspace_root + "/" + sysroot_src 313else : 314sysroot_src = _WORKSPACE_TEMPLATE + sysroot_src 315 316rustc = rust_analyzer_toolchain . rustc 317sysroot_dir , _ , bin_dir = rustc . dirname . rpartition ( "/" ) 318if bin_dir != "bin" : 319fail ( "The rustc path is expected to be relative to the sysroot as `bin/rustc`. Instead got: {}" . format ( 320rustc . path , 321)) 322 323sysroot = _OUTPUT_BASE_TEMPLATE + sysroot_dir 324 325toolchain_info = { 326"sysroot" : sysroot , 327"sysroot_src" : sysroot_src , 328} 329 330output = ctx . actions . declare_file ( ctx . label . name + ".rust_analyzer_toolchain.json" ) 331ctx . actions . write ( 332output = output , 333content = json . encode_indent ( toolchain_info , indent = " " * 4 ), 334) 335 336return [ DefaultInfo ( files = depset ([ output ]))] 337 338rust_analyzer_detect_sysroot = rule ( 339implementation = _rust_analyzer_detect_sysroot_impl , 340toolchains = [ 341"@rules_rust//rust:toolchain_type" , 342"@rules_rust//rust/rust_analyzer:toolchain_type" , 343], 344doc = dedent ( """\ 345Detect the sysroot and store in a file for use by the gen_rust_project tool. 346""" ), 347)