aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/traditional_profile.cc
blob: fd72b4fed5d1388d400cf4caf9117774cd8c8fec (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013 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/traditional_profile.hh>
#include <paludis/repositories/e/traditional_profile_file.hh>
#include <paludis/repositories/e/traditional_mask_file.hh>
#include <paludis/repositories/e/e_repository_exceptions.hh>
#include <paludis/repositories/e/e_repository.hh>
#include <paludis/repositories/e/eapi.hh>

#include <paludis/util/log.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/save.hh>
#include <paludis/util/system.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/util/join.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/set.hh>
#include <paludis/util/options.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/util/create_iterator-impl.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/map.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/util/fs_error.hh>
#include <paludis/util/upper_lower.hh>

#include <paludis/choice.hh>
#include <paludis/environment.hh>
#include <paludis/match_package.hh>
#include <paludis/distribution.hh>
#include <paludis/package_id.hh>
#include <paludis/metadata_key.hh>

#include <unordered_map>
#include <unordered_set>
#include <list>
#include <algorithm>
#include <set>
#include <vector>
#include <mutex>

#include <strings.h>

using namespace paludis;
using namespace paludis::erepository;

typedef std::unordered_map<std::string, std::shared_ptr<Set<UnprefixedChoiceName> > > KnownMap;

namespace
{
    typedef std::unordered_map<std::string, std::string, Hash<std::string> > EnvironmentVariablesMap;
    typedef std::unordered_map<QualifiedPackageName,
            std::list<std::pair<std::shared_ptr<const PackageDepSpec>, std::shared_ptr<const MaskInfo> > >,
            Hash<QualifiedPackageName> > PackageMaskMap;

    typedef std::unordered_map<ChoiceNameWithPrefix, bool, Hash<ChoiceNameWithPrefix> > FlagStatusMap;
    typedef std::list<std::pair<std::shared_ptr<const PackageDepSpec>, FlagStatusMap> > PackageFlagStatusMapList;

    struct StackedValues
    {
        std::string origin;

        FlagStatusMap use_mask;
        FlagStatusMap use_stable_mask;
        FlagStatusMap use_force;
        FlagStatusMap use_stable_force;
        PackageFlagStatusMapList package_use;
        PackageFlagStatusMapList package_use_mask;
        PackageFlagStatusMapList package_use_stable_mask;
        PackageFlagStatusMapList package_use_force;
        PackageFlagStatusMapList package_use_stable_force;

        StackedValues(const std::string & o) :
            origin(o)
        {
        }
    };

    typedef std::list<StackedValues> StackedValuesList;
}

namespace paludis
{
    template <>
    struct Imp<TraditionalProfile>
    {
        const Environment * const env;
        const EAPIForFileFunction eapi_for_file;
        const IsArchFlagFunction is_arch_flag;
        const bool has_master_repositories;

        TraditionalProfileFile<LineConfigFile> packages_file;
        TraditionalProfileFile<TraditionalMaskFile> package_mask_file;

        std::shared_ptr<FSPathSequence> profiles_with_parents;

        EnvironmentVariablesMap environment_variables;

        std::shared_ptr<SetSpecTree> system_packages;

        std::set<std::pair<ChoicePrefixName, UnprefixedChoiceName> > use;
        std::shared_ptr<Set<std::string> > use_expand;
        std::shared_ptr<Set<std::string> > use_expand_hidden;
        std::shared_ptr<Set<std::string> > use_expand_unprefixed;
        std::shared_ptr<Set<std::string> > use_expand_implicit;
        std::shared_ptr<Set<std::string> > iuse_implicit;
        std::unordered_map<std::string, std::shared_ptr<Set<std::string> > > use_expand_values;
        KnownMap known_choice_value_names;
        mutable std::mutex known_choice_value_names_for_separator_mutex;
        mutable std::unordered_map<char, KnownMap> known_choice_value_names_for_separator;
        StackedValuesList stacked_values_list;

        PackageMaskMap package_mask;

        Imp(const Environment * const e,
                const EAPIForFileFunction & p,
                const IsArchFlagFunction & a,
                const bool h) :
            env(e),
            eapi_for_file(p),
            is_arch_flag(a),
            has_master_repositories(h),
            packages_file(eapi_for_file),
            package_mask_file(eapi_for_file),
            profiles_with_parents(std::make_shared<FSPathSequence>()),
            system_packages(std::make_shared<SetSpecTree>(std::make_shared<AllDepSpec>())),
            use_expand(std::make_shared<Set<std::string>>()),
            use_expand_hidden(std::make_shared<Set<std::string>>()),
            use_expand_unprefixed(std::make_shared<Set<std::string>>()),
            use_expand_implicit(std::make_shared<Set<std::string>>()),
            iuse_implicit(std::make_shared<Set<std::string>>())
        {
        }
    };
}

namespace
{
    void load_environment(Pimp<TraditionalProfile> & _imp);

    void load_profile_directory_recursively(
            Pimp<TraditionalProfile> & _imp,
            const FSPath & dir);

    void load_profile_parent(
            Pimp<TraditionalProfile> & _imp,
            const FSPath & dir);

    void make_vars_from_file_vars(
            Pimp<TraditionalProfile> & _imp);

    void load_special_make_defaults_vars(
            Pimp<TraditionalProfile> & _imp,
            const FSPath & dir);

    void add_use_expand_to_use(
            Pimp<TraditionalProfile> & _imp);

    void fish_out_use_expand_names(
            Pimp<TraditionalProfile> & _imp);

    void load_profile_make_defaults(
            Pimp<TraditionalProfile> & _imp,
            const FSPath & dir);

    void load_basic_use_file(
            const FSPath & file,
            FlagStatusMap & m);

    void load_spec_use_file(
            const EAPI & eapi,
            const FSPath & file,
            PackageFlagStatusMapList & m);
}

namespace
{
    void load_environment(Pimp<TraditionalProfile> & _imp)
    {
        _imp->environment_variables["CONFIG_PROTECT"] = getenv_with_default("CONFIG_PROTECT", "/etc");
        _imp->environment_variables["CONFIG_PROTECT_MASK"] = getenv_with_default("CONFIG_PROTECT_MASK", "");
    }

    void load_profile_directory_recursively(
            Pimp<TraditionalProfile> & _imp,
            const FSPath & dir)
    {
        Context context("When adding profile directory '" + stringify(dir) + ":");

        if (! dir.stat().is_directory_or_symlink_to_directory())
        {
            Log::get_instance()->message("e.profile.not_a_directory", ll_warning, lc_context)
                << "Profile component '" << dir << "' is not a directory";
            return;
        }

        auto eapi(EAPIData::get_instance()->eapi_from_string(_imp->eapi_for_file(dir / "use.mask")));
        if (! eapi->supported())
            throw ERepositoryConfigurationError("Can't use profile directory '" + stringify(dir) +
                    "' because it uses an unsupported EAPI");

        load_profile_parent(_imp, dir);
        load_profile_make_defaults(_imp, dir);

        _imp->stacked_values_list.push_back(StackedValues(stringify(dir)));
        load_basic_use_file(dir / "use.mask", _imp->stacked_values_list.back().use_mask);
        load_basic_use_file(dir / "use.force", _imp->stacked_values_list.back().use_force);
        load_spec_use_file(*eapi, dir / "package.use", _imp->stacked_values_list.back().package_use);
        load_spec_use_file(*eapi, dir / "package.use.mask", _imp->stacked_values_list.back().package_use_mask);
        load_spec_use_file(*eapi, dir / "package.use.force", _imp->stacked_values_list.back().package_use_force);
        if (eapi->supported()->profile_options()->use_stable_mask_force())
        {
            load_basic_use_file(dir / "use.stable.mask", _imp->stacked_values_list.back().use_stable_mask);
            load_basic_use_file(dir / "use.stable.force", _imp->stacked_values_list.back().use_stable_force);
            load_spec_use_file(*eapi, dir / "package.use.stable.mask", _imp->stacked_values_list.back().package_use_stable_mask);
            load_spec_use_file(*eapi, dir / "package.use.stable.force", _imp->stacked_values_list.back().package_use_stable_force);
        }

        _imp->packages_file.add_file(dir / "packages");
        _imp->package_mask_file.add_file(dir / "package.mask");

        _imp->profiles_with_parents->push_back(dir);
    }

    void load_profile_parent(
            Pimp<TraditionalProfile> & _imp,
            const FSPath & dir)
    {
        Context context("When handling parent file for profile directory '" + stringify(dir) + ":");

        if (! (dir / "parent").stat().exists())
            return;

        LineConfigFile file(dir / "parent", { lcfo_disallow_continuations });

        LineConfigFile::ConstIterator i(file.begin()), i_end(file.end());
        bool once(false);
        if (i == i_end)
            Log::get_instance()->message("e.profile.parent.empty", ll_warning, lc_context) << "parent file is empty";
        else
            for ( ; i != i_end ; ++i)
            {
                if ('#' == i->at(0))
                {
                    if (! once)
                        Log::get_instance()->message("e.profile.parent.no_comments", ll_qa, lc_context)
                            << "Comments not allowed in '" << (dir / "parent") << "'";
                    once = true;
                    continue;
                }

                FSPath parent_dir(dir);
                do
                {
                    try
                    {
                        parent_dir = (parent_dir / *i).realpath();
                    }
                    catch (const FSError & e)
                    {
                        Log::get_instance()->message("e.profile.parent.skipping", ll_warning, lc_context)
                            << "Skipping parent '" << *i << "' due to exception: " << e.message() << " (" << e.what() << ")";
                        continue;
                    }

                    load_profile_directory_recursively(_imp, parent_dir);

                } while (false);
            }
    }

    void make_vars_from_file_vars(
            Pimp<TraditionalProfile> & _imp)
    {
        try
        {
            if (! _imp->has_master_repositories)
                for (TraditionalProfileFile<LineConfigFile>::ConstIterator i(_imp->packages_file.begin()),
                        i_end(_imp->packages_file.end()) ; i != i_end ; ++i)
                {
                    if (0 != i->second.compare(0, 1, "*", 0, 1))
                        continue;

                    Context context_spec("When parsing '" + i->second + "':");
                    std::shared_ptr<PackageDepSpec> spec(std::make_shared<PackageDepSpec>(
                                parse_elike_package_dep_spec(i->second.substr(1),
                                    i->first->supported()->package_dep_spec_parse_options(),
                                    i->first->supported()->version_spec_options())));

                    _imp->system_packages->top()->append(spec);
                }
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.packages.failure", ll_warning, lc_context) << "Loading packages "
                    " failed due to exception: " << e.message() << " (" << e.what() << ")";
        }

        for (TraditionalProfileFile<TraditionalMaskFile>::ConstIterator line(_imp->package_mask_file.begin()), line_end(_imp->package_mask_file.end()) ;
                line != line_end ; ++line)
        {
            if (line->second.first.empty())
                continue;

            try
            {
                std::shared_ptr<const PackageDepSpec> a(std::make_shared<PackageDepSpec>(
                            parse_elike_package_dep_spec(line->second.first,
                                line->first->supported()->package_dep_spec_parse_options(),
                                line->first->supported()->version_spec_options())));

                if (a->package_ptr())
                    _imp->package_mask[*a->package_ptr()].push_back(std::make_pair(a, line->second.second));
                else
                    Log::get_instance()->message("e.profile.package_mask.bad_spec", ll_warning, lc_context)
                        << "Loading package.mask spec '" << line->second.first << "' failed because specification does not restrict to a "
                        "unique package";
            }
            catch (const InternalError &)
            {
                throw;
            }
            catch (const Exception & e)
            {
                Log::get_instance()->message("e.profile.package_mask.bad_spec", ll_warning, lc_context)
                    << "Loading package.mask spec '" << line->second.first << "' failed due to exception '" << e.message() << "' ("
                    << e.what() << ")";
            }
        }
    }

    void load_special_make_defaults_vars(
            Pimp<TraditionalProfile> & _imp,
            const FSPath & dir)
    {
        auto eapi(EAPIData::get_instance()->eapi_from_string(_imp->eapi_for_file(dir / "make.defaults")));
        if (! eapi->supported())
            throw ERepositoryConfigurationError("Can't use profile directory '" + stringify(dir) +
                    "' because it uses an unsupported EAPI");

        std::string use_var(eapi->supported()->ebuild_environment_variables()->env_use());
        try
        {
            _imp->use.clear();
            if (! use_var.empty())
            {
                std::list<std::string> tokens;
                tokenise_whitespace(_imp->environment_variables[use_var], std::back_inserter(tokens));
                for (std::list<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end()) ;
                        t != t_end ; ++t)
                    _imp->use.insert(std::make_pair(ChoicePrefixName(""), UnprefixedChoiceName(*t)));
            }
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.make_defaults.use_failure", ll_warning, lc_context)
                << "Loading '" << use_var << "' failed due to exception: " << e.message() << " (" << e.what() << ")";
        }

        std::string use_expand_var(eapi->supported()->ebuild_environment_variables()->env_use_expand());
        try
        {
            _imp->use_expand->clear();
            if (! use_expand_var.empty())
                tokenise_whitespace(_imp->environment_variables[use_expand_var], _imp->use_expand->inserter());
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.make_defaults.use_expand_failure", ll_warning, lc_context)
                << "Loading '" << use_expand_var << "' failed due to exception: " << e.message() << " (" << e.what() << ")";
        }

        std::string use_expand_hidden_var(eapi->supported()->ebuild_environment_variables()->env_use_expand_hidden());
        try
        {
            _imp->use_expand_hidden->clear();
            if (! use_expand_hidden_var.empty())
                tokenise_whitespace(_imp->environment_variables[use_expand_hidden_var], _imp->use_expand_hidden->inserter());
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.make_defaults.use_expand_hidden_failure", ll_warning, lc_context)
                << "Loading '" << use_expand_hidden_var << "' failed due to exception: "
                << e.message() << " (" << e.what() << ")";
        }
    }

    void add_use_expand_to_use(
            Pimp<TraditionalProfile> & _imp)
    {
        Context context("When adding USE_EXPAND to USE:");

        _imp->stacked_values_list.push_back(StackedValues("use_expand special values"));

        for (Set<std::string>::ConstIterator x(_imp->use_expand->begin()), x_end(_imp->use_expand->end()) ;
                x != x_end ; ++x)
        {
            std::string lower_x(tolower(*x));
            std::list<std::string> uses;
            tokenise_whitespace(_imp->environment_variables[stringify(*x)], std::back_inserter(uses));
            for (std::list<std::string>::const_iterator u(uses.begin()), u_end(uses.end()) ;
                    u != u_end ; ++u)
                _imp->use.insert(std::make_pair(ChoicePrefixName(lower_x), UnprefixedChoiceName(*u)));
        }
    }

    void fish_out_use_expand_names(
            Pimp<TraditionalProfile> & _imp)
    {
        Context context("When finding all known USE_EXPAND names:");

        for (Set<std::string>::ConstIterator x(_imp->use_expand->begin()), x_end(_imp->use_expand->end()) ;
                x != x_end ; ++x)
            _imp->known_choice_value_names.insert(std::make_pair(tolower(*x), std::make_shared<Set<UnprefixedChoiceName>>()));

        for (std::set<std::pair<ChoicePrefixName, UnprefixedChoiceName> >::const_iterator u(_imp->use.begin()), u_end(_imp->use.end()) ;
                u != u_end ; ++u)
        {
            if (! stringify(u->first).empty())
            {
                KnownMap::iterator i(_imp->known_choice_value_names.find(stringify(u->first)));
                if (i == _imp->known_choice_value_names.end())
                    throw InternalError(PALUDIS_HERE, stringify(u->first));
                i->second->insert(u->second);
            }
        }
    }

    void handle_profile_arch_var(
            Pimp<TraditionalProfile> & _imp,
            const std::string & s)
    {
        Context context("When handling profile " + s + " variable:");

        std::string arch_s(_imp->environment_variables[s]);
        if (arch_s.empty())
            throw ERepositoryConfigurationError("Variable '" + s + "' is unset or empty");

        _imp->stacked_values_list.push_back(StackedValues("arch special values"));
        try
        {
            std::string arch(arch_s);

            _imp->use.insert(std::make_pair(ChoicePrefixName(""), UnprefixedChoiceName(arch)));
            _imp->stacked_values_list.back().use_force[ChoiceNameWithPrefix(arch)] = true;
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception &)
        {
            throw ERepositoryConfigurationError("Variable '" + s + "' has invalid value '" + arch_s + "'");
        }
    }

    bool is_incremental(const EAPI & e, const std::string & s)
    {
        Context c("When checking whether '" + s + "' is incremental:");

        return (! s.empty()) && (
                (s == e.supported()->ebuild_environment_variables()->env_use())
                || (s == e.supported()->ebuild_environment_variables()->env_use_expand())
                || (s == e.supported()->ebuild_environment_variables()->env_use_expand_hidden())
                || (s == e.supported()->ebuild_environment_variables()->env_use_expand_unprefixed())
                || (s == e.supported()->ebuild_environment_variables()->env_use_expand_implicit())
                || (s == e.supported()->ebuild_environment_variables()->env_iuse_implicit())
                || s == "CONFIG_PROTECT"
                || s == "CONFIG_PROTECT_MASK");
    }

    void load_profile_make_defaults(
            Pimp<TraditionalProfile> & _imp,
            const FSPath & dir)
    {
        Context context("When handling make.defaults file for profile directory '" + stringify(dir) + ":");

        if (! (dir / "make.defaults").stat().exists())
            return;

        auto eapi(EAPIData::get_instance()->eapi_from_string(_imp->eapi_for_file(dir / "make.defaults")));
        if (! eapi->supported())
            throw ERepositoryConfigurationError("Can't use profile directory '" + stringify(dir) +
                    "' because it uses an unsupported EAPI");

        KeyValueConfigFile file(dir / "make.defaults", { kvcfo_disallow_source, kvcfo_disallow_space_inside_unquoted_values, kvcfo_allow_inline_comments, kvcfo_allow_multiple_assigns_per_line },
                &KeyValueConfigFile::no_defaults, &KeyValueConfigFile::no_transformation);

        for (KeyValueConfigFile::ConstIterator k(file.begin()), k_end(file.end()) ;
                k != k_end ; ++k)
        {
            if (is_incremental(*eapi, k->first))
            {
                std::list<std::string> val, val_add;
                tokenise_whitespace(_imp->environment_variables[k->first], std::back_inserter(val));
                tokenise_whitespace(k->second, std::back_inserter(val_add));

                for (std::list<std::string>::const_iterator v(val_add.begin()), v_end(val_add.end()) ;
                        v != v_end ; ++v)
                {
                    if (v->empty())
                        continue;
                    if (*v == "-*")
                        val.clear();
                    else if ('-' == v->at(0))
                        val.remove(v->substr(1));
                    else
                        val.push_back(*v);
                }

                _imp->environment_variables[k->first] = join(val.begin(), val.end(), " ");
            }
            else
                _imp->environment_variables[k->first] = k->second;
        }

        std::string use_expand_var(eapi->supported()->ebuild_environment_variables()->env_use_expand());
        try
        {
            _imp->use_expand->clear();
            if (! use_expand_var.empty())
                tokenise_whitespace(_imp->environment_variables[use_expand_var], _imp->use_expand->inserter());
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.make_defaults.use_expand_failure", ll_warning, lc_context)
                << "Loading '" << use_expand_var << "' failed due to exception: " << e.message() << " (" << e.what() << ")";
        }

        std::string use_expand_unprefixed_var(eapi->supported()->ebuild_environment_variables()->env_use_expand_unprefixed());
        try
        {
            _imp->use_expand_unprefixed->clear();
            if (! use_expand_unprefixed_var.empty())
                tokenise_whitespace(_imp->environment_variables[use_expand_unprefixed_var], _imp->use_expand_unprefixed->inserter());
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.make_defaults.use_expand_unprefixed_failure", ll_warning, lc_context)
                << "Loading '" << use_expand_unprefixed_var << "' failed due to exception: " << e.message() << " (" << e.what() << ")";
        }

        std::string use_expand_implicit_var(eapi->supported()->ebuild_environment_variables()->env_use_expand_implicit());
        try
        {
            _imp->use_expand_implicit->clear();
            if (! use_expand_implicit_var.empty())
                tokenise_whitespace(_imp->environment_variables[use_expand_implicit_var], _imp->use_expand_implicit->inserter());
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.make_defaults.use_expand_implicit_failure", ll_warning, lc_context)
                << "Loading '" << use_expand_implicit_var << "' failed due to exception: " << e.message() << " (" << e.what() << ")";
        }

        std::string iuse_implicit_var(eapi->supported()->ebuild_environment_variables()->env_iuse_implicit());
        try
        {
            _imp->iuse_implicit->clear();
            if (! iuse_implicit_var.empty())
                tokenise_whitespace(_imp->environment_variables[iuse_implicit_var], _imp->iuse_implicit->inserter());
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.make_defaults.iuse_implicit_failure", ll_warning, lc_context)
                << "Loading '" << iuse_implicit_var << "' failed due to exception: " << e.message() << " (" << e.what() << ")";
        }

        std::string use_expand_values_part_var(eapi->supported()->ebuild_environment_variables()->env_use_expand_values_part());
        try
        {
            _imp->use_expand_values.clear();
            if (! use_expand_values_part_var.empty())
            {
                for (Set<std::string>::ConstIterator x(_imp->use_expand->begin()), x_end(_imp->use_expand->end()) ;
                        x != x_end ; ++x)
                {
                    std::shared_ptr<Set<std::string> > v(std::make_shared<Set<std::string>>());
                    tokenise_whitespace(_imp->environment_variables[use_expand_values_part_var + *x], v->inserter());
                    _imp->use_expand_values.insert(std::make_pair(*x, v));
                }

                for (Set<std::string>::ConstIterator x(_imp->use_expand_unprefixed->begin()), x_end(_imp->use_expand_unprefixed->end()) ;
                        x != x_end ; ++x)
                {
                    std::shared_ptr<Set<std::string> > v(std::make_shared<Set<std::string>>());
                    tokenise_whitespace(_imp->environment_variables[use_expand_values_part_var + *x], v->inserter());
                    _imp->use_expand_values.insert(std::make_pair(*x, v));
                }
            }
        }
        catch (const InternalError &)
        {
            throw;
        }
        catch (const Exception & e)
        {
            Log::get_instance()->message("e.profile.make_defaults.iuse_implicit_failure", ll_warning, lc_context)
                << "Loading '" << iuse_implicit_var << "' failed due to exception: " << e.message() << " (" << e.what() << ")";
        }
    }

    void load_basic_use_file(
            const FSPath & file,
            FlagStatusMap & m)
    {
        if (! file.stat().exists())
            return;

        Context context("When loading basic use file '" + stringify(file) + ":");
        LineConfigFile f(file, { lcfo_disallow_continuations });
        for (LineConfigFile::ConstIterator line(f.begin()), line_end(f.end()) ;
                line != line_end ; ++line)
        {
            std::list<std::string> tokens;
            tokenise_whitespace(*line, std::back_inserter(tokens));

            for (std::list<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end()) ;
                    t != t_end ; ++t)
            {
                try
                {
                    if (t->empty())
                        continue;
                    if ('-' == t->at(0))
                        m[ChoiceNameWithPrefix(t->substr(1))] = false;
                    else
                        m[ChoiceNameWithPrefix(*t)] = true;
                }
                catch (const InternalError &)
                {
                    throw;
                }
                catch (const Exception & e)
                {
                    Log::get_instance()->message("e.profile.failure", ll_warning, lc_context) << "Ignoring token '"
                        << *t << "' due to exception '" << e.message() << "' (" << e.what() << ")";
                }
            }
        }
    }

    void load_spec_use_file(
            const EAPI & eapi,
            const FSPath & file,
            PackageFlagStatusMapList & m)
    {
        if (! file.stat().exists())
            return;

        Context context("When loading specised use file '" + stringify(file) + ":");
        LineConfigFile f(file, { lcfo_disallow_continuations });
        for (LineConfigFile::ConstIterator line(f.begin()), line_end(f.end()) ;
                line != line_end ; ++line)
        {
            std::list<std::string> tokens;
            tokenise_whitespace(*line, std::back_inserter(tokens));

            if (tokens.empty())
                continue;

            try
            {
                std::shared_ptr<const PackageDepSpec> spec(std::make_shared<PackageDepSpec>(
                            parse_elike_package_dep_spec(*tokens.begin(), eapi.supported()->package_dep_spec_parse_options(),
                                eapi.supported()->version_spec_options())));
                PackageFlagStatusMapList::iterator n(m.insert(m.end(), std::make_pair(spec, FlagStatusMap())));

                for (std::list<std::string>::const_iterator t(next(tokens.begin())), t_end(tokens.end()) ;
                        t != t_end ; ++t)
                {
                    try
                    {
                        if (t->empty())
                            continue;
                        if ('-' == t->at(0))
                            n->second[ChoiceNameWithPrefix(t->substr(1))] = false;
                        else
                            n->second[ChoiceNameWithPrefix(*t)] = true;
                    }
                    catch (const InternalError &)
                    {
                        throw;
                    }
                    catch (const Exception & e)
                    {
                        Log::get_instance()->message("e.profile.failure", ll_warning, lc_context) << "Ignoring token '"
                            << *t << "' due to exception '" << e.message() << "' (" << e.what() << ")";
                    }
                }
            }
            catch (const PackageDepSpecError & e)
            {
                Log::get_instance()->message("e.profile.failure", ll_warning, lc_context) << "Ignoring line '"
                    << *line << "' due to exception '" << e.message() << "' (" << e.what() << ")";
            }
        }
    }
}

