Commit graph

74 commits

Author SHA1 Message Date
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
Ivan Lozano
fba2aa255c rust: Support new rust_stdlib_prebuilt_host type
Refactor Rust prebuilts to support the new rust_stdlib_prebuilt_host
module type, and change the format for depending on the prebuilt host
stdlibs.

Bug: 140642453
Test: m
Change-Id: Ifbc4741818777934e917631c788b20911856c44a
2021-11-12 08:58:17 -05: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
Seth Moore
3afac0b36f Flag an error on empty rust module srcs
It's an error if a rust module's srcs is empty. Prior to this change
the output was a panic that complains loudly about a slice error and
nothing else. This change doesn't stop the panic, but at least adds
a bit of context so devs who make a simple mistake can more easily
fix it.

Test: SOONG_GEN_RUST_PROJECT=1 m nothing
Change-Id: Id7d8465d533413c3000699661222a53a7c8678f3
2021-10-14 15:25:07 +00:00
Ivan Lozano
67eada34db rust: Refactor cfg and feature flag calculation
Move the cfg and feature flag calculation out of compilerFlags so that
it's a separate step.

The previous arrangement resulted in overridden compilerFlags which
must to set any additional cfgs/features before calling the base.
This is a bit confusing and undocumented behavior, so instead break
it out into a separate call that can itself be overriden.

Bug: N/A
Test: Soong tests pass
Change-Id: I28e4f707b3b3ca6eb621b7613c3737817f877bb8
2021-09-23 12:21:04 -04:00
Matthew Maurer
9f59e8db27 rust: Hook up InstallIn functions + Product
InstallIn functions must be hooked up not only to install to special
images, but also to receive special handling by android/arch.go such
that some images are implicitly multilib: first.

Also hook up more product details.

Bug: 178565008
Bug: 165791368
Test: lunch aosp_arm64; m
Change-Id: I1980d5aa9d55f78c222b98d60a404cd6ea5abbfb
2021-08-20 12:14:54 -07:00
Ivan Lozano
0ad64f50fd Merge "rust: Allow modules to use only generated sources" 2021-08-13 19:37:04 +00:00
Ivan Lozano
e4db0036d7 rust: Allow modules to use only generated sources
Removes the need for rust_test modules that test generated source to
define a source file that simply calls include()! to pull in the
generated source.

Bug: 196076408
Test: m libselinux_bindgen_test
Change-Id: I600301ab74f6ece861916fa2545b061c81b0d92d
2021-08-13 13:37:19 -04:00
Ivan Lozano
8711c5cf8c rust: Link libunwind into Rust static executables.
Rust static executables fail to build due to missing unwind symbols.
Make sure we link libunwind when building these.

Bug: 194386505
Test: rust_binary with static_executable: true builds.
Change-Id: I74bab25e186e181bc1ac8fdd608687707c662e4d
2021-08-13 13:15:31 -04:00
Ivan Lozano
a9a1fc0747 rust: Add support to emit certain Cargo env vars.
Some crates expect Cargo to provide certain environment variables. This
CL adds a compatability flag that emulates the behavior of Cargo by
setting these environment variables when building.

Bug: 171011485
Test: New soong tests pass
Test: quiche no longer requires patch removing CARGO_PKG_VERSION
Change-Id: I4c95c284846f6075428c6f61fe8c260f2e35fbd9
2021-08-12 13:18:34 +00:00
Ivan Lozano
45a9e3196c rust: Prevent manually defined lib link flags.
Adds a filter to prevent modules from manually defining certain linkage
flags. These flags should only be automatically generated based on
listed dependencies.

Bug: 161942165
Test: Soong tests pass
Change-Id: Ibc014391ef1de10188c2d139d41d0241d6c68b5f
2021-07-27 15:05:13 -04:00
Colin Cross
0c66bc615b Replace android.BuildOs with Config.BuildOS
Replace the android.BuildOs constant with Config.BuildOS so that it
can vary based on the product config.

Bug: 190084016
Test: all Soong tests
Change-Id: Ia67f872d8b2ab788747a22e3a9659dc21c9775cd
2021-07-20 12:46:48 -07: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
d7586b6526 Refactor vendor snapshot to use LinkableInterface.
Refactors the vendor snapshot support to use the LinkableInterface
so that support can be extended to Rust. This CL does not add
vendor snapshot support for Rust; that is left for a follow-on CL.

Bug: 184042776
Test: m nothing
Change-Id: Id0c4970ca00053484a52677d182153cbc454c301
2021-05-12 14:01:10 -04:00
Treehugger Robot
f9b3e22293 Merge "Remove unnecessary android:"path" tags in rust" 2021-04-21 15:53:44 +00:00
Liz Kammer
eda9398bbf Remove unnecessary android:"path" tags in rust
Custom_bindgen is handled by adding a relevant dependency tag.
Flags and Ld_flags are not paths/references to other modules.

Test: go test soong tests
Change-Id: Ic6a69521102318b3b9f29b59560bcefbc9aff8fa
2021-04-20 16:06:02 +00: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
Ivan Lozano
c08897c1e4 Add more Rust vendor image support.
This adds Rust vendor image support for all module types except
Rust prebuilts.

