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