TraditionalProfile::TraditionalProfile(
        const Environment * const env,
        const RepositoryName & name,
        const EAPIForFileFunction & eapi_for_file,
        const IsArchFlagFunction & is_arch_flag,
        const FSPathSequence & dirs,
        const std::string & arch_var_if_special,
        const bool profiles_explicitly_set,
        const bool has_master_repositories,
        const bool ignore_deprecated_profiles) :
    _imp(env, eapi_for_file, is_arch_flag, has_master_repositories)
{
    Context context("When loading profiles '" + join(dirs.begin(), dirs.end(), "' '") + "' for repository '" + stringify(name) + "':");

    if (dirs.empty())
        throw ERepositoryConfigurationError("No profiles directories specified");

    load_environment(_imp);

    for (FSPathSequence::ConstIterator d(dirs.begin()), d_end(dirs.end()) ;
            d != d_end ; ++d)
    {
        Context subcontext("When using directory '" + stringify(*d) + "':");

        if (profiles_explicitly_set && ! ignore_deprecated_profiles)
            if ((*d / "deprecated").stat().is_regular_file_or_symlink_to_regular_file())
                Log::get_instance()->message("e.profile.deprecated", ll_warning, lc_context) << "Profile directory '" << *d
                    << "' is deprecated. See the file '" << (*d / "deprecated") << "' for details";

        load_profile_directory_recursively(_imp, *d);
    }

    make_vars_from_file_vars(_imp);
    load_special_make_defaults_vars(_imp, *dirs.begin());
    add_use_expand_to_use(_imp);
    fish_out_use_expand_names(_imp);
    if (! arch_var_if_special.empty())
        handle_profile_arch_var(_imp, arch_var_if_special);
}

