2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "abstractprocessstep.h"
|
2023-01-13 12:38:22 +01:00
|
|
|
|
2018-11-17 21:19:04 +02:00
|
|
|
#include "processparameters.h"
|
2019-10-07 14:29:03 +03:00
|
|
|
#include "projectexplorer.h"
|
|
|
|
|
#include "projectexplorersettings.h"
|
2023-01-13 12:38:22 +01:00
|
|
|
#include "projectexplorertr.h"
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2020-04-16 13:53:05 +02:00
|
|
|
#include <utils/outputformatter.h>
|
2024-02-27 16:08:45 +01:00
|
|
|
#include <utils/qtcprocess.h>
|
2009-12-09 13:54:46 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2020-03-31 14:34:08 +02:00
|
|
|
#include <QTextDecoder>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-03-28 12:37:19 +02:00
|
|
|
#include <algorithm>
|
2018-11-17 21:19:04 +02:00
|
|
|
#include <memory>
|
2018-03-28 12:37:19 +02:00
|
|
|
|
2023-05-10 19:54:52 +02:00
|
|
|
using namespace Tasking;
|
2019-05-28 18:59:45 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2018-11-17 21:19:04 +02:00
|
|
|
namespace ProjectExplorer {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
|
|
|
|
\class ProjectExplorer::AbstractProcessStep
|
|
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The AbstractProcessStep class is a convenience class that can be
|
|
|
|
|
used as a base class instead of BuildStep.
|
2011-04-14 12:58:14 +02:00
|
|
|
|
|
|
|
|
It should be used as a base class if your buildstep just needs to run a process.
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
\list
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Use processParameters() to configure the process you want to run
|
2011-04-14 12:58:14 +02:00
|
|
|
(you need to do that before calling AbstractProcessStep::init()).
|
2013-02-06 08:50:23 +01:00
|
|
|
\li Inside YourBuildStep::init() call AbstractProcessStep::init().
|
|
|
|
|
\li Inside YourBuildStep::run() call AbstractProcessStep::run(), which automatically starts the process
|
2011-04-14 12:58:14 +02:00
|
|
|
and by default adds the output on stdOut and stdErr to the OutputWindow.
|
2013-02-06 08:50:23 +01:00
|
|
|
\li If you need to process the process output override stdOut() and/or stdErr.
|
2011-04-14 12:58:14 +02:00
|
|
|
\endlist
|
|
|
|
|
|
|
|
|
|
The two functions processStarted() and processFinished() are called after starting/finishing the process.
|
|
|
|
|
By default they add a message to the output window.
|
|
|
|
|
|
|
|
|
|
Use setEnabled() to control whether the BuildStep needs to run. (A disabled BuildStep immediately returns true,
|
|
|
|
|
from the run function.)
|
|
|
|
|
|
|
|
|
|
\sa ProjectExplorer::ProcessParameters
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\fn void ProjectExplorer::AbstractProcessStep::setEnabled(bool b)
|
|
|
|
|
|
2013-09-10 17:16:10 +02:00
|
|
|
Enables or disables a BuildStep.
|
2011-04-14 12:58:14 +02:00
|
|
|
|
2013-10-07 13:34:40 +02:00
|
|
|
Disabled BuildSteps immediately return true from their run function.
|
2013-09-10 17:16:10 +02:00
|
|
|
Should be called from init().
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\fn ProcessParameters *ProjectExplorer::AbstractProcessStep::processParameters()
|
|
|
|
|
|
2013-09-10 17:16:10 +02:00
|
|
|
Obtains a reference to the parameters for the actual process to run.
|
2011-04-14 12:58:14 +02:00
|
|
|
|
2013-09-10 17:16:10 +02:00
|
|
|
Should be used in init().
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
2018-11-17 21:19:04 +02:00
|
|
|
class AbstractProcessStep::Private
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-11-17 22:46:45 +02:00
|
|
|
Private(AbstractProcessStep *q) : q(q) {}
|
|
|
|
|
|
|
|
|
|
AbstractProcessStep *q;
|
2018-11-17 21:19:04 +02:00
|
|
|
ProcessParameters m_param;
|
2022-12-02 13:59:33 +01:00
|
|
|
ProcessParameters *m_displayedParams = &m_param;
|
2020-08-13 12:20:41 +02:00
|
|
|
std::function<CommandLine()> m_commandLineProvider;
|
|
|
|
|
std::function<FilePath()> m_workingDirectoryProvider;
|
|
|
|
|
std::function<void(Environment &)> m_environmentModifier;
|
2018-11-17 21:19:04 +02:00
|
|
|
bool m_ignoreReturnValue = false;
|
2019-09-10 23:39:29 +03:00
|
|
|
bool m_lowPriority = false;
|
2020-04-16 13:53:05 +02:00
|
|
|
OutputFormatter *outputFormatter = nullptr;
|
2018-11-17 21:19:04 +02:00
|
|
|
};
|
|
|
|
|
|
2021-04-23 04:25:27 +02:00
|
|
|
AbstractProcessStep::AbstractProcessStep(BuildStepList *bsl, Id id) :
|
2018-11-17 21:19:04 +02:00
|
|
|
BuildStep(bsl, id),
|
2018-11-17 22:46:45 +02:00
|
|
|
d(new Private(this))
|
2016-12-05 21:14:40 +01:00
|
|
|
{
|
|
|
|
|
}
|
2009-10-27 14:16:28 +01:00
|
|
|
|
2018-11-17 21:19:04 +02:00
|
|
|
AbstractProcessStep::~AbstractProcessStep()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 14:32:40 +02:00
|
|
|
void AbstractProcessStep::emitFaultyConfigurationMessage()
|
|
|
|
|
{
|
2023-01-13 12:38:22 +01:00
|
|
|
emit addOutput(Tr::tr("Configuration is faulty. Check the Issues view for details."),
|
2022-12-02 13:59:33 +01:00
|
|
|
OutputFormat::NormalMessage);
|
2014-06-20 14:32:40 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-28 07:52:22 +02:00
|
|
|
bool AbstractProcessStep::ignoreReturnValue() const
|
2011-09-22 12:59:31 +02:00
|
|
|
{
|
2018-11-17 21:19:04 +02:00
|
|
|
return d->m_ignoreReturnValue;
|
2011-09-22 12:59:31 +02:00
|
|
|
}
|
|
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
2013-09-10 17:16:10 +02:00
|
|
|
If \a ignoreReturnValue is set to true, then the abstractprocess step will
|
2011-04-14 12:58:14 +02:00
|
|
|
return success even if the return value indicates otherwise.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-10-15 19:06:51 +02:00
|
|
|
void AbstractProcessStep::setIgnoreReturnValue(bool b)
|
2009-06-22 16:11:45 +02:00
|
|
|
{
|
2018-11-17 21:19:04 +02:00
|
|
|
d->m_ignoreReturnValue = b;
|
2009-06-22 16:11:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-13 12:20:41 +02:00
|
|
|
void AbstractProcessStep::setEnvironmentModifier(const std::function<void (Environment &)> &modifier)
|
|
|
|
|
{
|
|
|
|
|
d->m_environmentModifier = modifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AbstractProcessStep::setUseEnglishOutput()
|
|
|
|
|
{
|
2021-05-20 11:57:21 +02:00
|
|
|
d->m_environmentModifier = [](Environment &env) { env.setupEnglishOutput(); };
|
2020-08-13 12:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AbstractProcessStep::setCommandLineProvider(const std::function<CommandLine()> &provider)
|
|
|
|
|
{
|
|
|
|
|
d->m_commandLineProvider = provider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AbstractProcessStep::setWorkingDirectoryProvider(const std::function<FilePath()> &provider)
|
|
|
|
|
{
|
|
|
|
|
d->m_workingDirectoryProvider = provider;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-14 12:58:14 +02:00
|
|
|
/*!
|
2013-09-10 17:16:10 +02:00
|
|
|
Reimplemented from BuildStep::init(). You need to call this from
|
|
|
|
|
YourBuildStep::init().
|
2011-04-14 12:58:14 +02:00
|
|
|
*/
|
|
|
|
|
|
2019-01-10 15:31:44 +01:00
|
|
|
bool AbstractProcessStep::init()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2022-10-18 14:17:35 +02:00
|
|
|
if (!setupProcessParameters(processParameters()))
|
|
|
|
|
return false;
|
2020-09-14 17:17:55 +02:00
|
|
|
|
|
|
|
|
return true;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-16 13:53:05 +02:00
|
|
|
void AbstractProcessStep::setupOutputFormatter(OutputFormatter *formatter)
|
|
|
|
|
{
|
|
|
|
|
formatter->setDemoteErrorsToWarnings(d->m_ignoreReturnValue);
|
|
|
|
|
d->outputFormatter = formatter;
|
|
|
|
|
BuildStep::setupOutputFormatter(formatter);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 09:23:55 +02:00
|
|
|
GroupItem AbstractProcessStep::defaultProcessTask()
|
|
|
|
|
{
|
|
|
|
|
const auto onSetup = [this](Process &process) {
|
|
|
|
|
return setupProcess(process) ? SetupResult::Continue : SetupResult::StopWithError;
|
|
|
|
|
};
|
2023-11-02 23:19:49 +01:00
|
|
|
const auto onDone = [this](const Process &process) { handleProcessDone(process); };
|
2023-11-02 16:14:50 +01:00
|
|
|
return ProcessTask(onSetup, onDone);
|
2023-07-12 09:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-11 22:19:23 +02:00
|
|
|
bool AbstractProcessStep::setupProcess(Process &process)
|
2022-12-06 06:49:30 +01:00
|
|
|
{
|
2023-07-11 22:19:23 +02:00
|
|
|
const FilePath workingDir = d->m_param.effectiveWorkingDirectory();
|
|
|
|
|
if (!workingDir.exists() && !workingDir.createDir()) {
|
|
|
|
|
emit addOutput(Tr::tr("Could not create directory \"%1\"").arg(workingDir.toUserOutput()),
|
|
|
|
|
OutputFormat::ErrorMessage);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-07-11 22:25:53 +02:00
|
|
|
if (!d->m_param.effectiveCommand().isExecutableFile()) {
|
|
|
|
|
emit addOutput(Tr::tr("The program \"%1\" does not exist or is not executable.")
|
|
|
|
|
.arg(d->m_displayedParams->effectiveCommand().toUserOutput()),
|
|
|
|
|
OutputFormat::ErrorMessage);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 21:20:24 +02:00
|
|
|
process.setUseCtrlCStub(HostOsInfo::isWindowsHost());
|
2023-07-11 22:19:23 +02:00
|
|
|
process.setWorkingDirectory(workingDir);
|
2020-04-21 11:36:24 +02:00
|
|
|
// Enforce PWD in the environment because some build tools use that.
|
|
|
|
|
// PWD can be different from getcwd in case of symbolic links (getcwd resolves symlinks).
|
|
|
|
|
// For example Clang uses PWD for paths in debug info, see QTCREATORBUG-23788
|
|
|
|
|
Environment envWithPwd = d->m_param.environment();
|
2023-07-11 22:19:23 +02:00
|
|
|
envWithPwd.set("PWD", workingDir.path());
|
2024-01-23 17:39:14 +01:00
|
|
|
process.setProcessMode(d->m_param.processMode());
|
2023-07-11 21:20:24 +02:00
|
|
|
process.setEnvironment(envWithPwd);
|
|
|
|
|
process.setCommand({d->m_param.effectiveCommand(), d->m_param.effectiveArguments(),
|
2022-12-06 06:49:30 +01:00
|
|
|
CommandLine::Raw});
|
2024-02-02 11:16:21 +01:00
|
|
|
if (d->m_lowPriority && projectExplorerSettings().lowBuildPriority)
|
2023-07-11 21:20:24 +02:00
|
|
|
process.setLowPriority();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2024-02-23 17:01:58 +01:00
|
|
|
process.setStdOutCodec(buildEnvironment().hasKey("VSLANG")
|
|
|
|
|
? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale());
|
|
|
|
|
process.setStdErrCodec(QTextCodec::codecForLocale());
|
|
|
|
|
|
|
|
|
|
process.setStdOutLineCallback([this](const QString &s){
|
|
|
|
|
emit addOutput(s, OutputFormat::Stdout, DontAppendNewline);
|
2022-12-02 12:40:12 +01:00
|
|
|
});
|
2024-02-23 17:01:58 +01:00
|
|
|
|
|
|
|
|
process.setStdErrLineCallback([this](const QString &s){
|
|
|
|
|
emit addOutput(s, OutputFormat::Stderr, DontAppendNewline);
|
2022-12-02 12:40:12 +01:00
|
|
|
});
|
2024-02-23 17:01:58 +01:00
|
|
|
|
2023-07-11 21:20:24 +02:00
|
|
|
connect(&process, &Process::started, this, [this] {
|
2023-07-11 20:16:35 +02:00
|
|
|
ProcessParameters *params = d->m_displayedParams;
|
2023-01-13 12:38:22 +01:00
|
|
|
emit addOutput(Tr::tr("Starting: \"%1\" %2")
|
2022-12-02 13:59:33 +01:00
|
|
|
.arg(params->effectiveCommand().toUserOutput(), params->prettyArguments()),
|
|
|
|
|
OutputFormat::NormalMessage);
|
|
|
|
|
});
|
2023-07-11 22:19:23 +02:00
|
|
|
return true;
|
2022-12-06 06:49:30 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2023-07-11 19:35:16 +02:00
|
|
|
void AbstractProcessStep::handleProcessDone(const Process &process)
|
|
|
|
|
{
|
2023-07-11 20:16:35 +02:00
|
|
|
const QString command = d->m_displayedParams->effectiveCommand().toUserOutput();
|
2023-07-11 19:35:16 +02:00
|
|
|
if (process.result() == ProcessResult::FinishedWithSuccess) {
|
|
|
|
|
emit addOutput(Tr::tr("The process \"%1\" exited normally.").arg(command),
|
|
|
|
|
OutputFormat::NormalMessage);
|
|
|
|
|
} else if (process.result() == ProcessResult::FinishedWithError) {
|
|
|
|
|
emit addOutput(Tr::tr("The process \"%1\" exited with code %2.")
|
|
|
|
|
.arg(command, QString::number(process.exitCode())),
|
|
|
|
|
OutputFormat::ErrorMessage);
|
|
|
|
|
} else if (process.result() == ProcessResult::StartFailed) {
|
|
|
|
|
emit addOutput(Tr::tr("Could not start process \"%1\" %2.")
|
2023-07-11 20:16:35 +02:00
|
|
|
.arg(command, d->m_displayedParams->prettyArguments()),
|
2023-07-11 19:35:16 +02:00
|
|
|
OutputFormat::ErrorMessage);
|
|
|
|
|
const QString errorString = process.errorString();
|
|
|
|
|
if (!errorString.isEmpty())
|
|
|
|
|
emit addOutput(errorString, OutputFormat::ErrorMessage);
|
|
|
|
|
} else {
|
|
|
|
|
emit addOutput(Tr::tr("The process \"%1\" crashed.").arg(command),
|
|
|
|
|
OutputFormat::ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 21:29:12 +03:00
|
|
|
void AbstractProcessStep::setLowPriority()
|
2019-09-10 23:39:29 +03:00
|
|
|
{
|
2019-10-09 21:29:12 +03:00
|
|
|
d->m_lowPriority = true;
|
2019-09-10 23:39:29 +03:00
|
|
|
}
|
|
|
|
|
|
2018-11-17 21:19:04 +02:00
|
|
|
ProcessParameters *AbstractProcessStep::processParameters()
|
|
|
|
|
{
|
|
|
|
|
return &d->m_param;
|
2013-02-12 12:56:02 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-10-18 14:17:35 +02:00
|
|
|
bool AbstractProcessStep::setupProcessParameters(ProcessParameters *params) const
|
2020-08-13 12:20:41 +02:00
|
|
|
{
|
|
|
|
|
params->setMacroExpander(macroExpander());
|
|
|
|
|
|
2021-04-23 04:25:27 +02:00
|
|
|
Environment env = buildEnvironment();
|
2020-08-13 12:20:41 +02:00
|
|
|
if (d->m_environmentModifier)
|
|
|
|
|
d->m_environmentModifier(env);
|
|
|
|
|
params->setEnvironment(env);
|
|
|
|
|
|
2022-10-18 14:17:35 +02:00
|
|
|
if (d->m_commandLineProvider)
|
|
|
|
|
params->setCommandLine(d->m_commandLineProvider());
|
|
|
|
|
|
|
|
|
|
FilePath workingDirectory;
|
2020-08-13 12:20:41 +02:00
|
|
|
if (d->m_workingDirectoryProvider)
|
2022-10-18 14:17:35 +02:00
|
|
|
workingDirectory = d->m_workingDirectoryProvider();
|
2020-08-13 12:20:41 +02:00
|
|
|
else
|
2022-10-18 14:17:35 +02:00
|
|
|
workingDirectory = buildDirectory();
|
2020-08-13 12:20:41 +02:00
|
|
|
|
2022-10-18 14:17:35 +02:00
|
|
|
const FilePath executable = params->effectiveCommand();
|
|
|
|
|
|
2022-11-30 16:39:21 +01:00
|
|
|
// E.g. the QMakeStep doesn't have set up anything when this is called
|
|
|
|
|
// as it doesn't set a command line provider, so executable might be empty.
|
|
|
|
|
const bool looksGood = executable.isEmpty() || executable.ensureReachable(workingDirectory);
|
|
|
|
|
QTC_ASSERT(looksGood, return false);
|
|
|
|
|
|
2023-03-29 13:45:42 +02:00
|
|
|
params->setWorkingDirectory(executable.withNewPath(workingDirectory.path()));
|
2022-10-18 14:17:35 +02:00
|
|
|
|
|
|
|
|
return true;
|
2020-08-13 12:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-02 13:59:33 +01:00
|
|
|
void AbstractProcessStep::setDisplayedParameters(ProcessParameters *params)
|
|
|
|
|
{
|
|
|
|
|
d->m_displayedParams = params;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 16:30:33 +02:00
|
|
|
GroupItem AbstractProcessStep::runRecipe()
|
2013-02-15 16:18:49 +01:00
|
|
|
{
|
2023-11-04 12:44:19 +01:00
|
|
|
return Group { ignoreReturnValue() ? finishAllAndSuccess : stopOnError, defaultProcessTask() };
|
2013-02-15 16:18:49 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-17 21:19:04 +02:00
|
|
|
} // namespace ProjectExplorer
|