aboutsummaryrefslogtreecommitdiff
path: root/paludis/environments/paludis/use_conf.cc
blob: c1a39174be98de693ebf003f0a5bf2fca12af4e9 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 2008 Ciaran McCreesh
 *
 * 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 "use_conf.hh"
#include <paludis/environment.hh>
#include <paludis/name.hh>
#include <paludis/dep_spec.hh>
#include <paludis/match_package.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/options.hh>
#include <paludis/package_id.hh>
#include <paludis/environments/paludis/paludis_environment.hh>
#include <paludis/environments/paludis/bashable_conf.hh>
#include <paludis/util/log.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/util/strip.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/set.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/hashes.hh>
#include <tr1/unordered_map>
#include <algorithm>
#include <list>
#include <vector>

using namespace paludis;
using namespace paludis::paludis_environment;

typedef std::tr1::unordered_map<UseFlagName, UseFlagState, Hash<UseFlagName> > UseFlagWithStateMap;
typedef std::list<std::string> MinusStarPrefixList;
typedef std::pair<UseFlagWithStateMap, MinusStarPrefixList> UseInfo;
typedef std::pair<std::tr1::shared_ptr<const PackageDepSpec>, UseInfo> PDSWithUseInfo;
typedef std::pair<std::tr1::shared_ptr<const SetSpecTree::ConstItem>, UseInfo> DSWithUseInfo;
typedef std::list<PDSWithUseInfo> PDSWithUseInfoList;
typedef std::tr1::unordered_map<QualifiedPackageName, PDSWithUseInfoList, Hash<QualifiedPackageName> > Qualified;
typedef std::list<PDSWithUseInfo> Unqualified;
typedef std::tr1::unordered_map<SetName, DSWithUseInfo, Hash<SetName> > Sets;

namespace paludis
{
    template<>
    struct Implementation<UseConf>
    {
        const PaludisEnvironment * const env;
        Qualified qualified;
        Unqualified unqualified;
        mutable Mutex set_mutex;
        mutable Sets sets;

        Implementation(const PaludisEnvironment * const e) :
            env(e)
        {
        }
    };
}

UseConf::UseConf(const PaludisEnvironment * const e) :
    PrivateImplementationPattern<UseConf>(new Implementation<UseConf>(e))
{
}

UseConf::~UseConf()
{
}

