From 00d1176940f543e55223107cce03740755b81b14 Mon Sep 17 00:00:00 2001 From: Corentin Kerisit Date: Fri, 2 Aug 2024 11:13:36 +0200 Subject: [PATCH] Support alwayslink for linking with clang (#2647) Simple approach to fix #1271 --------- Co-authored-by: scentini --- docs/defs.md | 43 ++++++++++++++++++++++++------------------ docs/flatten.md | 43 ++++++++++++++++++++++++------------------ rust/private/rust.bzl | 9 +++++++++ rust/private/rustc.bzl | 2 ++ 4 files changed, 61 insertions(+), 36 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index d1f6bd3c7b..4c20a2a410 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -91,10 +91,10 @@ Add additional rustc_flags from the command line with `--@rules_rust//:extra_rus ## rust_binary
-rust_binary(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name, crate_root,
-            crate_type, edition, env, experimental_use_cc_common_link, linker_script, malloc,
-            out_binary, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp,
-            version)
+rust_binary(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name,
+            crate_root, crate_type, edition, env, experimental_use_cc_common_link, linker_script,
+            malloc, out_binary, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags,
+            stamp, version)
 
Builds a Rust binary crate. @@ -194,6 +194,7 @@ is available under the key `dsym_folder` in `OutputGroupInfo`. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -219,9 +220,9 @@ is available under the key `dsym_folder` in `OutputGroupInfo`. ## rust_library
-rust_library(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name, crate_root,
-             disable_pipelining, edition, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags,
-             stamp, version)
+rust_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name,
+             crate_root, disable_pipelining, edition, proc_macro_deps, rustc_env, rustc_env_files,
+             rustc_flags, stamp, version)
 
Builds a Rust library crate. @@ -297,6 +298,7 @@ INFO: Elapsed time: 1.245s, Critical Path: 1.01s | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -366,9 +368,9 @@ rust_library( ## rust_proc_macro
-rust_proc_macro(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name,
-                crate_root, edition, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp,
-                version)
+rust_proc_macro(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features,
+                crate_name, crate_root, edition, proc_macro_deps, rustc_env, rustc_env_files,
+                rustc_flags, stamp, version)
 
Builds a Rust proc-macro crate. @@ -383,6 +385,7 @@ Builds a Rust proc-macro crate. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -401,9 +404,10 @@ Builds a Rust proc-macro crate. ## rust_shared_library
-rust_shared_library(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name,
-                    crate_root, edition, experimental_use_cc_common_link, malloc, platform,
-                    proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, version)
+rust_shared_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features,
+                    crate_name, crate_root, edition, experimental_use_cc_common_link, malloc,
+                    platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp,
+                    version)
 
Builds a Rust shared library. @@ -426,6 +430,7 @@ When building the whole binary in Bazel, use `rust_library` instead. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -447,9 +452,9 @@ When building the whole binary in Bazel, use `rust_library` instead. ## rust_static_library
-rust_static_library(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name,
-                    crate_root, edition, platform, proc_macro_deps, rustc_env, rustc_env_files,
-                    rustc_flags, stamp, version)
+rust_static_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features,
+                    crate_name, crate_root, edition, platform, proc_macro_deps, rustc_env,
+                    rustc_env_files, rustc_flags, stamp, version)
 
