Skip to content

Commit 4548aea

Browse files
committed
Move ambient gdb discovery from compiletest to bootstrap
1 parent fbab541 commit 4548aea

File tree

7 files changed

+61
-63
lines changed

7 files changed

+61
-63
lines changed

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,21 +2114,19 @@ 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-
{
2124-
cmd.arg("--adb-path").arg(adb_path);
2125-
cmd.arg("--adb-test-dir").arg(adb_test_dir);
2126-
cmd.arg("--android-cross-path").arg(android_cross_path);
2127-
}
2128-
21292117
if mode == "debuginfo" {
2130-
if let Some(debuggers::Gdb { gdb }) = debuggers::discover_gdb(builder) {
2131-
cmd.arg("--gdb").arg(gdb);
2118+
let android = debuggers::discover_android(builder, target);
2119+
if let Some(debuggers::Android { adb_path, adb_test_dir, android_cross_path }) =
2120+
&android
2121+
{
2122+
cmd.arg("--adb-path").arg(adb_path);
2123+
cmd.arg("--adb-test-dir").arg(adb_test_dir);
2124+
cmd.arg("--android-cross-path").arg(android_cross_path);
2125+
}
2126+
2127+
if let Some(debuggers::Gdb { gdb }) = debuggers::discover_gdb(builder, android.as_ref())
2128+
{
2129+
cmd.arg("--gdb").arg(gdb.as_ref());
21322130
}
21332131

21342132
if let Some(debuggers::Lldb { lldb_exe, lldb_version }) =

src/bootstrap/src/core/debuggers/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()
Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
1+
use std::borrow::Cow;
12
use std::path::Path;
23

34
use crate::core::builder::Builder;
5+
use crate::core::debuggers;
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<&debuggers::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+
let gdb = Cow::Borrowed(gdb);
19+
if gdb.as_os_str().is_empty() {
20+
// Treat an empty string as explicitly disabling gdb discovery.
21+
return None;
22+
} else {
23+
return Some(Gdb { gdb });
24+
}
25+
}
1126

12-
Some(Gdb { gdb })
27+
// Otherwise, fall back to whatever gdb is sitting around in PATH.
28+
// (That's the historical behavior, but maybe we should require opt-in?)
29+
30+
let gdb: Cow<'_, Path> = match android {
31+
Some(debuggers::Android { android_cross_path, .. }) => {
32+
android_cross_path.join("bin/gdb").into()
33+
}
34+
None => Path::new("gdb").into(),
35+
};
36+
37+
// Check whether an ambient gdb exists, by running `gdb --version`.
38+
let output = {
39+
let mut gdb_command = BootstrapCommand::new(gdb.as_ref()).allow_failure();
40+
gdb_command.arg("--version");
41+
gdb_command.run_capture(builder)
42+
};
43+
44+
if output.is_success() { Some(Gdb { gdb }) } else { None }
1345
}

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)