File indexing completed on 2024-05-19 04:41:20

0001 /*
0002     SPDX-FileCopyrightText: 2019 Daniel Mensinger <daniel@mensinger-ka.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "mesontargets.h"
0008 
0009 #include <debug.h>
0010 
0011 #include <QJsonArray>
0012 #include <QJsonObject>
0013 
0014 #include <algorithm>
0015 
0016 using namespace std;
0017 using namespace KDevelop;
0018 
0019 // MesonTargetSources
0020 
0021 MesonTargetSources::MesonTargetSources(const QJsonObject& json, MesonTarget* target)
0022     : m_target(target)
0023 {
0024     fromJSON(json);
0025 }
0026 
0027 MesonTargetSources::~MesonTargetSources() {}
0028 
0029 QString MesonTargetSources::language() const
0030 {
0031     return m_language;
0032 }
0033 
0034 QStringList MesonTargetSources::compiler() const
0035 {
0036     return m_compiler;
0037 }
0038 
0039 QStringList MesonTargetSources::paramerters() const
0040 {
0041     return m_paramerters;
0042 }
0043 
0044 KDevelop::Path::List MesonTargetSources::sources() const
0045 {
0046     return m_sources;
0047 }
0048 
0049 KDevelop::Path::List MesonTargetSources::generatedSources() const
0050 {
0051     return m_generatedSources;
0052 }
0053 
0054 KDevelop::Path::List MesonTargetSources::allSources() const
0055 {
0056     return m_sources + m_generatedSources;
0057 }
0058 
0059 KDevelop::Path::List MesonTargetSources::includeDirs() const
0060 {
0061     return m_includeDirs;
0062 }
0063 
0064 QHash<QString, QString> MesonTargetSources::defines() const
0065 {
0066     return m_defines;
0067 }
0068 
0069 QStringList MesonTargetSources::extraArgs() const
0070 {
0071     return m_extraArgs;
0072 }
0073 
0074 MesonTarget* MesonTargetSources::target()
0075 {
0076     return m_target;
0077 }
0078 
0079 void MesonTargetSources::fromJSON(const QJsonObject& json)
0080 {
0081     m_language = json[QStringLiteral("language")].toString();
0082 
0083     QJsonArray comp = json[QStringLiteral("compiler")].toArray();
0084     QJsonArray param = json[QStringLiteral("parameters")].toArray();
0085     QJsonArray src = json[QStringLiteral("sources")].toArray();
0086     QJsonArray gensrc = json[QStringLiteral("generated_sources")].toArray();
0087 
0088     transform(begin(comp), end(comp), back_inserter(m_compiler), [](const auto& x) { return x.toString(); });
0089     transform(begin(param), end(param), back_inserter(m_paramerters), [](const auto& x) { return x.toString(); });
0090     transform(begin(src), end(src), back_inserter(m_sources), [](const auto& x) { return Path(x.toString()); });
0091     transform(begin(gensrc), end(gensrc), back_inserter(m_generatedSources),
0092               [](const auto& x) { return Path(x.toString()); });
0093 
0094     splitParamerters();
0095     qCDebug(KDEV_Meson) << "    - language:" << m_language << "has" << m_sources.count() + m_generatedSources.count()
0096                         << "files with" << m_includeDirs.count() << "include directories and" << m_defines.count()
0097                         << "defines";
0098 }
0099 
0100 void MesonTargetSources::splitParamerters()
0101 {
0102     for (const QString& i : m_paramerters) {
0103         [&]() {
0104             for (auto j : { QStringLiteral("-I"), QStringLiteral("/I"), QStringLiteral("-isystem") }) {
0105                 if (i.startsWith(j)) {
0106                     m_includeDirs << Path(i.mid(j.size()));
0107                     return;
0108                 }
0109             }
0110 
0111             for (auto j : { QStringLiteral("-D"), QStringLiteral("/D") }) {
0112                 if (i.startsWith(j)) {
0113                     QString define = i.mid(j.size());
0114                     QString name = define;
0115                     QString value;
0116 
0117                     int equalPos = define.indexOf(QLatin1Char('='));
0118                     if (equalPos > 0) {
0119                         name = define.left(equalPos);
0120                         value = define.mid(equalPos + 1);
0121                     }
0122 
0123                     m_defines[name] = value;
0124                     return;
0125                 }
0126             }
0127 
0128             m_extraArgs << i;
0129         }();
0130     }
0131 }
0132 
0133 // MesonTarget
0134 
0135 MesonTarget::MesonTarget(const QJsonObject& json)
0136 {
0137     fromJSON(json);
0138 }
0139 
0140 MesonTarget::~MesonTarget() {}
0141 
0142 QString MesonTarget::name() const
0143 {
0144     return m_name;
0145 }
0146 
0147 QString MesonTarget::type() const
0148 {
0149     return m_type;
0150 }
0151 
0152 KDevelop::Path MesonTarget::definedIn() const
0153 {
0154     return m_definedIn;
0155 }
0156 
0157 KDevelop::Path::List MesonTarget::filename() const
0158 {
0159     return m_filename;
0160 }
0161 
0162 bool MesonTarget::buildByDefault() const
0163 {
0164     return m_buildByDefault;
0165 }
0166 
0167 bool MesonTarget::installed() const
0168 {
0169     return m_installed;
0170 }
0171 
0172 QVector<MesonSourcePtr> MesonTarget::targetSources()
0173 {
0174     return m_targetSources;
0175 }
0176 
0177 void MesonTarget::fromJSON(const QJsonObject& json)
0178 {
0179     m_name = json[QStringLiteral("name")].toString();
0180     m_type = json[QStringLiteral("type")].toString();
0181     m_definedIn = Path(json[QStringLiteral("defined_in")].toString());
0182     m_buildByDefault = json[QStringLiteral("build_by_default")].toBool();
0183     m_installed = json[QStringLiteral("installed")].toBool();
0184 
0185     QJsonArray files = json[QStringLiteral("filename")].toArray();
0186     transform(begin(files), end(files), back_inserter(m_filename), [](const auto& x) { return Path(x.toString()); });
0187 
0188     qCDebug(KDEV_Meson) << "  - " << m_type << m_name;
0189 
0190     for (const auto& i : json[QStringLiteral("target_sources")].toArray()) {
0191         m_targetSources << make_shared<MesonTargetSources>(i.toObject(), this);
0192     }
0193 }
0194 
0195 // MesonTargets
0196 
0197 MesonTargets::MesonTargets(const QJsonArray& json)
0198 {
0199     fromJSON(json);
0200 }
0201 
0202 MesonTargets::~MesonTargets() {}
0203 
0204 QVector<MesonTargetPtr> MesonTargets::targets()
0205 {
0206     return m_targets;
0207 }
0208 
0209 MesonSourcePtr MesonTargets::fileSource(KDevelop::Path p)
0210 {
0211     auto it = m_sourceHash.find(p);
0212     if (it == end(m_sourceHash)) {
0213         return nullptr;
0214     }
0215 
0216     return *it;
0217 }
0218 
0219 MesonSourcePtr MesonTargets::operator[](KDevelop::Path p)
0220 {
0221     return fileSource(p);
0222 }
0223 
0224 void MesonTargets::fromJSON(const QJsonArray& json)
0225 {
0226     qCDebug(KDEV_Meson) << "MINTRO: Loading targets from json...";
0227     for (const auto& i : json) {
0228         m_targets << make_shared<MesonTarget>(i.toObject());
0229     }
0230 
0231     buildHashMap();
0232     qCDebug(KDEV_Meson) << "MINTRO: Loaded" << m_targets.count() << "targets with" << m_sourceHash.count()
0233                         << "total files";
0234 }
0235 
0236 void MesonTargets::buildHashMap()
0237 {
0238     for (auto& i : m_targets) {
0239         for (auto j : i->targetSources()) {
0240             for (auto k : j->allSources()) {
0241                 m_sourceHash[k] = j;
0242             }
0243         }
0244     }
0245 }