void
UseConf::add(const FSEntry & filename)
{
    Context context("When adding source '" + stringify(filename) + "' as a use file:");

    std::tr1::shared_ptr<LineConfigFile> f(make_bashable_conf(filename));
    if (! f)
        return;

    for (LineConfigFile::ConstIterator line(f->begin()), line_end(f->end()) ;
            line != line_end ; ++line)
    {
        std::vector<std::string> tokens;
        tokenise_whitespace_quoted(*line, std::back_inserter(tokens));

        if (tokens.size() < 2)
            continue;

        if ("*" == tokens.at(0))
        {
            Log::get_instance()->message("paludis_environment.use_conf.deprecated", ll_warning, lc_context)
                << "Use of token '*' is deprecated, use '*/*' instead";
            tokens.at(0) = "*/*";
        }

        if (std::string::npos == tokens.at(0).find("/"))
        {
            Sets::iterator i(_imp->sets.insert(std::make_pair(SetName(tokens.at(0)), DSWithUseInfo())).first);
            std::string prefix_upper, prefix_lower;
            for (std::vector<std::string>::const_iterator t(next(tokens.begin())), t_end(tokens.end()) ;
                    t != t_end ; ++t)
            {
                if (*t == "-*")
                    i->second.second.second.push_back(strip_trailing(prefix_lower, "_"));
                else if ('-' == t->at(0))
                    i->second.second.first.insert(std::make_pair(
                                UseFlagName(prefix_lower + t->substr(1)), use_disabled)).first->second = use_disabled;
                else if (':' == t->at(t->length() - 1))
                {
                    std::transform(t->begin(), previous(t->end()), std::back_inserter(prefix_lower), &::tolower);
                    std::transform(t->begin(), previous(t->end()), std::back_inserter(prefix_upper), &::toupper);
                    prefix_lower.append("_");
                    prefix_upper.append("_");
                }
                else
                    i->second.second.first.insert(std::make_pair(
                                UseFlagName(prefix_lower + *t), use_enabled)).first->second = use_enabled;
            }
        }
        else
        {
            std::tr1::shared_ptr<PackageDepSpec> d(new PackageDepSpec(parse_user_package_dep_spec(
                            tokens.at(0), _imp->env,
                            UserPackageDepSpecOptions() + updso_allow_wildcards + updso_no_disambiguation)));

            if (d->additional_requirements_ptr())
            {
                Log::get_instance()->message("paludis_environment.use_conf.bad_spec", ll_warning, lc_context)
                    << "Dependency specification '" << stringify(*d)
                    << "' includes use requirements, which cannot be used in use.conf";
                continue;
            }

            if (d->package_ptr())
            {
                Qualified::iterator ii(_imp->qualified.insert(std::make_pair(*d->package_ptr(), PDSWithUseInfoList())).first);
                PDSWithUseInfoList::iterator i(ii->second.insert(ii->second.end(), PDSWithUseInfo(d, UseInfo())));

                std::string prefix_upper, prefix_lower;
                for (std::vector<std::string>::const_iterator t(next(tokens.begin())), t_end(tokens.end()) ;
                        t != t_end ; ++t)
                {
                    if (*t == "-*")
                        i->second.second.push_back(strip_trailing(prefix_lower, "_"));
                    else if ('-' == t->at(0))
                        i->second.first.insert(std::make_pair(
                                    UseFlagName(prefix_lower + t->substr(1)), use_disabled)).first->second = use_disabled;
                    else if (':' == t->at(t->length() - 1))
                    {
                        std::transform(t->begin(), previous(t->end()), std::back_inserter(prefix_lower), &::tolower);
                        std::transform(t->begin(), previous(t->end()), std::back_inserter(prefix_upper), &::toupper);
                        prefix_lower.append("_");
                        prefix_upper.append("_");
                    }
                    else
                        i->second.first.insert(std::make_pair(UseFlagName(prefix_lower + *t), use_enabled)).first->second = use_enabled;
                }
            }
            else
            {
                Unqualified::iterator i(_imp->unqualified.insert(_imp->unqualified.end(), PDSWithUseInfo(d, UseInfo())));

                std::string prefix_upper, prefix_lower;
                for (std::vector<std::string>::const_iterator t(next(tokens.begin())), t_end(tokens.end()) ;
                        t != t_end ; ++t)
                {
                    if (*t == "-*")
                        i->second.second.push_back(strip_trailing(prefix_lower, "_"));
                    else if ('-' == t->at(0))
                        i->second.first.insert(
                                std::make_pair(UseFlagName(prefix_lower + t->substr(1)), use_disabled)).first->second = use_disabled;
                    else if (':' == t->at(t->length() - 1))
                    {
                        std::transform(t->begin(), previous(t->end()), std::back_inserter(prefix_lower), &::tolower);
                        std::transform(t->begin(), previous(t->end()), std::back_inserter(prefix_upper), &::toupper);
                        prefix_lower.append("_");
                        prefix_upper.append("_");
                    }
                    else
                        i->second.first.insert(std::make_pair(UseFlagName(prefix_lower + *t), use_enabled)).first->second = use_enabled;
                }
            }
        }
    }
}

