2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-10-14 18:05:43 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-10-14 18:05:43 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-10-14 18:05:43 +02: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:58:39 +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-10-14 18:05:43 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +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-10-14 18:05:43 +02:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2010-10-14 18:05:43 +02:00
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
2010-10-14 18:05:43 +02:00
|
|
|
#include "environment.h"
|
2021-05-11 14:34:56 +02:00
|
|
|
#include "commandline.h"
|
2022-02-18 00:56:14 +01:00
|
|
|
#include "processenums.h"
|
2022-02-18 02:41:50 +01:00
|
|
|
#include "processinterface.h"
|
2022-02-14 11:14:08 +01:00
|
|
|
#include "qtcassert.h"
|
2010-10-14 18:05:43 +02:00
|
|
|
|
2013-03-18 14:47:33 +01:00
|
|
|
#include <QProcess>
|
2021-05-05 18:21:22 +02:00
|
|
|
|
|
|
|
|
QT_FORWARD_DECLARE_CLASS(QDebug)
|
2022-01-25 13:55:54 +01:00
|
|
|
QT_FORWARD_DECLARE_CLASS(QTextCodec)
|
2013-03-18 14:47:33 +01:00
|
|
|
|
2021-06-14 15:09:11 +02:00
|
|
|
class tst_QtcProcess;
|
|
|
|
|
|
2010-10-14 18:05:43 +02:00
|
|
|
namespace Utils {
|
2021-05-05 18:21:22 +02:00
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
namespace Internal { class QtcProcessPrivate; }
|
|
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
class DeviceProcessHooks;
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT QtcProcess : public ProcessInterface
|
2021-05-05 18:21:22 +02:00
|
|
|
{
|
2021-05-12 14:25:50 +02:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
2021-05-05 18:21:22 +02:00
|
|
|
public:
|
2022-02-10 19:25:03 +01:00
|
|
|
QtcProcess(QObject *parent = nullptr);
|
|
|
|
|
~QtcProcess();
|
|
|
|
|
|
2022-02-17 23:25:54 +01:00
|
|
|
// ProcessInterface related
|
|
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
void start() override;
|
2022-03-04 08:30:19 +01:00
|
|
|
void interrupt() override;
|
2022-02-18 01:53:33 +01:00
|
|
|
void terminate() override;
|
|
|
|
|
void kill() override;
|
|
|
|
|
void close() final;
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
QByteArray readAllStandardOutput() override;
|
|
|
|
|
QByteArray readAllStandardError() override;
|
|
|
|
|
qint64 write(const QByteArray &input) override;
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
qint64 processId() const override;
|
|
|
|
|
QProcess::ProcessState state() const override;
|
|
|
|
|
int exitCode() const override;
|
|
|
|
|
QProcess::ExitStatus exitStatus() const override;
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
QProcess::ProcessError error() const final;
|
|
|
|
|
QString errorString() const override;
|
|
|
|
|
void setErrorString(const QString &str) final;
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
bool waitForStarted(int msecs = 30000) final;
|
|
|
|
|
bool waitForReadyRead(int msecs = 30000) final;
|
|
|
|
|
bool waitForFinished(int msecs = 30000) final;
|
2022-02-17 23:25:54 +01:00
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
void kickoffProcess() final;
|
|
|
|
|
qint64 applicationMainThreadID() const final;
|
2022-02-17 23:25:54 +01:00
|
|
|
|
|
|
|
|
// ProcessSetupData related
|
|
|
|
|
|
2022-02-10 19:25:03 +01:00
|
|
|
void setProcessImpl(ProcessImpl processImpl);
|
|
|
|
|
|
|
|
|
|
void setTerminalMode(TerminalMode mode);
|
|
|
|
|
TerminalMode terminalMode() const;
|
2022-02-18 00:56:14 +01:00
|
|
|
bool usesTerminal() const { return terminalMode() != TerminalMode::Off; }
|
2022-02-10 19:25:03 +01:00
|
|
|
|
|
|
|
|
void setProcessMode(ProcessMode processMode);
|
|
|
|
|
ProcessMode processMode() const;
|
|
|
|
|
|
2021-05-05 16:05:53 +02:00
|
|
|
void setEnvironment(const Environment &env);
|
2021-07-23 11:13:59 +02:00
|
|
|
void unsetEnvironment();
|
2021-05-05 16:05:53 +02:00
|
|
|
const Environment &environment() const;
|
2022-02-03 13:20:15 +01:00
|
|
|
bool hasEnvironment() const;
|
2021-05-05 16:05:53 +02:00
|
|
|
|
2022-02-17 16:27:11 +01:00
|
|
|
void setRemoteEnvironment(const Environment &env);
|
|
|
|
|
Environment remoteEnvironment() const;
|
|
|
|
|
|
2021-05-05 16:05:53 +02:00
|
|
|
void setCommand(const CommandLine &cmdLine);
|
|
|
|
|
const CommandLine &commandLine() const;
|
2019-05-28 18:59:45 +02:00
|
|
|
|
2021-06-03 10:39:15 +02:00
|
|
|
void setWorkingDirectory(const FilePath &dir);
|
2022-02-17 23:25:54 +01:00
|
|
|
FilePath workingDirectory() const;
|
|
|
|
|
|
|
|
|
|
void setWriteData(const QByteArray &writeData);
|
2021-05-14 15:21:54 +02:00
|
|
|
|
2022-02-17 23:25:54 +01:00
|
|
|
void setUseCtrlCStub(bool enabled); // debug only
|
2021-05-05 16:05:53 +02:00
|
|
|
void setLowPriority();
|
|
|
|
|
void setDisableUnixTerminal();
|
2021-09-08 14:51:49 +02:00
|
|
|
void setRunAsRoot(bool on);
|
2022-02-01 14:51:23 +01:00
|
|
|
bool isRunAsRoot() const;
|
2022-01-21 16:15:17 +01:00
|
|
|
void setAbortOnMetaChars(bool abort);
|
|
|
|
|
|
2022-02-17 23:25:54 +01:00
|
|
|
void setProcessChannelMode(QProcess::ProcessChannelMode mode);
|
|
|
|
|
void setStandardInputFile(const QString &inputFile);
|
|
|
|
|
|
|
|
|
|
void setExtraData(const QString &key, const QVariant &value);
|
|
|
|
|
QVariant extraData(const QString &key) const;
|
|
|
|
|
|
|
|
|
|
void setExtraData(const QVariantHash &extraData);
|
|
|
|
|
QVariantHash extraData() const;
|
|
|
|
|
|
|
|
|
|
static void setRemoteProcessHooks(const DeviceProcessHooks &hooks);
|
|
|
|
|
|
|
|
|
|
// TODO: Some usages of this method assume that Starting phase is also a running state
|
|
|
|
|
// i.e. if isRunning() returns false, they assume NotRunning state, what may be an error.
|
|
|
|
|
bool isRunning() const; // Short for state() == QProcess::Running.
|
|
|
|
|
|
|
|
|
|
// Other enhancements.
|
|
|
|
|
// These (or some of them) may be potentially moved outside of the class.
|
|
|
|
|
// For some we may aggregate in another public utils class (or subclass of QtcProcess)?
|
|
|
|
|
|
|
|
|
|
// TODO: How below 3 methods relate to QtcProcess? Action: move them somewhere else.
|
|
|
|
|
// Helpers to find binaries. Do not use it for other path variables
|
|
|
|
|
// and file types.
|
|
|
|
|
static QString locateBinary(const QString &binary);
|
|
|
|
|
static QString locateBinary(const QString &path, const QString &binary);
|
|
|
|
|
static QString normalizeNewlines(const QString &text);
|
|
|
|
|
|
|
|
|
|
// TODO: Unused currently? Should it serve as a compartment for contrary of remoteEnvironment?
|
|
|
|
|
static Environment systemEnvironmentForBinary(const FilePath &filePath);
|
|
|
|
|
|
2021-08-25 12:34:54 +02:00
|
|
|
static bool startDetached(const CommandLine &cmd, const FilePath &workingDirectory = {},
|
2021-08-12 12:33:28 +02:00
|
|
|
qint64 *pid = nullptr);
|
2022-01-24 19:57:52 +01:00
|
|
|
|
|
|
|
|
// Starts the command and waits for finish.
|
|
|
|
|
// User input processing is enabled when WithEventLoop was passed.
|
2022-03-02 04:12:25 +01:00
|
|
|
void runBlocking(EventLoopMode eventLoopMode = EventLoopMode::Off);
|
2021-06-22 04:24:13 +02:00
|
|
|
|
2021-05-06 11:15:28 +02:00
|
|
|
/* Timeout for hanging processes (triggers after no more output
|
|
|
|
|
* occurs on stderr/stdout). */
|
|
|
|
|
void setTimeoutS(int timeoutS);
|
|
|
|
|
|
2022-02-17 23:25:54 +01:00
|
|
|
// TODO: We should specify the purpose of the codec, e.g. setCodecForStandardChannel()
|
2021-05-06 11:15:28 +02:00
|
|
|
void setCodec(QTextCodec *c);
|
|
|
|
|
void setTimeOutMessageBoxEnabled(bool);
|
2022-03-02 04:12:25 +01:00
|
|
|
void setExitCodeInterpreter(const ExitCodeInterpreter &interpreter);
|
2021-05-06 11:15:28 +02:00
|
|
|
|
|
|
|
|
void setStdOutCallback(const std::function<void(const QString &)> &callback);
|
2021-06-04 11:40:35 +02:00
|
|
|
void setStdOutLineCallback(const std::function<void(const QString &)> &callback);
|
2021-05-06 11:15:28 +02:00
|
|
|
void setStdErrCallback(const std::function<void(const QString &)> &callback);
|
2021-06-04 11:40:35 +02:00
|
|
|
void setStdErrLineCallback(const std::function<void(const QString &)> &callback);
|
2021-05-06 11:15:28 +02:00
|
|
|
|
2021-05-06 13:07:36 +02:00
|
|
|
bool stopProcess();
|
|
|
|
|
bool readDataFromProcess(int timeoutS, QByteArray *stdOut, QByteArray *stdErr,
|
|
|
|
|
bool showTimeOutMessageBox);
|
|
|
|
|
|
2022-03-02 04:12:25 +01:00
|
|
|
ProcessResult result() const;
|
|
|
|
|
void setResult(const ProcessResult &result);
|
2021-05-12 14:25:50 +02:00
|
|
|
|
|
|
|
|
QByteArray allRawOutput() const;
|
|
|
|
|
QString allOutput() const;
|
|
|
|
|
|
|
|
|
|
QString stdOut() const;
|
|
|
|
|
QString stdErr() const;
|
|
|
|
|
|
|
|
|
|
QByteArray rawStdOut() const;
|
|
|
|
|
|
2021-05-14 13:12:46 +02:00
|
|
|
QString exitMessage();
|
2021-05-12 14:25:50 +02:00
|
|
|
|
2021-09-09 23:03:39 +02:00
|
|
|
QString toStandaloneCommandLine() const;
|
|
|
|
|
|
2022-02-28 14:55:54 +01:00
|
|
|
protected:
|
|
|
|
|
// TODO: remove these methods on QtcProcess de-virtualization
|
|
|
|
|
virtual void emitStarted();
|
|
|
|
|
virtual void emitFinished();
|
|
|
|
|
virtual void emitErrorOccurred(QProcess::ProcessError error);
|
|
|
|
|
|
2021-05-06 13:07:36 +02:00
|
|
|
private:
|
2022-02-21 12:49:14 +01:00
|
|
|
void setProcessInterface(ProcessInterface *interface);
|
|
|
|
|
|
2021-05-14 19:00:50 +02:00
|
|
|
friend QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug str, const QtcProcess &r);
|
2021-05-12 14:25:50 +02:00
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
friend class Internal::QtcProcessPrivate;
|
2021-05-06 13:07:36 +02:00
|
|
|
Internal::QtcProcessPrivate *d = nullptr;
|
|
|
|
|
|
2021-06-14 15:09:11 +02:00
|
|
|
friend tst_QtcProcess;
|
|
|
|
|
void beginFeed();
|
|
|
|
|
void feedStdOut(const QByteArray &data);
|
|
|
|
|
void endFeed();
|
2021-05-06 13:07:36 +02:00
|
|
|
};
|
|
|
|
|
|
2022-02-18 01:53:33 +01:00
|
|
|
class DeviceProcessHooks
|
2022-02-14 11:14:08 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2022-02-21 12:49:14 +01:00
|
|
|
std::function<ProcessInterface *(const FilePath &)> processImplHook;
|
|
|
|
|
// TODO: remove this hook
|
2022-02-18 01:53:33 +01:00
|
|
|
std::function<void(QtcProcess &)> startProcessHook;
|
|
|
|
|
std::function<Environment(const FilePath &)> systemEnvironmentForBinary;
|
2022-02-14 11:14:08 +01:00
|
|
|
};
|
|
|
|
|
|
2013-07-12 09:00:12 +02:00
|
|
|
} // namespace Utils
|