xref: /aosp_15_r20/tools/asuite/adevice/src/main.rs (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker //! Update an Android device with locally built changes.
2*c2e18aaaSAndroid Build Coastguard Worker mod adevice;
3*c2e18aaaSAndroid Build Coastguard Worker mod cli;
4*c2e18aaaSAndroid Build Coastguard Worker mod commands;
5*c2e18aaaSAndroid Build Coastguard Worker mod device;
6*c2e18aaaSAndroid Build Coastguard Worker mod fingerprint;
7*c2e18aaaSAndroid Build Coastguard Worker mod metrics;
8*c2e18aaaSAndroid Build Coastguard Worker mod progress;
9*c2e18aaaSAndroid Build Coastguard Worker mod restart_chooser;
10*c2e18aaaSAndroid Build Coastguard Worker mod tracking;
11*c2e18aaaSAndroid Build Coastguard Worker use tracing::{error, info};
12*c2e18aaaSAndroid Build Coastguard Worker 
13*c2e18aaaSAndroid Build Coastguard Worker use crate::adevice::Profiler;
14*c2e18aaaSAndroid Build Coastguard Worker use crate::adevice::RealHost;
15*c2e18aaaSAndroid Build Coastguard Worker use crate::device::RealDevice;
16*c2e18aaaSAndroid Build Coastguard Worker use crate::metrics::MetricSender;
17*c2e18aaaSAndroid Build Coastguard Worker use crate::metrics::Metrics;
18*c2e18aaaSAndroid Build Coastguard Worker 
19*c2e18aaaSAndroid Build Coastguard Worker use clap::Parser;
20*c2e18aaaSAndroid Build Coastguard Worker use std::fs::File;
21*c2e18aaaSAndroid Build Coastguard Worker use std::path::PathBuf;
22*c2e18aaaSAndroid Build Coastguard Worker 
23*c2e18aaaSAndroid Build Coastguard Worker use anyhow::Result;
24*c2e18aaaSAndroid Build Coastguard Worker 
main() -> Result<()>25*c2e18aaaSAndroid Build Coastguard Worker fn main() -> Result<()> {
26*c2e18aaaSAndroid Build Coastguard Worker     let total_time = std::time::Instant::now();
27*c2e18aaaSAndroid Build Coastguard Worker     let host = RealHost::new();
28*c2e18aaaSAndroid Build Coastguard Worker     let cli = cli::Cli::parse();
29*c2e18aaaSAndroid Build Coastguard Worker     let mut profiler = Profiler::default();
30*c2e18aaaSAndroid Build Coastguard Worker     let device = RealDevice::new(cli.global_options.serial.clone());
31*c2e18aaaSAndroid Build Coastguard Worker     let mut metrics = Metrics::default();
32*c2e18aaaSAndroid Build Coastguard Worker     let result = crate::adevice::adevice(
33*c2e18aaaSAndroid Build Coastguard Worker         &host,
34*c2e18aaaSAndroid Build Coastguard Worker         &device,
35*c2e18aaaSAndroid Build Coastguard Worker         &cli,
36*c2e18aaaSAndroid Build Coastguard Worker         &mut std::io::stdout(),
37*c2e18aaaSAndroid Build Coastguard Worker         &mut metrics,
38*c2e18aaaSAndroid Build Coastguard Worker         log_file(),
39*c2e18aaaSAndroid Build Coastguard Worker         &mut profiler,
40*c2e18aaaSAndroid Build Coastguard Worker     );
41*c2e18aaaSAndroid Build Coastguard Worker 
42*c2e18aaaSAndroid Build Coastguard Worker     // cleanup tasks (metrics, profiling)
43*c2e18aaaSAndroid Build Coastguard Worker     match result {
44*c2e18aaaSAndroid Build Coastguard Worker         Ok(()) => metrics.add_exit_event("", 0),
45*c2e18aaaSAndroid Build Coastguard Worker         Err(ref err) => {
46*c2e18aaaSAndroid Build Coastguard Worker             progress::stop();
47*c2e18aaaSAndroid Build Coastguard Worker             metrics.add_exit_event(&err.to_string(), 1);
48*c2e18aaaSAndroid Build Coastguard Worker             error!("\n{}", err.to_string());
49*c2e18aaaSAndroid Build Coastguard Worker         }
50*c2e18aaaSAndroid Build Coastguard Worker     }
51*c2e18aaaSAndroid Build Coastguard Worker     progress::stop();
52*c2e18aaaSAndroid Build Coastguard Worker     profiler.total = total_time.elapsed();
53*c2e18aaaSAndroid Build Coastguard Worker     metrics.add_profiler_events(&profiler);
54*c2e18aaaSAndroid Build Coastguard Worker     println!(
55*c2e18aaaSAndroid Build Coastguard Worker         "\nFinished in {} secs, [Logfile at $ANDROID_BUILD_TOP/out/adevice.log]",
56*c2e18aaaSAndroid Build Coastguard Worker         profiler.total.as_secs()
57*c2e18aaaSAndroid Build Coastguard Worker     );
58*c2e18aaaSAndroid Build Coastguard Worker     info!("TIMING: {}", profiler.to_string());
59*c2e18aaaSAndroid Build Coastguard Worker 
60*c2e18aaaSAndroid Build Coastguard Worker     result
61*c2e18aaaSAndroid Build Coastguard Worker }
62*c2e18aaaSAndroid Build Coastguard Worker 
63*c2e18aaaSAndroid Build Coastguard Worker /// Return a file open at $ANDROID_BUILD_TOP/out/adevice.log or None
64*c2e18aaaSAndroid Build Coastguard Worker /// Ideally, use the file_rotate crate: https://docs.rs/file-rotate/latest/file_rotate/ as well.
log_file() -> Option<File>65*c2e18aaaSAndroid Build Coastguard Worker fn log_file() -> Option<File> {
66*c2e18aaaSAndroid Build Coastguard Worker     match std::env::var("ANDROID_BUILD_TOP") {
67*c2e18aaaSAndroid Build Coastguard Worker         Ok(top) if !top.is_empty() => {
68*c2e18aaaSAndroid Build Coastguard Worker             let path = PathBuf::from(top).join("out").join("adevice.log");
69*c2e18aaaSAndroid Build Coastguard Worker             File::create(path).ok()
70*c2e18aaaSAndroid Build Coastguard Worker         }
71*c2e18aaaSAndroid Build Coastguard Worker         _ => None,
72*c2e18aaaSAndroid Build Coastguard Worker     }
73*c2e18aaaSAndroid Build Coastguard Worker }
74