Commit graph

52 commits

Author SHA1 Message Date
Colin Cross
004bd3f526 Revert "Split Rust crate builds into compile and link phases."
This reverts commit e7c71c344d.
The intermediates created by the separated rust compile steps are very
large, on the order of 60GB.  This is more than CI can support for now,
revert the split into compile and link phases.  This comes at the expense
of incremental build time, especially when modifying C/C++ sources that
are dependencies of rust modules.

Bug: 293349612
Test: builds
Change-Id: I81169e7d0727330c8de5e7688dcdc87fe7b8d3b5
2023-10-02 22:15:55 -07:00
Sam Delmerico
63ca14e9b7 Revert^4 "add crate_root property to rust modules"
9c64274b27

Change-Id: I1c4dad76842cebf18cd82e04e435910f09038d07
2023-09-25 12:13:17 +00:00
Wen-yi Chu
9c64274b27 Revert^3 "add crate_root property to rust modules"
60375c4adf

Change-Id: I356e936139592893a47d4c6b9e747f24470b11f9
2023-09-22 22:05:54 +00:00
Wen-yi Chu
41326c1f41 Revert "support sandboxed rust rules"
Revert submission 2629131-sandbox-rust-inputs

Reason for revert: Fail on android build.

Reverted changes: /q/submissionid:2629131-sandbox-rust-inputs

Change-Id: Ifd9aa46e80a12d8f4ffa0a2daa74b96727cbb7e6
2023-09-22 22:05:54 +00:00
Sam Delmerico
52d1cc334b Merge changes from topics "revert-2746976-revert-2605644-rulebuilder-ninja-vars-OAAWYCDDLT-KMAGKVIXAT", "sandbox-rust-inputs" into main
* changes:
  support sandboxed rust rules
  conditionally escape rule builder command
  Revert^2 "allow Ninja variables in RuleBuilder API"
  Revert^2 "add crate_root property to rust modules"
  Revert^2 "add rust_toolchain_rustc_prebuilt module type"
2023-09-20 18:48:21 +00:00
Chris Parsons
637458d326 Have ConvertWBp2build use Bp2buildMutatorContext
This no-op refactoring facilitates some upcoming functional changes for
"bp2build allowlist v2". The work requires that the bp2build conversion
mutator be changed from a TopDown mutator to a BottomUp mutator.
Refactoring all bp2build-related methods so that they use Bp2buildMutatorContext
makes it easier to make this functional change without touching tens of
files and multiple projects.

Bug: 285631638
Test: m bp2build
Change-Id: I3d1ef3064146e959c6f0dc315350fc9764bf2bd2
2023-09-20 14:49:35 +00:00
Sam Delmerico
a588d153c8 support sandboxed rust rules
This commit adds support for compiling rust rules inside the sbox
sandbox. To compile a rust module with sandboxing enabled, the entry
point to the crate must be specified via the `crate_root` property, and
all input sources and compile-time data must be specified via the `srcs`
and `compile_data` properties.

Bug: 286077158
Change-Id: I8c9dc5cf7578037a583b4be2e2f73cf20ffd4408
2023-09-15 22:46:56 +00:00
Sam Delmerico
60375c4adf Revert^2 "add crate_root property to rust modules"
5162ff14fc

Change-Id: I9a76df938639676ee4aa7bdd2b2902e0e6df08d5
2023-09-15 22:41:51 +00:00
Sam Delmerico
5162ff14fc Revert "add crate_root property to rust modules"
Revert submission 2605644-rulebuilder-ninja-vars

Reason for revert: b/299568218

Reverted changes: /q/submissionid:2605644-rulebuilder-ninja-vars

Change-Id: I17645fcff117909c09a016157235805f0974340b
2023-09-08 16:10:47 +00:00
Sam Delmerico
553edff9dd add crate_root property to rust modules
The crate_root property will be used to specify the entry point for a
rustc compilation. This will allow the srcs property to be used to
collect all src inputs to rustc rather than just the entry point.

