diff --git a/docs/flatten.md b/docs/flatten.md index 0900ae62d0..3041104307 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 | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | None | +| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | @rules_rust//ffi/cc/allocator_library | | binary_ext | The extension for binaries created from rustc. | String | required | | | cargo | The location of the cargo binary. Can be a direct source or a filegroup containing one item. | Label | optional | None | | clippy_driver | The location of the clippy-driver binary. Can be a direct source or a filegroup containing one item. | Label | optional | None | @@ -1237,7 +1237,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu | experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | Label | optional | //rust/settings:experimental_use_cc_common_link | | extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | [] | | extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] | -| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | None | +| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | @rules_rust//ffi/cc/global_allocator_library | | llvm_cov | 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. | Label | optional | None | | llvm_profdata | The location of the llvm-profdata binary. Can be a direct source or a filegroup containing one item. If llvm_cov is None, this can be None as well and rust code is not instrumented for coverage. | Label | optional | None | | llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | None | @@ -1300,14 +1300,14 @@ A given instance of this rule should be accompanied by a toolchain_repository_pr | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this repository. | Name | required | | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | "" | +| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | "@rules_rust//ffi/cc/allocator_library" | | 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. | Dictionary: String -> String | optional | {} | | dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | False | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | String | optional | "" | | exec_triple | The Rust-style target that this compiler runs on | String | required | | | extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | [] | | extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] | -| global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | "" | +| global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | "@rules_rust//ffi/cc/global_allocator_library" | | iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" | | opt_level | Rustc optimization levels. For more details see the documentation for rust_toolchain.opt_level. | Dictionary: String -> String | optional | {} | | repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index e89cf64381..7bef29c2ea 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 | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | None | +| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | @rules_rust//ffi/cc/allocator_library | | binary_ext | The extension for binaries created from rustc. | String | required | | | cargo | The location of the cargo binary. Can be a direct source or a filegroup containing one item. | Label | optional | None | | clippy_driver | The location of the clippy-driver binary. Can be a direct source or a filegroup containing one item. | Label | optional | None | @@ -105,7 +105,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu | experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | Label | optional | //rust/settings:experimental_use_cc_common_link | | extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | [] | | extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] | -| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | None | +| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | @rules_rust//ffi/cc/global_allocator_library | | llvm_cov | 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. | Label | optional | None | | llvm_profdata | The location of the llvm-profdata binary. Can be a direct source or a filegroup containing one item. If llvm_cov is None, this can be None as well and rust code is not instrumented for coverage. | Label | optional | None | | llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | None | @@ -168,14 +168,14 @@ A given instance of this rule should be accompanied by a toolchain_repository_pr | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this repository. | Name | required | | -| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | "" | +| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | String | optional | "@rules_rust//ffi/cc/allocator_library" | | 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. | Dictionary: String -> String | optional | {} | | dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | False | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | String | optional | "" | | exec_triple | The Rust-style target that this compiler runs on | String | required | | | extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | [] | | extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] | -| global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | "" | +| global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | "@rules_rust//ffi/cc/global_allocator_library" | | iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" | | opt_level | Rustc optimization levels. For more details see the documentation for rust_toolchain.opt_level. | Dictionary: String -> String | optional | {} | | repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | diff --git a/examples/android/BUILD.bazel b/examples/android/BUILD.bazel index c49af78ead..cad573f94b 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 d0259a6c58..0000000000 --- a/examples/android/allocator_library.cc +++ /dev/null @@ -1,54 +0,0 @@ -#include - -// 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 81cba9caa6..c1ddafccd4 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 0000000000..570a8af09c --- /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 similarity index 100% rename from examples/ios/allocator_library.cc rename to 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 0000000000..e69de29bb2 diff --git a/ffi/cc/global_allocator_library/BUILD.bazel b/ffi/cc/global_allocator_library/BUILD.bazel new file mode 100644 index 0000000000..16480416e5 --- /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 0000000000..e69de29bb2 diff --git a/test/cc_common_link/global_allocator_library.cc b/ffi/cc/global_allocator_library/global_allocator_library.cc similarity index 100% rename from test/cc_common_link/global_allocator_library.cc rename to ffi/cc/global_allocator_library/global_allocator_library.cc diff --git a/rust/extensions.bzl b/rust/extensions.bzl index ce323ba7ad..883946c9ea 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 7be1375248..a083a29d83 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 af34e8fb27..3a59d49d73 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 ef952e2f8a..f66146bee7 100644 --- a/test/cc_common_link/BUILD.bazel +++ b/test/cc_common_link/BUILD.bazel @@ -7,18 +7,6 @@ load( "rust_test", ) -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 e45b8f8a55..c725ffe761 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 b058f282ea..0000000000 --- a/test/cc_common_link/allocator_library.cc +++ /dev/null @@ -1,58 +0,0 @@ -#include - -// 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 6e5f2eb887..061168403c 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 531cbb27fb..6f44bffdce 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 443a90a8f9..2848ab3c7e 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 6a46634660..3aa18cc3d1 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):