aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/make_use.cc
blob: c056b16c2f92cc4fb486cf4d275517b9f91f0add (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2010, 2011 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 <paludis/repositories/e/make_use.hh>
#include <paludis/repositories/e/eapi.hh>
#include <paludis/util/log.hh>
#include <paludis/choice.hh>
#include <paludis/metadata_key.hh>
#include <paludis/elike_choices.hh>

#include <algorithm>

using namespace paludis;
using namespace paludis::erepository;

std::string
paludis::erepository::make_use(const Environment * const,
        const ERepositoryID & id,
        std::shared_ptr<const Profile> profile)
{
    if (! id.eapi()->supported())
    {
        Log::get_instance()->message("e.ebuild.unknown_eapi", ll_warning, lc_context)
            << "Can't make the USE string for '" << id << "' because its EAPI is unsupported";
        return "";
    }

    std::string use;

    if (id.choices_key())
    {
        auto choices(id.choices_key()->parse_value());
        for (Choices::ConstIterator k(choices->begin()), k_end(choices->end()) ;
                k != k_end ; ++k)
        {
            if ((*k)->prefix() == canonical_build_options_prefix())
                continue;

            for (Choice::ConstIterator i((*k)->begin()), i_end((*k)->end()) ;
                    i != i_end ; ++i)
                if ((*i)->enabled())
                    use += stringify((*i)->name_with_prefix()) + " ";
        }
    }

    if (! id.eapi()->supported()->ebuild_environment_variables()->env_arch().empty())
        use += profile->environment_variable(id.eapi()->supported()->ebuild_environment_variables()->env_arch()) + " ";

    return use;
}

std::shared_ptr<Map<std::string, std::string> >
paludis::erepository::make_expand(const Environment * const,
        const ERepositoryID & e,
        std::shared_ptr<const Profile> profile)
{
    std::shared_ptr<Map<std::string, std::string> > expand_vars(std::make_shared<Map<std::string, std::string> >());

    if (! e.eapi()->supported())
    {
        Log::get_instance()->message("e.ebuild.unknown_eapi", ll_warning, lc_context)
            << "Can't make the USE_EXPAND strings for '" << e << "' because its EAPI is unsupported";
        return expand_vars;
    }

    if (! e.choices_key())
        return expand_vars;

    auto choices(e.choices_key()->parse_value());

    for (Set<std::string>::ConstIterator x(profile->use_expand()->begin()), x_end(profile->use_expand()->end()) ;
            x != x_end ; ++x)
    {
        expand_vars->insert(stringify(*x), "");

        Choices::ConstIterator k(std::find_if(choices->begin(), choices->end(),
                    std::bind(std::equal_to<std::string>(), *x,
                        std::bind(std::mem_fn(&Choice::raw_name), std::placeholders::_1))));
        if (k == choices->end())
            continue;

        for (Choice::ConstIterator i((*k)->begin()), i_end((*k)->end()) ;
                i != i_end ; ++i)
            if ((*i)->enabled())
            {
                std::string value;
                Map<std::string, std::string>::ConstIterator v(expand_vars->find(stringify(*x)));
                if (expand_vars->end() != v)
                {
                    value = v->second;
                    if (! value.empty())
                        value.append(" ");
                    expand_vars->erase(v);
                }
                value.append(stringify((*i)->unprefixed_name()));
                expand_vars->insert(stringify(*x), value);
            }
    }

    return expand_vars;
}