Bug: 286077158
Test: m libnum_traits
Change-Id: I1a167182305dcc11cc927d562ceed622153111d3
2023-08-31 18:03:33 +00:00
Vinh Tran
093a57edb2 Implement bp2build converter for rust_binary
Test: go test
Bug: 295910295
Change-Id: Ieb18d9071f28e53a0463614594cee0fd755b7b3c
2023-08-25 17:58:53 -04:00
Ivan Lozano
add122a828 rust: Add vendor and recovery dylib support.
Adds dylib support for vendor and recovery images.

This changes the default linkage for vendor and recovery images to
dylib, which matches the platform default linkage. This also means that
by default, dylib-std variants are used for rlib dependencies.

Bug: 204303985
Test: Soong tests.
Test: m dist vendor-snapshot
Test: RECOVERY_SNAPSHOT_VERSION=current m dist recovery-snapshot
Change-Id: If84074b8615a70c45e7e162abeb853dc8c34d49a
2023-07-14 12:43:09 -04:00
Peter Collingbourne
e7c71c344d Split Rust crate builds into compile and link phases.
Teach rustc to generate object files instead of a linked executable
for binary and shared library crates. This lets us skip the lengthy
Rust compilation phase when only the C++ dependencies of these crates
have changed.

This works using a Python script that replaces the linker invoked by
rustc and converts the linker command line into a rspfile that can be
used during the link phase. The script makes certain assumptions about
how rustc invokes the linker, but since we control the version of Rust
we use, we can update the script if those assumptions ever break. I've
asked the Rust developers about adding an official way to do this so
hopefully we won't need to rely on this hack in the future.

The rustc rule for Kythe has been altered to pass linker=true, somewhat
consistent with the main rustc rule, which now doesn't invoke the actual
linker either. `m xref_rust` still succeeds, but hopefully we can find
someone who knows more about the rest of the Kythe pipeline who can let
us know whether it should still work. Otherwise, let's use this CL as
an opportunity to find out whether the rest of the pipeline is happy
with linker=true, since otherwise we will find it hard to maintain the
Kythe rule in the future.

Change-Id: Ifb9c8689c1bd78c0afcf6fdfd111140d20377536
2023-04-12 15:51:41 -07:00
Colin Cross
9382c0628c Drop rust dependency on musl_linker_script
musl_linker_script is no longer required as the entry point is now
_start.

Bug: 242601708
Test: m USE_HOST_MUSL=true host-native
Change-Id: I5acfaeb181a2dd39aac72edd94bd2030881d9d27
2022-09-30 15:12:25 -07:00
Sasha Smundak
a76acba553 Add cross-referencing support for Rust
Bug: 222044478
Test: run TARGET_BUILD_VARIANT=userdebug TARGET_PRODUCT=cf_x86_64_phone XREF_CORPUS=googleplex-android.googlesource.com/codesearch/android build/soong/build_kzip.bash
Change-Id: Ia12eed53fafd0cbbbf2cc499fa20a0f5a40031de
2022-04-20 20:02:28 -07:00
Colin Cross
e32f093120 Support building rust modules against musl libc
Add a rust toolchain for musl libc, use std library built from source,
and add default dependencies on musl libc.

Bug: 216192129
Test: m USE_HOST_MUSL=true host-native
Change-Id: Ic5ff4487db9693aeb08a13405f4d18465eecdc4b
2022-01-24 17:33:05 -08:00
Colin Cross
fe605e14ee Support multiple crtbegin and crtend dependencies
Musl libc with an embedded linker uses multiple crtbegin dependencies,
convert rust's CrtBegin and CrtEnd to lists.

Bug: 190084016
Test: m USE_HOST_MUSL=true host-native
Change-Id: Ie843801e87b1f38ace84502d9e4f938a92ec1fa2
2022-01-24 17:33:05 -08:00
Ivan Lozano
94e8a651fa Merge changes I26198187,Ia904d07b
* changes:
  rust: Support MTE memtag_heap sanitizer
  rust: Support global sanitizers
