1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #pragma once
27
28 #include "fileutils.h"
29 #include "hostosinfo.h"
30 #include "namevaluedictionary.h"
31 #include "namevalueitem.h"
32 #include "optional.h"
33
34 #include <QStringList>
35
36 #include <functional>
37
QT_FORWARD_DECLARE_CLASS(QProcessEnvironment)38 QT_FORWARD_DECLARE_CLASS(QProcessEnvironment)
39
40 namespace Utils {
41
42 class QTCREATOR_UTILS_EXPORT Environment final : public NameValueDictionary
43 {
44 public:
45 using NameValueDictionary::NameValueDictionary;
46
47 static Environment systemEnvironment();
48
49 QProcessEnvironment toProcessEnvironment() const;
50
51 void appendOrSet(const QString &key, const QString &value, const QString &sep = QString());
52 void prependOrSet(const QString &key, const QString &value, const QString &sep = QString());
53
54 void appendOrSetPath(const QString &value);
55 void prependOrSetPath(const QString &value);
56
57 void prependOrSetLibrarySearchPath(const QString &value);
58 void prependOrSetLibrarySearchPaths(const QStringList &values);
59
60 void setupEnglishOutput();
61
62 using PathFilter = std::function<bool(const FilePath &)>;
63 FilePath searchInPath(const QString &executable,
64 const FilePaths &additionalDirs = FilePaths(),
65 const PathFilter &func = PathFilter()) const;
66 FilePaths findAllInPath(const QString &executable,
67 const FilePaths &additionalDirs = FilePaths(),
68 const PathFilter &func = PathFilter()) const;
69
70 FilePaths path() const;
71 FilePaths pathListValue(const QString &varName) const;
72 QStringList appendExeExtensions(const QString &executable) const;
73
74 bool isSameExecutable(const QString &exe1, const QString &exe2) const;
75
76 QString expandedValueForKey(const QString &key) const;
77 QString expandVariables(const QString &input) const;
78 FilePath expandVariables(const FilePath &input) const;
79 QStringList expandVariables(const QStringList &input) const;
80
81 static void modifySystemEnvironment(const EnvironmentItems &list); // use with care!!!
82 static void setSystemEnvironment(const Environment &environment); // don't use at all!!!
83
84 private:
85 static FilePath searchInDirectory(const QStringList &execs, const FilePath &directory,
86 QSet<FilePath> &alreadyChecked);
87 };
88
89 class QTCREATOR_UTILS_EXPORT EnvironmentChange final
90 {
91 public:
92 using Item = std::function<void(Environment &)>;
93
94 EnvironmentChange() = default;
95
96 void applyToEnvironment(Environment &) const;
97
98 void addSetValue(const QString &key, const QString &value);
99 void addUnsetValue(const QString &key);
100 void addPrependToPath(const QString &value);
101 void addAppendToPath(const QString &value);
102 void addModify(const NameValueItems &items);
103 void addChange(const Item &item) { m_changeItems.append(item); }
104
105 private:
106 QList<Item> m_changeItems;
107 };
108
109 class QTCREATOR_UTILS_EXPORT EnvironmentProvider
110 {
111 public:
112 QByteArray id;
113 QString displayName;
114 std::function<Environment()> environment;
115
116 static void addProvider(EnvironmentProvider &&provider);
117 static const QVector<EnvironmentProvider> providers();
118 static optional<EnvironmentProvider> provider(const QByteArray &id);
119 };
120
121 } // namespace Utils
122