-
Notifications
You must be signed in to change notification settings - Fork 602
feature: introduce link_deps attribute for native library linkage #4024
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
Open
Suyashagarw
wants to merge
1
commit into
bazelbuild:main
Choose a base branch
from
Suyashagarw:link-deps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| load("@bazel_skylib//rules:build_test.bzl", "build_test") | ||
| load("@rules_cc//cc:defs.bzl", "cc_library") | ||
| load("//rust:defs.bzl", "rust_binary", "rust_library") | ||
| load(":link_deps_test.bzl", "link_deps_test") | ||
|
|
||
| # A dummy library to be used as a dependency | ||
| rust_library( | ||
| name = "leaf_lib", | ||
| srcs = ["lib.rs"], | ||
| ) | ||
|
|
||
| # Test 1: rust_library supports link_deps | ||
| rust_library( | ||
| name = "main_lib", | ||
| srcs = ["lib.rs"], | ||
| link_deps = [":leaf_lib"], | ||
| tags = ["manual"], | ||
| ) | ||
|
|
||
| link_deps_test( | ||
| name = "link_deps_success_test", | ||
| target_under_test = ":main_lib", | ||
| ) | ||
|
|
||
| # Test 2: Build test linking a C library via link_deps | ||
| cc_library( | ||
| name = "c_lib", | ||
| srcs = ["c_lib.c"], | ||
| ) | ||
|
|
||
| rust_binary( | ||
| name = "rust_bin", | ||
| srcs = ["main.rs"], | ||
| link_deps = [":c_lib"], | ||
| ) | ||
|
|
||
| build_test( | ||
| name = "rust_bin_link_deps_test", | ||
| targets = [":rust_bin"], | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| int c_function() { return 42; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """Unit tests for link_deps.""" | ||
|
|
||
| load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") | ||
| load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") | ||
| load("//rust:defs.bzl", "rust_common") | ||
|
|
||
| def _link_deps_test_impl(ctx): | ||
| env = analysistest.begin(ctx) | ||
| target = analysistest.target_under_test(env) | ||
|
|
||
| # Verify that CcInfo (symbols) is present | ||
| asserts.true(env, CcInfo in target, "Target should provide CcInfo from link_deps") | ||
|
Suyashagarw marked this conversation as resolved.
|
||
|
|
||
| dep_info = target[rust_common.dep_info] | ||
|
|
||
| # Verify that leaf_lib is NOT in direct_crates | ||
| for direct_crate in dep_info.direct_crates.to_list(): | ||
| asserts.not_equals(env, "leaf_lib", direct_crate.name, "link_deps should not be added to direct_crates") | ||
|
|
||
| # Verify that transitive_noncrates specifically contains leaf_lib as a linking input | ||
| linker_inputs = dep_info.transitive_noncrates.to_list() | ||
| found_leaf_lib = False | ||
| for linker_input in linker_inputs: | ||
| if linker_input.owner == Label("//test/unit/link_deps:leaf_lib"): | ||
| found_leaf_lib = True | ||
| break | ||
| asserts.true(env, found_leaf_lib, "link_deps should pass leaf_lib as a linker input") | ||
|
|
||
| return analysistest.end(env) | ||
|
|
||
| link_deps_test = analysistest.make(_link_deps_test_impl) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| extern "C" { | ||
| fn c_function() -> i32; | ||
| } | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| assert_eq!(c_function(), 42); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.