Bug: 184042776
Test: New Soong tests.
Test: Example cc_library vendor module can depend on rust_ffi_shared.
Test: Example rust_library vendor-only module compiles.

Change-Id: Iaa30ad51fdaedcbf14687da5472581f6af62ff59
2021-04-13 13:17:12 -04:00
Thiébaud Weksteen
e54504e19c Merge "rust: Add cfgs property" 2021-04-08 08:57:22 +00:00
Jiyong Park
e54f07e38a Stripped rust bin/libs are included in APEX
Previously, when a rust bin or library is selected for an APEX,
OutputFile() was used to get the file to be used. However, OutputFile()
always returns the unstripped output file even when the stripped output
file is available. As a result, APEX having a rust module was very big
due to the debugging information that exists in the unstripped file.

When a rust module is directly installed to the built-in partitions, we
use the stripped one whenever it's available. To make the same happen
when the rust module is placed in an APEX, OutputFile() is modified to
return the unstripped output if it's available.

Bug: 181751814
Test: TARGET_BUILD_APPS=com.android.virt m
The size is reduced from 180MB to 43MB

Change-Id: I6f8479e6a4794aac8bf94a84afdf65d417c75db0
2021-04-07 22:23:31 +09:00
Thiébaud Weksteen
c44e7372ed rust: Add cfgs property
Rust configuration options ("--cfg") are currently defined using the
"flags" property. Adds a specific property to be able to forward these
to the rust-analyzer configuration (rust-project.json).

Bug: 183727250
Test: m libstd
Change-Id: Ida89097814bcd1a45c02a8a79ec5a8e9e59701bd
2021-04-07 15:04:10 +02:00
Ivan Lozano
63bb7680c9 rust: Add whole_static_libs, revert static_lib
Revert the static_lib behavior to the previous behavior (pass static
libs to the linker rather than via to rustc using `-lstatic=<lib>`). To
bundle static libraries into libraries, provide the whole_static_libs
property which retains the current static_libs behavior.

Passing all static libraries via -lstatic was resulting in odd bloat
where in some cases static symbols were duplicated in binaries and
libraries. This split makes it possible to be explicit about when static
libraries should be bundled.

Bug: 183182230
Test: mma system/bt; mma system/security/keystore2; mma external/rust

Change-Id: Ic2dde5d1542dca5ce145aa3a3fbd9ea54440d991
2021-03-25 13:50:17 -04: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
Thiébaud Weksteen
ee6a89ba44 Export OUT_DIR variable to rust-project.json
This variable is required by rust-analyzer to correctly process crates
that uses the include!(concat!(env!("OUT_DIR"), ...)) pattern.

Adds an initialize method to baseCompiler to save the computed path for
this directory. It is not possible to use the BeginMutator as the
BaseModuleContext does not contain enough information to use
PathForModuleOut.

Bug: 175004835
Test: SOONG_GEN_RUST_PROJECT=1 m nothing; inspect rust-project.json
Change-Id: If47b3832d3cca5712ae87773c174a61f5ee27bf8
2021-03-15 08:13:20 +00:00
Ivan Lozano
36eb24b3ab Merge "rust: Emit android vndk cfg flag." 2021-02-14 16:20:46 +00:00
Ivan Lozano
f76cdf7c20 rust: Emit android vndk cfg flag.
Pass an "android_vndk" cfg flag that indicates this code
targets the vndk. This can be useful in instances where code
might need to behave differently.

This also includes a fix to make sure our vendor Soong tests
are correctly configured.

Bug: 179907868
Test: Soong tests pass.
Test: Example module emits new cfg flags.
Change-Id: I01cdf91f6f9d42cd8a759266d5170479664bf4bc
2021-02-12 12:17:50 -05:00
Ivan Lozano
c5d34ec100 rust: Use prebuilts for x86 host target.
Use prebuilts for x86 host targets to make both x86 and x86_64 builds
more consistent with one another. This fixes building x86 binaries.

Bug: 162063992
Bug: 169347277
Test: Build 32-bit host module.
Change-Id: Id11eb0f3b949439cec090060a33b755af94b5883
2021-02-09 14:22:00 -05: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
Treehugger Robot
2a3094c70f Merge "Migrate Rust to LLVM coverage." 2021-01-26 15:28:36 +00:00
Ivan Lozano
6cd99e6ab4 Add Rust fuzzing support.
Add a rust_fuzz module which builds a libfuzzer binary that enabes
asan+sancov. This relies on the libfuzzer-sys crate.

