1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "genericproject.h"
#include "genericbuildconfiguration.h"
#include "genericmakestep.h"
#include "genericprojectconstants.h"
#include <coreplugin/documentmanager.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
#include <cpptools/cpptoolsconstants.h>
#include <cpptools/cppmodelmanagerinterface.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/abi.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/headerpath.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <qtsupport/customexecutablerunconfiguration.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <QDir>
#include <QProcessEnvironment>
using namespace Core;
using namespace ProjectExplorer;
namespace GenericProjectManager {
namespace Internal {
////////////////////////////////////////////////////////////////////////////////////
//
// GenericProject
//
////////////////////////////////////////////////////////////////////////////////////
GenericProject::GenericProject(Manager *manager, const QString &fileName)
: m_manager(manager),
m_fileName(fileName)
{
setProjectContext(Context(GenericProjectManager::Constants::PROJECTCONTEXT));
setProjectLanguages(Context(ProjectExplorer::Constants::LANG_CXX));
QFileInfo fileInfo(m_fileName);
QDir dir = fileInfo.dir();
m_projectName = fileInfo.completeBaseName();
m_filesFileName = QFileInfo(dir, m_projectName + QLatin1String(".files")).absoluteFilePath();
m_includesFileName = QFileInfo(dir, m_projectName + QLatin1String(".includes")).absoluteFilePath();
m_configFileName = QFileInfo(dir, m_projectName + QLatin1String(".config")).absoluteFilePath();
m_creatorIDocument = new GenericProjectFile(this, m_fileName, GenericProject::Everything);
m_filesIDocument = new GenericProjectFile(this, m_filesFileName, GenericProject::Files);
m_includesIDocument = new GenericProjectFile(this, m_includesFileName, GenericProject::Configuration);
m_configIDocument = new GenericProjectFile(this, m_configFileName, GenericProject::Configuration);
DocumentManager::addDocument(m_creatorIDocument);
DocumentManager::addDocument(m_filesIDocument);
DocumentManager::addDocument(m_includesIDocument);
DocumentManager::addDocument(m_configIDocument);
m_rootNode = new GenericProjectNode(this, m_creatorIDocument);
m_manager->registerProject(this);
}
GenericProject::~GenericProject()
{
m_codeModelFuture.cancel();
m_manager->unregisterProject(this);
delete m_rootNode;
}
QString GenericProject::filesFileName() const
{
return m_filesFileName;
}
QString GenericProject::includesFileName() const
{
return m_includesFileName;
}
QString GenericProject::configFileName() const
{
return m_configFileName;
}
static QStringList readLines(const QString &absoluteFileName)
{
QStringList lines;
QFile file(absoluteFileName);
if (file.open(QFile::ReadOnly)) {
QTextStream stream(&file);
forever {
QString line = stream.readLine();
if (line.isNull())
break;
lines.append(line);
}
}
return lines;
}
bool GenericProject::saveRawFileList(const QStringList &rawFileList)
{
// Make sure we can open the file for writing
Utils::FileSaver saver(filesFileName(), QIODevice::Text);
if (!saver.hasError()) {
QTextStream stream(saver.file());
foreach (const QString &filePath, rawFileList)
stream << filePath << QLatin1Char('\n');
saver.setResult(&stream);
}
if (!saver.finalize(ICore::mainWindow()))
return false;
refresh(GenericProject::Files);
return true;
}
bool GenericProject::addFiles(const QStringList &filePaths)
{
QStringList newList = m_rawFileList;
QDir baseDir(QFileInfo(m_fileName).dir());
foreach (const QString &filePath, filePaths)
newList.append(baseDir.relativeFilePath(filePath));
return saveRawFileList(newList);
}
bool GenericProject::removeFiles(const QStringList &filePaths)
{
QStringList newList = m_rawFileList;
foreach (const QString &filePath, filePaths) {
QHash<QString, QString>::iterator i = m_rawListEntries.find(filePath);
if (i != m_rawListEntries.end())
newList.removeOne(i.value());
}
return saveRawFileList(newList);
}
bool GenericProject::setFiles(const QStringList &filePaths)
{
QStringList newList;
QDir baseDir(QFileInfo(m_fileName).dir());
foreach (const QString &filePath, filePaths)
newList.append(baseDir.relativeFilePath(filePath));
return saveRawFileList(newList);
}
bool GenericProject::renameFile(const QString &filePath, const QString &newFilePath)
{
QStringList newList = m_rawFileList;
QHash<QString, QString>::iterator i = m_rawListEntries.find(filePath);
if (i != m_rawListEntries.end()) {
int index = newList.indexOf(i.value());
if (index != -1) {
QDir baseDir(QFileInfo(m_fileName).dir());
newList.replace(index, baseDir.relativeFilePath(newFilePath));
}
}
return saveRawFileList(newList);
}
void GenericProject::parseProject(RefreshOptions options)
{
if (options & Files) {
m_rawListEntries.clear();
m_rawFileList = readLines(filesFileName());
m_files = processEntries(m_rawFileList, &m_rawListEntries);
}
if (options & Configuration) {
m_projectIncludePaths = processEntries(readLines(includesFileName()));
// TODO: Possibly load some configuration from the project file
//QSettings projectInfo(m_fileName, QSettings::IniFormat);
m_defines.clear();
QFile configFile(configFileName());
if (configFile.open(QFile::ReadOnly)) {
// convert from local/file encoding to UTF-8
QTextStream configStream(&configFile);
m_defines = configStream.readAll().toUtf8();
}
}
if (options & Files)
emit fileListChanged();
}
void GenericProject::refresh(RefreshOptions options)
{
QSet<QString> oldFileList;
if (!(options & Configuration))
oldFileList = m_files.toSet();
parseProject(options);
if (options & Files)
m_rootNode->refresh(oldFileList);
CppTools::CppModelManagerInterface *modelManager =
CppTools::CppModelManagerInterface::instance();
if (modelManager) {
CppTools::CppModelManagerInterface::ProjectInfo pinfo = modelManager->projectInfo(this);
pinfo.clearProjectParts();
CppTools::ProjectPart::Ptr part(new CppTools::ProjectPart);
Kit *k = activeTarget() ? activeTarget()->kit() : KitManager::defaultKit();
if (ToolChain *tc = ToolChainKitInformation::toolChain(k)) {
QStringList cxxflags; // FIXME: Can we do better?
part->evaluateToolchain(tc, cxxflags, cxxflags,
SysRootKitInformation::sysRoot(k));
}
part->cxxVersion = CppTools::ProjectPart::CXX11; // assume C++11
part->includePaths += allIncludePaths();
part->defines += m_defines;
// ### add _defines.
// Add any C/C++ files to be parsed
CppTools::ProjectFileAdder adder(part->files);
foreach (const QString &file, files())
adder.maybeAdd(file);
m_codeModelFuture.cancel();
pinfo.appendProjectPart(part);
setProjectLanguage(ProjectExplorer::Constants::LANG_CXX, !part->files.isEmpty());
m_codeModelFuture = modelManager->updateProjectInfo(pinfo);
}
}
/**
* Expands environment variables in the given \a string when they are written
* like $$(VARIABLE).
*/
static void expandEnvironmentVariables(const QProcessEnvironment &env, QString &string)
{
static QRegExp candidate(QLatin1String("\\$\\$\\((.+)\\)"));
int index = candidate.indexIn(string);
while (index != -1) {
const QString value = env.value(candidate.cap(1));
string.replace(index, candidate.matchedLength(), value);
index += value.length();
index = candidate.indexIn(string, index);
}
}
/**
* Expands environment variables and converts the path from relative to the
* project to an absolute path.
*
* The \a map variable is an optional argument that will map the returned
* absolute paths back to their original \a entries.
*/
QStringList GenericProject::processEntries(const QStringList &paths,
QHash<QString, QString> *map) const
{
const QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
const QDir projectDir(QFileInfo(m_fileName).dir());
QFileInfo fileInfo;
QStringList absolutePaths;
foreach (const QString &path, paths) {
QString trimmedPath = path.trimmed();
if (trimmedPath.isEmpty())
continue;
expandEnvironmentVariables(env, trimmedPath);
trimmedPath = Utils::FileName::fromUserInput(trimmedPath).toString();
fileInfo.setFile(projectDir, trimmedPath);
if (fileInfo.exists()) {
const QString absPath = fileInfo.absoluteFilePath();
absolutePaths.append(absPath);
if (map)
map->insert(absPath, trimmedPath);
}
}
absolutePaths.removeDuplicates();
return absolutePaths;
}
QStringList GenericProject::allIncludePaths() const
{
QStringList paths;
paths += m_includePaths;
paths += m_projectIncludePaths;
paths.removeDuplicates();
return paths;
}
QStringList GenericProject::projectIncludePaths() const
{
return m_projectIncludePaths;
}
QStringList GenericProject::files() const
{
return m_files;
}
QStringList GenericProject::includePaths() const
{
return m_includePaths;
}
void GenericProject::setIncludePaths(const QStringList &includePaths)
{
m_includePaths = includePaths;
}
QByteArray GenericProject::defines() const
{
return m_defines;
}
QString GenericProject::displayName() const
{
return m_projectName;
}
Id GenericProject::id() const
{
return Id(Constants::GENERICPROJECT_ID);
}
IDocument *GenericProject::document() const
{
return m_creatorIDocument;
}
IProjectManager *GenericProject::projectManager() const
{
return m_manager;
}
GenericProjectNode *GenericProject::rootProjectNode() const
{
return m_rootNode;
}
QStringList GenericProject::files(FilesMode fileMode) const
{
Q_UNUSED(fileMode)
return m_files;
}
QStringList GenericProject::buildTargets() const
{
QStringList targets;
targets.append(QLatin1String("all"));
targets.append(QLatin1String("clean"));
return targets;
}
bool GenericProject::fromMap(const QVariantMap &map)
{
if (!Project::fromMap(map))
return false;
Kit *defaultKit = KitManager::defaultKit();
if (!activeTarget() && defaultKit)
addTarget(createTarget(defaultKit));
// Sanity check: We need both a buildconfiguration and a runconfiguration!
QList<Target *> targetList = targets();
foreach (Target *t, targetList) {
if (!t->activeBuildConfiguration()) {
removeTarget(t);
delete t;
continue;
}
if (!t->activeRunConfiguration())
t->addRunConfiguration(new QtSupport::CustomExecutableRunConfiguration(t));
}
setIncludePaths(allIncludePaths());
refresh(Everything);
return true;
}
////////////////////////////////////////////////////////////////////////////////////
//
// GenericProjectFile
//
////////////////////////////////////////////////////////////////////////////////////
GenericProjectFile::GenericProjectFile(GenericProject *parent, QString fileName, GenericProject::RefreshOptions options)
: IDocument(parent),
m_project(parent),
m_options(options)
{
setFilePath(fileName);
}
bool GenericProjectFile::save(QString *, const QString &, bool)
{
return false;
}
QString GenericProjectFile::defaultPath() const
{
return QString();
}
QString GenericProjectFile::suggestedFileName() const
{
return QString();
}
QString GenericProjectFile::mimeType() const
{
return QLatin1String(Constants::GENERICMIMETYPE);
}
bool GenericProjectFile::isModified() const
{
return false;
}
bool GenericProjectFile::isSaveAsAllowed() const
{
return false;
}
IDocument::ReloadBehavior GenericProjectFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
{
Q_UNUSED(state)
Q_UNUSED(type)
return BehaviorSilent;
}
bool GenericProjectFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
{
Q_UNUSED(errorString)
Q_UNUSED(flag)
if (type == TypePermissions)
return true;
m_project->refresh(m_options);
return true;
}
} // namespace Internal
} // namespace GenericProjectManager
|