profileevaluator.cpp 8.04 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

con's avatar
con committed
30
#include "profileevaluator.h"
31

32
#include "qmakeglobals.h"
33
#include "ioutils.h"
con's avatar
con committed
34

35
#include <QDir>
36

37
using namespace QMakeInternal;
38

con's avatar
con committed
39 40
QT_BEGIN_NAMESPACE

41 42
void ProFileEvaluator::initialize()
{
43
    QMakeEvaluator::initStatics();
44 45
}

46
ProFileEvaluator::ProFileEvaluator(ProFileGlobals *option, QMakeParser *parser, QMakeVfs *vfs,
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
47
                                   QMakeHandler *handler)
48
  : d(new QMakeEvaluator(option, parser, vfs, handler))
con's avatar
con committed
49 50 51 52 53 54 55 56 57 58
{
}

ProFileEvaluator::~ProFileEvaluator()
{
    delete d;
}

bool ProFileEvaluator::contains(const QString &variableName) const
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
59
    return d->m_valuemapStack.top().contains(ProKey(variableName));
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
60 61
}

62 63 64 65 66 67 68 69 70
QString ProFileEvaluator::value(const QString &variable) const
{
    const QStringList &vals = values(variable);
    if (!vals.isEmpty())
        return vals.first();

    return QString();
}

con's avatar
con committed
71 72
QStringList ProFileEvaluator::values(const QString &variableName) const
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
73
    const ProStringList &values = d->values(ProKey(variableName));
74 75 76
    QStringList ret;
    ret.reserve(values.size());
    foreach (const ProString &str, values)
77
        ret << d->m_option->expandEnvVars(str.toQString());
78
    return ret;
con's avatar
con committed
79 80 81 82
}

QStringList ProFileEvaluator::values(const QString &variableName, const ProFile *pro) const
{
83
    // It makes no sense to put any kind of magic into expanding these
84
    const ProStringList &values = d->m_valuemapStack.first().value(ProKey(variableName));
85 86 87 88
    QStringList ret;
    ret.reserve(values.size());
    foreach (const ProString &str, values)
        if (str.sourceFile() == pro)
89
            ret << d->m_option->expandEnvVars(str.toQString());
90
    return ret;
con's avatar
con committed
91 92
}

93 94
QString ProFileEvaluator::sysrootify(const QString &path, const QString &baseDir) const
{
95
    ProFileGlobals *option = static_cast<ProFileGlobals *>(d->m_option);
96 97 98 99 100 101
#ifdef Q_OS_WIN
    Qt::CaseSensitivity cs = Qt::CaseInsensitive;
#else
    Qt::CaseSensitivity cs = Qt::CaseSensitive;
#endif
    const bool isHostSystemPath =
102
        option->sysroot.isEmpty() || path.startsWith(option->sysroot, cs)
103 104
        || path.startsWith(baseDir, cs) || path.startsWith(d->m_outputDir, cs)
        || !QFileInfo(option->sysroot + path).exists();
105

106
    return isHostSystemPath ? path : option->sysroot + path;
107 108
}

109 110 111 112 113
QStringList ProFileEvaluator::absolutePathValues(
        const QString &variable, const QString &baseDirectory) const
{
    QStringList result;
    foreach (const QString &el, values(variable)) {
114
        QString absEl = IoUtils::isAbsolutePath(el)
115
            ? sysrootify(el, baseDirectory) : IoUtils::resolvePath(baseDirectory, el);
116 117
        if (IoUtils::fileType(absEl) == IoUtils::FileIsDir)
            result << QDir::cleanPath(absEl);
118 119 120 121 122 123 124 125 126 127
    }
    return result;
}