Bug: 147140513
Test: Local rust_fuzz example builds, fuzzes with asan+sancov.
Change-Id: I57db3b8d25869791824ccfab768d13b0bb9d42fa
2021-01-25 11:11:10 -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
Jaewoong Jung
18aefc1977 Remove unnecessary snake case variables.
Test: m nothing + TreeHugger
Change-Id: I99f7162944daa6c57c6ae4763261e108bb5cb6b1
2020-12-22 12:38:35 -08: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
2b0811310e rust: Add libstd linkage mutator for rlibs.
The current state of linkage is that device targets always link
libstd dynamically except for rust_ffi_static which requires a static
libstd linkage. However this prevents producing rust_ffi_static
modules which depend on other Rust libraries as those dependencies
will link libstd dynamically and cause a collision. We also want our
rust_test modules to statically link in libstd as well.

This adds a linkage mutator for rlibs that creates a variant for each
libstd linkage. Dependent modules can then select the variant that
matches their linkage of libstd.

Also fixes an issue where installation paths were being generated for
rlibs and static libs even though they weren't being installed. This broke
when adding the linkage mutator as Make would complain about multiple
targets producing the same output.

Bug: 168729404
Test: rust_ffi_static module with other rustlib dependency can be built.
Change-Id: I955b484bf5809e8fc5517750c7f8df82d3ca8895
2020-09-18 16:35:14 -04:00
Jiyong Park
b5d2dd210b Don't disable rust modules for linux_bionic
With If4300583edfb6d75bd4d984b38f73b6a406b4447, linux_bionic supports
arm64 and rust toolchain was configured to support the new target.
However, rust module types still have been disabled for linux_bionic
targets which forces many rust modules to be annotated with
target.linux_bionic_arm64.enabled: true, which will be very cumbersome.
Therefore, stop disabling the rust modules for linux_bionic.

Bug: 159685774
Test: HOST_CROSS_OS=linux_bionic m nothing
Change-Id: I68b97a43e8252fb82da6e87b3481fa237e1f2691
2020-09-15 12:56:13 +09: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
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
Thiébaud Weksteen
9e8451e524 rust: modify linting properties
Move the linting properties to an enum with 4 possible options:
"default", "android", "vendor" or "none". The previous logic for
default, based on the module's location, is kept. It is now possible to
force the upgrade to a certain lint level for some modules (e.g.
external/[...]/android). Update the unit tests and documentation.

Bug: 163400111
Test: m
Change-Id: I8e464b04401158ed2d3c518a9b72f145a9835c99
2020-08-13 15:58:09 +02:00
Thiébaud Weksteen
4318e1c650 rust: fully disable all lints when no_lint is set
If the no_lint attribute is set, we now explicitly disable any warning
or error.

Bug: 162897623
Test: cd external/selinux; mma; check the build log for warnings
Change-Id: I5338bda77ab25ded7cc43cc3afd29419fcbd9808
2020-08-07 13:56:35 +02:00
Stephen Crane
da931d4abd [rust] Disable lints for generated bindings
Bindgen generated bindings will not pass lints, so we need to disable
them when building bindings crates.

Test: m libbinder_ndk_bindgen
Change-Id: I4536f15f8505ff8d5436f1e6cbc2035e6801276d
2020-08-04 13:04:32 -07:00
Ivan Lozano
89e4882d37 Merge "[rust] Add SourceProviders as crates support." 2020-08-04 17:46:22 +00:00
Ivan Lozano
26ecd6c597 [rust] Add SourceProviders as crates support.
This allows SourceProvider modules to create rust_library variants so
that generated source can be referenced as an external crate rather than
via an include macro. This is done by including rust_bindgen modules
like any other library, as a dependency in either rlibs, dylibs, or
rustlibs.

This renames the stem and flags properties for rust_bindgen modules to
source_stem and bindgen_flags, respectively. This deconflicts with the
usage in baseCompiler.

This also removes 'subName' from the Module struct and moves it over to
SourceProvider, which was the only user. This allows us to set it in
baseSourceProvider's AndroidMk; setting it in Module's AndroidMk was
causing problems finding NOTICE files for bindgen library variants.

Bug: 159064919
Test: New Soong tests pass.
Test: Local test rust_binary can use rust_bindgen module as a crate.

Change-Id: Ieb2cb614c2dd0b5aa7120541d77f6f822a6a1806
2020-08-04 08:13:24 -04:00
Thiébaud Weksteen
e81c924f0c Move getEdition to a baseCompiler method
Test: cd build/soong; mma
Test: cd external/rust/; mma
Change-Id: Ide63b2e633e7c4345c6338e107f5d66688e4cf58
2020-08-03 11:00:43 +02: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
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
bb3add1104 rust: Suppress default sysroot unconditionally
With proper prebuilt modules, we can avoid any rustc implicit sysroot
searching.

Asd a bonus, this should make rust-project.json generation correctly
grab otherwise implicit dependencies.

Prebuilt rlibs may include several dependency rlibs. Without a
link_dirs attribute, every dependency (even if unexported) would need a
separate module.

Previously we were casing out on exact structs, which might be OK when
libraryDecorator and procMacroDecorator were the only possibilities, but
repeating the logic for three types is too much. Using an interface
makes this logic scale better.

Bug: 159591910
Test: cd external/rust; mma; m crosvm.experimental
Change-Id: Ia1124e09f48cd05e39f094bbcb988622ebd2272f
2020-07-01 11:27:12 -07:00