2021-11-12 19:26:04 +00:00
Ivan Lozano
8d10fc39af rust: Refactor stripped output file path
Rust installed files reside in "$MODULE_OUT/stripped/" when they are
stripped, otherwise they reside in "$MODULE_OUT". However, other parts
of Soong assume that installed files are always in $MODULE_OUT
(cc_modules place *unstripped* files in $MODULE_OUT/unstripped).

This notably causes problems when adding Rust modules as test data in
AndroidMkDataPaths. When Rust modules are parsed by AndroidMkDataPaths,
if they are stripped then they incorrectly get installed as test data
with the path:
<install_root>/<relative_install_path>/stripped/file.

This CL refactors how we handle Rust stripped output such that the
installed file always resides in $MODULE_OUT.

Bug: 171710847
Test: Installed files now always reside in $MODULE_OUT
Change-Id: I53a6ff57a0a5a55cd95ea78ae592ce22abfa20c9
2021-11-09 21:43:58 -05:00
Ivan Lozano
62cd0388eb rust: Support MTE memtag_heap sanitizer
This CL adds support for the MTE memtag_heap sanitizer. This is
controlled via inclusion of an ELF note.

Bug: 170672854
Test: Heap MTE-enabled Rust test binary triggers MTE
Change-Id: I2619818785e86a94667d02b30d102c83456b7925
2021-11-04 10:51:54 -04:00
Ivan Lozano
21fa0a5844 rust: Add common interface for binaries
Structs embedding binaryDecorator (rust_test, rust_benchmark, rust_fuzz)
are binaries as well, but won't pass checks against *binaryDecorator,
such as the check in StaticExecutable().

Add a binaryInterface that can be checked instead to simplify these
checks and ensure we catch all binaries.

Bug: 170672854
Test: rust_test, rust_benchmark return true StaticallyLinked
Change-Id: I2373d3663373a6977260785602a02d39a41320fe
2021-11-01 14:57:44 -04:00
Liz Kammer
187d5445e8 Remove IsDependencyRoot from interface
This is equivalent to Binary() -- reduce the interface and improve
clarity.

Test: go test soong tests
Change-Id: I770f5ce79fd4d888586d31ec5e67be88153626b6
2021-07-07 16:29:09 -04:00
Ivan Lozano
1921e8003d Rust cdylib/statliclib support for vendor snapshot.
Adds support for platform vendor_available Rust FFI libraries and
binaries to be included in the vendor snapshot.

Because rlib and dylibs are not yet in snapshots, libstd cannot be
included in a vendor snapshot. As a result, vendor-specific Rust code
can't be guaranteed to work with the platform-provided vendor_available
modules built with a newer toolchain. For now, a check is added
indicating vendor-specific Rust code is unsupported.

This changes the linkage for vendor variants of these modules to default
to rlib linkage since dylibs cannot be included in the snapshot yet.

Bug: 184042776
Test: m nothing # new Soong tests pass
Change-Id: I502eaa4bb962eb87ff868fcf49b435f0d2f982e6
2021-06-03 08:32:05 -04:00
Dan Albert
06feee9352 Rustdoc support.
Adds `m rustdoc` which generates documentation for all Rust libraries
to $OUT_DIR/soong/rustdoc.

Follow up work:

* Generate an index page that lists all modules.
* Preserve the artifacts so we can have an always-up-to-date go link.

Test: m rustdoc
Bug: None
Change-Id: Id2d6b9cbab5b02e36b575567563d7cc7606b9401
2021-04-16 13:38:01 -07:00
Thiébaud Weksteen
f1ff54a10b rust: Drop libgcc dependency
Rustc unstable option "link-native-libraries" is used to prevent the
linkage of other libraries via the #[link] directive. Add a dependency
to libclang_rt.builtins, similarly to cc.

Bug: 141331117
Test: lunch aosp_crosshatch-userdebug; m
Change-Id: I5c232291a5dd08a99e6a12a1295e30bb8e4fcaf1
2021-03-23 13:55:40 +01:00
Ivan Lozano
729d5cf097 Merge "rust: Pass cc static libs to rustc." 2021-02-05 15:53:33 +00:00
Ivan Lozano
3dfa12db6c rust: Pass cc static libs to rustc.
Pass static libraries to rustc via the "-lstatic" flag so that rustc can
reason how to handle these libraries. This means that these static
libraries get bundled into rlibs, reducing the need to redeclare these
dependencies in dependent modules.

