rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

[BUG] Can't link to c++ dependencies on v0.46.0

Open brt-adam-snaider opened this issue 1 year ago • 1 comments

$ bazel build -c opt @aos//aos:init_rs                                                                                                                                                                                       
ERROR: /home/adam.snaider/.cache/bazel/_bazel_adam.snaider/bca7a8928611241c3a03674f4fc2245d/external/aos/aos/BUILD:205:16: in rust_library rule @@aos//aos:init_rs:                                                                                                   
Traceback (most recent call last):                                                                                                                                                                                                                                    
        File "/home/adam.snaider/.cache/bazel/_bazel_adam.snaider/bca7a8928611241c3a03674f4fc2245d/external/rules_rust/rust/private/rust.bzl", line 87, column 32, in _rust_library_impl                                                                              
                return _rust_library_common(ctx, "rlib")                                                                                                                                                                                                              
        File "/home/adam.snaider/.cache/bazel/_bazel_adam.snaider/bca7a8928611241c3a03674f4fc2245d/external/rules_rust/rust/private/rust.bzl", line 183, column 32, in _rust_library_common                                                                           
                return rustc_compile_action(                                                                                                                                                                                                                          
        File "/home/adam.snaider/.cache/bazel/_bazel_adam.snaider/bca7a8928611241c3a03674f4fc2245d/external/rules_rust/rust/private/rustc.bzl", line 1455, column 28, in rustc_compile_action                                                                         
                runfiles = ctx.runfiles(                                                                                                                                                                                                                              
Error in runfiles: at index 0 of files, got element of type NoneType, want File                                                                                                                                                                                       
ERROR: /home/adam.snaider/.cache/bazel/_bazel_adam.snaider/bca7a8928611241c3a03674f4fc2245d/external/aos/aos/BUILD:205:16: Analysis of target '@@aos//aos:init_rs' failed                                                                                             
ERROR: Analysis of target '@@aos//aos:init_rs' failed; build aborted                                                                                                                                                                                                  
INFO: Elapsed time: 0.224s, Critical Path: 0.01s                                                                                                                                                                                                                      
INFO: 1 process: 1 internal.                                                                                                                                                                                                                                          
ERROR: Build did NOT complete successfully    

This error only seems to happen when I try to add a cc_library as a dependency.

Issue doesn't not appear to be present on 0.45.1

brt-adam-snaider avatar Jun 14 '24 20:06 brt-adam-snaider

Can you provide a minimal example so we can dig into this farther?

UebelAndre avatar Jun 15 '24 03:06 UebelAndre

@UebelAndre

Here is a minimal example

load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library")

cc_import(
    name = "nvenc",
    interface_library = select({
        "@platforms//cpu:aarch64": "Lib/linux/stubs/aarch64/libnvidia-encode.so",
        "@platforms//cpu:x86_64": "Lib/linux/stubs/x86_64/libnvidia-encode.so",
    }),
    visibility = ["//visibility:public"],
    hdrs = [
        "Interface/nvEncodeAPI.h",
    ],
    system_provided = 1,
)

write_file(
    name = "empty",
    out = "empty.cc",
    content = [""],
)

# `cc_library` required to satisfy bindgen
cc_library(
    name = "nvenc_cc_library",
    srcs = [":empty"],
    deps = [
        ":nvenc",
    ],
)

rust_bindgen_library(
    name = "nvenc_sys",
    bindgen_flags = [
        "--allowlist-type=NV.+",
        "--allowlist-function=NvEnc.+",
        "--allowlist-var=NV.+",
        "--default-enum-style=rust",
        "--no-layout-tests",
        "--with-derive-default",
    ],
    cc_lib = ":nvenc_cc_library",
    header = "Interface/nvEncodeAPI.h",
    rustc_flags = [
        "-Anon_upper_case_globals",
        "-Anon_camel_case_types",
        "-Anon_snake_case",
    ],
    visibility = ["//visibility:public"],
)

What I did to work around this is via a patch

    runfiles = ctx.runfiles(
         files = getattr(ctx.files, "data", []) +
                 ([] if experimental_use_coverage_metadata_files else coverage_runfiles) +
-                dynamic_libraries,
+                [lib for lib in dynamic_libraries if lib != None],

Ryang20718 avatar Nov 19 '24 00:11 Ryang20718