| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "profileevaluator.h" |
| 5 | |
| 6 | #include "qmakeglobals.h" |
| 7 | #include "ioutils.h" |
| 8 | #include "qmakevfs.h" |
| 9 | |
| 10 | #include <QDir> |
| 11 | |
| 12 | using namespace QMakeInternal; |
| 13 | using namespace Qt::Literals::StringLiterals; |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | void ProFileEvaluator::initialize() |
| 18 | { |
| 19 | QMakeEvaluator::initStatics(); |
| 20 | } |
| 21 | |
| 22 | ProFileEvaluator::ProFileEvaluator(ProFileGlobals *option, QMakeParser *parser, QMakeVfs *vfs, |
| 23 | QMakeHandler *handler) |
| 24 | : d(new QMakeEvaluator(option, parser, vfs, handler)) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | ProFileEvaluator::~ProFileEvaluator() |
| 29 | { |
| 30 | delete d; |
| 31 | } |
| 32 | |
| 33 | bool ProFileEvaluator::contains(const QString &variableName) const |
| 34 | { |
| 35 | return d->m_valuemapStack.top().contains(key: ProKey(variableName)); |
| 36 | } |
| 37 | |
| 38 | QString ProFileEvaluator::value(const QString &variable) const |
| 39 | { |
| 40 | const QStringList &vals = values(variableName: variable); |
| 41 | if (!vals.isEmpty()) |
| 42 | return vals.first(); |
| 43 | |
| 44 | return QString(); |
| 45 | } |
| 46 | |
| 47 | QStringList ProFileEvaluator::values(const QString &variableName) const |
| 48 | { |
| 49 | const ProStringList &values = d->values(variableName: ProKey(variableName)); |
| 50 | QStringList ret; |
| 51 | ret.reserve(asize: values.size()); |
| 52 | for (const ProString &str : values) |
| 53 | ret << d->m_option->expandEnvVars(str: str.toQString()); |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | QStringList ProFileEvaluator::values(const QString &variableName, const ProFile *pro) const |
| 58 | { |
| 59 | // It makes no sense to put any kind of magic into expanding these |
| 60 | const ProStringList &values = d->m_valuemapStack.front().value(key: ProKey(variableName)); |
| 61 | QStringList ret; |
| 62 | ret.reserve(asize: values.size()); |
| 63 | for (const ProString &str : values) |
| 64 | if (str.sourceFile() == pro->id()) |
| 65 | ret << d->m_option->expandEnvVars(str: str.toQString()); |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | QString ProFileEvaluator::sysrootify(const QString &path, const QString &baseDir) const |
| 70 | { |
| 71 | ProFileGlobals *option = static_cast<ProFileGlobals *>(d->m_option); |
| 72 | #ifdef Q_OS_WIN |
| 73 | Qt::CaseSensitivity cs = Qt::CaseInsensitive; |
| 74 | #else |
| 75 | Qt::CaseSensitivity cs = Qt::CaseSensitive; |
| 76 | #endif |
| 77 | const bool isHostSystemPath = |
| 78 | option->sysroot.isEmpty() || path.startsWith(s: option->sysroot, cs) |
| 79 | || path.startsWith(s: baseDir, cs) || path.startsWith(s: d->m_outputDir, cs); |
| 80 | |
| 81 | return isHostSystemPath ? path : option->sysroot + path; |
| 82 | } |
| 83 | |
| 84 | QStringList ProFileEvaluator::absolutePathValues( |
| 85 | const QString &variable, const QString &baseDirectory) const |
| 86 | { |
| 87 | QStringList result; |
| 88 | for (const QString &el : values(variableName: variable)) { |
| 89 | QString absEl = IoUtils::isAbsolutePath(fileName: el) |
| 90 | ? sysrootify(path: el, baseDir: baseDirectory) : IoUtils::resolvePath(baseDir: baseDirectory, fileName: el); |
| 91 | if (IoUtils::fileType(fileName: absEl) == IoUtils::FileIsDir) |
| 92 | result << QDir::cleanPath(path: absEl); |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | QStringList ProFileEvaluator::absoluteFileValues( |
| 98 | const QString &variable, const QString &baseDirectory, const QStringList &searchDirs, |
| 99 | const ProFile *pro) const |
| 100 | { |
| 101 | QStringList result; |
| 102 | const auto vals = pro ? values(variableName: variable, pro) : values(variableName: variable); |
| 103 | for (const QString &el : vals) { |
| 104 | QString absEl; |
| 105 | if (IoUtils::isAbsolutePath(fileName: el)) { |
| 106 | const QString elWithSysroot = QDir::cleanPath(path: sysrootify(path: el, baseDir: baseDirectory)); |
| 107 | if (d->m_vfs->exists(fn: elWithSysroot, flags: QMakeVfs::VfsCumulative)) { |
| 108 | result << elWithSysroot; |
| 109 | goto next; |
| 110 | } |
| 111 | absEl = elWithSysroot; |
| 112 | } else { |
| 113 | for (const QString &dir : searchDirs) { |
| 114 | QString fn = QDir::cleanPath(path: dir + u'/' + el); |
| 115 | if (d->m_vfs->exists(fn, flags: QMakeVfs::VfsCumulative)) { |
| 116 | result << fn; |
| 117 | goto next; |
| 118 | } |
| 119 | } |
| 120 | if (baseDirectory.isEmpty()) |
| 121 | goto next; |
| 122 | absEl = QDir::cleanPath(path: baseDirectory + u'/' + el); |
| 123 | } |
| 124 | { |
| 125 | int nameOff = absEl.lastIndexOf(c: u'/'); |
| 126 | QString absDir = d->m_tmp1.setRawData(unicode: absEl.constData(), size: nameOff); |
| 127 | // NOTE: This does not support virtual files. That shouldn't be a problem, |
| 128 | // because no sane project would add generated files by wildcard. |
| 129 | if (IoUtils::fileType(fileName: absDir) == IoUtils::FileIsDir) { |
| 130 | QString wildcard = d->m_tmp2.setRawData(unicode: absEl.constData() + nameOff + 1, |
| 131 | size: absEl.size() - nameOff - 1); |
| 132 | if (wildcard.contains(c: u'*') || wildcard.contains(c: u'?')) { |
| 133 | QDir theDir(absDir); |
| 134 | for (const QString &fn : theDir.entryList(nameFilters: QStringList(wildcard))) |
| 135 | if (fn != "."_L1 && fn != ".."_L1 ) |
| 136 | result << absDir + u'/' + fn; |
| 137 | } // else if (acceptMissing) |
| 138 | } |
| 139 | } |
| 140 | next: ; |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | ProFileEvaluator::TemplateType ProFileEvaluator::templateType() const |
| 146 | { |
| 147 | const ProStringList &templ = d->values(variableName: ProKey("TEMPLATE" )); |
| 148 | if (templ.size() >= 1) { |
| 149 | const QString &t = templ.at(i: 0).toQString(); |
| 150 | if (!t.compare(other: "app"_L1 , cs: Qt::CaseInsensitive)) |
| 151 | return TT_Application; |
| 152 | if (!t.compare(other: "lib"_L1 , cs: Qt::CaseInsensitive)) |
| 153 | return TT_Library; |
| 154 | if (!t.compare(other: "script"_L1 , cs: Qt::CaseInsensitive)) |
| 155 | return TT_Script; |
| 156 | if (!t.compare(other: "aux"_L1 , cs: Qt::CaseInsensitive)) |
| 157 | return TT_Aux; |
| 158 | if (!t.compare(other: "subdirs"_L1 , cs: Qt::CaseInsensitive)) |
| 159 | return TT_Subdirs; |
| 160 | } |
| 161 | return TT_Unknown; |
| 162 | } |
| 163 | |
| 164 | bool ProFileEvaluator::loadNamedSpec(const QString &specDir, bool hostSpec) |
| 165 | { |
| 166 | d->m_qmakespec = specDir; |
| 167 | d->m_hostBuild = hostSpec; |
| 168 | |
| 169 | d->updateMkspecPaths(); |
| 170 | return d->loadSpecInternal(); |
| 171 | } |
| 172 | |
| 173 | bool ProFileEvaluator::accept(ProFile *pro, QMakeEvaluator::LoadFlags flags) |
| 174 | { |
| 175 | return d->visitProFile(pro, type: QMakeHandler::EvalProjectFile, flags) == QMakeEvaluator::ReturnTrue; |
| 176 | } |
| 177 | |
| 178 | QString ProFileEvaluator::propertyValue(const QString &name) const |
| 179 | { |
| 180 | return d->m_option->propertyValue(name: ProKey(name)).toQString(); |
| 181 | } |
| 182 | |
| 183 | QString ProFileEvaluator::resolvedMkSpec() const |
| 184 | { |
| 185 | return d->m_qmakespec; |
| 186 | } |
| 187 | |
| 188 | #ifdef PROEVALUATOR_CUMULATIVE |
| 189 | void ProFileEvaluator::setCumulative(bool on) |
| 190 | { |
| 191 | d->m_cumulative = on; |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | void ProFileEvaluator::(const QHash<QString, QStringList> &) |
| 196 | { |
| 197 | ProValueMap map; |
| 198 | for (auto it = extraVars.cbegin(), end = extraVars.cend() ; it != end; ++it) |
| 199 | map.insert(key: ProKey(it.key()), value: ProStringList(it.value())); |
| 200 | d->setExtraVars(map); |
| 201 | } |
| 202 | |
| 203 | void ProFileEvaluator::(const QStringList &) |
| 204 | { |
| 205 | d->setExtraConfigs(ProStringList(extraConfigs)); |
| 206 | } |
| 207 | |
| 208 | void ProFileEvaluator::setOutputDir(const QString &dir) |
| 209 | { |
| 210 | d->m_outputDir = dir; |
| 211 | } |
| 212 | |
| 213 | QT_END_NAMESPACE |
| 214 | |