aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Wagner-Hall <dwagnerhall@apple.com>2023-09-29 14:45:38 +0100
committerGitHub <noreply@github.com>2023-09-29 14:45:38 +0100
commitaa4b3a862a8200c9422b7f7e050b12c7ef2c2a61 (patch)
tree45ce9fda0574318ae13c0173e7c470c546959309
parentac5aa1edc653d50d97eac26692b6919b9c6dc1b8 (diff)
downloadbazelbuild-rules_rust-aa4b3a862a8200c9422b7f7e050b12c7ef2c2a61.tar.gz
Set up default allocator_library and global_allocator_library (#2172)
This makes C++ depending on Rust Just Work out of the box, while still giving people hooks to customise these if they need. In particular, this means we can centrally manage and update our default definition of these symbols, rather than require people to vendor them and keep their copies up to date as new versions of Rust come out. We provide this as a best-effort tool which we hope will be useful to people, and will attempt to maintain the C++ code included here, but make not guarantees about its universality or correctness. If you need more specific behaviours, you should set your own `allocator_library` and `global_allocator_library` attributes on your toolchains.
-rw-r--r--docs/flatten.md8
-rw-r--r--docs/rust_repositories.md8
-rw-r--r--examples/android/BUILD.bazel7
-rw-r--r--examples/android/allocator_library.cc54
-rw-r--r--examples/ios/BUILD.bazel7
-rw-r--r--ffi/cc/allocator_library/BUILD.bazel14
-rw-r--r--ffi/cc/allocator_library/allocator_library.cc (renamed from examples/ios/allocator_library.cc)0
-rw-r--r--ffi/cc/allocator_library/empty.cc0
-rw-r--r--ffi/cc/global_allocator_library/BUILD.bazel14
-rw-r--r--ffi/cc/global_allocator_library/empty.cc0
-rw-r--r--ffi/cc/global_allocator_library/global_allocator_library.cc (renamed from test/cc_common_link/global_allocator_library.cc)0
-rw-r--r--rust/extensions.bzl2
-rw-r--r--rust/repositories.bzl2
-rw-r--r--rust/toolchain.bzl2
-rw-r--r--test/cc_common_link/BUILD.bazel12
-rw-r--r--test/cc_common_link/WORKSPACE.bazel1
-rw-r--r--test/cc_common_link/allocator_library.cc58
-rw-r--r--test/cc_common_link/with_global_alloc/WORKSPACE.bazel2
-rw-r--r--test/no_std/WORKSPACE.bazel2
-rw-r--r--test/unit/cc_info/cc_info_test.bzl2
-rw-r--r--test/unit/stdlib/stdlib.bzl2
21 files changed, 43 insertions, 154 deletions
diff --git a/docs/flatten.md b/docs/flatten.md
index 0900ae62..30411043 100644
--- a/docs/flatten.md
+++ b/docs/flatten.md
@@ -1225,7 +1225,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
-| <a id="rust_toolchain-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
+| <a id="rust_toolchain-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>@rules_rust//ffi/cc/allocator_library</code> |
| <a id="rust_toolchain-binary_ext"></a>binary_ext | The extension for binaries created from rustc. | String | required | |
| <a id="rust_toolchain-cargo"></a>cargo | The location of the <code>cargo</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-clippy_driver"></a>clippy_driver | The location of the <code>clippy-driver</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
@@ -1237,7 +1237,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
| <a id="rust_toolchain-experimental_use_cc_common_link"></a>experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//rust/settings:experimental_use_cc_common_link</code> |
| <a id="rust_toolchain-extra_exec_rustc_flags"></a>extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | <code>[]</code> |
| <a id="rust_toolchain-extra_rustc_flags"></a>extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
-| <a id="rust_toolchain-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions for when a global allocator is present. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
+| <a id="rust_toolchain-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions for when a global allocator is present. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>@rules_rust//ffi/cc/global_allocator_library</code> |
| <a id="rust_toolchain-llvm_cov"></a>llvm_cov | The location of the <code>llvm-cov</code> binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-llvm_profdata"></a>llvm_profdata | The location of the <code>llvm-profdata</code> binary. Can be a direct source or a filegroup containing one item. If <code>llvm_cov</code> is None, this can be None as well and rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-llvm_tools"></a>llvm_tools | LLVM tools that are shipped with the Rust toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
@@ -1300,14 +1300,14 @@ A given instance of this rule should be accompanied by a toolchain_repository_pr
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_toolchain_tools_repository-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
-| <a id="rust_toolchain_tools_repository-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | <code>""</code> |
+| <a id="rust_toolchain_tools_repository-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | <code>"@rules_rust//ffi/cc/allocator_library"</code> |
| <a id="rust_toolchain_tools_repository-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{}</code> |
| <a id="rust_toolchain_tools_repository-dev_components"></a>dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | <code>False</code> |
| <a id="rust_toolchain_tools_repository-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | String | optional | <code>""</code> |
| <a id="rust_toolchain_tools_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
| <a id="rust_toolchain_tools_repository-extra_exec_rustc_flags"></a>extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | <code>[]</code> |
| <a id="rust_toolchain_tools_repository-extra_rustc_flags"></a>extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
-| <a id="rust_toolchain_tools_repository-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | <code>""</code> |
+| <a id="rust_toolchain_tools_repository-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | <code>"@rules_rust//ffi/cc/global_allocator_library"</code> |
| <a id="rust_toolchain_tools_repository-iso_date"></a>iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | <code>""</code> |
| <a id="rust_toolchain_tools_repository-opt_level"></a>opt_level | Rustc optimization levels. For more details see the documentation for <code>rust_toolchain.opt_level</code>. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{}</code> |
| <a id="rust_toolchain_tools_repository-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | required | |
diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md
index e89cf643..7bef29c2 100644
--- a/docs/rust_repositories.md
+++ b/docs/rust_repositories.md
@@ -93,7 +93,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
-| <a id="rust_toolchain-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
+| <a id="rust_toolchain-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>@rules_rust//ffi/cc/allocator_library</code> |
| <a id="rust_toolchain-binary_ext"></a>binary_ext | The extension for binaries created from rustc. | String | required | |
| <a id="rust_toolchain-cargo"></a>cargo | The location of the <code>cargo</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-clippy_driver"></a>clippy_driver | The location of the <code>clippy-driver</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
@@ -105,7 +105,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
| <a id="rust_toolchain-experimental_use_cc_common_link"></a>experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//rust/settings:experimental_use_cc_common_link</code> |
| <a id="rust_toolchain-extra_exec_rustc_flags"></a>extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | <code>[]</code> |
| <a id="rust_toolchain-extra_rustc_flags"></a>extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
-| <a id="rust_toolchain-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions for when a global allocator is present. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
+| <a id="rust_toolchain-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions for when a global allocator is present. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>@rules_rust//ffi/cc/global_allocator_library</code> |
| <a id="rust_toolchain-llvm_cov"></a>llvm_cov | The location of the <code>llvm-cov</code> binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-llvm_profdata"></a>llvm_profdata | The location of the <code>llvm-profdata</code> binary. Can be a direct source or a filegroup containing one item. If <code>llvm_cov</code> is None, this can be None as well and rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-llvm_tools"></a>llvm_tools | LLVM tools that are shipped with the Rust toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
@@ -168,14 +168,14 @@ A given instance of this rule should be accompanied by a toolchain_repository_pr
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_toolchain_tools_repository-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
-| <a id="rust_toolchain_tools_repository-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | <code>""</code> |
+| <a id="rust_toolchain_tools_repository-allocator_library"></a>allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | <code>"@rules_rust//ffi/cc/allocator_library"</code> |
| <a id="rust_toolchain_tools_repository-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{}</code> |
| <a id="rust_toolchain_tools_repository-dev_components"></a>dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | <code>False</code> |
| <a id="rust_toolchain_tools_repository-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | String | optional | <code>""</code> |
| <a id="rust_toolchain_tools_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
| <a id="rust_toolchain_tools_repository-extra_exec_rustc_flags"></a>extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | <code>[]</code> |
| <a id="rust_toolchain_tools_repository-extra_rustc_flags"></a>extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
-| <a id="rust_toolchain_tools_repository-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | <code>""</code> |
+| <a id="rust_toolchain_tools_repository-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | <code>"@rules_rust//ffi/cc/global_allocator_library"</code> |
| <a id="rust_toolchain_tools_repository-iso_date"></a>iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | <code>""</code> |
| <a id="rust_toolchain_tools_repository-opt_level"></a>opt_level | Rustc optimization levels. For more details see the documentation for <code>rust_toolchain.opt_level</code>. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{}</code> |
| <a id="rust_toolchain_tools_repository-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | required | |
diff --git a/examples/android/BUILD.bazel b/examples/android/BUILD.bazel
index c49af78e..cad573f9 100644
--- a/examples/android/BUILD.bazel
+++ b/examples/android/BUILD.bazel
@@ -2,17 +2,10 @@ load("@build_bazel_rules_android//android:rules.bzl", "android_binary", "android
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_rust//rust:defs.bzl", "rust_library")
-cc_library(
- name = "allocator_library",
- srcs = ["allocator_library.cc"],
- visibility = ["//visibility:public"],
-)
-
rust_library(
name = "rust_lib",
srcs = ["demo.rs"],
edition = "2018",
- deps = [":allocator_library"],
)
cc_library(
diff --git a/examples/android/allocator_library.cc b/examples/android/allocator_library.cc
deleted file mode 100644
index d0259a6c..00000000
--- a/examples/android/allocator_library.cc
+++ /dev/null
@@ -1,54 +0,0 @@
-#include <stdint.h>
-
-// This file has some exciting magic to get Rust code linking in a cc_binary.
-// The Rust compiler generates some similar symbol aliases when it links, so we
-// have to do it manually. We mark all our symbols as weak so that linking this
-// via Rust tooling to produce a binary with a Rust main works.
-//
-// It is intended to be used in rust_toolchain.allocator_library.
-//
-// https://github.com/rust-lang/rust/blob/master/library/alloc/src/alloc.rs
-// and https://github.com/rust-lang/rust/blob/master/library/std/src/alloc.rs
-// are the best source of docs I've found on these functions and variables.
-// https://doc.rust-lang.org/std/alloc/index.html talks about how this is
-// intended to be used.
-//
-// Also note
-// https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html for
-// the sizes of the various integer types.
-//
-// This file strongly assumes that the default allocator is used. It will
-// not work with any other allocated switched in via `#[global_allocator]`.
-
-// New feature as of https://github.com/rust-lang/rust/pull/88098.
-__attribute__((weak)) uint8_t __rust_alloc_error_handler_should_panic = 0;
-
-extern "C" uint8_t *__rdl_alloc(uintptr_t size, uintptr_t align);
-extern "C" __attribute__((weak)) uint8_t *__rust_alloc(uintptr_t size,
- uintptr_t align) {
- return __rdl_alloc(size, align);
-}
-extern "C" void __rdl_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align);
-extern "C" __attribute__((weak)) void __rust_dealloc(uint8_t *ptr,
- uintptr_t size,
- uintptr_t align) {
- __rdl_dealloc(ptr, size, align);
-}
-extern "C" uint8_t *__rdl_realloc(uint8_t *ptr, uintptr_t old_size,
- uintptr_t align, uintptr_t new_size);
-extern "C" __attribute__((weak)) uint8_t *__rust_realloc(uint8_t *ptr,
- uintptr_t old_size,
- uintptr_t align,
- uintptr_t new_size) {
- return __rdl_realloc(ptr, old_size, align, new_size);
-}
-extern "C" uint8_t *__rdl_alloc_zeroed(uintptr_t size, uintptr_t align);
-extern "C" __attribute__((weak)) uint8_t *__rust_alloc_zeroed(uintptr_t size,
- uintptr_t align) {
- return __rdl_alloc_zeroed(size, align);
-}
-extern "C" void __rdl_oom(uintptr_t size, uintptr_t align);
-extern "C" __attribute__((weak)) void __rust_alloc_error_handler(
- uintptr_t size, uintptr_t align) {
- __rdl_oom(size, align);
-}
diff --git a/examples/ios/BUILD.bazel b/examples/ios/BUILD.bazel
index 81cba9ca..c1ddafcc 100644
--- a/examples/ios/BUILD.bazel
+++ b/examples/ios/BUILD.bazel
@@ -3,18 +3,11 @@ load("@build_bazel_rules_apple//apple:macos.bzl", "macos_application")
load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library")
load("@rules_rust//rust:defs.bzl", "rust_library")
-cc_library(
- name = "allocator_library",
- srcs = ["allocator_library.cc"],
- tags = ["manual"],
-)
-
rust_library(
name = "rust_lib",
srcs = ["demo.rs"],
edition = "2018",
tags = ["manual"],
- deps = [":allocator_library"],
)
# TODO: Remove this once rules_rust doesn't support bazel 5.x
diff --git a/ffi/cc/allocator_library/BUILD.bazel b/ffi/cc/allocator_library/BUILD.bazel
new file mode 100644
index 00000000..570a8af0
--- /dev/null
+++ b/ffi/cc/allocator_library/BUILD.bazel
@@ -0,0 +1,14 @@
+load("@rules_cc//cc:defs.bzl", "cc_library")
+
+cc_library(
+ name = "allocator_library",
+ srcs = select({
+ # Windows doesn't support weak symbol linkage.
+ # If someone can make this work on Windows, please do!
+ # For now we will silently not supply any symbols, because it would be very messy to conditionally define the default allocator library on toolchains depending on the platform.
+ "@platforms//os:windows": ["empty.cc"],
+ "//conditions:default": ["allocator_library.cc"],
+ }),
+ tags = ["manual"],
+ visibility = ["//visibility:public"],
+)
diff --git a/examples/ios/allocator_library.cc b/ffi/cc/allocator_library/allocator_library.cc
index b058f282..b058f282 100644
--- a/examples/ios/allocator_library.cc
+++ b/ffi/cc/allocator_library/allocator_library.cc
diff --git a/ffi/cc/allocator_library/empty.cc b/ffi/cc/allocator_library/empty.cc
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/ffi/cc/allocator_library/empty.cc
diff --git a/ffi/cc/global_allocator_library/BUILD.bazel b/ffi/cc/global_allocator_library/BUILD.bazel
new file mode 100644
index 00000000..16480416
--- /dev/null
+++ b/ffi/cc/global_allocator_library/BUILD.bazel
@@ -0,0 +1,14 @@
+load("@rules_cc//cc:defs.bzl", "cc_library")
+
+cc_library(
+ name = "global_allocator_library",
+ srcs = select({
+ # Windows doesn't support weak symbol linkage.
+ # If someone can make this work on Windows, please do!
+ # For now we will silently not supply any symbols, because it would be very messy to conditionally define the default allocator library on toolchains depending on the platform.
+ "@platforms//os:windows": ["empty.cc"],
+ "//conditions:default": ["global_allocator_library.cc"],
+ }),
+ tags = ["manual"],
+ visibility = ["//visibility:public"],
+)
diff --git a/ffi/cc/global_allocator_library/empty.cc b/ffi/cc/global_allocator_library/empty.cc
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/ffi/cc/global_allocator_library/empty.cc
diff --git a/test/cc_common_link/global_allocator_library.cc b/ffi/cc/global_allocator_library/global_allocator_library.cc
index 1b8e0921..1b8e0921 100644
--- a/test/cc_common_link/global_allocator_library.cc
+++ b/ffi/cc/global_allocator_library/global_allocator_library.cc
diff --git a/rust/extensions.bzl b/rust/extensions.bzl
index ce323ba7..883946c9 100644
--- a/rust/extensions.bzl
+++ b/rust/extensions.bzl
@@ -25,7 +25,7 @@ def _rust_impl(ctx):
)
rust_toolchain = tag_class(attrs = {
- "allocator_library": attr.string(),
+ "allocator_library": attr.string(default = "@rules_rust//ffi/cc/allocator_library"),
"dev_components": attr.bool(default = False),
"edition": attr.string(),
"extra_target_triples": attr.string_list(default = DEFAULT_EXTRA_TARGET_TRIPLES),
diff --git a/rust/repositories.bzl b/rust/repositories.bzl
index 7be13752..a083a29d 100644
--- a/rust/repositories.bzl
+++ b/rust/repositories.bzl
@@ -381,6 +381,7 @@ rust_toolchain_tools_repository = repository_rule(
attrs = {
"allocator_library": attr.string(
doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
+ default = "@rules_rust//ffi/cc/allocator_library",
),
"auth": attr.string_dict(
doc = (
@@ -410,6 +411,7 @@ rust_toolchain_tools_repository = repository_rule(
),
"global_allocator_library": attr.string(
doc = "Target that provides allocator functions when a global allocator is used with cc_common.link.",
+ default = "@rules_rust//ffi/cc/global_allocator_library",
),
"iso_date": attr.string(
doc = "The date of the tool (or None, if the version is a specific version).",
diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl
index af34e8fb..3a59d49d 100644
--- a/rust/toolchain.bzl
+++ b/rust/toolchain.bzl
@@ -654,6 +654,7 @@ rust_toolchain = rule(
attrs = {
"allocator_library": attr.label(
doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.",
+ default = "@rules_rust//ffi/cc/allocator_library",
),
"binary_ext": attr.string(
doc = "The extension for binaries created from rustc.",
@@ -709,6 +710,7 @@ rust_toolchain = rule(
),
"global_allocator_library": attr.label(
doc = "Target that provides allocator functions for when a global allocator is present.",
+ default = "@rules_rust//ffi/cc/global_allocator_library",
),
"llvm_cov": attr.label(
doc = "The location of the `llvm-cov` binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage.",
diff --git a/test/cc_common_link/BUILD.bazel b/test/cc_common_link/BUILD.bazel
index ef952e2f..f66146be 100644
--- a/test/cc_common_link/BUILD.bazel
+++ b/test/cc_common_link/BUILD.bazel
@@ -8,18 +8,6 @@ load(
)
cc_library(
- name = "allocator_library",
- srcs = ["allocator_library.cc"],
- visibility = ["//visibility:public"],
-)
-
-cc_library(
- name = "global_allocator_library",
- srcs = ["global_allocator_library.cc"],
- visibility = ["//visibility:public"],
-)
-
-cc_library(
name = "cclinkstampdep",
linkstamp = "cclinkstampdep.cc",
)
diff --git a/test/cc_common_link/WORKSPACE.bazel b/test/cc_common_link/WORKSPACE.bazel
index e45b8f8a..c725ffe7 100644
--- a/test/cc_common_link/WORKSPACE.bazel
+++ b/test/cc_common_link/WORKSPACE.bazel
@@ -10,7 +10,6 @@ load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_regi
rules_rust_dependencies()
rust_register_toolchains(
- allocator_library = "@test_cc_common_link//:allocator_library",
edition = "2018",
)
diff --git a/test/cc_common_link/allocator_library.cc b/test/cc_common_link/allocator_library.cc
deleted file mode 100644
index b058f282..00000000
--- a/test/cc_common_link/allocator_library.cc
+++ /dev/null
@@ -1,58 +0,0 @@
-#include <stdint.h>
-
-// This file has some exciting magic to get Rust code linking in a cc_binary.
-// The Rust compiler generates some similar symbol aliases when it links, so we
-// have to do it manually. We mark all our symbols as weak so that linking this
-// via Rust tooling to produce a binary with a Rust main works.
-//
-// It is intended to be used in rust_toolchain.allocator_library.
-//
-// https://github.com/rust-lang/rust/blob/master/library/alloc/src/alloc.rs
-// and https://github.com/rust-lang/rust/blob/master/library/std/src/alloc.rs
-// are the best source of docs I've found on these functions and variables.
-// https://doc.rust-lang.org/std/alloc/index.html talks about how this is
-// intended to be used.
-//
-// Also note
-// https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html for
-// the sizes of the various integer types.
-//
-// This file strongly assumes that the default allocator is used. It will
-// not work with any other allocated switched in via `#[global_allocator]`.
-
-// New feature as of https://github.com/rust-lang/rust/pull/88098.
-__attribute__((weak)) uint8_t __rust_alloc_error_handler_should_panic = 0;
-
-extern "C" uint8_t *__rdl_alloc(uintptr_t size, uintptr_t align);
-extern "C" __attribute__((weak)) uint8_t *__rust_alloc(uintptr_t size,
- uintptr_t align) {
- return __rdl_alloc(size, align);
-}
-extern "C" void __rdl_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align);
-extern "C" __attribute__((weak)) void __rust_dealloc(uint8_t *ptr,
- uintptr_t size,
- uintptr_t align) {
- __rdl_dealloc(ptr, size, align);
-}
-extern "C" uint8_t *__rdl_realloc(uint8_t *ptr, uintptr_t old_size,
- uintptr_t align, uintptr_t new_size);
-extern "C" __attribute__((weak)) uint8_t *__rust_realloc(uint8_t *ptr,
- uintptr_t old_size,
- uintptr_t align,
- uintptr_t new_size) {
- return __rdl_realloc(ptr, old_size, align, new_size);
-}
-extern "C" uint8_t *__rdl_alloc_zeroed(uintptr_t size, uintptr_t align);
-extern "C" __attribute__((weak)) uint8_t *__rust_alloc_zeroed(uintptr_t size,
- uintptr_t align) {
- return __rdl_alloc_zeroed(size, align);
-}
-extern "C" void __rdl_oom(uintptr_t size, uintptr_t align);
-extern "C" __attribute__((weak)) void __rust_alloc_error_handler(
- uintptr_t size, uintptr_t align) {
- __rdl_oom(size, align);
-}
-
-// New requirement as of Rust 1.71.0. For more details see
-// https://github.com/rust-lang/rust/issues/73632.
-__attribute__((weak)) uint8_t __rust_no_alloc_shim_is_unstable = 0;
diff --git a/test/cc_common_link/with_global_alloc/WORKSPACE.bazel b/test/cc_common_link/with_global_alloc/WORKSPACE.bazel
index 6e5f2eb8..06116840 100644
--- a/test/cc_common_link/with_global_alloc/WORKSPACE.bazel
+++ b/test/cc_common_link/with_global_alloc/WORKSPACE.bazel
@@ -15,9 +15,7 @@ load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_regi
rules_rust_dependencies()
rust_register_toolchains(
- allocator_library = "@test_cc_common_link//:allocator_library",
edition = "2018",
- global_allocator_library = "@test_cc_common_link//:global_allocator_library",
)
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
diff --git a/test/no_std/WORKSPACE.bazel b/test/no_std/WORKSPACE.bazel
index 531cbb27..6f44bffd 100644
--- a/test/no_std/WORKSPACE.bazel
+++ b/test/no_std/WORKSPACE.bazel
@@ -15,9 +15,7 @@ load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_regi
rules_rust_dependencies()
rust_register_toolchains(
- allocator_library = "@test_cc_common_link//:allocator_library",
edition = "2021",
- global_allocator_library = "@test_cc_common_link//:global_allocator_library",
versions = [
"nightly/2023-04-20",
"1.68.0",
diff --git a/test/unit/cc_info/cc_info_test.bzl b/test/unit/cc_info/cc_info_test.bzl
index 443a90a8..2848ab3c 100644
--- a/test/unit/cc_info/cc_info_test.bzl
+++ b/test/unit/cc_info/cc_info_test.bzl
@@ -51,7 +51,7 @@ def _collect_user_link_flags(env, tut):
def _rlib_provides_cc_info_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
- _assert_cc_info_has_library_to_link(env, tut, "rlib", 3)
+ _assert_cc_info_has_library_to_link(env, tut, "rlib", 4)
return analysistest.end(env)
def _rlib_with_dep_only_has_stdlib_linkflags_once_test_impl(ctx):
diff --git a/test/unit/stdlib/stdlib.bzl b/test/unit/stdlib/stdlib.bzl
index 6a466346..3aa18cc3 100644
--- a/test/unit/stdlib/stdlib.bzl
+++ b/test/unit/stdlib/stdlib.bzl
@@ -33,7 +33,7 @@ def _stdlibs(tut):
for li in tut[CcInfo].linking_context.linker_inputs.to_list()
for lib in li.libraries
]
- stdlibs = [lib for lib in libs if (tut.label.name not in lib.basename)]
+ stdlibs = [lib for lib in libs if (lib and tut.label.name not in lib.basename)]
return stdlibs
def _libstd_ordering_test_impl(ctx):