Builds a Rust static library. @@ -472,6 +477,7 @@ When building the whole binary in Bazel, use `rust_library` instead. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -491,8 +497,8 @@ When building the whole binary in Bazel, use `rust_library` instead. ## rust_test
-rust_test(name, deps, srcs, data, aliases, compile_data, crate, crate_features, crate_name,
-          crate_root, edition, env, experimental_use_cc_common_link, malloc, platform,
+rust_test(name, deps, srcs, data, aliases, alwayslink, compile_data, crate, crate_features,
+          crate_name, crate_root, edition, env, experimental_use_cc_common_link, malloc, platform,
           proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, use_libtest_harness,
           version)
 
@@ -626,6 +632,7 @@ Run the test with `bazel test //hello_lib:greeting_test`. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate | Target inline tests declared in the given crate

These tests are typically those that would be held out under `#[cfg(test)]` declarations. | Label | optional | `None` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | diff --git a/docs/flatten.md b/docs/flatten.md index aed88de5c5..3e929a1d26 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -199,10 +199,10 @@ A toolchain for [rust-analyzer](https://rust-analyzer.github.io/). ## rust_binary
-rust_binary(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name, crate_root,
-            crate_type, edition, env, experimental_use_cc_common_link, linker_script, malloc,
-            out_binary, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp,
-            version)
+rust_binary(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name,
+            crate_root, crate_type, edition, env, experimental_use_cc_common_link, linker_script,
+            malloc, out_binary, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags,
+            stamp, version)
 
Builds a Rust binary crate. @@ -302,6 +302,7 @@ is available under the key `dsym_folder` in `OutputGroupInfo`. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -610,9 +611,9 @@ rust_binary( ## rust_library
-rust_library(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name, crate_root,
-             disable_pipelining, edition, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags,
-             stamp, version)
+rust_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features, crate_name,
+             crate_root, disable_pipelining, edition, proc_macro_deps, rustc_env, rustc_env_files,
+             rustc_flags, stamp, version)
 
Builds a Rust library crate. @@ -688,6 +689,7 @@ INFO: Elapsed time: 1.245s, Critical Path: 1.01s | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -757,9 +759,9 @@ rust_library( ## rust_proc_macro
-rust_proc_macro(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name,
-                crate_root, edition, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp,
-                version)
+rust_proc_macro(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features,
+                crate_name, crate_root, edition, proc_macro_deps, rustc_env, rustc_env_files,
+                rustc_flags, stamp, version)
 
Builds a Rust proc-macro crate. @@ -774,6 +776,7 @@ Builds a Rust proc-macro crate. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -865,9 +868,10 @@ rust_binary( ## rust_shared_library
-rust_shared_library(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name,
-                    crate_root, edition, experimental_use_cc_common_link, malloc, platform,
-                    proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, version)
+rust_shared_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features,
+                    crate_name, crate_root, edition, experimental_use_cc_common_link, malloc,
+                    platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp,
+                    version)
 
Builds a Rust shared library. @@ -890,6 +894,7 @@ When building the whole binary in Bazel, use `rust_library` instead. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -911,9 +916,9 @@ When building the whole binary in Bazel, use `rust_library` instead. ## rust_static_library
-rust_static_library(name, deps, srcs, data, aliases, compile_data, crate_features, crate_name,
-                    crate_root, edition, platform, proc_macro_deps, rustc_env, rustc_env_files,
-                    rustc_flags, stamp, version)
+rust_static_library(name, deps, srcs, data, aliases, alwayslink, compile_data, crate_features,
+                    crate_name, crate_root, edition, platform, proc_macro_deps, rustc_env,
+                    rustc_env_files, rustc_flags, stamp, version)
 
Builds a Rust static library. @@ -936,6 +941,7 @@ When building the whole binary in Bazel, use `rust_library` instead. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | | crate_name | Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | `""` | @@ -974,8 +980,8 @@ A dedicated filegroup-like rule for Rust stdlib artifacts. ## rust_test
-rust_test(name, deps, srcs, data, aliases, compile_data, crate, crate_features, crate_name,
-          crate_root, edition, env, experimental_use_cc_common_link, malloc, platform,
+rust_test(name, deps, srcs, data, aliases, alwayslink, compile_data, crate, crate_features,
+          crate_name, crate_root, edition, env, experimental_use_cc_common_link, malloc, platform,
           proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, use_libtest_harness,
           version)
 
@@ -1109,6 +1115,7 @@ Run the test with `bazel test //hello_lib:greeting_test`. | srcs | List of Rust `.rs` source files used to build the library.

If `srcs` contains more than one file, then there must be a file either named `lib.rs`. Otherwise, `crate_root` must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | `[]` | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | List of labels | optional | `[]` | | aliases | Remap crates to a new name or moniker for linkage to this target

These are other `rust_library` targets and will be presented as the new name given. | Dictionary: Label -> String | optional | `{}` | +| alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.

This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` | | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | `[]` | | crate | Target inline tests declared in the given crate

These tests are typically those that would be held out under `#[cfg(test)]` declarations. | Label | optional | `None` | | crate_features | List of features to enable for this crate.

Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` | diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index d4dff1b529..2a248e3bb5 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -556,6 +556,15 @@ _common_attrs = { These are other `rust_library` targets and will be presented as the new name given. """), ), + "alwayslink": attr.bool( + doc = dedent("""\ + If 1, any binary that depends (directly or indirectly) on this library + will link in all the object files even if some contain no symbols referenced by the binary. + + This attribute is used by the C++ Starlark API when passing CcInfo providers. + """), + default = False, + ), "compile_data": attr.label_list( doc = dedent("""\ List of files used by this rule at compile time. diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 5012678362..bfd96ed95b 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1611,6 +1611,7 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co static_library = crate_info.output, # TODO(hlopko): handle PIC/NOPIC correctly pic_static_library = crate_info.output, + alwayslink = getattr(attr, "alwayslink", False), ) elif crate_info.type in ("rlib", "lib"): # bazel hard-codes a check for endswith((".a", ".pic.a", @@ -1626,6 +1627,7 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co static_library = dot_a, # TODO(hlopko): handle PIC/NOPIC correctly pic_static_library = dot_a, + alwayslink = getattr(attr, "alwayslink", False), ) elif crate_info.type == "cdylib": library_to_link = cc_common.create_library_to_link(