QStringList ProFileEvaluator::absoluteFileValues(
        const QString &variable, const QString &baseDirectory, const QStringList &searchDirs,
        const ProFile *pro) const
{
    QStringList result;
    foreach (const QString &el, pro ? values(variable, pro) : values(variable)) {
128 129
        QString absEl;
        if (IoUtils::isAbsolutePath(el)) {
130
            const QString elWithSysroot = sysrootify(el, baseDirectory);
131 132
            if (IoUtils::exists(elWithSysroot)) {
                result << QDir::cleanPath(elWithSysroot);
133 134
                goto next;
            }
135
            absEl = elWithSysroot;
136 137
        } else {
            foreach (const QString &dir, searchDirs) {
138 139 140
                QString fn = dir + QLatin1Char('/') + el;
                if (IoUtils::exists(fn)) {
                    result << QDir::cleanPath(fn);
141 142 143 144 145
                    goto next;
                }
            }
            if (baseDirectory.isEmpty())
                goto next;
146
            absEl = baseDirectory + QLatin1Char('/') + el;
147 148
        }
        {
149 150
            absEl = QDir::cleanPath(absEl);
            int nameOff = absEl.lastIndexOf(QLatin1Char('/'));
151
            QString absDir = d->m_tmp1.setRawData(absEl.constData(), nameOff);
152
            if (IoUtils::exists(absDir)) {
153
                QString wildcard = d->m_tmp2.setRawData(absEl.constData() + nameOff + 1,
154
                                                        absEl.length() - nameOff - 1);
155
                if (wildcard.contains(QLatin1Char('*')) || wildcard.contains(QLatin1Char('?'))) {
156
                    wildcard.detach(); // Keep m_tmp out of QRegExp's cache
157
                    QDir theDir(absDir);
158
                    foreach (const QString &fn, theDir.entryList(QStringList(wildcard)))
159
                        if (fn != QLatin1String(".") && fn != QLatin1String(".."))
160
                            result << absDir + QLatin1Char('/') + fn;
161 162 163 164 165 166 167 168
                } // else if (acceptMissing)
            }
        }
      next: ;
    }
    return result;
}

169
ProFileEvaluator::TemplateType ProFileEvaluator::templateType() const
con's avatar
con committed
170
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
171
    const ProStringList &templ = d->values(ProKey("TEMPLATE"));
con's avatar
con committed
172
    if (templ.count() >= 1) {
173
        const QString &t = templ.at(0).toQString();
174
        if (!t.compare(QLatin1String("app"), Qt::CaseInsensitive))
con's avatar
con committed
175
            return TT_Application;
176
        if (!t.compare(QLatin1String("lib"), Qt::CaseInsensitive))
con's avatar
con committed
177
            return TT_Library;
178
        if (!t.compare(QLatin1String("script"), Qt::CaseInsensitive))
con's avatar
con committed
179
            return TT_Script;
180 181
        if (!t.compare(QLatin1String("aux"), Qt::CaseInsensitive))
            return TT_Aux;
182
        if (!t.compare(QLatin1String("subdirs"), Qt::CaseInsensitive))
con's avatar
con committed
183 184 185 186 187
            return TT_Subdirs;
    }
    return TT_Unknown;
}

188 189 190 191 192 193 194 195 196
bool ProFileEvaluator::loadNamedSpec(const QString &specDir, bool hostSpec)
{
    d->m_qmakespec = specDir;
    d->m_hostBuild = hostSpec;

    d->updateMkspecPaths();
    return d->loadSpecInternal();
}

197
bool ProFileEvaluator::accept(ProFile *pro, QMakeEvaluator::LoadFlags flags)
con's avatar
con committed
198
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
199
    return d->visitProFile(pro, QMakeHandler::EvalProjectFile, flags) == QMakeEvaluator::ReturnTrue;
con's avatar
con committed
200 201 202 203
}

QString ProFileEvaluator::propertyValue(const QString &name) const
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
204
    return d->m_option->propertyValue(ProKey(name)).toQString();
con's avatar
con committed
205 206
}

207 208
QString ProFileEvaluator::resolvedMkSpec() const
{
209
    return d->m_qmakespec;
210 211
}

212
#ifdef PROEVALUATOR_CUMULATIVE
213 214 215 216
void ProFileEvaluator::setCumulative(bool on)
{
    d->m_cumulative = on;
}
217
#endif
218

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
void ProFileEvaluator::setExtraVars(const QHash<QString, QStringList> &extraVars)
{
    ProValueMap map;
    QHash<QString, QStringList>::const_iterator it = extraVars.constBegin();
    QHash<QString, QStringList>::const_iterator end = extraVars.constEnd();
    for ( ; it != end; ++it)
        map.insert(ProKey(it.key()), ProStringList(it.value()));
    d->setExtraVars(map);
}

void ProFileEvaluator::setExtraConfigs(const QStringList &extraConfigs)
{
     d->setExtraConfigs(ProStringList(extraConfigs));
}

234 235 236 237 238
void ProFileEvaluator::setOutputDir(const QString &dir)
{
    d->m_outputDir = dir;
}

con's avatar
con committed
239
QT_END_NAMESPACE