Skip to content

Commit fa9a8f4

Browse files
authored
Rollup merge of #149710 - Zalathar:ambient-gdb, r=jieyouxu
Move ambient gdb discovery from compiletest to bootstrap - Follow-up to #148099 --- This code takes the compiletest code for discovering an “ambient” `gdb` in the user's path, and moves it to bootstrap. One of the eventual goals is to allow compiletest to assume that if it has been asked to run the debuginfo-gdb suite, then it *must* have been passed an explicit `--gdb`, though we aren't quite there yet. r? jieyouxu
2 parents a76db55 + 8f35bd1 commit fa9a8f4

File tree

9 files changed

+54
-61
lines changed

9 files changed

+54
-61
lines changed

src/bootstrap/src/core/debuggers/android.rs renamed to src/bootstrap/src/core/android.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ pub(crate) struct Android {
1010
}
1111

1212
pub(crate) fn discover_android(builder: &Builder<'_>, target: TargetSelection) -> Option<Android> {
13+
if !target.contains("android") {
14+
return None;
15+
}
16+
1317
let adb_path = "adb";
1418
// See <https://github.com/rust-lang/rust/pull/102755>.
1519
let adb_test_dir = "/data/local/tmp/work";
1620

17-
let android_cross_path = if target.contains("android") && !builder.config.dry_run() {
21+
let android_cross_path = if !builder.config.dry_run() {
1822
builder.cc(target).parent().unwrap().parent().unwrap().to_owned()
1923
} else {
2024
PathBuf::new()

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::core::builder::{
2929
};
3030
use crate::core::config::TargetSelection;
3131
use crate::core::config::flags::{Subcommand, get_completion, top_level_help};
32-
use crate::core::debuggers;
32+
use crate::core::{android, debuggers};
3333
use crate::utils::build_stamp::{self, BuildStamp};
3434
use crate::utils::exec::{BootstrapCommand, command};
3535
use crate::utils::helpers::{
@@ -2114,21 +2114,18 @@ Please disable assertions with `rust.debug-assertions = false`.
21142114
builder.config.python.as_ref().expect("python is required for running rustdoc tests"),
21152115
);
21162116

2117-
// FIXME(#148099): Currently we set these Android-related flags in all
2118-
// modes, even though they should only be needed in "debuginfo" mode,
2119-
// because the GDB-discovery code in compiletest currently assumes that
2120-
// `--android-cross-path` is always set for Android targets.
2121-
if let Some(debuggers::Android { adb_path, adb_test_dir, android_cross_path }) =
2122-
debuggers::discover_android(builder, target)
2123-
{
2117+
// Discover and set some flags related to running tests on Android targets.
2118+
let android = android::discover_android(builder, target);
2119+
if let Some(android::Android { adb_path, adb_test_dir, android_cross_path }) = &android {
21242120
cmd.arg("--adb-path").arg(adb_path);
21252121
cmd.arg("--adb-test-dir").arg(adb_test_dir);
21262122
cmd.arg("--android-cross-path").arg(android_cross_path);
21272123
}
21282124

21292125
if mode == "debuginfo" {
2130-
if let Some(debuggers::Gdb { gdb }) = debuggers::discover_gdb(builder) {
2131-
cmd.arg("--gdb").arg(gdb);
2126+
if let Some(debuggers::Gdb { gdb }) = debuggers::discover_gdb(builder, android.as_ref())
2127+
{
2128+
cmd.arg("--gdb").arg(gdb.as_ref());
21322129
}
21332130

21342131
if let Some(debuggers::Lldb { lldb_exe, lldb_version }) =
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
1+
use std::borrow::Cow;
12
use std::path::Path;
23

4+
use crate::core::android;
35
use crate::core::builder::Builder;
6+
use crate::utils::exec::BootstrapCommand;
47

58
pub(crate) struct Gdb<'a> {
6-
pub(crate) gdb: &'a Path,
9+
pub(crate) gdb: Cow<'a, Path>,
710
}
811

9-
pub(crate) fn discover_gdb<'a>(builder: &'a Builder<'_>) -> Option<Gdb<'a>> {
10-
let gdb = builder.config.gdb.as_deref()?;
12+
pub(crate) fn discover_gdb<'a>(
13+
builder: &'a Builder<'_>,
14+
android: Option<&android::Android>,
15+
) -> Option<Gdb<'a>> {
16+
// If there's an explicitly-configured gdb, use that.
17+
if let Some(gdb) = builder.config.gdb.as_deref() {
18+
// FIXME(Zalathar): Consider returning None if gdb is an empty string,
19+
// as a way to explicitly disable ambient gdb discovery.
20+
let gdb = Cow::Borrowed(gdb);
21+
return Some(Gdb { gdb });
22+
}
1123

12-
Some(Gdb { gdb })
24+
// Otherwise, fall back to whatever gdb is sitting around in PATH.
25+
// (That's the historical behavior, but maybe we should require opt-in?)
26+
27+
let gdb: Cow<'_, Path> = match android {
28+
Some(android::Android { android_cross_path, .. }) => {
29+
android_cross_path.join("bin/gdb").into()
30+
}
31+
None => Path::new("gdb").into(),
32+
};
33+
34+
// Check whether an ambient gdb exists, by running `gdb --version`.
35+
let output = {
36+
let mut gdb_command = BootstrapCommand::new(gdb.as_ref()).allow_failure();
37+
gdb_command.arg("--version");
38+
gdb_command.run_capture(builder)
39+
};
40+
41+
if output.is_success() { Some(Gdb { gdb }) } else { None }
1342
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
//! Code for discovering debuggers and debugger-related configuration, so that
22
//! it can be passed to compiletest when running debuginfo tests.
33
4-
pub(crate) use self::android::{Android, discover_android};
54
pub(crate) use self::gdb::{Gdb, discover_gdb};
65
pub(crate) use self::lldb::{Lldb, discover_lldb};
76

8-
mod android;
97
mod gdb;
108
mod lldb;

src/bootstrap/src/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub(crate) mod android;
12
pub(crate) mod build_steps;
23
pub(crate) mod builder;
34
pub(crate) mod config;

src/tools/compiletest/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ pub struct Config {
566566
///
567567
/// FIXME: take a look at this; this is piggy-backing off of gdb code paths but only for
568568
/// `arm-linux-androideabi` target.
569-
pub android_cross_path: Utf8PathBuf,
569+
pub android_cross_path: Option<Utf8PathBuf>,
570570

571571
/// Extra parameter to run adb on `arm-linux-androideabi`.
572572
///

src/tools/compiletest/src/debuggers.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ pub(crate) fn configure_lldb(config: &Config) -> Option<Arc<Config>> {
5454
Some(Arc::new(Config { debugger: Some(Debugger::Lldb), ..config.clone() }))
5555
}
5656

57-
/// Returns `true` if the given target is an Android target for the
58-
/// purposes of GDB testing.
59-
pub(crate) fn is_android_gdb_target(target: &str) -> bool {
60-
matches!(
61-
&target[..],
62-
"arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android"
63-
)
64-
}
65-
6657
/// Returns `true` if the given target is a MSVC target for the purposes of CDB testing.
6758
fn is_pc_windows_msvc_target(target: &str) -> bool {
6859
target.ends_with("-pc-windows-msvc")
@@ -129,35 +120,6 @@ pub(crate) fn extract_cdb_version(full_version_line: &str) -> Option<[u16; 4]> {
129120
Some([major, minor, patch, build])
130121
}
131122

132-
pub(crate) fn discover_gdb(
133-
gdb: Option<String>,
134-
target: &str,
135-
android_cross_path: &Utf8Path,
136-
) -> Option<Utf8PathBuf> {
137-
#[cfg(not(windows))]
138-
const GDB_FALLBACK: &str = "gdb";
139-
#[cfg(windows)]
140-
const GDB_FALLBACK: &str = "gdb.exe";
141-
142-
let fallback_gdb = || {
143-
if is_android_gdb_target(target) {
144-
let mut gdb_path = android_cross_path.to_string();
145-
gdb_path.push_str("/bin/gdb");
146-
gdb_path
147-
} else {
148-
GDB_FALLBACK.to_owned()
149-
}
150-
};
151-
152-
let gdb = match gdb {
153-
None => fallback_gdb(),
154-
Some(ref s) if s.is_empty() => fallback_gdb(), // may be empty if configure found no gdb
155-
Some(ref s) => s.to_owned(),
156-
};
157-
158-
Some(Utf8PathBuf::from(gdb))
159-
}
160-
161123
pub(crate) fn query_gdb_version(gdb: &Utf8Path) -> Option<u32> {
162124
let mut version_line = None;
163125
if let Ok(output) = Command::new(&gdb).arg("--version").output() {

src/tools/compiletest/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ fn parse_config(args: Vec<String>) -> Config {
256256
}
257257

258258
let target = opt_str2(matches.opt_str("target"));
259-
let android_cross_path = opt_path(matches, "android-cross-path");
259+
let android_cross_path = matches.opt_str("android-cross-path").map(Utf8PathBuf::from);
260260
// FIXME: `cdb_version` is *derived* from cdb, but it's *not* technically a config!
261261
let cdb = debuggers::discover_cdb(matches.opt_str("cdb"), &target);
262262
let cdb_version = cdb.as_deref().and_then(debuggers::query_cdb_version);
263263
// FIXME: `gdb_version` is *derived* from gdb, but it's *not* technically a config!
264-
let gdb = debuggers::discover_gdb(matches.opt_str("gdb"), &target, &android_cross_path);
264+
let gdb = matches.opt_str("gdb").map(Utf8PathBuf::from);
265265
let gdb_version = gdb.as_deref().and_then(debuggers::query_gdb_version);
266266
// FIXME: `lldb_version` is *derived* from lldb, but it's *not* technically a config!
267267
let lldb = matches.opt_str("lldb").map(Utf8PathBuf::from);

src/tools/compiletest/src/runtest/debuginfo.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tracing::debug;
88

99
use super::debugger::DebuggerCommands;
1010
use super::{Debugger, Emit, ProcRes, TestCx, Truncated, WillExecute};
11-
use crate::debuggers::{extract_gdb_version, is_android_gdb_target};
11+
use crate::debuggers::extract_gdb_version;
1212

1313
impl TestCx<'_> {
1414
pub(super) fn run_debuginfo_test(&self) {
@@ -122,13 +122,15 @@ impl TestCx<'_> {
122122
let exe_file = self.make_exe_name();
123123

124124
let debugger_run_result;
125-
if is_android_gdb_target(&self.config.target) {
125+
// If bootstrap gave us an `--android-cross-path`, assume the target
126+
// needs Android-specific handling.
127+
if let Some(android_cross_path) = self.config.android_cross_path.as_deref() {
126128
cmds = cmds.replace("run", "continue");
127129

128130
// write debugger script
129131
let mut script_str = String::with_capacity(2048);
130132
script_str.push_str(&format!("set charset {}\n", Self::charset()));
131-
script_str.push_str(&format!("set sysroot {}\n", &self.config.android_cross_path));
133+
script_str.push_str(&format!("set sysroot {android_cross_path}\n"));
132134
script_str.push_str(&format!("file {}\n", exe_file));
133135
script_str.push_str("target remote :5039\n");
134136
script_str.push_str(&format!(

0 commit comments

Comments
 (0)