2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-11-12 17:23:55 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-11-12 17:23:55 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-11-12 17:23:55 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-11-12 17:23:55 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 17:14:20 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-11-12 17:23:55 +01:00
|
|
|
|
|
|
|
|
#include "processparameters.h"
|
|
|
|
|
|
2015-01-10 23:40:32 +02:00
|
|
|
#include <utils/fileutils.h>
|
2014-10-15 14:45:31 +02:00
|
|
|
#include <utils/macroexpander.h>
|
2010-11-12 17:23:55 +01:00
|
|
|
#include <utils/qtcprocess.h>
|
2019-10-18 13:02:35 +02:00
|
|
|
#include <utils/theme/theme.h>
|
2010-11-12 17:23:55 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
2010-11-12 17:23:55 +01:00
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
|
|
|
|
\class ProjectExplorer::ProcessParameters
|
|
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The ProcessParameters class aggregates all parameters needed to start
|
|
|
|
|
a process.
|
2011-04-14 12:58:14 +02:00
|
|
|
|
|
|
|
|
It offers a set of functions which expand macros and environment variables
|
|
|
|
|
inside the raw parameters to obtain final values for starting a process
|
|
|
|
|
or for display purposes.
|
|
|
|
|
|
|
|
|
|
\sa ProjectExplorer::AbstractProcessStep
|
|
|
|
|
*/
|
|
|
|
|
|
2019-05-15 13:59:43 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
|
|
namespace ProjectExplorer {
|
2010-11-12 17:23:55 +01:00
|
|
|
|
2020-10-05 14:43:23 +02:00
|
|
|
ProcessParameters::ProcessParameters() = default;
|
2010-11-12 17:23:55 +01:00
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
2019-06-21 08:31:37 +02:00
|
|
|
Sets the command to run.
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
2019-06-21 08:31:37 +02:00
|
|
|
void ProcessParameters::setCommandLine(const CommandLine &cmdLine)
|
2010-11-12 17:23:55 +01:00
|
|
|
{
|
2019-06-21 08:31:37 +02:00
|
|
|
m_command = cmdLine;
|
2010-11-12 17:23:55 +01:00
|
|
|
m_effectiveCommand.clear();
|
|
|
|
|
m_effectiveArguments.clear();
|
2020-08-25 02:33:43 +02:00
|
|
|
|
|
|
|
|
effectiveCommand();
|
|
|
|
|
effectiveArguments();
|
2010-11-12 17:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
2013-09-10 17:16:10 +02:00
|
|
|
Sets the \a workingDirectory for the process for a build configuration.
|
|
|
|
|
|
|
|
|
|
Should be called from init().
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
void ProcessParameters::setWorkingDirectory(const FilePath &workingDirectory)
|
2010-11-12 17:23:55 +01:00
|
|
|
{
|
|
|
|
|
m_workingDirectory = workingDirectory;
|
|
|
|
|
m_effectiveWorkingDirectory.clear();
|
2020-08-25 02:33:43 +02:00
|
|
|
|
|
|
|
|
effectiveWorkingDirectory();
|
2010-11-12 17:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
|
|
|
|
\fn void ProjectExplorer::ProcessParameters::setEnvironment(const Utils::Environment &env)
|
2013-09-10 17:16:10 +02:00
|
|
|
Sets the environment \a env for running the command.
|
2011-04-14 12:58:14 +02:00
|
|
|
|
2013-09-10 17:16:10 +02:00
|
|
|
Should be called from init().
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2014-10-15 14:45:31 +02:00
|
|
|
\fn void ProjectExplorer::ProcessParameters::setMacroExpander(Utils::MacroExpander *mx)
|
2013-09-10 17:16:10 +02:00
|
|
|
Sets the macro expander \a mx to use on the command, arguments, and working
|
|
|
|
|
dir.
|
2011-04-14 12:58:14 +02:00
|
|
|
|
2013-09-10 17:16:10 +02:00
|
|
|
\note The caller retains ownership of the object.
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2013-09-10 17:16:10 +02:00
|
|
|
Gets the fully expanded working directory.
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath ProcessParameters::effectiveWorkingDirectory() const
|
2010-11-12 17:23:55 +01:00
|
|
|
{
|
|
|
|
|
if (m_effectiveWorkingDirectory.isEmpty()) {
|
2021-05-31 16:24:51 +02:00
|
|
|
m_effectiveWorkingDirectory = m_workingDirectory;
|
|
|
|
|
QString path = m_workingDirectory.path();
|
2010-11-12 17:23:55 +01:00
|
|
|
if (m_macroExpander)
|
2021-05-31 16:24:51 +02:00
|
|
|
path = m_macroExpander->expand(path);
|
|
|
|
|
m_effectiveWorkingDirectory.setPath(QDir::cleanPath(m_environment.expandVariables(path)));
|
2010-11-12 17:23:55 +01:00
|
|
|
}
|
|
|
|
|
return m_effectiveWorkingDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
2013-09-10 17:16:10 +02:00
|
|
|
Gets the fully expanded command name to run.
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath ProcessParameters::effectiveCommand() const
|
2010-11-12 17:23:55 +01:00
|
|
|
{
|
|
|
|
|
if (m_effectiveCommand.isEmpty()) {
|
2019-06-21 08:31:37 +02:00
|
|
|
FilePath cmd = m_command.executable();
|
2010-11-12 17:23:55 +01:00
|
|
|
if (m_macroExpander)
|
2014-10-15 14:45:31 +02:00
|
|
|
cmd = m_macroExpander->expand(cmd);
|
2021-04-26 17:12:49 +02:00
|
|
|
if (cmd.needsDevice()) {
|
|
|
|
|
// Assume this is already good. FIXME: It is possibly not, so better fix searchInPath.
|
|
|
|
|
m_effectiveCommand = cmd;
|
|
|
|
|
} else {
|
|
|
|
|
m_effectiveCommand = m_environment.searchInPath(cmd.toString(),
|
|
|
|
|
{effectiveWorkingDirectory()});
|
|
|
|
|
}
|
2010-11-12 17:23:55 +01:00
|
|
|
m_commandMissing = m_effectiveCommand.isEmpty();
|
|
|
|
|
if (m_commandMissing)
|
|
|
|
|
m_effectiveCommand = cmd;
|
|
|
|
|
}
|
|
|
|
|
return m_effectiveCommand;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
2013-09-10 17:16:10 +02:00
|
|
|
Returns \c true if effectiveCommand() would return only a fallback.
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
2010-11-12 17:23:55 +01:00
|
|
|
bool ProcessParameters::commandMissing() const
|
|
|
|
|
{
|
|
|
|
|
effectiveCommand();
|
|
|
|
|
return m_commandMissing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ProcessParameters::effectiveArguments() const
|
|
|
|
|
{
|
|
|
|
|
if (m_effectiveArguments.isEmpty()) {
|
2019-06-21 08:31:37 +02:00
|
|
|
m_effectiveArguments = m_command.arguments();
|
2010-11-12 17:23:55 +01:00
|
|
|
if (m_macroExpander)
|
2014-10-15 14:45:31 +02:00
|
|
|
m_effectiveArguments = m_macroExpander->expand(m_effectiveArguments);
|
2010-11-12 17:23:55 +01:00
|
|
|
}
|
|
|
|
|
return m_effectiveArguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ProcessParameters::prettyCommand() const
|
|
|
|
|
{
|
2019-06-21 08:31:37 +02:00
|
|
|
QString cmd = m_command.executable().toString();
|
2010-11-12 17:23:55 +01:00
|
|
|
if (m_macroExpander)
|
2014-10-15 14:45:31 +02:00
|
|
|
cmd = m_macroExpander->expand(cmd);
|
2020-10-05 14:43:23 +02:00
|
|
|
return FilePath::fromString(cmd).fileName();
|
2010-11-12 17:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ProcessParameters::prettyArguments() const
|
|
|
|
|
{
|
|
|
|
|
QString margs = effectiveArguments();
|
2019-05-15 13:59:43 +02:00
|
|
|
QString workDir = effectiveWorkingDirectory().toString();
|
2021-05-06 13:07:36 +02:00
|
|
|
ProcessArgs::SplitError err;
|
|
|
|
|
ProcessArgs args =
|
|
|
|
|
ProcessArgs::prepareArgs(margs, &err, HostOsInfo::hostOs(), &m_environment, &workDir);
|
|
|
|
|
if (err != ProcessArgs::SplitOk)
|
2010-11-12 17:23:55 +01:00
|
|
|
return margs; // Sorry, too complex - just fall back.
|
2014-02-05 10:43:21 +01:00
|
|
|
return args.toString();
|
2010-11-12 17:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
2019-10-18 13:02:35 +02:00
|
|
|
static QString invalidCommandMessage(const QString &displayName)
|
|
|
|
|
{
|
|
|
|
|
return QString("<b>%1:</b> <font color='%3'>%2</font>")
|
|
|
|
|
.arg(displayName,
|
|
|
|
|
QtcProcess::tr("Invalid command"),
|
|
|
|
|
creatorTheme()->color(Theme::TextColorError).name());
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 17:23:55 +01:00
|
|
|
QString ProcessParameters::summary(const QString &displayName) const
|
|
|
|
|
{
|
2019-10-18 13:02:35 +02:00
|
|
|
if (m_commandMissing)
|
|
|
|
|
return invalidCommandMessage(displayName);
|
|
|
|
|
|
2010-11-12 17:23:55 +01:00
|
|
|
return QString::fromLatin1("<b>%1:</b> %2 %3")
|
|
|
|
|
.arg(displayName,
|
2021-05-06 13:07:36 +02:00
|
|
|
ProcessArgs::quoteArg(prettyCommand()),
|
2010-11-12 17:23:55 +01:00
|
|
|
prettyArguments());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ProcessParameters::summaryInWorkdir(const QString &displayName) const
|
|
|
|
|
{
|
2019-10-18 13:02:35 +02:00
|
|
|
if (m_commandMissing)
|
|
|
|
|
return invalidCommandMessage(displayName);
|
|
|
|
|
|
2010-11-12 17:23:55 +01:00
|
|
|
return QString::fromLatin1("<b>%1:</b> %2 %3 in %4")
|
|
|
|
|
.arg(displayName,
|
2021-05-06 13:07:36 +02:00
|
|
|
ProcessArgs::quoteArg(prettyCommand()),
|
2010-11-12 17:23:55 +01:00
|
|
|
prettyArguments(),
|
2019-05-15 13:59:43 +02:00
|
|
|
QDir::toNativeSeparators(effectiveWorkingDirectory().toString()));
|
2010-11-12 17:23:55 +01:00
|
|
|
}
|
2012-11-28 15:29:55 +01:00
|
|
|
|
2019-05-15 13:59:43 +02:00
|
|
|
} // ProcessExplorer
|