Implement support for dylib linkage (#2414)

This PR implements dylib linkage against the standard library behind a
feature flag
`--@rules_rust//rust/settings:experimental_use_dylib_linkage`.


The main part of this feature is
[here](https://github.com/bazelbuild/rules_rust/pull/2414/files#diff-2a806da393e47c07ffe67c78ace69eb488b4ac44b029a46d8237b8e2a05637beR258)
where we skip exporting static rust stdlibs and export only `libstd.so`
instead.

This feature is useful when the subset of libstd being statically linked
to downstream shared libraries and binaries is **larger** than the
entire dylib version of libstd. The following diagram is the high level
of what dylib linkage is trying to achieve.


![Untitled Diagram
drawio](https://github.com/bazelbuild/rules_rust/assets/13268391/d19f18f5-c2d1-4ddc-b170-773a6004f732)

Running the feature against `android_binary` yields a size reduction on
the shared library produced by `android_binary` because it doesn't
statically link the rust stdlibs anymore.

```
> bazel build //:android_app --config=android_x86_64 
> unzip -l bazel-bin/android_app.apk
Archive:  bazel-bin/android_app.apk
  Length      Date    Time    Name
---------  ---------- -----   ----
  1381968  2010-01-01 00:00   lib/x86_64/libandroid_app.so <--- static link with rust stdlibs
---------                     -------
  1390294                     9 files
```

```
> bazel build //:android_app --config=android_x86_64 --config=dylib_linkage
> unzip -l bazel-bin/android_app.apk
Archive:  bazel-bin/android_app.apk
  Length      Date    Time    Name
---------  ---------- -----   ----
     8080  2010-01-01 00:00   lib/x86_64/libandroid_app.so <--- reduced size because of dynamic linking
 13055776  2010-01-01 00:00   lib/x86_64/libstd-8d416d49cf02ecea.so
---------                     -------
 13072400                     10 files
 ```

Here, the benefit comes when there are enough shared libraries statically linking against the rust stdlibs. "Enough" here means that the total up size of those libraries being more than just the entire `libstd.so`.

TODO: I'm leaving this PR without unit tests until I get some feedback or suggestions on my approach.

---------

Co-authored-by: scentini <rosica@google.com>
13 files changed
tree: 647270b2803eb0f47f16807becf33485fdce520d
  1. .bazelci/
  2. .bcr/
  3. .github/
  4. bindgen/
  5. cargo/
  6. crate_universe/
  7. docs/
  8. examples/
  9. ffi/
  10. nix/
  11. proto/
  12. rust/
  13. test/
  14. tools/
  15. util/
  16. wasm_bindgen/
  17. .bazelignore
  18. .bazelrc
  19. .clang-format
  20. .envrc
  21. .gitattributes
  22. .gitignore
  23. .prettierrc.toml
  24. .rustfmt.toml
  25. ARCHITECTURE.md
  26. AUTHORS
  27. BUILD.bazel
  28. CODEOWNERS
  29. COMPATIBILITY.md
  30. CONTRIBUTING.md
  31. CONTRIBUTORS
  32. LICENSE.txt
  33. MODULE.bazel
  34. README.md
  35. version.bzl
  36. WORKSPACE.bazel
README.md

Rust Rules

  • Postsubmit Build status

Overview

This repository provides rules for building Rust projects with Bazel.

Community

General discussions and announcements take place in the GitHub Discussions, but there are additional places where community members gather to discuss rules_rust.

Documentation

Please refer to the full documentation.