TraditionalProfile::~TraditionalProfile()
{
}

std::shared_ptr<const FSPathSequence>
TraditionalProfile::profiles_with_parents() const
{
    return _imp->profiles_with_parents;
}

bool
TraditionalProfile::use_masked(
        const std::shared_ptr<const EbuildID> & id,
        const std::shared_ptr<const Choice> & choice,
        const UnprefixedChoiceName & value_unprefixed,
        const ChoiceNameWithPrefix & value_prefixed
        ) const
{
    if (stringify(choice->prefix()).empty() && _imp->is_arch_flag(value_unprefixed) &&
            (! use_state_ignoring_masks(id, choice, value_unprefixed, value_prefixed).is_true()))
        return true;

    bool result(false);
    for (StackedValuesList::const_iterator i(_imp->stacked_values_list.begin()),
            i_end(_imp->stacked_values_list.end()) ; i != i_end ; ++i)
    {
        FlagStatusMap::const_iterator f(i->use_mask.find(value_prefixed));
        if (i->use_mask.end() != f)
            result = f->second;

        if (id->is_stable())
        {
            FlagStatusMap::const_iterator fs(i->use_stable_mask.find(value_prefixed));
            if (i->use_stable_mask.end() != fs)
                result = fs->second;
        }

        for (PackageFlagStatusMapList::const_iterator g(i->package_use_mask.begin()),
                g_end(i->package_use_mask.end()) ; g != g_end ; ++g)
        {
            if (! match_package(*_imp->env, *g->first, id, nullptr, { }))
                continue;

            FlagStatusMap::const_iterator h(g->second.find(value_prefixed));
            if (g->second.end() != h)
                result = h->second;
        }

        if (id->is_stable())
        {
            for (PackageFlagStatusMapList::const_iterator gs(i->package_use_stable_mask.begin()),
                     gs_end(i->package_use_stable_mask.end()) ; gs != gs_end ; ++gs)
            {
                if (! match_package(*_imp->env, *gs->first, id, nullptr, { }))
                    continue;

                FlagStatusMap::const_iterator hs(gs->second.find(value_prefixed));
                if (gs->second.end() != hs)
                    result = hs->second;
            }
        }
    }

    return result;
}

