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

/*
 * Copyright (c) 2010 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/do_pretend_action.hh>
#include <paludis/repositories/e/check_userpriv.hh>
#include <paludis/repositories/e/eapi.hh>
#include <paludis/repositories/e/eapi_phase.hh>
#include <paludis/repositories/e/make_use.hh>
#include <paludis/repositories/e/myoptions_requirements_verifier.hh>
#include <paludis/repositories/e/ebuild.hh>
#include <paludis/repositories/e/can_skip_phase.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/dep_spec_flattener.hh>
#include <paludis/metadata_key.hh>
#include <paludis/environment.hh>
#include <paludis/action.hh>

#include <algorithm>

using namespace paludis;
using namespace paludis::erepository;

bool
paludis::erepository::do_pretend_action(
        const Environment * const env,
        const ERepository * const repo,
        const std::shared_ptr<const ERepositoryID> & id,
        const PretendAction & a)
{
    using namespace std::placeholders;

    Context context("When running pretend for '" + stringify(*id) + "':");

    if (! id->eapi()->supported())
        return false;

    bool result(true);

    if (! id->raw_myoptions_key())
        if (id->eapi()->supported()->ebuild_phases()->ebuild_pretend().empty())
            return result;

    bool userpriv_restrict;
    {
        DepSpecFlattener<PlainTextSpecTree, PlainTextDepSpec> restricts(env);
        if (id->restrict_key())
            id->restrict_key()->value()->top()->accept(restricts);

        userpriv_restrict =
            indirect_iterator(restricts.end()) != std::find_if(indirect_iterator(restricts.begin()), indirect_iterator(restricts.end()),
                    std::bind(std::equal_to<std::string>(), std::bind(std::mem_fn(&StringDepSpec::text), _1), "userpriv")) ||
            indirect_iterator(restricts.end()) != std::find_if(indirect_iterator(restricts.begin()), indirect_iterator(restricts.end()),
                    std::bind(std::equal_to<std::string>(), std::bind(std::mem_fn(&StringDepSpec::text), _1), "nouserpriv"));
    }
    bool userpriv_ok((! userpriv_restrict) && (env->reduced_gid() != getgid()) &&
            check_userpriv(FSPath(repo->params().builddir()), env, id->eapi()->supported()->userpriv_cannot_use_root()));

    std::string use(make_use(env, *id, repo->profile()));
    std::shared_ptr<Map<std::string, std::string> > expand_vars(make_expand(
                env, *id, repo->profile()));

    std::shared_ptr<const FSPathSequence> exlibsdirs(repo->layout()->exlibsdirs(id->name()));

    std::shared_ptr<OutputManager> output_manager;

    if (id->raw_myoptions_key())
    {
        MyOptionsRequirementsVerifier verifier(id);
        id->raw_myoptions_key()->value()->top()->accept(verifier);

        if (verifier.unmet_requirements() && ! verifier.unmet_requirements()->empty())
        {
            EAPIPhases phases(id->eapi()->supported()->ebuild_phases()->ebuild_bad_options());
            if (phases.begin_phases() == phases.end_phases())
                throw InternalError(PALUDIS_HERE, "using myoptions but no ebuild_bad_options phase");

            for (EAPIPhases::ConstIterator phase(phases.begin_phases()), phase_end(phases.end_phases()) ;
                    phase != phase_end ; ++phase)
            {
                if (! output_manager)
                    output_manager = a.options.make_output_manager()(a);

                EbuildCommandParams command_params(make_named_values<EbuildCommandParams>(
                            n::builddir() = repo->params().builddir(),
                            n::clearenv() = phase->option("clearenv"),
                            n::commands() = join(phase->begin_commands(), phase->end_commands(), " "),
                            n::distdir() = repo->params().distdir(),
                            n::ebuild_dir() = repo->layout()->package_directory(id->name()),
                            n::ebuild_file() = id->fs_location_key()->value(),
                            n::eclassdirs() = repo->params().eclassdirs(),
                            n::environment() = env,
                            n::exlibsdirs() = exlibsdirs,
                            n::files_dir() = repo->layout()->package_directory(id->name()) / "files",
                            n::maybe_output_manager() = output_manager,
                            n::package_builddir() = repo->params().builddir() / (stringify(id->name().category()) + "-" + stringify(id->name().package()) + "-" + stringify(id->version()) + "-bad_options"),
                            n::package_id() = id,
                            n::portdir() =
                                (repo->params().master_repositories() && ! repo->params().master_repositories()->empty()) ?
                                (*repo->params().master_repositories()->begin())->params().location() : repo->params().location(),
                            n::root() = a.options.destination()->installed_root_key() ?
                                stringify(a.options.destination()->installed_root_key()->value()) :
                                "/",
                            n::sandbox() = phase->option("sandbox"),
                            n::sydbox() = phase->option("sydbox"),
                            n::userpriv() = phase->option("userpriv") && userpriv_ok
                            ));

                EbuildBadOptionsCommand bad_options_cmd(command_params,
                        make_named_values<EbuildBadOptionsCommandParams>(
                            n::expand_vars() = expand_vars,
                            n::profiles() = repo->params().profiles(),
                            n::profiles_with_parents() = repo->profile()->profiles_with_parents(),
                            n::unmet_requirements() = verifier.unmet_requirements(),
                            n::use() = use,
                            n::use_expand() = join(repo->profile()->use_expand()->begin(), repo->profile()->use_expand()->end(), " "),
                            n::use_expand_hidden() = join(repo->profile()->use_expand_hidden()->begin(), repo->profile()->use_expand_hidden()->end(), " ")
                            ));

                if (! bad_options_cmd())
                    throw ActionFailedError("Bad options phase died");
            }

            result = false;
        }
    }

    if (id->eapi()->supported()->ebuild_phases()->ebuild_pretend().empty())
        return result;

    EAPIPhases phases(id->eapi()->supported()->ebuild_phases()->ebuild_pretend());
    for (EAPIPhases::ConstIterator phase(phases.begin_phases()), phase_end(phases.end_phases()) ;
            phase != phase_end ; ++phase)
    {
        if (can_skip_phase(id, *phase))
            continue;

        if (! output_manager)
            output_manager = a.options.make_output_manager()(a);

        EbuildCommandParams command_params(make_named_values<EbuildCommandParams>(
                n::builddir() = repo->params().builddir(),
                n::clearenv() = phase->option("clearenv"),
                n::commands() = join(phase->begin_commands(), phase->end_commands(), " "),
                n::distdir() = repo->params().distdir(),
                n::ebuild_dir() = repo->layout()->package_directory(id->name()),
                n::ebuild_file() = id->fs_location_key()->value(),
                n::eclassdirs() = repo->params().eclassdirs(),
                n::environment() = env,
                n::exlibsdirs() = exlibsdirs,
                n::files_dir() = repo->layout()->package_directory(id->name()) / "files",
                n::maybe_output_manager() = output_manager,
                n::package_builddir() = repo->params().builddir() / (stringify(id->name().category()) + "-" + stringify(id->name().package()) + "-" + stringify(id->version()) + "-pretend"),
                n::package_id() = id,
                n::portdir() =
                    (repo->params().master_repositories() && ! repo->params().master_repositories()->empty()) ?
                    (*repo->params().master_repositories()->begin())->params().location() : repo->params().location(),
                n::root() = a.options.destination()->installed_root_key() ?
                    stringify(a.options.destination()->installed_root_key()->value()) :
                    "/",
                n::sandbox() = phase->option("sandbox"),
                n::sydbox() = phase->option("sydbox"),
                n::userpriv() = phase->option("userpriv") && userpriv_ok
                ));

        EbuildPretendCommand pretend_cmd(command_params,
                make_named_values<EbuildPretendCommandParams>(
                    n::destination() = a.options.destination(),
                    n::expand_vars() = expand_vars,
                    n::is_from_pbin() = id->eapi()->supported()->is_pbin(),
                    n::profiles() = repo->params().profiles(),
                    n::profiles_with_parents() = repo->profile()->profiles_with_parents(),
                    n::replacing_ids() = a.options.replacing(),
                    n::use() = use,
                    n::use_expand() = join(repo->profile()->use_expand()->begin(), repo->profile()->use_expand()->end(), " "),
                    n::use_expand_hidden() = join(repo->profile()->use_expand_hidden()->begin(), repo->profile()->use_expand_hidden()->end(), " ")
                    ));

        if (! pretend_cmd())
            return false;
    }

    return result;
}