aboutsummaryrefslogtreecommitdiff
path: root/paludis/qa/dep_flags_check.cc
blob: 8c977c695d0ceaec208c08658be27cfaf82265b8 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/dep_spec.hh>
#include <paludis/package_database_entry.hh>
#include <paludis/environment.hh>
#include <paludis/portage_dep_parser.hh>
#include <paludis/qa/dep_flags_check.hh>
#include <paludis/util/iterator.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/qa/qa_environment.hh>
#include <paludis/util/visitor-impl.hh>
#include <set>
#include <algorithm>

using namespace paludis;
using namespace paludis::qa;

namespace
{
    struct Checker :
        ConstVisitor<GenericSpecTree>,
        ConstVisitor<GenericSpecTree>::VisitConstSequence<Checker, AllDepSpec>,
        ConstVisitor<GenericSpecTree>::VisitConstSequence<Checker, AnyDepSpec>
    {
        using ConstVisitor<GenericSpecTree>::VisitConstSequence<Checker, AllDepSpec>::visit_sequence;
        using ConstVisitor<GenericSpecTree>::VisitConstSequence<Checker, AnyDepSpec>::visit_sequence;

        CheckResult & result;
        const std::string role;
        const Environment * const env;
        const std::set<UseFlagName> & iuse;

        Checker(CheckResult & rr, const std::string & r,
                const Environment * const e, const std::set<UseFlagName> & i) :
            result(rr),
            role(r),
            env(e),
            iuse(i)
        {
        }

        void visit_leaf(const PackageDepSpec &)
        {
        }

        void visit_sequence(const UseDepSpec & u,
                GenericSpecTree::ConstSequenceIterator cur,
                GenericSpecTree::ConstSequenceIterator end)
        {
            tr1::shared_ptr<const Repository> r(env->package_database()->fetch_repository(env->package_database()->
                        favourite_repository()));

            if (! r->use_interface)
                throw InternalError(PALUDIS_HERE, "Confused: Repository does not have a UseInterface.");

            if (r->use_interface->arch_flags()->count(u.flag()))
            {
                if (role == "DEPEND" || role == "RDEPEND" || role == "PDEPEND")
                {
                    if (u.inverse())
                        result << Message(qal_maybe, "Inverse arch flag '" + stringify(u.flag()) +
                                "' in " + role);
                }
                else if (role != "SRC_URI")
                    result << Message(qal_major, "Arch flag '" + stringify(u.flag()) +
                            "' in " + role);
            }
            else
            {
                do
                {
                    bool found_match(false);
                    tr1::shared_ptr<const UseFlagNameCollection> c(r->use_interface->use_expand_prefixes());

                    for (UseFlagNameCollection::Iterator i(c->begin()), i_end(c->end()) ;
                            i != i_end ; ++i)
                    {
                        std::string prefix(stringify(*i)), flag(stringify(u.flag()));
                        if (0 == flag.compare(0, prefix.length(), prefix))
                        {
                            found_match = true;
                            break;
                        }
                    }

                    if (found_match)
                        break;

                    if (iuse.end() == iuse.find(u.flag()))
                        result << Message(qal_major, "Conditional flag '" + stringify(u.flag()) +
                                "' in " + role + " not in IUSE");
                } while (false);
            }

            std::for_each(cur, end, accept_visitor(*this));
        }

        void visit_leaf(const PlainTextDepSpec &)
        {
        }

        void visit_leaf(const URIDepSpec &)
        {
        }

        void visit_leaf(const BlockDepSpec &)
        {
        }
    };
}

DepFlagsCheck::DepFlagsCheck()
{
}

CheckResult
DepFlagsCheck::operator() (const EbuildCheckData & e) const
{
    CheckResult result(stringify(e.name) + "-" + stringify(e.version),
            identifier());

    try
    {
        PackageDatabaseEntry ee(e.name, e.version,
                e.environment->main_repository()->name());
        tr1::shared_ptr<const VersionMetadata> metadata(
                e.environment->package_database()->fetch_repository(ee.repository)->version_metadata(ee.name, ee.version));

        if (metadata->ebuild_interface == 0)
            result << Message(qal_fatal, "Not an ebuild");
        else
        {
            std::set<UseFlagName> iuse;
            std::copy(metadata->ebuild_interface->iuse()->begin(), metadata->ebuild_interface->iuse()->end(),
                    transform_inserter(std::inserter(iuse, iuse.begin()),
                        SelectMember<IUseFlag, UseFlagName, &IUseFlag::flag>()));
            iuse.insert(UseFlagName("bootstrap"));
            iuse.insert(UseFlagName("build"));

            Checker depend_checker(result, "DEPEND", e.environment, iuse);
            metadata->deps_interface->build_depend()->accept(depend_checker);

            Checker rdepend_checker(result, "RDEPEND", e.environment, iuse);
            metadata->deps_interface->run_depend()->accept(rdepend_checker);

            Checker pdepend_checker(result, "PDEPEND", e.environment, iuse);
            metadata->deps_interface->post_depend()->accept(pdepend_checker);

            Checker provide_checker(result, "PROVIDE", e.environment, iuse);
            metadata->ebuild_interface->provide()->accept(provide_checker);

            Checker license_checker(result, "LICENSE", e.environment, iuse);
            metadata->license_interface->license()->accept(license_checker);

            Checker src_uri_checker(result, "SRC_URI", e.environment, iuse);
            metadata->ebuild_interface->src_uri()->accept(src_uri_checker);
        }
    }
    catch (const InternalError &)
    {
        throw;
    }
    catch (const Exception & err)
    {
        result << Message(qal_fatal, "Caught Exception '" + err.message() + "' ("
                + err.what() + ")");
    }

    return result;
}

const std::string &
DepFlagsCheck::identifier()
{
    static const std::string id("dep_flags");
    return id;
}