bool
TraditionalProfile::use_forced(
        const std::shared_ptr<const EbuildID> & id,
        const std::shared_ptr<const Choice> & choice,
        const UnprefixedChoiceName & value_unprefixed,
        const ChoiceNameWithPrefix & value_prefixed
        ) const
{
    if (use_masked(id, choice, value_unprefixed, value_prefixed))
        return false;
    if (stringify(choice->prefix()).empty() && _imp->is_arch_flag(value_unprefixed))
        return true;

    bool result(false);
    for (StackedValuesList::const_iterator i(_imp->stacked_values_list.begin()),
            i_end(_imp->stacked_values_list.end()) ; i != i_end ; ++i)
    {
        FlagStatusMap::const_iterator f(i->use_force.find(value_prefixed));
        if (i->use_force.end() != f)
            result = f->second;

        if (id->is_stable())
        {
            FlagStatusMap::const_iterator fs(i->use_stable_force.find(value_prefixed));
            if (i->use_stable_force.end() != fs)
                result = fs->second;
        }

        for (PackageFlagStatusMapList::const_iterator g(i->package_use_force.begin()),
                g_end(i->package_use_force.end()) ; g != g_end ; ++g)
        {
            if (! match_package(*_imp->env, *g->first, id, nullptr, { }))
                continue;

            FlagStatusMap::const_iterator h(g->second.find(value_prefixed));
            if (g->second.end() != h)
                result = h->second;
        }

        if (id->is_stable())
        {
            for (PackageFlagStatusMapList::const_iterator gs(i->package_use_stable_force.begin()),
                     gs_end(i->package_use_stable_force.end()) ; gs != gs_end ; ++gs)
            {
                if (! match_package(*_imp->env, *gs->first, id, nullptr, { }))
                    continue;

                FlagStatusMap::const_iterator hs(gs->second.find(value_prefixed));
                if (gs->second.end() != hs)
                    result = hs->second;
            }
        }
    }

    return result;
}

