File: qmakefile.cpp

package info (click to toggle)
kdevelop 4%3A5.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 52,544 kB
  • sloc: cpp: 254,897; python: 3,380; sh: 1,271; ansic: 657; xml: 221; php: 95; makefile: 36; lisp: 13; sed: 12
file content (212 lines) | stat: -rw-r--r-- 6,997 bytes parent folder | download
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
/***************************************************************************
 *   This file is part of KDevelop                                         *
 *   Copyright 2007 Andreas Pakulat <apaku@gmx.de>                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "qmakefile.h"

#include <QDir>
#include <QFileInfo>

#include <debug.h>
#include "parser/ast.h"
#include "qmakedriver.h"

#define ifDebug(x)

//@TODO: Make the globbing stuff work with drives on win32

void resolveShellGlobbingInternal(QStringList& entries, const QStringList& segments, const QFileInfo& match, QDir& dir,
                                  int offset);

QStringList resolveShellGlobbingInternal(const QStringList& segments, QDir& dir, int offset = 0)
{
    if (offset >= segments.size()) {
        return QStringList();
    }

    const QString& pathPattern = segments.at(offset);

    QStringList entries;
    if (pathPattern.contains(QLatin1Char('*')) ||
        pathPattern.contains(QLatin1Char('?')) ||
        pathPattern.contains(QLatin1Char('['))) {
        // pattern contains globbing chars
        foreach (const QFileInfo& match, dir.entryInfoList(QStringList() << pathPattern,
                                                           QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Unsorted)) {
            resolveShellGlobbingInternal(entries, segments, match, dir, offset);
        }
    } else {
        // pattern is "simple" hence be fast, but make sure the file exists
        QFileInfo info(dir.filePath(pathPattern));
        if (info.exists()) {
            resolveShellGlobbingInternal(entries, segments, info, dir, offset);
        }
    }

    return entries;
}

void resolveShellGlobbingInternal(QStringList& entries, const QStringList& segments, const QFileInfo& match, QDir& dir,
                                  int offset)
{
    if (match.isDir() && offset + 1 < segments.size()) {
        dir.cd(match.fileName());
        entries += resolveShellGlobbingInternal(segments, dir, offset + 1);
        dir.cdUp();
    } else {
        entries << match.canonicalFilePath();
    }
}

QStringList resolveShellGlobbingInternal(const QString& pattern, const QString& dir)
{
    if (pattern.isEmpty()) {
        return QStringList();
    }

    QDir dir_(pattern.startsWith(QLatin1Char('/')) ? QStringLiteral("/") : dir);

    // break up pattern into path segments
    return resolveShellGlobbingInternal(pattern.split(QLatin1Char('/'), QString::SkipEmptyParts), dir_);
}

QMakeFile::QMakeFile(QString file)
    : m_ast(nullptr)
    , m_projectFile(std::move(file))
    , m_project(nullptr)
{
    Q_ASSERT(!m_projectFile.isEmpty());
}

bool QMakeFile::read()
{
    Q_ASSERT(!m_projectFile.isEmpty());
    QFileInfo fi(m_projectFile);
    ifDebug(qCDebug(KDEV_QMAKE) << "Is" << m_projectFile << "a dir?" << fi.isDir();) if (fi.isDir())
    {
        QDir dir(m_projectFile);
        QStringList l = dir.entryList(QStringList() << QStringLiteral("*.pro"));

        QString projectfile;

        if (!l.count() || (l.count() && l.indexOf(fi.baseName() + QLatin1String(".pro")) != -1)) {
            projectfile = fi.baseName() + QLatin1String(".pro");
        } else {
            projectfile = l.first();
        }
        m_projectFile += QLatin1Char('/') + projectfile;
    }
    QMake::Driver d;
    d.readFile(m_projectFile);

    if (!d.parse(&m_ast)) {
        qCWarning(KDEV_QMAKE) << "Couldn't parse project:" << m_projectFile;
        delete m_ast;
        m_ast = nullptr;
        m_projectFile = QString();
        return false;
    } else {
        ifDebug(qCDebug(KDEV_QMAKE) << "found ast:" << m_ast->statements.count();) QMakeFileVisitor visitor(this, this);
        /// TODO: cleanup, re-use m_variableValues directly in the visitor
        visitor.setVariables(m_variableValues);
        m_variableValues = visitor.visitFile(m_ast);
        ifDebug(qCDebug(KDEV_QMAKE) << "Variables found:" << m_variableValues;)
    }
    return true;
}

QMakeFile::~QMakeFile()
{
    delete m_ast;
    m_ast = nullptr;
}

QString QMakeFile::absoluteDir() const
{
    return QFileInfo(m_projectFile).absoluteDir().canonicalPath();
}

QString QMakeFile::absoluteFile() const
{
    return m_projectFile;
}

QMake::ProjectAST* QMakeFile::ast() const
{
    return m_ast;
}

QStringList QMakeFile::variables() const
{
    return m_variableValues.keys();
}

QStringList QMakeFile::variableValues(const QString& variable) const
{
    return m_variableValues.value(variable, QStringList());
}

bool QMakeFile::containsVariable(const QString& variable) const
{
    return m_variableValues.contains(variable);
}

QMakeFile::VariableMap QMakeFile::variableMap() const
{
    return m_variableValues;
}

QStringList QMakeFile::resolveVariable(const QString& variable, VariableInfo::VariableType type) const
{
    if (type == VariableInfo::QMakeVariable && m_variableValues.contains(variable)) {
        return m_variableValues.value(variable);
    } else {
        qCWarning(KDEV_QMAKE) << "unresolved variable:" << variable << "type:" << type;
        return QStringList();
    }
}

QStringList QMakeFile::resolveShellGlobbing(const QString& pattern, const QString& base) const
{
    return resolveShellGlobbingInternal(pattern, base.isEmpty() ? absoluteDir() : base);
}

QString QMakeFile::resolveToSingleFileName(const QString& file, const QString& base) const
{
    QStringList l = resolveFileName(file, base);
    if (l.isEmpty())
        return QString();
    else
        return l.first();
}

QStringList QMakeFile::resolveFileName(const QString& file, const QString& base) const
{
    return resolveShellGlobbing(file, base);
}

void QMakeFile::setProject(KDevelop::IProject* project)
{
    m_project = project;
}

KDevelop::IProject* QMakeFile::project() const
{
    return m_project;
}