rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

add cc_common.link support for rust_library and rust_test

Open krasimirgg opened this issue 2 years ago • 0 comments

This PR adds support for using cc_common.link to link rust_binary-es and rust_test-s, following https://github.com/bazelbuild/rules_rust/issues/1488:

  • a new boolean build setting --//rules_rust/rust/settings:experimental_use_cc_common_link, defaulting to false,
  • a new rust_toolchain label attribute experimental_use_cc_common_link, defaulting to the build setting above.
  • a new temporary int rust_{binary|test|shared_library}.experimental_use_cc_common_link attribute with possible values [-1, 0, 1] defaulting to -1. This is to support gradually moving targets to/from using the new feature; after the feature is stable, we'll remove this attribute.

Given rust rust_{binary|test}(name = bin), and a rust_toolchain, common_cc.link is enabled if:

  • bin.experimental_use_cc_common_link is 1, or
  • bin.experimental_use_cc_common_link is -1 and rust_toolchain.experimental_use_cc_common_link points to a label whose value is true. Otherwise common_cc.link is disabled.

When enabled, instead of emitting one rustc_compile_action that does both compilation and linking using rustc, we emit 2 (sometimes 3) actions:

  • A rustc one with –emit=obj to produce an .o file
  • A cc_common.link one to produce the final binary (or appropriate binary artefacts for other crates that require linking).
  • Sometimes the final binary artifact is expected to be written to a file outside the package. cc_common.link doesn't support writing to outside the package output dir, so in such cases first construct the binary artifact inside the package subdir, then symlink the expected output artifact to the one produced.

To test this, I've set up a new test/cc_common_link directory and a build bot to build and test everything there with ---//rules_rust/rust/settings:experimental_use_cc_common_link set to true. This also acts as an example directory on how to set up a workspace where this is enabled by default.

This feature requires on an appropriate rust_toolchain.allocator_library, similarly to how we need one to be able to build a cc_binary with a rust dependency. So I picked up the shared allocator_library repository updates from @bsilver8192's https://github.com/bazelbuild/rules_rust/pull/1350 to set this up.

krasimirgg avatar Aug 01 '22 13:08 krasimirgg