Give a warning to use --copy-out

* Suggest --copy-out if it is not used, cargo output files are found,
  and env!("CARGO_OUT") is found in .rs files.
* Expect to see this warning for libsqlite3-sys and grpcio-sys,
  which fit to the use case of --copy-out but they have locally
  defined .bp files and non-original output files.
* Fix one pylint warning of extra long line.

Bug: 172299436
Test: run cargo with same flags for all rust/crates/*
Test: run cargo without --copy-out for anyhow, protobuf, clang-sys, etc.
Change-Id: Ic346c8e1146b1bb77e63c4cb12947f202b0f458e
This commit is contained in:
Chih-Hung Hsieh
2020-11-03 15:05:58 -08:00
parent 87b2d90748
commit 60140756b8

View File

@@ -1116,24 +1116,33 @@ class Runner(object):
rust_version = version rust_version = version
return '.'.join(rust_version) return '.'.join(rust_version)
def copy_out_files(self): def find_out_files(self):
"""Copy build.rs output files to ./out and set up build_out_files."""
if not self.args.copy_out or self.checked_out_files:
return
self.checked_out_files = True
# list1 has build.rs output for normal crates # list1 has build.rs output for normal crates
list1 = glob.glob(TARGET_TMP + '/*/*/build/' + self.root_pkg + '-*/out/*') list1 = glob.glob(TARGET_TMP + '/*/*/build/' + self.root_pkg + '-*/out/*')
# list2 has build.rs output for proc-macro crates # list2 has build.rs output for proc-macro crates
list2 = glob.glob(TARGET_TMP + '/*/build/' + self.root_pkg + '-*/out/*') list2 = glob.glob(TARGET_TMP + '/*/build/' + self.root_pkg + '-*/out/*')
return list1 + list2
def copy_out_files(self):
"""Copy build.rs output files to ./out and set up build_out_files."""
if self.checked_out_files:
return
self.checked_out_files = True
cargo_out_files = self.find_out_files()
out_files = set() out_files = set()
if list1 or list2: if cargo_out_files:
os.makedirs('out', exist_ok=True) os.makedirs('out', exist_ok=True)
for path in list1 + list2: for path in cargo_out_files:
file_name = path.split('/')[-1] file_name = path.split('/')[-1]
out_files.add(file_name) out_files.add(file_name)
shutil.copy(path, 'out/' + file_name) shutil.copy(path, 'out/' + file_name)
self.build_out_files = sorted(out_files) self.build_out_files = sorted(out_files)
def has_used_out_dir(self):
"""Returns true if env!("OUT_DIR") is found."""
return 0 == os.system('grep -rl --exclude build.rs --include \\*.rs' +
' \'env!("OUT_DIR")\' * > /dev/null')
def copy_out_module_name(self): def copy_out_module_name(self):
if self.args.copy_out and self.build_out_files: if self.args.copy_out and self.build_out_files:
return 'copy_' + self.root_pkg + '_build_out' return 'copy_' + self.root_pkg + '_build_out'
@@ -1328,7 +1337,8 @@ class Runner(object):
return self return self
if self.args.verbose: if self.args.verbose:
print('### INFO: applying local patch file:', self.args.patch) print('### INFO: applying local patch file:', self.args.patch)
os.system('patch -s --no-backup-if-mismatch ./Android.bp ' + self.args.patch) os.system('patch -s --no-backup-if-mismatch ./Android.bp ' +
self.args.patch)
return self return self
def gen_bp(self): def gen_bp(self):
@@ -1337,7 +1347,11 @@ class Runner(object):
print('Dry-run skip: read', CARGO_OUT, 'write Android.bp') print('Dry-run skip: read', CARGO_OUT, 'write Android.bp')
elif os.path.exists(CARGO_OUT): elif os.path.exists(CARGO_OUT):
self.find_root_pkg() self.find_root_pkg()
self.copy_out_files() if self.args.copy_out:
self.copy_out_files()
elif self.find_out_files() and self.has_used_out_dir():
print('WARNING: ' + self.root_pkg + ' has cargo output files; ' +
'please rerun with the --copy-out flag.')
with open(CARGO_OUT, 'r') as cargo_out: with open(CARGO_OUT, 'r') as cargo_out:
self.parse(cargo_out, 'Android.bp') self.parse(cargo_out, 'Android.bp')
self.crates.sort(key=get_module_name) self.crates.sort(key=get_module_name)