Tribool
TraditionalProfile::use_state_ignoring_masks(
        const std::shared_ptr<const PackageID> & id,
        const std::shared_ptr<const Choice> & choice,
        const UnprefixedChoiceName & value_unprefixed,
        const ChoiceNameWithPrefix & value_prefixed
        ) const
{
    std::pair<ChoicePrefixName, UnprefixedChoiceName> prefix_value(choice->prefix(), value_unprefixed);
    Tribool result(_imp->use.end() != _imp->use.find(prefix_value) ? Tribool(true) : Tribool(indeterminate));

    for (StackedValuesList::const_iterator i(_imp->stacked_values_list.begin()),
            i_end(_imp->stacked_values_list.end()) ; i != i_end ; ++i)
    {
        for (PackageFlagStatusMapList::const_iterator g(i->package_use.begin()),
                g_end(i->package_use.end()) ; g != g_end ; ++g)
        {
            if (! match_package(*_imp->env, *g->first, id, nullptr, { }))
                continue;

            FlagStatusMap::const_iterator h(g->second.find(value_prefixed));
            if (g->second.end() != h)
                result = h->second ? true : false;
        }
    }

    return result;
}

namespace
{
    void
    add_flag_status_map(const std::shared_ptr<Set<UnprefixedChoiceName> > result,
            const FlagStatusMap & m, const std::string & prefix)
    {
        for (FlagStatusMap::const_iterator it(m.begin()),
                 it_end(m.end()); it_end != it; ++it)
            if (0 == stringify(it->first).compare(0, prefix.length(), prefix))
                result->insert(UnprefixedChoiceName(stringify(it->first).substr(prefix.length())));
    }

