aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/mesonprojectmanager/target.h
blob: bae7ab15ce364bf0d04faa895c5da961465f055a (plain)
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
// Copyright (C) 2020 Alexis Jeandet.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <utils/algorithm.h>
#include <utils/filepath.h>

#include <QDir>
#include <QVariant>

#include <optional>

namespace MesonProjectManager::Internal {

inline QStringList cleanPath(QStringList &&paths)
{
    return Utils::transform(paths, QDir::cleanPath);
}

struct Target
{
    enum class Type {
        executable,
        run,
        custom,
        sharedLibrary,
        sharedModule,
        staticLibrary,
        jar,
        unknown
    };
    struct SourceGroup
    {
        const QString language;
        const QStringList compiler;
        const QStringList parameters;
        const QStringList sources;
        const QStringList generatedSources;
        SourceGroup(QString &&language,
                    QStringList &&compiler,
                    QStringList &&parameters,
                    QStringList &&sources,
                    QStringList &&generatedSources)
            : language{std::move(language)}
            , compiler{std::move(compiler)}
            , parameters{std::move(parameters)}
            , sources{cleanPath(std::move(sources))}
            , generatedSources{cleanPath(std::move(generatedSources))}
        {}
    };
    using SourceGroupList = std::vector<SourceGroup>;
    const Type type;
    const QString name;
    const QString id;
    const QString definedIn;
    const QStringList fileName;
    const QStringList extraFiles;
    const std::optional<QString> subproject;
    const SourceGroupList sources;
    const bool buildByDefault;

    static Type toType(const QString &typeStr)
    {
        if (typeStr == "executable")
            return Type::executable;
        if (typeStr == "static library")
            return Type::staticLibrary;
        if (typeStr == "shared library")
            return Type::sharedLibrary;
        if (typeStr == "shared module")
            return Type::sharedModule;
        if (typeStr == "custom")
            return Type::custom;
        if (typeStr == "run")
            return Type::run;
        if (typeStr == "jar")
            return Type::jar;
        return Type::unknown;
    }

    static QString typeToString(const Type type)
    {
        switch (type) {
        case Type::executable:
            return QStringLiteral("executable");
        case Type::staticLibrary:
            return QStringLiteral("static library");
        case Type::sharedLibrary:
            return QStringLiteral("shared library");
        case Type::sharedModule:
            return QStringLiteral("shared module");
        case Type::custom:
            return QStringLiteral("custom");
        case Type::run:
            return QStringLiteral("run");
        case Type::jar:
            return QStringLiteral("jar");
        case Type::unknown:
            return QStringLiteral("unknown");
        }
        return QStringLiteral("unknown");
    }

    inline static QString unique_name(const Target &target, const Utils::FilePath &projectDir)
    {
        auto relative_path = Utils::FilePath::fromString(target.definedIn).canonicalPath().relativeChildPath(projectDir.canonicalPath()).parentDir();
        if (target.type == Type::sharedModule)
            return relative_path.pathAppended(Utils::FilePath::fromString(target.fileName[0]).fileName()).toUrlishString();
        return relative_path.pathAppended(target.name).toUrlishString();
    }

    Target(const QString &type,
           QString &&name,
           QString &&id,
           QString &&definedIn,
           QStringList &&fileName,
           QStringList &&extraFiles,
           QString &&subproject,
           SourceGroupList &&sources,
           bool buildByDefault)
        : type{toType(type)}
        , name{std::move(name)}
        , id{std::move(id)}
        , definedIn{QDir::cleanPath(definedIn)}
        , fileName{cleanPath(std::move(fileName))}
        , extraFiles{cleanPath(std::move(extraFiles))}
        , subproject{subproject.isNull() ? std::nullopt
                                         : std::optional<QString>{std::move(subproject)}}
        , sources{std::move(sources)}
        , buildByDefault{buildByDefault}
    {}
};

using TargetsList = std::vector<Target>;

template<class function>
void for_each_source_group(const TargetsList &targets, const function &f)
{
    for (const Target &target : targets) {
        for (const Target::SourceGroup &group : target.sources) {
            f(target, group);
        }
    }
}

} // namespace MesonProjectManager::Internal