UseFlagState
UseConf::query(const UseFlagName & f, const PackageID & e) const
{
    Context context("When checking state of flag '" + stringify(f) + "' for '" + stringify(e) + "':");

    UseFlagState result(use_unspecified);

    bool ignore_empty_minus_star(false);
    if ((*e.repository())[k::use_interface()])
    {
        std::tr1::shared_ptr<const UseFlagNameSet> prefixes((*e.repository())[k::use_interface()]->use_expand_prefixes());
        for (UseFlagNameSet::ConstIterator p(prefixes->begin()), p_end(prefixes->end()) ;
                p != p_end ; ++p)
            if (0 == p->data().compare(0, p->data().length(), stringify(f), 0, p->data().length()))
            {
                ignore_empty_minus_star = true;
                break;
            }
    }

    /* highest priority: specific */
    Qualified::const_iterator q(_imp->qualified.find(e.name()));
    if (_imp->qualified.end() != q)
    {
        for (PDSWithUseInfoList::const_iterator p(q->second.begin()), p_end(q->second.end()) ; p != p_end ; ++p)
        {
            if (! match_package(*_imp->env, *p->first, e))
                continue;

            UseFlagWithStateMap::const_iterator i(p->second.first.find(f));
            if (p->second.first.end() != i)
                result = i->second;

            if (use_unspecified == result)
                for (MinusStarPrefixList::const_iterator m(p->second.second.begin()), m_end(p->second.second.end()) ;
                        m != m_end ; ++m)
                {
                    if (m->empty() && ignore_empty_minus_star)
                        continue;

                    if (0 == m->compare(0, m->length(), stringify(f), 0, m->length()))
                    {
                        result = use_disabled;
                        break;
                    }
                }
        }
    }

    if (use_unspecified != result)
        return result;

    /* next: named sets */
    for (Sets::iterator r(_imp->sets.begin()), r_end(_imp->sets.end()) ; r != r_end ; ++r)
    {
        Lock lock(_imp->set_mutex);
        if (! r->second.first)
        {
            r->second.first = _imp->env->set(r->first);
            if (! r->second.first)
            {
                Log::get_instance()->message("paludis_environment.use_conf.unknown_set", ll_warning, lc_no_context) << "Set name '"
                    << r->first << "' does not exist";
                r->second.first.reset(new ConstTreeSequence<SetSpecTree, AllDepSpec>(
                            std::tr1::shared_ptr<AllDepSpec>(new AllDepSpec)));
            }
        }

        if (! match_package_in_set(*_imp->env, *r->second.first, e))
            continue;

        UseFlagWithStateMap::const_iterator i(r->second.second.first.find(f));
        if (i != r->second.second.first.end())
            result = i->second;

        if (use_unspecified == result)
            for (MinusStarPrefixList::const_iterator m(r->second.second.second.begin()), m_end(r->second.second.second.end()) ;
                    m != m_end ; ++m)
            {
                if (m->empty() && ignore_empty_minus_star)
                    continue;

                if (0 == m->compare(0, m->length(), stringify(f), 0, m->length()))
                {
                    result = use_disabled;
                    break;
                }
            }
    }

    if (use_unspecified != result)
        return result;

    /* last: unspecific */

    for (Unqualified::const_iterator p(_imp->unqualified.begin()), p_end(_imp->unqualified.end()) ; p != p_end ; ++p)
    {
        if (! match_package(*_imp->env, *p->first, e))
            continue;

        UseFlagWithStateMap::const_iterator i(p->second.first.find(f));
        if (p->second.first.end() != i)
            result = i->second;

        if (use_unspecified == result)
            for (MinusStarPrefixList::const_iterator m(p->second.second.begin()), m_end(p->second.second.end()) ;
                    m != m_end ; ++m)
            {
                if (m->empty() && ignore_empty_minus_star)
                    continue;

                if (0 == m->compare(0, m->length(), stringify(f), 0, m->length()))
                {
                    result = use_disabled;
                    break;
                }
            }
    }

    return result;
}

std::tr1::shared_ptr<const UseFlagNameSet>
UseConf::known_use_expand_names(const UseFlagName & prefix, const PackageID & e) const
{
    Context context("When loading known use expand names for prefix '" + stringify(prefix) + ":");

    std::tr1::shared_ptr<UseFlagNameSet> result(new UseFlagNameSet);
    std::string prefix_lower;
    std::transform(prefix.data().begin(), prefix.data().end(), std::back_inserter(prefix_lower), &::tolower);
    prefix_lower.append("_");

    Qualified::const_iterator q(_imp->qualified.find(e.name()));
    if (_imp->qualified.end() != q)
        for (PDSWithUseInfoList::const_iterator p(q->second.begin()), p_end(q->second.end()) ; p != p_end ; ++p)
        {
            if (! match_package(*_imp->env, *p->first, e))
                continue;

            for (UseFlagWithStateMap::const_iterator i(p->second.first.begin()), i_end(p->second.first.end()) ;
                    i != i_end ; ++i)
                if (0 == i->first.data().compare(0, prefix_lower.length(), prefix_lower))
                    result->insert(i->first);
        }

    {
        Lock lock(_imp->set_mutex);
        for (Sets::iterator r(_imp->sets.begin()), r_end(_imp->sets.end()) ; r != r_end ; ++r)
        {
            if (! r->second.first)
            {
                r->second.first = _imp->env->set(r->first);
                if (! r->second.first)
                {
                    Log::get_instance()->message("paludis_environment.use_conf.unknown_set", ll_warning, lc_no_context) << "Set name '"
                        << r->first << "' does not exist";
                    r->second.first.reset(new ConstTreeSequence<SetSpecTree, AllDepSpec>(
                                std::tr1::shared_ptr<AllDepSpec>(new AllDepSpec)));
                }
            }

            if (! match_package_in_set(*_imp->env, *r->second.first, e))
                continue;

            for (UseFlagWithStateMap::const_iterator i(r->second.second.first.begin()), i_end(r->second.second.first.end()) ;
                    i != i_end ; ++i)
                if (0 == i->first.data().compare(0, prefix_lower.length(), prefix_lower))
                    result->insert(i->first);
        }
    }

    for (Unqualified::const_iterator p(_imp->unqualified.begin()), p_end(_imp->unqualified.end()) ; p != p_end ; ++p)
    {
        if (! match_package(*_imp->env, *p->first, e))
            continue;

        for (UseFlagWithStateMap::const_iterator i(p->second.first.begin()), i_end(p->second.first.end()) ;
                i != i_end ; ++i)
            if (0 == i->first.data().compare(0, prefix_lower.length(), prefix_lower))
                result->insert(i->first);
    }

    return result;
}