    void
    add_package_flag_status_map_list(const std::shared_ptr<Set<UnprefixedChoiceName> > result,
            const PackageFlagStatusMapList & m, const std::string & prefix)
    {
        for (PackageFlagStatusMapList::const_iterator it(m.begin()),
                 it_end(m.end()); it_end != it; ++it)
            add_flag_status_map(result, it->second, prefix);
    }
}

const std::shared_ptr<const Set<UnprefixedChoiceName> >
TraditionalProfile::known_choice_value_names(
        const std::shared_ptr<const ERepositoryID> & id,
        const std::shared_ptr<const Choice> & choice
        ) const
{
    std::unique_lock<std::mutex> l(_imp->known_choice_value_names_for_separator_mutex);

    char separator(id->eapi()->supported()->choices_options()->use_expand_separator());
    std::unordered_map<char, KnownMap>::iterator it(_imp->known_choice_value_names_for_separator.find(separator));
    if (_imp->known_choice_value_names_for_separator.end() == it)
        it = _imp->known_choice_value_names_for_separator.insert(std::make_pair(separator, KnownMap())).first;

    std::string lower_x(tolower(choice->raw_name()));
    KnownMap::const_iterator it2(it->second.find(lower_x));
    if (it->second.end() == it2)
    {
        std::shared_ptr<Set<UnprefixedChoiceName> > result(std::make_shared<Set<UnprefixedChoiceName>>());
        it2 = it->second.insert(std::make_pair(lower_x, result)).first;

        KnownMap::const_iterator i(_imp->known_choice_value_names.find(lower_x));
        if (_imp->known_choice_value_names.end() == i)
            throw InternalError(PALUDIS_HERE, lower_x);
        std::copy(i->second->begin(), i->second->end(), result->inserter());

        std::string prefix(lower_x);
        prefix += separator;

        for (StackedValuesList::const_iterator sit(_imp->stacked_values_list.begin()),
                 sit_end(_imp->stacked_values_list.end()); sit_end != sit; ++sit)
        {
            add_flag_status_map(result, sit->use_mask, prefix);
            add_flag_status_map(result, sit->use_force, prefix);
            add_package_flag_status_map_list(result, sit->package_use, prefix);
            add_package_flag_status_map_list(result, sit->package_use_mask, prefix);
            add_package_flag_status_map_list(result, sit->package_use_force, prefix);
        }
    }

    return it2->second;
}

