summaryrefslogtreecommitdiff
path: root/src/lib/pkgconfig/pcpackage.cpp
blob: cba783708c1c11e50fcc6b86b7205b997d764756 (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
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
/****************************************************************************
**
** Copyright (C) 2021 Ivan Komissarov (abbapoh@gmail.com)
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "pcpackage.h"

#include <algorithm>

namespace qbs {

using ComparisonType = PcPackage::RequiredVersion::ComparisonType;

std::string_view PcPackage::Flag::typeToString(Type t)
{
    switch (t) {
    case Type::LibraryName: return "LibraryName";
    case Type::StaticLibraryName: return "StaticLibraryName";
    case Type::LibraryPath: return "LibraryPath";
    case Type::Framework: return "Framework";
    case Type::FrameworkPath: return "FrameworkPath";
    case Type::LinkerFlag: return "LinkerFlag";
    case Type::IncludePath: return "IncludePath";
    case Type::SystemIncludePath: return "SystemIncludePath";
    case Type::DirAfterIncludePath: return "DirAfterIncludePath";
    case Type::Define: return "Define";
    case Type::CompilerFlag: return "CompilerFlag";
    }
    return {};
}

std::optional<PcPackage::Flag::Type> PcPackage::Flag::typeFromString(std::string_view s)
{
    if (s == "LibraryName")
        return Type::LibraryName;
    else if (s == "StaticLibraryName")
        return Type::StaticLibraryName;
    else if (s == "LibraryPath")
        return Type::LibraryPath;
    else if (s == "Framework")
        return Type::Framework;
    else if (s == "FrameworkPath")
        return Type::FrameworkPath;
    else if (s == "LinkerFlag")
        return Type::LinkerFlag;
    else if (s == "IncludePath")
        return Type::IncludePath;
    else if (s == "SystemIncludePath")
        return Type::SystemIncludePath;
    else if (s == "DirAfterIncludePath")
        return Type::DirAfterIncludePath;
    else if (s == "Define")
        return Type::Define;
    else if (s == "CompilerFlag")
        return Type::CompilerFlag;
    return std::nullopt;
}

std::string_view PcPackage::RequiredVersion::comparisonToString(ComparisonType t)
{
    switch (t) {
    case ComparisonType::LessThan: return "LessThan";
    case ComparisonType::GreaterThan: return "GreaterThan";
    case ComparisonType::LessThanEqual: return "LessThanEqual";
    case ComparisonType::GreaterThanEqual: return "GreaterThanEqual";
    case ComparisonType::Equal: return "Equal";
    case ComparisonType::NotEqual: return "NotEqual";
    case ComparisonType::AlwaysMatch: return "AlwaysMatch";
    }
    return {};
}

std::optional<ComparisonType> PcPackage::RequiredVersion::comparisonFromString(std::string_view s)
{
    if (s == "LessThan")
        return ComparisonType::LessThan;
    else if (s == "GreaterThan")
        return ComparisonType::GreaterThan;
    else if (s == "LessThanEqual")
        return ComparisonType::LessThanEqual;
    else if (s == "GreaterThanEqual")
        return ComparisonType::GreaterThanEqual;
    else if (s == "Equal")
        return ComparisonType::Equal;
    else if (s == "NotEqual")
        return ComparisonType::NotEqual;
    else if (s == "AlwaysMatch")
        return ComparisonType::AlwaysMatch;
    return std::nullopt;
}

PcPackage PcPackage::prependSysroot(std::string_view sysroot) &&
{
    PcPackage package(std::move(*this));

    const auto doAppend = [](std::vector<Flag> flags, std::string_view sysroot)
    {
        if (sysroot.empty())
            return flags;
        for (auto &flag : flags) {
            if (flag.type == Flag::Type::IncludePath
                    || flag.type == Flag::Type::SystemIncludePath
                    || flag.type == Flag::Type::DirAfterIncludePath
                    || flag.type == Flag::Type::LibraryPath) {
                flag.value = std::string(sysroot) + std::move(flag.value);
            }
        }
        return flags;
    };

    package.libs = doAppend(std::move(package.libs), sysroot);
    package.libsPrivate = doAppend(std::move(package.libsPrivate), sysroot);
    package.cflags = doAppend(std::move(package.cflags), sysroot);
    return package;
}

PcPackage PcPackage::removeSystemLibraryPaths(
        const std::unordered_set<std::string> &libraryPaths) &&
{
    PcPackage package(std::move(*this));
    if (libraryPaths.empty())
        return package;

    const auto doRemove = [&libraryPaths](std::vector<Flag> flags)
    {
        const auto predicate = [&libraryPaths](const Flag &flag)
        {
            return flag.type == Flag::Type::LibraryPath && libraryPaths.count(flag.value);
        };
        flags.erase(std::remove_if(flags.begin(), flags.end(), predicate), flags.end());
        return flags;
    };
    package.libs = doRemove(package.libs);
    package.libsPrivate = doRemove(package.libsPrivate);
    return package;
}

} // namespace qbs