Additionally, exported depFlags were never used anywhere, so remove this
from flagExporter.

Bug: 175886967
Bug: 179122837
Test: m
Test: Checked that static symbols aren't duplicated in a resulting
      binary when it appears in a dependency graph more than once.
Change-Id: Ifeb3c384e4fad8704206997242a0156ddcb87d2c
2021-02-04 16:29:02 -05:00
Liz Kammer
356f7d45c1 bp2build: convert paths/module refs to Bazel label
This currently expands all globs, still need to support converting glob
syntax.

Test: go build_conversion_test
Test: GENERATE_BAZEL_FILES=true m nothing
Test: m nothing
Bug: 165114590
Change-Id: If7b26e8e663d17566fad9614ca87a8da1f095284
2021-02-04 13:45:56 -05:00
Joel Galenson
fa049385b8 Migrate Rust to LLVM coverage.
Bug: 177675913
Test: Manually compile, run, and see output with llvm-cov.
Change-Id: I66729cff87a848782e9fa1b95cbbc06318c5761a
2021-01-15 19:44:07 +00:00
Ivan Lozano
3968d8f678 Refactor CC to prep for Rust sanitizable modules.
Adds a PlatformSanitizable interface which both CC and Rust can
implement so that the sanitizer mutators in CC can sanitize Rust
shared/static libraries appropriately.

Bug: 147140513
Test: m nothing
Change-Id: Ib31103b6c4902a4d5df2565c0d7c981298d100a3
2021-01-08 08:53:46 -05:00
Ivan Lozano
ea08613dd3 Move prefer_rlib from binary to base compiler.
Moves the prefer_rlib property out from being exclusively a binary
property to one thats part of the base compiler properties. This
provides a little more control over the libstd linkage in our libraries.
Specifically, this enables a usecase where rust_ffi_shared needs to link
against libstd statically rather than dynamically.

Bug: 175121262
Test: New Soong tests pass.
Change-Id: If68014c684a75ba70e9d7dacbb01c7d360dc25a1
2020-12-09 09:55:12 -05:00
Ivan Lozano
bf63d00c54 rust: Add static binary support
Adds the "static_executable" property to rust_binary modules which
allows for building fully static executables. This only impacts bionic
targets.

Bug: 169434439
Test: rust_binary module with static_executable true builds, runs on
      device.

Change-Id: I83c19fddd070859b7e56d248237cfd73e1768519
2020-10-02 12:31:23 -04:00
Ivan Lozano
dd0554722a rust: Refactor staticStd to stdLinkage
Instead of returning a boolean, return an enum value to improve
readability and provide greater flexibility for future modifications.

Bug: 168729404
Test: Soong tests pass
Change-Id: Iddcdae8c34be09e476404382e43d1ea5935bae65
2020-09-28 13:26:05 -04:00
Ivan Lozano
11200870b0 rust: Add prefer_rlib property for static libstd.
Adds the prefer_rlib property to allow linking libstd statically for
device rust binaries. This also changes the default behavior of rustlibs
to also prefer rlib linkage. This is because dylibs do not provide
rlib-libstd variants and always link in libstd dynamically. Thus a
binary requesting libstd rlib linkage should not attempt to link against
dylibs that link libstd dynamically.

Bug: 168729404
Test: New Soong test passes.
Change-Id: Idf8dfbbce8fd936f55a3fb323b17a1a7f0ee954e
2020-09-28 12:01:47 -04:00
Thiébaud Weksteen
fabaff6bd7 rust: strip libraries and binaries
Reuses the cc.Stripper logic. Abstracts Stripper to avoid the spreading
of references to the cc package.

rustc requires unstripped libraries (precisely, with the `.rustc`
section) when building dependent targets. Contrary to cc, the output of
a compiler module will remain unstripped and only an extra build rule
will be added. This rule will be referenced at install time (in
baseCompiler.install or androidmk).

