Debugger: Consolidate search code for python-path

Not just to deduplicate/reuse code but also to make make it clear that
different places where lldb is called, the same preparations have to be
done.

Change-Id: I104aca845fd7b42f63443bda2502574f4d28b411
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Alessandro Portale
2021-11-08 13:07:00 +01:00
parent 1b6d374d62
commit d33cd5194a
3 changed files with 22 additions and 26 deletions

View File

@@ -144,8 +144,6 @@ void DebuggerItem::createId()
void DebuggerItem::reinitializeFromFile(const Environment &sysEnv, QString *error) void DebuggerItem::reinitializeFromFile(const Environment &sysEnv, QString *error)
{ {
const bool isAndroid = m_command.path().contains("/ndk/")
|| m_command.path().contains("/ndk-bundle/");
// CDB only understands the single-dash -version, whereas GDB and LLDB are // CDB only understands the single-dash -version, whereas GDB and LLDB are
// happy with both -version and --version. So use the "working" -version // happy with both -version and --version. So use the "working" -version
// except for the experimental LLDB-MI which insists on --version. // except for the experimental LLDB-MI which insists on --version.
@@ -167,10 +165,12 @@ void DebuggerItem::reinitializeFromFile(const Environment &sysEnv, QString *erro
return; return;
} }
Environment env = sysEnv.size() == 0 ? Environment::systemEnvironment() : sysEnv;
// Prevent calling lldb on Windows because the lldb from the llvm package is linked against // Prevent calling lldb on Windows because the lldb from the llvm package is linked against
// python but does not contain a python dll. // python but does not contain a python dll.
const bool isAndroidNdkLldb = DebuggerItem::addAndroidLldbPythonEnv(m_command, env);
if (HostOsInfo::isWindowsHost() && m_command.fileName().startsWith("lldb") if (HostOsInfo::isWindowsHost() && m_command.fileName().startsWith("lldb")
&& !isAndroid) { && !isAndroidNdkLldb) {
QString errorMessage; QString errorMessage;
m_version = winGetDLLVersion(WinDLLFileVersion, m_version = winGetDLLVersion(WinDLLFileVersion,
m_command.absoluteFilePath().path(), m_command.absoluteFilePath().path(),
@@ -180,16 +180,6 @@ void DebuggerItem::reinitializeFromFile(const Environment &sysEnv, QString *erro
return; return;
} }
Environment env = sysEnv.toProcessEnvironment().isEmpty() ? Environment::systemEnvironment()
: sysEnv;
if (isAndroid && m_command.fileName().startsWith("lldb")) {
FilePath pythonPath = m_command.parentDir().parentDir().pathAppended("python3");
if (HostOsInfo::isAnyUnixHost())
pythonPath = pythonPath.pathAppended("bin");
if (pythonPath.exists())
env.prependOrSetPath(pythonPath.toUserOutput());
}
QtcProcess proc; QtcProcess proc;
proc.setEnvironment(env); proc.setEnvironment(env);
proc.setCommand({m_command, {version}}); proc.setCommand({m_command, {version}});
@@ -275,6 +265,22 @@ void DebuggerItem::reinitializeFromFile(const Environment &sysEnv, QString *erro
m_engineType = NoEngineType; m_engineType = NoEngineType;
} }
bool DebuggerItem::addAndroidLldbPythonEnv(const Utils::FilePath &lldbCmd, Utils::Environment &env)
{
if (lldbCmd.baseName().contains("lldb") &&
(lldbCmd.path().contains("/ndk/") || lldbCmd.path().contains("/ndk-bundle/"))) {
const FilePath pythonDir = lldbCmd.parentDir().parentDir().pathAppended("python3");
const FilePath pythonBinDir =
HostOsInfo::isAnyUnixHost() ? pythonDir.pathAppended("bin") : pythonDir;
if (pythonBinDir.exists()) {
env.set("PYTHONHOME", pythonDir.path());
env.prependOrSetPath(pythonBinDir.path());
return true;
}
}
return false;
}
QString DebuggerItem::engineTypeName() const QString DebuggerItem::engineTypeName() const
{ {
switch (m_engineType) { switch (m_engineType) {

View File

@@ -107,6 +107,8 @@ public:
QString detectionSource() const { return m_detectionSource; } QString detectionSource() const { return m_detectionSource; }
void setDetectionSource(const QString &source) { m_detectionSource = source; } void setDetectionSource(const QString &source) { m_detectionSource = source; }
static bool addAndroidLldbPythonEnv(const Utils::FilePath &lldbCmd, Utils::Environment &env);
private: private:
DebuggerItem(const QVariant &id); DebuggerItem(const QVariant &id);
void initMacroExpander(); void initMacroExpander();

View File

@@ -204,18 +204,6 @@ static QString adapterStartFailed()
return LldbEngine::tr("Adapter start failed."); return LldbEngine::tr("Adapter start failed.");
} }
static void addAndroidPythonDir(const FilePath &lldbCmd, Environment &env)
{
if (!lldbCmd.path().contains("/ndk/") && !lldbCmd.path().contains("/ndk-bundle/"))
return;
FilePath androidPythonDir = lldbCmd.parentDir().parentDir().pathAppended("python3");
if (HostOsInfo::isAnyUnixHost())
androidPythonDir = androidPythonDir.pathAppended("bin");
if (androidPythonDir.exists())
env.prependOrSetPath(androidPythonDir.path());
}
void LldbEngine::setupEngine() void LldbEngine::setupEngine()
{ {
QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state()); QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
@@ -225,7 +213,7 @@ void LldbEngine::setupEngine()
showMessage("STARTING LLDB: " + lldbCmd.toUserOutput()); showMessage("STARTING LLDB: " + lldbCmd.toUserOutput());
Environment environment = runParameters().debugger.environment; Environment environment = runParameters().debugger.environment;
environment.appendOrSet("PYTHONUNBUFFERED", "1"); // avoid flushing problem on macOS environment.appendOrSet("PYTHONUNBUFFERED", "1"); // avoid flushing problem on macOS
addAndroidPythonDir(lldbCmd, environment); DebuggerItem::addAndroidLldbPythonEnv(lldbCmd, environment);
m_lldbProc.setEnvironment(environment); m_lldbProc.setEnvironment(environment);
if (runParameters().debugger.workingDirectory.isDir()) if (runParameters().debugger.workingDirectory.isDir())