1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qbs.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "installoptions.h"
41 
42 #include "jsonhelper.h"
43 #include "stringconstants.h"
44 
45 #include <language/language.h>
46 
47 #include <QtCore/qdir.h>
48 #include <QtCore/qshareddata.h>
49 
50 namespace qbs {
51 namespace Internal {
52 
53 class InstallOptionsPrivate : public QSharedData
54 {
55 public:
InstallOptionsPrivate()56     InstallOptionsPrivate()
57         : useSysroot(false), removeExisting(false), dryRun(false),
58           keepGoing(false), logElapsedTime(false)
59     {}
60 
61     QString installRoot;
62     bool useSysroot;
63     bool removeExisting;
64     bool dryRun;
65     bool keepGoing;
66     bool logElapsedTime;
67 };
68 
effectiveInstallRoot(const InstallOptions & options,const TopLevelProject * project)69 QString effectiveInstallRoot(const InstallOptions &options, const TopLevelProject *project)
70 {
71     const QString installRoot = options.installRoot();
72     if (!installRoot.isEmpty())
73         return installRoot;
74 
75     if (options.installIntoSysroot()) {
76         return project->buildConfiguration().value(StringConstants::qbsModule()).toMap()
77                 .value(QStringLiteral("sysroot")).toString();
78     }
79     return project->buildConfiguration().value(StringConstants::qbsModule()).toMap()
80             .value(StringConstants::installRootProperty()).toString();
81 }
82 
83 } // namespace Internal
84 
85 /*!
86  * \class InstallOptions
87  * \brief The \c InstallOptions class comprises parameters that influence the behavior of
88  * install operations.
89  */
90 
InstallOptions()91 InstallOptions::InstallOptions() : d(new Internal::InstallOptionsPrivate)
92 {
93 }
94 
95 InstallOptions::InstallOptions(const InstallOptions &other) = default;
96 
97 InstallOptions::InstallOptions(InstallOptions &&other) Q_DECL_NOEXCEPT = default;
98 
99 InstallOptions &InstallOptions::operator=(const InstallOptions &other) = default;
100 
101 InstallOptions &InstallOptions::operator=(InstallOptions &&other) Q_DECL_NOEXCEPT = default;
102 
103 InstallOptions::~InstallOptions() = default;
104 
105 /*!
106  * \brief The default install root, relative to the build directory.
107  */
defaultInstallRoot()108 QString InstallOptions::defaultInstallRoot()
109 {
110     return QStringLiteral("install-root");
111 }
112 
113 /*!
114  * Returns the base directory for the installation.
115  * The \c qbs.installPrefix path is relative to this root. If the string is empty, either the value of
116  * qbs.sysroot or "<build dir>/install-root" will be used, depending on what \c installIntoSysroot()
117  * returns.
118  * The default is empty.
119  */
installRoot() const120 QString InstallOptions::installRoot() const
121 {
122     return d->installRoot;
123 }
124 
125 /*!
126  * \brief Sets the base directory for the installation.
127  * \note The argument must either be an empty string or an absolute path to a directory
128  *       (which might not yet exist, in which case it will be created).
129  */
setInstallRoot(const QString & installRoot)130 void InstallOptions::setInstallRoot(const QString &installRoot)
131 {
132     d->installRoot = QDir::cleanPath(installRoot);
133     if (!QDir(installRoot).isRoot()) {
134         while (d->installRoot.endsWith(QLatin1Char('/')))
135             d->installRoot.chop(1);
136     }
137 }
138 
139 /*!
140  * Returns whether to use the sysroot as the default install root.
141  * The default is false.
142  */
installIntoSysroot() const143 bool InstallOptions::installIntoSysroot() const
144 {
145     return d->useSysroot;
146 }
147 
setInstallIntoSysroot(bool useSysroot)148 void InstallOptions::setInstallIntoSysroot(bool useSysroot)
149 {
150     d->useSysroot = useSysroot;
151 }
152 
153 /*!
154  * \brief Returns true iff an existing installation will be removed prior to installing.
155  * The default is false.
156  */
removeExistingInstallation() const157 bool InstallOptions::removeExistingInstallation() const
158 {
159     return d->removeExisting;
160 }
161 
162 /*!
163  * Controls whether to remove an existing installation before installing.
164  * \note qbs may do some safety checks and refuse to remove certain directories such as
165  *       a user's home directory. You should still be careful with this option, since it
166  *       deletes recursively.
167  */
setRemoveExistingInstallation(bool removeExisting)168 void InstallOptions::setRemoveExistingInstallation(bool removeExisting)
169 {
170     d->removeExisting = removeExisting;
171 }
172 
173 /*!
174  * \brief Returns true iff qbs will not actually copy any files, but just show what would happen.
175  * The default is false.
176  */
dryRun() const177 bool InstallOptions::dryRun() const
178 {
179     return d->dryRun;
180 }
181 
182 /*!
183  * \brief Controls whether installation will actually take place.
184  * If the argument is true, then qbs will emit information about which files would be copied
185  * instead of actually doing it.
186  */
setDryRun(bool dryRun)187 void InstallOptions::setDryRun(bool dryRun)
188 {
189     d->dryRun = dryRun;
190 }
191 
192 /*!
193  * Returns true iff installation will continue if an error occurs.
194  * The default is false.
195  */
keepGoing() const196 bool InstallOptions::keepGoing() const
197 {
198     return d->keepGoing;
199 }
200 
201 /*!
202  * \brief Controls whether to abort on errors.
203  * If the argument is true, then if a file cannot be copied e.g. due to a permission problem,
204  * a warning will be printed and the installation will continue. If the argument is false,
205  * then the installation will abort immediately in case of an error.
206  */
setKeepGoing(bool keepGoing)207 void InstallOptions::setKeepGoing(bool keepGoing)
208 {
209     d->keepGoing = keepGoing;
210 }
211 
212 /*!
213  * \brief Returns true iff the time the operation takes will be logged.
214  * The default is false.
215  */
logElapsedTime() const216 bool InstallOptions::logElapsedTime() const
217 {
218     return d->logElapsedTime;
219 }
220 
221 /*!
222  * \brief Controls whether the installation time will be measured and logged.
223  */
setLogElapsedTime(bool logElapsedTime)224 void InstallOptions::setLogElapsedTime(bool logElapsedTime)
225 {
226     d->logElapsedTime = logElapsedTime;
227 }
228 
fromJson(const QJsonObject & data)229 qbs::InstallOptions qbs::InstallOptions::fromJson(const QJsonObject &data)
230 {
231     using namespace Internal;
232     InstallOptions opt;
233     setValueFromJson(opt.d->installRoot, data, "install-root");
234     setValueFromJson(opt.d->useSysroot, data, "use-sysroot");
235     setValueFromJson(opt.d->removeExisting, data, "clean-install-root");
236     setValueFromJson(opt.d->dryRun, data, "dry-run");
237     setValueFromJson(opt.d->keepGoing, data, "keep-going");
238     setValueFromJson(opt.d->logElapsedTime, data, "log-time");
239     return opt;
240 }
241 
242 } // namespace qbs
243