This change drastically reduces the size of the installed libraries:
(unstripped, from out/target/product/crosshatch/system)
$ find . -name \*.dylib.so -print0 | du -c --files0-from=-
149996  total

(stripped, with this change)
$ find . -name \*.dylib.so -print0 | du -c --files0-from=-
42380   total

Bug: 153430439
Test: cd external/rust; mma
Change-Id: I94fd8bbcec97e0610aa325d3db4460be84d01734
2020-08-28 10:50:17 +02:00
Ivan Lozano
b0e99edab1 Merge "[rust] Pass cc dependencies as linker flags." 2020-08-27 20:50:16 +00:00
Ivan Lozano
2093af23c0 [rust] Pass cc dependencies as linker flags.
In order to support cc dependencies which do not start with the 'lib'
prefix, we can't pass them through the -l flag. Instead, we can pass
them directly to linker flags.

Bug: 166151658
Test: cd external/rust/; mma
Test: cd external/crosvm/; mma
Test: Test linking to a cc dep that does not begin with 'lib'

Change-Id: I5acbf3d3405e66446f3eae600b35683c4eb3d8a5
2020-08-26 17:07:29 -04:00
Ivan Lozano
042504f7d6 Link device binaries dynamically by default.
Device binaries currently are linked statically by default. Instead we
should be linking these dynamic by default. To avoid conflicts when
manually specifying rlib dependencies on modules, we always link libstd
dynamically for all device modules except static libraries.

This removes the "prefer_dynamic" property entirely to avoid confusion.

Bug: 165161105
Test: m profcollectd is built dynamically.
Test: cd external/rust/; mma
Test: cd external/crosvm/; mma
Change-Id: I25ac897040acbcc2d97c791a33e8e01610632272
2020-08-25 13:32:53 -04:00
Ivan Lozano
3feff6c130 Merge changes I4efdf333,I4abaf8e7
* changes:
  Ensure hermetic device rust_bindgen.
  Generate deps file for rust_bindgen modules.
2020-07-29 00:25:42 +00:00
Ivan Lozano
45901edb9a Ensure hermetic device rust_bindgen.
rust_bindgen was not hermetic previously as it would pull in host
headers for device targets. This fixes that by using the same flags we
use when compiling with Clang. This also makes sure our rust_bindgen
headers are built as similar as possible to their respective
cc_libraries.

This also pulls in the bionic dependencies as well, which provide the
headers required for device targets.

Bug: 162007475
Test: device rust_bindgen deps file does not reference host headers.

Change-Id: I4efdf333e011a6c6d73a0345e5485823f166d17a
2020-07-28 14:53:57 -04:00
Ivan Lozano
07cbaf4d89 Enforce correct variant usage for rust_bindgen.
Modules defined in the srcs property are automatically added as
dependencies with AddDependency(), which will use any variant available.
This can cause incorrect architecture bindings to be silently pulled in,
such as when a host module uses a rust_bindgen module that doesn't
create a host variant.

This moves populating depPaths.SrcDeps over to depsToPaths and adds a
check for SourceProviders to make sure the correct OS and architecture
is being used.

Bug: 161826371
Test: Soong no longer silently pulls in bindings for the wrong
      target.
Test: New Soong test to catch this case passes.
Change-Id: I2b3651cf6fc7dabf4081434df1c455e637f5b3a4
2020-07-22 20:34:22 +00:00
Ivan Lozano
43845688bc Allow rust module dependency on SourceProviders.
Allow rust modules to depend on and use generated source code provided
by SourceProvider modules and genrule modules without resorting to
hardcoded output paths.

All generated sources are now copied to a dependent module's
intermediates directory, then OUT_DIR is set to point to that path when
calling rustc. This matches the common convention used in most rust
crates to include generated source code from the path defined in the
OUT_DIR environment variable.

A couple other small notable changes are included in this CL:

* prebuiltLibraries can no longer include generated source files as they
  should be prebuilt.
* srcPathFromModuleSrcs now excludes the main source file from the
  second return value so its a list of only the generated sources.

