1*90c8c64dSAndroid Build Coastguard Worker# cargo_embargo 2*90c8c64dSAndroid Build Coastguard Worker 3*90c8c64dSAndroid Build Coastguard Worker`cargo_embargo` is a tool to generate `Android.bp` files automatically for external Rust crates 4*90c8c64dSAndroid Build Coastguard Workerwhich are intended to build with cargo. It can be built with `m cargo_embargo` and run with 5*90c8c64dSAndroid Build Coastguard Worker`cargo_embargo generate cargo_embargo.json` in a directory containing one or more Rust packages. If 6*90c8c64dSAndroid Build Coastguard Workersuccessful this will write out an `Android.bp` file. 7*90c8c64dSAndroid Build Coastguard Worker 8*90c8c64dSAndroid Build Coastguard Worker## Configuration 9*90c8c64dSAndroid Build Coastguard Worker 10*90c8c64dSAndroid Build Coastguard Worker`cargo_embargo` is configured with a JSON configuration file usually called `cargo_embargo.json`. 11*90c8c64dSAndroid Build Coastguard WorkerThis can contain a number of options, specified at several levels. A config may cover one or more 12*90c8c64dSAndroid Build Coastguard Workerpackages, and have one or more variants. All packages under `external/rust/crates` use a separate 13*90c8c64dSAndroid Build Coastguard Worker`cargo_embargo.json` file per package, but other users (such as crosvm) may use a single 14*90c8c64dSAndroid Build Coastguard Worker`cargo_embargo.json` for a tree of many packages. Most configurations have a single variant, but 15*90c8c64dSAndroid Build Coastguard Workermultiple variants are used in some cases to provide both `std` and `no_std` variants of a package. 16*90c8c64dSAndroid Build Coastguard Worker 17*90c8c64dSAndroid Build Coastguard WorkerThe overall structure of a config file looks like this: 18*90c8c64dSAndroid Build Coastguard Worker 19*90c8c64dSAndroid Build Coastguard Worker```json 20*90c8c64dSAndroid Build Coastguard Worker{ 21*90c8c64dSAndroid Build Coastguard Worker // Top-level options, for all packages and variants. 22*90c8c64dSAndroid Build Coastguard Worker "package": { 23*90c8c64dSAndroid Build Coastguard Worker "package_name": { 24*90c8c64dSAndroid Build Coastguard Worker // Options for all variants of a package. 25*90c8c64dSAndroid Build Coastguard Worker }, 26*90c8c64dSAndroid Build Coastguard Worker "another_package_name": { 27*90c8c64dSAndroid Build Coastguard Worker // ... 28*90c8c64dSAndroid Build Coastguard Worker } 29*90c8c64dSAndroid Build Coastguard Worker }, 30*90c8c64dSAndroid Build Coastguard Worker "variants": [ 31*90c8c64dSAndroid Build Coastguard Worker { 32*90c8c64dSAndroid Build Coastguard Worker // Options for a specific variant of all packages. 33*90c8c64dSAndroid Build Coastguard Worker "package": { 34*90c8c64dSAndroid Build Coastguard Worker "package_name": { 35*90c8c64dSAndroid Build Coastguard Worker // Options for a specific variant of a specific package. 36*90c8c64dSAndroid Build Coastguard Worker } 37*90c8c64dSAndroid Build Coastguard Worker } 38*90c8c64dSAndroid Build Coastguard Worker }, 39*90c8c64dSAndroid Build Coastguard Worker { 40*90c8c64dSAndroid Build Coastguard Worker // Options for another variant. 41*90c8c64dSAndroid Build Coastguard Worker } 42*90c8c64dSAndroid Build Coastguard Worker ] 43*90c8c64dSAndroid Build Coastguard Worker} 44*90c8c64dSAndroid Build Coastguard Worker``` 45*90c8c64dSAndroid Build Coastguard Worker 46*90c8c64dSAndroid Build Coastguard WorkerIf a package is not included in the `package` map then it is assumed to use default options. If the 47*90c8c64dSAndroid Build Coastguard Worker`package` map is omitted then all packages will use default options. If `variants` is omitted then 48*90c8c64dSAndroid Build Coastguard Workerthere is assumed to be a single variant. Thus `{}` is a valid config file for a single variant with 49*90c8c64dSAndroid Build Coastguard Workerall default options. 50*90c8c64dSAndroid Build Coastguard Worker 51*90c8c64dSAndroid Build Coastguard WorkerA typical config file for a simple package may look like: 52*90c8c64dSAndroid Build Coastguard Worker 53*90c8c64dSAndroid Build Coastguard Worker```json 54*90c8c64dSAndroid Build Coastguard Worker{ 55*90c8c64dSAndroid Build Coastguard Worker "run_cargo": false, 56*90c8c64dSAndroid Build Coastguard Worker "tests": true 57*90c8c64dSAndroid Build Coastguard Worker} 58*90c8c64dSAndroid Build Coastguard Worker``` 59*90c8c64dSAndroid Build Coastguard Worker 60*90c8c64dSAndroid Build Coastguard WorkerThis expands to a single variant with the given options, and all packages (i.e. the only one) using 61*90c8c64dSAndroid Build Coastguard Workerdefault options. 62*90c8c64dSAndroid Build Coastguard Worker 63*90c8c64dSAndroid Build Coastguard Worker### Top-level configuration options 64*90c8c64dSAndroid Build Coastguard Worker 65*90c8c64dSAndroid Build Coastguard WorkerThese options may all be specified at the top level of the config file, or overridden per variant. 66*90c8c64dSAndroid Build Coastguard Worker 67*90c8c64dSAndroid Build Coastguard Worker| Name | Type | Default | Meaning | 68*90c8c64dSAndroid Build Coastguard Worker| -------------------------- | ------------------------- | ----------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 69*90c8c64dSAndroid Build Coastguard Worker| `tests` | boolean | `false` | Whether to output `rust_test` modules. | 70*90c8c64dSAndroid Build Coastguard Worker| `features` | list of strings | - | Set of features to enable. If not set, uses the default crate features. | 71*90c8c64dSAndroid Build Coastguard Worker| `workspace` | boolean | `false` | Whether to build with `--workspace`. | 72*90c8c64dSAndroid Build Coastguard Worker| `workspace_excludes` | list of strings | `[]` | When workspace is enabled, list of `--exclude` crates. | 73*90c8c64dSAndroid Build Coastguard Worker| `global_defaults` | string | - | Value to use for every generated module's `defaults` field. | 74*90c8c64dSAndroid Build Coastguard Worker| `apex_available` | list of strings | `["//apex_available:platform", "//apex_available:anyapex"]` | Value to use for every generated library module's `apex_available` field. | 75*90c8c64dSAndroid Build Coastguard Worker| `native_bridge_supported` | boolean | `false` | Value to use for every generated library module's `native_bridge_supported` field. | 76*90c8c64dSAndroid Build Coastguard Worker| `product_available` | boolean | `true` | Value to use for every generated library module's `product_available` field. | 77*90c8c64dSAndroid Build Coastguard Worker| `ramdisk_available` | boolean | `false` | Value to use for every generated library module's `ramdisk_available` field. | 78*90c8c64dSAndroid Build Coastguard Worker| `recovery_available` | boolean | `false` | Value to use for every generated library module's `recovery_available` field. | 79*90c8c64dSAndroid Build Coastguard Worker| `vendor_available` | boolean | `true` | Value to use for every generated library module's `vendor_available` field. | 80*90c8c64dSAndroid Build Coastguard Worker| `vendor_ramdisk_available` | boolean | `false` | Value to use for every generated library module's `vendor_ramdisk_available` field. | 81*90c8c64dSAndroid Build Coastguard Worker| `min_sdk_version` | string | - | Minimum SDK version for generated modules' `min_sdk_version` field. | 82*90c8c64dSAndroid Build Coastguard Worker| `module_name_overrides` | string => string | `{}` | Map of renames for modules. For example, if a "libfoo" would be generated and there is an entry ("libfoo", "libbar"), the generated module will be called "libbar" instead. | 83*90c8c64dSAndroid Build Coastguard Worker| `cfg_blocklist` | list of strings | `[]` | `cfg` flags in this list will not be included. | 84*90c8c64dSAndroid Build Coastguard Worker| `extra_cfg` | list of strings | `[]` | Extra `cfg` flags to enable in output modules. | 85*90c8c64dSAndroid Build Coastguard Worker| `module_blocklist` | list of strings | `[]` | Modules in this list will not be generated. | 86*90c8c64dSAndroid Build Coastguard Worker| `module_visibility` | string => list of strings | `{}` | Modules name => Soong "visibility" property. | 87*90c8c64dSAndroid Build Coastguard Worker| `run_cargo` | boolean | `true` | Whether to run the cargo build and parse its output, rather than just figuring things out from the cargo metadata. | 88*90c8c64dSAndroid Build Coastguard Worker| `generate_androidbp` | boolean | `true` | Whether to generate Android build rules in an `Android.bp` file. | 89*90c8c64dSAndroid Build Coastguard Worker| `generate_rulesmk` | boolean | `false` | Whether to generate Trusty build rules in `rules.mk` and `Android.bp` files. | 90*90c8c64dSAndroid Build Coastguard Worker 91*90c8c64dSAndroid Build Coastguard WorkerOf particular note, it is preferable to set `run_cargo` to `false` where possible as it is 92*90c8c64dSAndroid Build Coastguard Workersignificantly faster. However, this may miss important details in more complicated cases, such as 93*90c8c64dSAndroid Build Coastguard Workerpackages with a `build.rs`, so it is recommended to run with `run_cargo` set to `true` initially, 94*90c8c64dSAndroid Build Coastguard Workerand then compare the output when it is changed to `false`. 95*90c8c64dSAndroid Build Coastguard Worker 96*90c8c64dSAndroid Build Coastguard Worker### Per-package configuration options 97*90c8c64dSAndroid Build Coastguard Worker 98*90c8c64dSAndroid Build Coastguard WorkerThese options may be specified per package. Most may also be overridden per variant. They may not be 99*90c8c64dSAndroid Build Coastguard Workerspecified outside of a package. 100*90c8c64dSAndroid Build Coastguard Worker 101*90c8c64dSAndroid Build Coastguard Worker| Name | Type | Default | Per variant | Meaning | 102*90c8c64dSAndroid Build Coastguard Worker| ----------------------- | ------------------------- | ------- | ----------- | ------------------------------------------------------------------------------------------------------------------ | 103*90c8c64dSAndroid Build Coastguard Worker| `add_toplevel_block` | path | - | no | File with content to append to the end of the generated Android.bp. | 104*90c8c64dSAndroid Build Coastguard Worker| `patch` | path | - | no | Patch file to apply after Android.bp is generated. | 105*90c8c64dSAndroid Build Coastguard Worker| `rulesmk_patch` | path | - | no | Patch file to apply after rules.mk is generated. | 106*90c8c64dSAndroid Build Coastguard Worker| `license_text` | list of paths | - | no | Files to use for `license_text` in `license` module. | 107*90c8c64dSAndroid Build Coastguard Worker| `alloc` | boolean | `false` | yes | Link against `alloc`. Only valid if `no_std` is also true. | 108*90c8c64dSAndroid Build Coastguard Worker| `device_supported` | boolean | `true` | yes | Whether to compile for device. Defaults to true. | 109*90c8c64dSAndroid Build Coastguard Worker| `host_supported` | boolean | `true` | yes | Whether to compile for host. Defaults to true. | 110*90c8c64dSAndroid Build Coastguard Worker| `host_first_multilib` | boolean | `false` | yes | Add a `compile_multilib: "first"` property to host modules. | 111*90c8c64dSAndroid Build Coastguard Worker| `force_rlib` | boolean | `false` | yes | Generate "rust_library_rlib" instead of "rust_library". | 112*90c8c64dSAndroid Build Coastguard Worker| `no_presubmit` | boolean | `false` | yes | Whether to disable "unit_test" for "rust_test" modules. | 113*90c8c64dSAndroid Build Coastguard Worker| `add_module_block` | path | - | yes | File with content to append to the end of each generated module. | 114*90c8c64dSAndroid Build Coastguard Worker| `dep_blocklist` | list of strings | `[]` | yes | Modules in this list will not be added as dependencies of generated modules. | 115*90c8c64dSAndroid Build Coastguard Worker| `no_std` | boolean | `false` | yes | Don't link against `std`, only `core`. | 116*90c8c64dSAndroid Build Coastguard Worker| `copy_out` | boolean | `false` | yes | Copy `build.rs` output to `./out/*` and add a genrule to copy `./out/*` to genrule output. | 117*90c8c64dSAndroid Build Coastguard Worker| `test_data` | string => list of strings | `{}` | yes | Add the given files to the given tests' `data` property. The key is the test source filename relative to the crate | 118*90c8c64dSAndroid Build Coastguard Worker| `whole_static_libs` | list of strings | `[]` | yes | Static libraries in this list will instead be added as whole_static_libs. | 119*90c8c64dSAndroid Build Coastguard Worker| `exported_c_header_dir` | list of paths | `[]` | yes | Directories with headers to export for C usage. | 120*90c8c64dSAndroid Build Coastguard Worker 121*90c8c64dSAndroid Build Coastguard Worker## Auto-config 122*90c8c64dSAndroid Build Coastguard Worker 123*90c8c64dSAndroid Build Coastguard WorkerFor importing a new package, you may start by running cargo_embargo's autoconfig mode: 124*90c8c64dSAndroid Build Coastguard Worker 125*90c8c64dSAndroid Build Coastguard Worker``` 126*90c8c64dSAndroid Build Coastguard Workercargo_embargo autoconfig cargo_embargo.json 127*90c8c64dSAndroid Build Coastguard Worker``` 128*90c8c64dSAndroid Build Coastguard Worker 129*90c8c64dSAndroid Build Coastguard WorkerThis will attempt to generate a suitable `cargo_embargo.json` for the package in the current 130*90c8c64dSAndroid Build Coastguard Workerdirectory, by trying with `run_cargo` both `true` and `false`, and including tests if there are any. 131