Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The owner name is used instead of the filename when passing a static library using -lstatic. #2402

Closed
ghost opened this issue Jan 8, 2024 · 2 comments · Fixed by #2405
Closed

Comments

@ghost
Copy link

ghost commented Jan 8, 2024

Hello all,

In _generate_cc_link_build_info, -lstatic is used to tell rustc to link against a native static library:

linker_flags.append("-lstatic={}".format(lib.static_library.owner.name))

While this code should work in 99% of cases, it does not work when the archive does not bear the name of the rule that produced it.

Here is an example of a WORKSPACE and a BUILD that showcases the issue:

`WORKSPACE`
workspace(name = "w")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "zlib",
    build_file = "@w//:BUILD.zlib.bazel",
    sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
    strip_prefix = "zlib-1.2.11",
    urls = [
        "https://zlib.net/zlib-1.2.11.tar.gz",
        "https://storage.googleapis.com/mirror.tensorflow.org/zlib.net/zlib-1.2.11.tar.gz",
    ],
)

http_archive(
    name = "rules_rust",
    sha256 = "a761d54e49db06f863468e6bba4a13252b1bd499e8f706da65e279b3bcbc5c52",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.36.2/rules_rust-v0.36.2.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains(
    edition = "2018",
)

load("@rules_rust//bindgen:repositories.bzl", "rust_bindgen_dependencies", "rust_bindgen_register_toolchains")

rust_bindgen_dependencies()

rust_bindgen_register_toolchains()

load("@rules_rust//bindgen:transitive_repositories.bzl", "rust_bindgen_transitive_dependencies")

rust_bindgen_transitive_dependencies()

(BUILD.zlib.bazel can be found here)

`BUILD`
load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library")

cc_library(
    name = "foo",
    srcs = ["foo.c"],
    hdrs = ["foo.h"],
)

genrule(
    name = "foo_copy",
    srcs = [":foo"],
    outs = ["libfoo2.a"],
    cmd = "cp $(execpath :foo) $(execpath libfoo2.a)",
)

cc_library(
    name = "foo_proxy",
    srcs = ["libfoo2.a"],
    hdrs = ["foo.h"],
)

rust_bindgen_library(
    name = "foo_bindgen",
    cc_lib = ":foo_proxy",
    header = "foo.h",
)

And $ touch foo.{c,h}.

The owner of libfoo2.a is foo_copy:

$ bazel cquery --output=starlark :foo_proxy --starlark:expr="providers(target)['CcInfo'].linking_context.linker_inputs.to_list()[0].libraries[0].static_library.owner.name"
foo_copy

But the filename is libfoo2.a:

$ bazel cquery --output=starlark :foo_proxy --starlark:expr="providers(target)['CcInfo'].linking_context.linker_inputs.to_list()[0].libraries[0].static_library.basename"
libfoo2.a

Building :foo_bindgen raises the following issue:

$ ERROR: /Users/pawn/poc/BUILD:22:21: Compiling Rust rlib foo_bindgen (1 files) failed: (Exit 1): process_wrapper failed: error executing command (from target //:foo_bindgen) bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/external/rules_rust/util/process_wrapper/process_wrapper --arg-file bazel-out/darwin_arm64-fastbuild/bin/foo_bindgen__bindgen.link_search_paths --arg-file ... (remaining 22 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
error: could not find native static library `foo_copy`, perhaps an -L flag is missing?

error: aborting due to previous error

Target //:foo_bindgen failed to build

Instead of using lib.static_library.owner.name, we should use something like lib.static_library.basename[3:-lib.static_library.extension - 1] after checking the correctness of the filename.

What do you think?

@illicitonion
Copy link
Collaborator

Sounds pretty reasonable to me - cc @UebelAndre who made the change in the first place.

@UebelAndre
Copy link
Collaborator

Sounds reasonable to me too!

UebelAndre added a commit that referenced this issue Jan 9, 2024
…`. (#2405)

This commit fixes #2402 by using [`get_lib_name_default`] to pass
libraries names to `rustc` through `-lstatic` instead of relying on the
owner's name of the `File` object.

[`get_lib_name_default`]:
https://github.com/bazelbuild/rules_rust/blob/a1e9f9600cd22ecfdd08fc4a7572ccbdeba97395/rust/private/utils.bzl#L101

Co-authored-by: UebelAndre <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants