fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
a5853fd3
rust-analyzer
1# Copyright 2019 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"""Rust Bindgen rules""" 16 17load ( "//rust:defs.bzl" , "rust_library" ) 18 19# buildifier: disable=bzl-visibility 20load ( "//rust/private:rustc.bzl" , "get_linker_and_args" ) 21 22# buildifier: disable=bzl-visibility 23load ( "//rust/private:utils.bzl" , "find_cc_toolchain" , "get_preferred_artifact" ) 24 25# TODO(hlopko): use the more robust logic from rustc.bzl also here, through a reasonable API. 26def _get_libs_for_static_executable ( dep ): 27"""find the libraries used for linking a static executable. 28 29Args: 30dep (Target): A cc_library target. 31 32Returns: 33depset: A depset[File] 34""" 35linker_inputs = dep [ CcInfo ]. linking_context . linker_inputs . to_list () 36return depset ([ get_preferred_artifact ( lib , use_pic = False ) for li in linker_inputs for lib in li . libraries ]) 37 38def rust_bindgen_library ( 39name , 40header , 41cc_lib , 42bindgen_flags = None , 43clang_flags = None , 44rustfmt = True , 45** kwargs ): 46"""Generates a rust source file for `header`, and builds a rust_library. 47 48Arguments are the same as `rust_bindgen`, and `kwargs` are passed directly to rust_library. 49 50Args: 51name (str): A unique name for this target. 52header (str): The label of the .h file to generate bindings for. 53cc_lib (str): The label of the cc_library that contains the .h file. This is used to find the transitive includes. 54bindgen_flags (list, optional): Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details. 55clang_flags (list, optional): Flags to pass directly to the clang executable. 56rustfmt (bool, optional): Enable or disable running rustfmt on the generated file. 57**kwargs: Arguments to forward to the underlying `rust_library` rule. 58""" 59 60tags = kwargs . get ( "tags" ) or [] 61if "tags" in kwargs : 62kwargs . pop ( "tags" ) 63 64deps = kwargs . get ( "deps" ) or [] 65if "deps" in kwargs : 66kwargs . pop ( "deps" ) 67 68rust_bindgen ( 69name = name + "__bindgen" , 70header = header , 71cc_lib = cc_lib , 72bindgen_flags = bindgen_flags or [], 73clang_flags = clang_flags or [], 74rustfmt = rustfmt , 75tags = tags , 76) 77 78rust_library ( 79name = name , 80srcs = [ name + "__bindgen.rs" ], 81tags = tags + [ "__bindgen" , "noclippy" ], 82deps = deps + [ cc_lib ], 83** kwargs 84) 85 86def _rust_bindgen_impl ( ctx ): 87toolchain = ctx . toolchains [ Label ( "//bindgen:toolchain_type" )] 88rustfmt_toolchain = ctx . toolchains [ Label ( "//rust/rustfmt:toolchain_type" )] 89 90# nb. We can't grab the cc_library`s direct headers, so a header must be provided. 91cc_lib = ctx . attr . cc_lib 92header = ctx . file . header 93cc_header_list = ctx . attr . cc_lib [ CcInfo ]. compilation_context . headers . to_list () 94if header not in cc_header_list : 95fail ( "Header {} is not in {}'s transitive headers." . format ( ctx . attr . header , cc_lib ), "header" ) 96 97toolchain = ctx . toolchains [ Label ( "//bindgen:toolchain_type" )] 98bindgen_bin = toolchain . bindgen 99rustfmt_bin = rustfmt_toolchain . rustfmt 100clang_bin = toolchain . clang 101libclang = toolchain . libclang 102libstdcxx = toolchain . libstdcxx 103 104# rustfmt is not where bindgen expects to find it, so we format manually 105bindgen_args = [ "--no-rustfmt-bindings" ] + ctx . attr . bindgen_flags 106clang_args = ctx . attr . clang_flags 107 108output = ctx . outputs . out 109 110# libclang should only have 1 output file 111libclang_dir = _get_libs_for_static_executable ( libclang ). to_list ()[ 0 ]. dirname 112include_directories = cc_lib [ CcInfo ]. compilation_context . includes . to_list () 113quote_include_directories = cc_lib [ CcInfo ]. compilation_context . quote_includes . to_list () 114system_include_directories = cc_lib [ CcInfo ]. compilation_context . system_includes . to_list () 115 116# Vanilla usage of bindgen produces formatted output, here we do the same if we have `rustfmt` in our toolchain. 117run_rustfmt = toolchain . default_rustfmt or ctx . attr . rustfmt 118if run_rustfmt : 119unformatted_output = ctx . actions . declare_file ( output . basename + ".unformatted" ) 120else : 121unformatted_output = output 122 123args = ctx . actions . args () 124args . add_all ( bindgen_args ) 125args . add ( header . path ) 126args . add ( "--output" , unformatted_output ) 127args . add ( "--" ) 128args . add_all ( include_directories , before_each = "-I" ) 129args . add_all ( quote_include_directories , before_each = "-iquote" ) 130args . add_all ( system_include_directories , before_each = "-isystem" ) 131args . add_all ( clang_args ) 132 133env = { 134"CLANG_PATH" : clang_bin . path , 135"LIBCLANG_PATH" : libclang_dir , 136"RUST_BACKTRACE" : "1" , 137} 138cc_toolchain , feature_configuration = find_cc_toolchain ( ctx ) 139_ , _ , linker_env = get_linker_and_args ( ctx , ctx . attr , "bin" , cc_toolchain , feature_configuration , None ) 140env . update ( ** linker_env ) 141 142# Set the dynamic linker search path so that clang uses the libstdcxx from the toolchain. 143# DYLD_LIBRARY_PATH is LD_LIBRARY_PATH on macOS. 144if libstdcxx : 145env [ "LD_LIBRARY_PATH" ] = ":" . join ([ f . dirname for f in _get_libs_for_static_executable ( libstdcxx ). to_list ()]) 146env [ "DYLD_LIBRARY_PATH" ] = env [ "LD_LIBRARY_PATH" ] 147 148ctx . actions . run ( 149executable = bindgen_bin , 150inputs = depset ( 151[ header ], 152transitive = [ 153cc_lib [ CcInfo ]. compilation_context . headers , 154_get_libs_for_static_executable ( libclang ), 155] + ([ 156_get_libs_for_static_executable ( libstdcxx ), 157] if libstdcxx else []), 158), 159outputs = [ unformatted_output ], 160mnemonic = "RustBindgen" , 161progress_message = "Generating bindings for {}.." . format ( header . path ), 162env = env , 163arguments = [ args ], 164tools = [ clang_bin ], 165) 166 167if run_rustfmt : 168rustfmt_args = ctx . actions . args () 169rustfmt_args . add ( "--stdout-file" , output ) 170rustfmt_args . add ( "--" ) 171rustfmt_args . add ( rustfmt_bin ) 172rustfmt_args . add ( "--emit" , "stdout" ) 173rustfmt_args . add ( "--quiet" ) 174rustfmt_args . add ( unformatted_output ) 175 176ctx . actions . run ( 177executable = ctx . executable . _process_wrapper , 178inputs = [ unformatted_output ], 179outputs = [ output ], 180arguments = [ rustfmt_args ], 181tools = [ rustfmt_toolchain . all_files ], 182mnemonic = "RustfmtBindgen" , 183) 184 185rust_bindgen = rule ( 186doc = "Generates a rust source file from a cc_library and a header." , 187implementation = _rust_bindgen_impl , 188attrs = { 189"bindgen_flags" : attr . string_list ( 190doc = "Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details." , 191), 192"cc_lib" : attr . label ( 193doc = "The cc_library that contains the `.h` file. This is used to find the transitive includes." , 194providers = [ CcInfo ], 195), 196"clang_flags" : attr . string_list ( 197doc = "Flags to pass directly to the clang executable." , 198), 199"header" : attr . label ( 200doc = "The `.h` file to generate bindings for." , 201allow_single_file = True , 202), 203"rustfmt" : attr . bool ( 204doc = "Enable or disable running rustfmt on the generated file." , 205default = True , 206), 207"_cc_toolchain" : attr . label ( 208default = Label ( "@bazel_tools//tools/cpp:current_cc_toolchain" ), 209), 210"_process_wrapper" : attr . label ( 211default = Label ( "//util/process_wrapper" ), 212executable = True , 213allow_single_file = True , 214cfg = "exec" , 215), 216}, 217outputs = { "out" : "%{name}.rs" }, 218fragments = [ "cpp" ], 219toolchains = [ 220str ( Label ( "//bindgen:toolchain_type" )), 221str ( Label ( "//rust:toolchain_type" )), 222str ( Label ( "//rust/rustfmt:toolchain_type" )), 223"@bazel_tools//tools/cpp:toolchain_type" , 224], 225incompatible_use_toolchain_transition = True , 226) 227 228def _rust_bindgen_toolchain_impl ( ctx ): 229if ctx . attr . rustfmt : 230# buildifier: disable=print 231( "The `rustfmt` attribute is deprecated. Please remove it on {} and register a `rustfmt_toolchain` instead." . format ( 232ctx . label , 233)) 234 235return platform_common . ToolchainInfo ( 236bindgen = ctx . executable . bindgen , 237clang = ctx . executable . clang , 238libclang = ctx . attr . libclang , 239libstdcxx = ctx . attr . libstdcxx , 240default_rustfmt = ctx . attr . default_rustfmt , 241) 242 243rust_bindgen_toolchain = rule ( 244_rust_bindgen_toolchain_impl , 245doc = """\ 246The tools required for the `rust_bindgen` rule. 247 248This rule depends on the [`bindgen`](https://crates.io/crates/bindgen) binary crate, and it 249in turn depends on both a clang binary and the clang library. To obtain these dependencies, 250`rust_bindgen_dependencies` imports bindgen and its dependencies. 251 252```python 253load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain") 254 255rust_bindgen_toolchain( 256name = "bindgen_toolchain_impl", 257bindgen = "//my/rust:bindgen", 258clang = "//my/clang:clang", 259libclang = "//my/clang:libclang.so", 260libstdcxx = "//my/cpp:libstdc++", 261) 262 263toolchain( 264name = "bindgen_toolchain", 265toolchain = "bindgen_toolchain_impl", 266toolchain_type = "@rules_rust//bindgen:toolchain_type", 267) 268``` 269 270This toolchain will then need to be registered in the current `WORKSPACE`. 271For additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html). 272""" , 273attrs = { 274"bindgen" : attr . label ( 275doc = "The label of a `bindgen` executable." , 276executable = True , 277cfg = "exec" , 278), 279"clang" : attr . label ( 280doc = "The label of a `clang` executable." , 281executable = True , 282cfg = "exec" , 283), 284"default_rustfmt" : attr . bool ( 285doc = "If set, `rust_bindgen` targets will always format generated sources with `rustfmt`." , 286mandatory = False , 287), 288"libclang" : attr . label ( 289doc = "A cc_library that provides bindgen's runtime dependency on libclang." , 290cfg = "exec" , 291providers = [ CcInfo ], 292), 293"libstdcxx" : attr . label ( 294doc = "A cc_library that satisfies libclang's libstdc++ dependency. This is used to make the execution of clang hermetic. If None, system libraries will be used instead." , 295cfg = "exec" , 296providers = [ CcInfo ], 297mandatory = False , 298), 299"rustfmt" : attr . label ( 300doc = "**Deprecated**: Instead, register a `rustfmt_toolchain` and refer to the `rust_bindgen_toolchain.default_rustfmt` and `rust_bindgen.rustfmt` attributes." , 301executable = True , 302cfg = "exec" , 303mandatory = False , 304), 305}, 306)