const std::string
TraditionalProfile::environment_variable(const std::string & s) const
{
    EnvironmentVariablesMap::const_iterator i(_imp->environment_variables.find(s));
    if (_imp->environment_variables.end() == i)
        return "";
    else
        return i->second;
}

const std::shared_ptr<const SetSpecTree>
TraditionalProfile::system_packages() const
{
    return _imp->system_packages;
}

const std::shared_ptr<const MasksInfo>
TraditionalProfile::profile_masks(const std::shared_ptr<const PackageID> & id) const
{
    auto result(std::make_shared<MasksInfo>());
    PackageMaskMap::const_iterator rr(_imp->package_mask.find(id->name()));
    if (_imp->package_mask.end() != rr)
    {
        for (std::list<std::pair<std::shared_ptr<const PackageDepSpec>, std::shared_ptr<const MaskInfo> > >::const_iterator k(rr->second.begin()),
                k_end(rr->second.end()) ; k != k_end ; ++k)
            if (match_package(*_imp->env, *k->first, id, nullptr, { }))
                result->push_back(*k->second);
    }

    return result;
}

const std::shared_ptr<const Set<std::string> >
TraditionalProfile::use_expand() const
{
    return _imp->use_expand;
}

const std::shared_ptr<const Set<std::string> >
TraditionalProfile::use_expand_hidden() const
{
    return _imp->use_expand_hidden;
}

const std::shared_ptr<const Set<std::string> >
TraditionalProfile::use_expand_unprefixed() const
{
    return _imp->use_expand_unprefixed;
}

const std::shared_ptr<const Set<std::string> >
TraditionalProfile::use_expand_implicit() const
{
    return _imp->use_expand_implicit;
}

const std::shared_ptr<const Set<std::string> >
TraditionalProfile::use_expand_values(const std::string & x) const
{
    Context context("When finding USE_EXPAND_VALUES_" + x + ":");
    std::unordered_map<std::string, std::shared_ptr<Set<std::string> > >::const_iterator i(_imp->use_expand_values.find(x));
    if (i == _imp->use_expand_values.end())
        throw InternalError(PALUDIS_HERE, "Oops. We need USE_EXPAND_VALUES_" + x + ", but it's not in the map");
    return i->second;
}

const std::shared_ptr<const Set<std::string> >
TraditionalProfile::iuse_implicit() const
{
    return _imp->iuse_implicit;
}