Use x86_64-unknown-linux-gnu for host cargo build

* Now a host cargo build uses the same --target flag as a default
  device build.
* When --device is given without --no-host or other device flag,
  we can use one cargo build to get the required rustc flags for
  both host and device builds.
* We still need merge_host_device because cargo build --test could
  recompile a library and create duplicated calls to rustc.
* Fix a gpylint warning missing-function-docstring.
* This change also detected a bug in
  external/rust/crates/structopt-derive/Android.bp
  The cargo build --tests for device was optimized out
  and the device test module was missing in Android.bp.

Bug: 169872957
Test: regenerate all .bp files in external/rust/crates
Change-Id: Ieabdc5a99534a60e4d459c9a966f7a867104c785
This commit is contained in:
Chih-Hung Hsieh
2020-09-30 13:09:30 -07:00
parent 0e7bb0182d
commit e1b7bb667b

View File

@@ -27,15 +27,8 @@ The Cargo.toml file should work at least for the host platform.
--device flag, for example: --device flag, for example:
cargo2android.py --run --device cargo2android.py --run --device
This is equivalent to using the --cargo flag to add extra builds: Note that cargo build is only called once with the default target
cargo2android.py --run x86_64-unknown-linux-gnu.
--cargo "build"
--cargo "build --target x86_64-unknown-linux-gnu"
On MacOS, use x86_64-apple-darwin as target triple.
Here the host target triple is used as a fake cross compilation target.
If the crate's Cargo.toml and environment configuration works for an
Android target, use that target triple as the cargo build flag.
(3) To build default and test crates, for host and device, use both (3) To build default and test crates, for host and device, use both
--device and --tests flags: --device and --tests flags:
@@ -43,8 +36,6 @@ The Cargo.toml file should work at least for the host platform.
This is equivalent to using the --cargo flag to add extra builds: This is equivalent to using the --cargo flag to add extra builds:
cargo2android.py --run cargo2android.py --run
--cargo "build"
--cargo "build --tests"
--cargo "build --target x86_64-unknown-linux-gnu" --cargo "build --target x86_64-unknown-linux-gnu"
--cargo "build --tests --target x86_64-unknown-linux-gnu" --cargo "build --tests --target x86_64-unknown-linux-gnu"
@@ -305,16 +296,13 @@ class Crate(object):
def merge(self, other, outf_name): def merge(self, other, outf_name):
"""Try to merge crate into self.""" """Try to merge crate into self."""
# Cargo build --tests could recompile a library for tests.
# We need to merge such duplicated calls to rustc, with
# the algorithm in merge_host_device.
should_merge_host_device = self.merge_host_device(other) should_merge_host_device = self.merge_host_device(other)
should_merge_test = False should_merge_test = False
if not should_merge_host_device: if not should_merge_host_device:
should_merge_test = self.merge_test(other) should_merge_test = self.merge_test(other)
# A for-device test crate can be merged with its for-host version,
# or merged with a different test for the same host or device.
# Since we run cargo once for each device or host, test crates for the
# first device or host will be merged first. Then test crates for a
# different device or host should be allowed to be merged into a
# previously merged one, maybe for a different device or host.
if should_merge_host_device or should_merge_test: if should_merge_host_device or should_merge_test:
self.runner.init_bp_file(outf_name) self.runner.init_bp_file(outf_name)
with open(outf_name, 'a') as outf: # to write debug info with open(outf_name, 'a') as outf: # to write debug info
@@ -332,8 +320,6 @@ class Crate(object):
self.write('\n// Before merge definition (2):') self.write('\n// Before merge definition (2):')
other.dump_debug_info() other.dump_debug_info()
# Merge properties of other to self. # Merge properties of other to self.
self.host_supported = self.host_supported or other.host_supported
self.device_supported = self.device_supported or other.device_supported
self.has_warning = self.has_warning or other.has_warning self.has_warning = self.has_warning or other.has_warning
if not self.target: # okay to keep only the first target triple if not self.target: # okay to keep only the first target triple
self.target = other.target self.target = other.target
@@ -367,7 +353,7 @@ class Crate(object):
dir_name = os.path.dirname(dir_name) dir_name = os.path.dirname(dir_name)
def add_codegens_flag(self, flag): def add_codegens_flag(self, flag):
# ignore options not used in Android """Ignore options not used in Android."""
# 'prefer-dynamic' does not work with common flag -C lto # 'prefer-dynamic' does not work with common flag -C lto
# 'embed-bitcode' is ignored; we might control LTO with other .bp flag # 'embed-bitcode' is ignored; we might control LTO with other .bp flag
# 'codegen-units' is set in Android global config or by default # 'codegen-units' is set in Android global config or by default
@@ -485,11 +471,8 @@ class Crate(object):
self.errors += 'ERROR: cannot generate both lib and rlib crate types\n' self.errors += 'ERROR: cannot generate both lib and rlib crate types\n'
if not self.root_pkg: if not self.root_pkg:
self.root_pkg = self.crate_name self.root_pkg = self.crate_name
if self.target: self.device_supported = self.runner.args.device
self.device_supported = True self.host_supported = not self.runner.args.no_host
self.host_supported = True # assume host supported for all builds
if self.runner.args.no_host: # unless --no-host was specified
self.host_supported = False
self.cfgs = sorted(set(self.cfgs)) self.cfgs = sorted(set(self.cfgs))
self.features = sorted(set(self.features)) self.features = sorted(set(self.features))
self.codegens = sorted(set(self.codegens)) self.codegens = sorted(set(self.codegens))
@@ -803,7 +786,7 @@ class Crate(object):
self.write(' stem: "' + self.stem + '",') self.write(' stem: "' + self.stem + '",')
if self.has_warning and not self.cap_lints and not self.default_srcs: if self.has_warning and not self.cap_lints and not self.default_srcs:
self.write(' // has rustc warnings') self.write(' // has rustc warnings')
if self.host_supported and self.device_supported: if self.host_supported and self.device_supported and self.module_type != 'rust_proc_macro':
self.write(' host_supported: true,') self.write(' host_supported: true,')
if not self.defaults: if not self.defaults:
self.write(' crate_name: "' + self.crate_name + '",') self.write(' crate_name: "' + self.crate_name + '",')
@@ -1055,18 +1038,14 @@ class Runner(object):
if args.cargo: if args.cargo:
self.cargo = ['clean'] + args.cargo self.cargo = ['clean'] + args.cargo
else: else:
self.cargo = ['clean', 'build']
if args.no_host: # do not run "cargo build" for host
self.cargo = ['clean']
default_target = '--target x86_64-unknown-linux-gnu' default_target = '--target x86_64-unknown-linux-gnu'
if args.device: # Use the same target for both host and default device builds.
self.cargo.append('build ' + default_target) # Same target is used as default in host x86_64 Android compilation.
# Note: b/169872957, prebuilt cargo failed to build vsock
# on x86_64-unknown-linux-musl systems.
self.cargo = ['clean', 'build ' + default_target]
if args.tests: if args.tests:
if not args.no_host:
self.cargo.append('build --tests')
self.cargo.append('build --tests ' + default_target) self.cargo.append('build --tests ' + default_target)
elif args.tests and not args.no_host:
self.cargo.append('build --tests')
def setup_cargo_path(self): def setup_cargo_path(self):
"""Find cargo in the --cargo_bin or prebuilt rust bin directory.""" """Find cargo in the --cargo_bin or prebuilt rust bin directory."""