Bug: 159064919
Test: Local example rust_library compiles with rust_bindgen dependency.
Test: Local example rust_library compiles with genrule dependency.
Test: Collision detected when multiple providers produce similar output.
Test: New Soong tests pass.
Change-Id: I59f54a25368c680b9086420c47ec24ab8cd1de6b
2020-07-20 13:40:31 -04:00
Chih-Hung Hsieh
bbd25aeb42 Specify module dependency in the srcs list
* "srcs" list contains one main Rust source file,
  followed by optional dependent modules.
* A dependent module included in the "srcs" list is
  the module name prefixed with ":".
* Add a simple test.

Bug: 160331255
Test: make and manual test build dependencies on genrule modules
Change-Id: I4f079138c2599158810b6412fce81b612a3f64a4
2020-07-08 23:50:00 -07:00
Matthew Maurer
0f003b1851 rust: Add rustlibs auto dependency selection
Adds the rustlibs dependency type which will automatically select
between rlib and dylib based on the type of the library.

Bug: 143217452
Test: cd external/rust; mma
Change-Id: I97faadae98bf957090a32939cfb2d3a10f74a057
2020-07-01 11:27:12 -07:00
Ivan Lozano
8a23fa4819 [Rust] Remove unused variables and deduplicate.
Bug: 158731826
Test: cd external/rust/crates; mma
Change-Id: I8c9e5cfeaf941b9676b92451b227c15db9a41bbc
2020-06-16 10:28:25 -04:00
Ivan Lozano
a0cd8f9acb Add gcov coverage support to Rust modules.
This adds gcov coverage support for Rust device library and binary
modules (including test modules). Support is provided to pass Rust
static library gcno files to CC modules and visa versa.

Additional changes:
 * Begin mutator added for Rust modules.
 * SuffixInList added to android package.
 * CoverageEnabled added to Coverage interface.
 * CoverageFiles added to LinkableLibrary interface.
 * Fix in coverage mutator for non-CC modules which marked the wrong
   variant as the coverage variant.
 * Added coverage libraries to the cc.GatherRequiredDepsForTest.

Bug: 146448203
Test: NATIVE_COVERAGE=true COVERAGE_PATHS='*' m -j <rust_module>
Change-Id: If20728bdde42a1dd544a35a40f0d981b80a5835f
2020-05-05 10:30:15 -04:00
Chih-Hung Hsieh
9a4a7bab41 Install rust tests under nativetest(64)
* Now the installation directories match those for C/C++ tests:
  * The relative_install_path refers to path under nativetest(64).
  * Device test files are installed in data/nativetest(64).
  * Automatically generated configuration files and copied test binaries
    are still in the "testcases" directory.
* Change host test configuration to run test binary files
  in testcases/<mutated_module_name>/<arch_type>/<stem_name>

Bug: 140938178
Test: atest --include-subdirs under external/rust/crates
Change-Id: I4b29afb897f4ba8749e87f79857c5b1a959bb2b0
2019-12-13 14:51:32 -08:00
Jeffrey Vander Stoep
f3a2d5abc3 Merge changes from topic "rust-sysroot"
* changes:
  Enable x86_64 device support
  Build Rust Device Sysroots in Soong
2019-11-14 18:28:13 +00:00
Matthew Maurer
99020b04fb Build Rust Device Sysroots in Soong
In order to ensure we are using current platform Bionic for any platform
Rust binaries, we need to build the sysroot in Soong. This will also
enable us too hook the "test" crate if necessary.

While both a dynamic and static sysroot are available, on device only a
dynamic sysroot will be injected. On host, we continue using the sysroot
used to build the compiler as before.

Bug: 139486496
Change-Id: I127377e5b056610ceb5015a34d266250320fbc31
2019-11-13 17:46:19 -08:00
Ivan Lozano
b2df9f88eb Soong Rust source clean up.
Break up some of the longer lines in the Rust source and refactor the
TransformSrcto* functions to take fewer arguments.

Bug: N/A
Test: m -j crosvm.experimental
Change-Id: Ia0deb8bfe3e1bfd07ef35633e9a7e6f5e799b3a3
2019-11-05 12:16:46 -08:00