forked from microsoft/vcpkg-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsourceparagraph.cpp
1932 lines (1673 loc) · 77.7 KB
/
sourceparagraph.cpp
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
#include <vcpkg/base/checks.h>
#include <vcpkg/base/expected.h>
#include <vcpkg/base/jsonreader.h>
#include <vcpkg/base/message_sinks.h>
#include <vcpkg/base/span.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/util.h>
#include <vcpkg/configuration.h>
#include <vcpkg/documentation.h>
#include <vcpkg/metrics.h>
#include <vcpkg/packagespec.h>
#include <vcpkg/platform-expression.h>
#include <vcpkg/sourceparagraph.h>
#include <vcpkg/triplet.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/versiondeserializers.h>
namespace vcpkg
{
bool operator==(const DependencyConstraint& lhs, const DependencyConstraint& rhs)
{
if (lhs.type != rhs.type) return false;
if (lhs.value != rhs.value) return false;
return lhs.port_version == rhs.port_version;
}
Optional<Version> DependencyConstraint::try_get_minimum_version() const
{
if (type == VersionConstraintKind::None)
{
return nullopt;
}
return Version{
value,
port_version,
};
}
bool Dependency::has_platform_expressions() const
{
return !platform.is_empty() || Util::any_of(features, [](const auto f) { return !f.platform.is_empty(); });
}
FullPackageSpec Dependency::to_full_spec(View<std::string> feature_list, Triplet target, Triplet host_triplet) const
{
InternalFeatureSet internal_feature_list(feature_list.begin(), feature_list.end());
internal_feature_list.push_back("core");
if (default_features)
{
internal_feature_list.push_back("default");
}
return FullPackageSpec{{name, host ? host_triplet : target}, std::move(internal_feature_list)};
}
bool operator==(const Dependency& lhs, const Dependency& rhs)
{
if (lhs.name != rhs.name) return false;
if (lhs.features != rhs.features) return false;
if (!structurally_equal(lhs.platform, rhs.platform)) return false;
if (lhs.extra_info != rhs.extra_info) return false;
if (lhs.constraint != rhs.constraint) return false;
if (lhs.host != rhs.host) return false;
return true;
}
bool operator!=(const Dependency& lhs, const Dependency& rhs);
bool operator==(const DependencyOverride& lhs, const DependencyOverride& rhs)
{
if (lhs.name != rhs.name) return false;
if (lhs.version != rhs.version) return false;
if (lhs.scheme != rhs.scheme) return false;
return lhs.extra_info == rhs.extra_info;
}
bool operator!=(const DependencyOverride& lhs, const DependencyOverride& rhs);
bool operator==(const DependencyRequestedFeature& lhs, const DependencyRequestedFeature& rhs)
{
return lhs.name == rhs.name && structurally_equal(lhs.platform, rhs.platform);
}
bool operator!=(const DependencyRequestedFeature& lhs, const DependencyRequestedFeature& rhs)
{
return !(lhs == rhs);
}
struct UrlDeserializer : Json::StringDeserializer
{
LocalizedString type_name() const override { return msg::format(msgAUrl); }
static const UrlDeserializer instance;
};
const UrlDeserializer UrlDeserializer::instance;
template<class Lhs, class Rhs>
static bool paragraph_equal(const Lhs& lhs, const Rhs& rhs)
{
return std::equal(
lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](const std::string& lhs, const std::string& rhs) {
return Strings::trim(StringView(lhs)) == Strings::trim(StringView(rhs));
});
}
bool operator==(const SourceParagraph& lhs, const SourceParagraph& rhs)
{
if (lhs.name != rhs.name) return false;
if (lhs.raw_version != rhs.raw_version) return false;
if (lhs.version_scheme != rhs.version_scheme) return false;
if (lhs.port_version != rhs.port_version) return false;
if (!paragraph_equal(lhs.description, rhs.description)) return false;
if (!paragraph_equal(lhs.maintainers, rhs.maintainers)) return false;
if (lhs.homepage != rhs.homepage) return false;
if (lhs.documentation != rhs.documentation) return false;
if (lhs.dependencies != rhs.dependencies) return false;
if (lhs.default_features != rhs.default_features) return false;
if (lhs.license != rhs.license) return false;
if (!structurally_equal(lhs.supports_expression, rhs.supports_expression)) return false;
if (lhs.extra_info != rhs.extra_info) return false;
return true;
}
bool operator==(const FeatureParagraph& lhs, const FeatureParagraph& rhs)
{
if (lhs.name != rhs.name) return false;
if (lhs.dependencies != rhs.dependencies) return false;
if (!paragraph_equal(lhs.description, rhs.description)) return false;
if (lhs.extra_info != rhs.extra_info) return false;
return true;
}
bool operator==(const SourceControlFile& lhs, const SourceControlFile& rhs)
{
if (*lhs.core_paragraph != *rhs.core_paragraph) return false;
return std::equal(lhs.feature_paragraphs.begin(),
lhs.feature_paragraphs.end(),
rhs.feature_paragraphs.begin(),
rhs.feature_paragraphs.end(),
[](const std::unique_ptr<FeatureParagraph>& lhs,
const std::unique_ptr<FeatureParagraph>& rhs) { return *lhs == *rhs; });
}
namespace SourceParagraphFields
{
static constexpr StringLiteral BUILD_DEPENDS = "Build-Depends";
static constexpr StringLiteral DEFAULT_FEATURES = "Default-Features";
static constexpr StringLiteral DESCRIPTION = "Description";
static constexpr StringLiteral FEATURE = "Feature";
static constexpr StringLiteral MAINTAINERS = "Maintainer";
static constexpr StringLiteral NAME = "Source";
static constexpr StringLiteral VERSION = "Version";
static constexpr StringLiteral PORT_VERSION = "Port-Version";
static constexpr StringLiteral HOMEPAGE = "Homepage";
static constexpr StringLiteral TYPE = "Type";
static constexpr StringLiteral SUPPORTS = "Supports";
}
static Span<const StringView> get_list_of_valid_fields()
{
static const StringView valid_fields[] = {
SourceParagraphFields::NAME,
SourceParagraphFields::VERSION,
SourceParagraphFields::PORT_VERSION,
SourceParagraphFields::DESCRIPTION,
SourceParagraphFields::MAINTAINERS,
SourceParagraphFields::BUILD_DEPENDS,
SourceParagraphFields::HOMEPAGE,
SourceParagraphFields::TYPE,
SourceParagraphFields::SUPPORTS,
SourceParagraphFields::DEFAULT_FEATURES,
};
return valid_fields;
}
static void trim_all(std::vector<std::string>& arr)
{
for (auto& el : arr)
{
Strings::inplace_trim(el);
}
}
namespace
{
constexpr static struct Canonicalize
{
struct FeatureLess
{
bool operator()(const std::unique_ptr<FeatureParagraph>& lhs,
const std::unique_ptr<FeatureParagraph>& rhs) const
{
return (*this)(*lhs, *rhs);
}
bool operator()(const FeatureParagraph& lhs, const FeatureParagraph& rhs) const
{
return lhs.name < rhs.name;
}
};
struct FeatureEqual
{
bool operator()(const std::unique_ptr<FeatureParagraph>& lhs,
const std::unique_ptr<FeatureParagraph>& rhs) const
{
return (*this)(*lhs, *rhs);
}
bool operator()(const FeatureParagraph& lhs, const FeatureParagraph& rhs) const
{
return lhs.name == rhs.name;
}
};
struct DependencyFeatureLess
{
bool operator()(const DependencyRequestedFeature& lhs, const DependencyRequestedFeature& rhs) const
{
if (lhs.name == rhs.name)
{
auto platform_cmp = compare(lhs.platform, rhs.platform);
return platform_cmp < 0;
}
return lhs.name < rhs.name;
}
};
// assume canonicalized feature list
struct DependencyLess
{
bool operator()(const std::unique_ptr<Dependency>& lhs, const std::unique_ptr<Dependency>& rhs) const
{
return (*this)(*lhs, *rhs);
}
bool operator()(const Dependency& lhs, const Dependency& rhs) const
{
auto cmp = lhs.name.compare(rhs.name);
if (cmp < 0) return true;
if (cmp > 0) return false;
// same dependency name
// order by platform string:
auto platform_cmp = compare(lhs.platform, rhs.platform);
if (platform_cmp < 0) return true;
if (platform_cmp > 0) return false;
// then order by features
// smaller list first, then lexicographical
if (lhs.features.size() < rhs.features.size()) return true;
if (rhs.features.size() < lhs.features.size()) return false;
// then finally order by feature list
if (std::lexicographical_compare(lhs.features.begin(),
lhs.features.end(),
rhs.features.begin(),
rhs.features.end(),
DependencyFeatureLess{}))
{
return true;
}
return false;
}
};
template<class T>
void operator()(std::unique_ptr<T>& ptr) const
{
(*this)(*ptr);
}
void operator()(Dependency& dep) const
{
std::sort(dep.features.begin(), dep.features.end(), DependencyFeatureLess{});
dep.extra_info.sort_keys();
}
void operator()(SourceParagraph& spgh) const
{
std::for_each(spgh.dependencies.begin(), spgh.dependencies.end(), *this);
std::sort(spgh.dependencies.begin(), spgh.dependencies.end(), DependencyLess{});
std::sort(spgh.default_features.begin(), spgh.default_features.end(), DependencyFeatureLess{});
spgh.extra_info.sort_keys();
}
void operator()(FeatureParagraph& fpgh) const
{
std::for_each(fpgh.dependencies.begin(), fpgh.dependencies.end(), *this);
std::sort(fpgh.dependencies.begin(), fpgh.dependencies.end(), DependencyLess{});
fpgh.extra_info.sort_keys();
}
[[nodiscard]] std::unique_ptr<ParseControlErrorInfo> operator()(SourceControlFile& scf) const
{
(*this)(*scf.core_paragraph);
std::for_each(scf.feature_paragraphs.begin(), scf.feature_paragraphs.end(), *this);
std::sort(scf.feature_paragraphs.begin(), scf.feature_paragraphs.end(), FeatureLess{});
auto adjacent_equal =
std::adjacent_find(scf.feature_paragraphs.begin(), scf.feature_paragraphs.end(), FeatureEqual{});
if (adjacent_equal != scf.feature_paragraphs.end())
{
auto error_info = std::make_unique<ParseControlErrorInfo>();
error_info->name = scf.core_paragraph->name;
error_info->error = msg::format_error(msgMultipleFeatures,
msg::package_name = scf.core_paragraph->name,
msg::feature = (*adjacent_equal)->name);
return error_info;
}
return nullptr;
}
} canonicalize{};
}
static ParseExpected<SourceParagraph> parse_source_paragraph(StringView origin, Paragraph&& fields)
{
ParagraphParser parser(std::move(fields));
auto spgh = std::make_unique<SourceParagraph>();
parser.required_field(SourceParagraphFields::NAME, spgh->name);
parser.required_field(SourceParagraphFields::VERSION, spgh->raw_version);
auto pv_str = parser.optional_field(SourceParagraphFields::PORT_VERSION);
if (!pv_str.empty())
{
auto pv_opt = Strings::strto<int>(pv_str);
if (auto pv = pv_opt.get())
{
spgh->port_version = *pv;
}
else
{
parser.add_type_error(SourceParagraphFields::PORT_VERSION, msg::format(msgANonNegativeInteger));
}
}
spgh->description = Strings::split(parser.optional_field(SourceParagraphFields::DESCRIPTION), '\n');
trim_all(spgh->description);
spgh->maintainers = Strings::split(parser.optional_field(SourceParagraphFields::MAINTAINERS), '\n');
trim_all(spgh->maintainers);
spgh->homepage = parser.optional_field(SourceParagraphFields::HOMEPAGE);
TextRowCol textrowcol;
std::string buf;
parser.optional_field(SourceParagraphFields::BUILD_DEPENDS, {buf, textrowcol});
auto maybe_dependencies = parse_dependencies_list(buf, origin, textrowcol);
if (const auto dependencies = maybe_dependencies.get())
{
spgh->dependencies = *dependencies;
}
else
{
return ParseControlErrorInfo::from_error(origin, std::move(maybe_dependencies).error());
}
buf.clear();
parser.optional_field(SourceParagraphFields::DEFAULT_FEATURES, {buf, textrowcol});
auto maybe_default_features = parse_default_features_list(buf, origin, textrowcol);
if (const auto default_features = maybe_default_features.get())
{
for (auto&& default_feature : *default_features)
{
if (default_feature == "core")
{
return ParseControlErrorInfo::from_error(origin, msg::format_error(msgDefaultFeatureCore));
}
if (default_feature == "default")
{
return ParseControlErrorInfo::from_error(origin, msg::format_error(msgDefaultFeatureDefault));
}
if (!Json::IdentifierDeserializer::is_ident(default_feature))
{
return ParseControlErrorInfo::from_error(origin,
msg::format_error(msgDefaultFeatureIdentifier)
.append_raw('\n')
.append(msgNoteMessage)
.append(msgParseIdentifierError,
msg::value = default_feature,
msg::url = docs::manifests_url));
}
spgh->default_features.push_back({std::move(default_feature)});
}
}
else
{
return ParseControlErrorInfo::from_error(origin, std::move(maybe_default_features).error());
}
auto supports_expr = parser.optional_field(SourceParagraphFields::SUPPORTS);
if (!supports_expr.empty())
{
auto maybe_expr = PlatformExpression::parse_platform_expression(
supports_expr, PlatformExpression::MultipleBinaryOperators::Allow);
if (auto expr = maybe_expr.get())
{
spgh->supports_expression = std::move(*expr);
}
else
{
parser.add_type_error(SourceParagraphFields::SUPPORTS, msg::format(msgAPlatformExpression));
}
}
// This is leftover from a previous attempt to add "alias ports", not currently used.
(void)parser.optional_field(SourceParagraphFields::TYPE);
auto err = parser.error_info(spgh->name.empty() ? origin : spgh->name);
if (err)
return err;
else
return spgh;
}
static ParseExpected<FeatureParagraph> parse_feature_paragraph(StringView origin, Paragraph&& fields)
{
ParagraphParser parser(std::move(fields));
auto fpgh = std::make_unique<FeatureParagraph>();
parser.required_field(SourceParagraphFields::FEATURE, fpgh->name);
fpgh->description = Strings::split(parser.required_field(SourceParagraphFields::DESCRIPTION), '\n');
trim_all(fpgh->description);
auto maybe_dependencies =
parse_dependencies_list(parser.optional_field(SourceParagraphFields::BUILD_DEPENDS), origin);
if (maybe_dependencies.has_value())
{
fpgh->dependencies = maybe_dependencies.value_or_exit(VCPKG_LINE_INFO);
}
else
{
return ParseControlErrorInfo::from_error(origin, std::move(maybe_dependencies).error());
}
auto err = parser.error_info(fpgh->name.empty() ? origin : fpgh->name);
if (err)
return err;
else
return fpgh;
}
ParseExpected<SourceControlFile> SourceControlFile::parse_control_file(StringView origin,
std::vector<Paragraph>&& control_paragraphs)
{
if (control_paragraphs.size() == 0)
{
auto ret = std::make_unique<ParseControlErrorInfo>();
ret->name = origin.to_string();
return ret;
}
auto control_file = std::make_unique<SourceControlFile>();
auto maybe_source = parse_source_paragraph(origin, std::move(control_paragraphs.front()));
if (const auto source = maybe_source.get())
control_file->core_paragraph = std::move(*source);
else
return std::move(maybe_source).error();
control_paragraphs.erase(control_paragraphs.begin());
for (auto&& feature_pgh : control_paragraphs)
{
auto maybe_feature = parse_feature_paragraph(origin, std::move(feature_pgh));
if (const auto feature = maybe_feature.get())
control_file->feature_paragraphs.emplace_back(std::move(*feature));
else
return std::move(maybe_feature).error();
}
if (auto maybe_error = canonicalize(*control_file))
{
return maybe_error;
}
return control_file;
}
struct PlatformExprDeserializer final : Json::IDeserializer<PlatformExpression::Expr>
{
virtual LocalizedString type_name() const override { return msg::format(msgAPlatformExpression); }
virtual Optional<PlatformExpression::Expr> visit_string(Json::Reader& r, StringView sv) const override
{
auto opt =
PlatformExpression::parse_platform_expression(sv, PlatformExpression::MultipleBinaryOperators::Deny);
if (auto res = opt.get())
{
return std::move(*res);
}
else
{
r.add_generic_error(type_name(), std::move(opt).error());
return PlatformExpression::Expr();
}
}
static const PlatformExprDeserializer instance;
};
const PlatformExprDeserializer PlatformExprDeserializer::instance;
struct DefaultFeatureNameDeserializer : Json::IDeserializer<std::string>
{
virtual LocalizedString type_name() const override { return msg::format(msgADefaultFeature); }
virtual Optional<std::string> visit_string(Json::Reader& r, StringView sv) const override
{
if (sv == "core")
{
r.add_generic_error(type_name(), msg::format(msgDefaultFeatureCore));
return sv.to_string();
}
if (sv == "default")
{
r.add_generic_error(type_name(), msg::format(msgDefaultFeatureDefault));
return sv.to_string();
}
return Json::FeatureNameDeserializer::instance.visit_string(r, sv);
}
static const DefaultFeatureNameDeserializer instance;
};
const DefaultFeatureNameDeserializer DefaultFeatureNameDeserializer::instance;
struct DefaultFeatureDeserializer : Json::IDeserializer<DependencyRequestedFeature>
{
LocalizedString type_name() const override { return msg::format(msgADefaultFeature); }
constexpr static StringLiteral NAME = "name";
constexpr static StringLiteral PLATFORM = "platform";
Span<const StringView> valid_fields() const override
{
static const StringView t[] = {
NAME,
PLATFORM,
};
return t;
}
Optional<DependencyRequestedFeature> visit_string(Json::Reader& r, StringView sv) const override
{
return DefaultFeatureNameDeserializer::instance.visit_string(r, sv).map(
[](std::string&& name) { return DependencyRequestedFeature{std::move(name)}; });
}
Optional<DependencyRequestedFeature> visit_object(Json::Reader& r, const Json::Object& obj) const override
{
DependencyRequestedFeature answer;
r.required_object_field(type_name(), obj, NAME, answer.name, DefaultFeatureNameDeserializer::instance);
r.optional_object_field(obj, PLATFORM, answer.platform, PlatformExprDeserializer::instance);
return answer;
}
const static DefaultFeatureDeserializer instance;
};
const DefaultFeatureDeserializer DefaultFeatureDeserializer::instance;
struct DefaultFeatureArrayDeserializer : Json::ArrayDeserializer<DefaultFeatureDeserializer>
{
LocalizedString type_name() const override { return msg::format(msgAnArrayOfDefaultFeatures); }
static const DefaultFeatureArrayDeserializer instance;
};
const DefaultFeatureArrayDeserializer DefaultFeatureArrayDeserializer::instance;
struct DependencyFeatureNameDeserializer : Json::IDeserializer<std::string>
{
virtual LocalizedString type_name() const override { return msg::format(msgAFeatureName); }
virtual Optional<std::string> visit_string(Json::Reader& r, StringView sv) const override
{
if (sv == "core")
{
r.add_generic_error(type_name(), msg::format(msgDependencyFeatureCore));
return sv.to_string();
}
if (sv == "default")
{
r.add_generic_error(type_name(), msg::format(msgDependencyFeatureDefault));
return sv.to_string();
}
return Json::FeatureNameDeserializer::instance.visit_string(r, sv);
}
static const DependencyFeatureNameDeserializer instance;
};
const DependencyFeatureNameDeserializer DependencyFeatureNameDeserializer::instance;
struct DependencyFeatureDeserializer : Json::IDeserializer<DependencyRequestedFeature>
{
LocalizedString type_name() const override { return msg::format(msgADependencyFeature); }
constexpr static StringLiteral NAME = "name";
constexpr static StringLiteral PLATFORM = "platform";
Span<const StringView> valid_fields() const override
{
static const StringView t[] = {
NAME,
PLATFORM,
};
return t;
}
Optional<DependencyRequestedFeature> visit_string(Json::Reader& r, StringView sv) const override
{
return DependencyFeatureNameDeserializer::instance.visit_string(r, sv).map(
[](std::string&& name) { return DependencyRequestedFeature{std::move(name)}; });
}
Optional<DependencyRequestedFeature> visit_object(Json::Reader& r, const Json::Object& obj) const override
{
DependencyRequestedFeature result;
r.required_object_field(type_name(), obj, NAME, result.name, DependencyFeatureNameDeserializer::instance);
r.optional_object_field(obj, PLATFORM, result.platform, PlatformExprDeserializer::instance);
return result;
}
const static DependencyFeatureDeserializer instance;
};
const DependencyFeatureDeserializer DependencyFeatureDeserializer::instance;
struct DependencyFeatureArrayDeserializer : Json::ArrayDeserializer<DependencyFeatureDeserializer>
{
LocalizedString type_name() const override { return msg::format(msgAnArrayOfFeatures); }
static const DependencyFeatureArrayDeserializer instance;
};
const DependencyFeatureArrayDeserializer DependencyFeatureArrayDeserializer::instance;
struct DependencyDeserializer final : Json::IDeserializer<Dependency>
{
virtual LocalizedString type_name() const override { return msg::format(msgADependency); }
constexpr static StringLiteral NAME = "name";
constexpr static StringLiteral HOST = "host";
constexpr static StringLiteral FEATURES = "features";
constexpr static StringLiteral DEFAULT_FEATURES = "default-features";
constexpr static StringLiteral PLATFORM = "platform";
constexpr static StringLiteral VERSION_GE = "version>=";
virtual Span<const StringView> valid_fields() const override
{
static constexpr StringView t[] = {
NAME,
HOST,
FEATURES,
DEFAULT_FEATURES,
PLATFORM,
VERSION_GE,
};
return t;
}
virtual Optional<Dependency> visit_string(Json::Reader& r, StringView sv) const override
{
return Json::PackageNameDeserializer::instance.visit_string(r, sv).map(
[](std::string&& name) { return Dependency{std::move(name)}; });
}
virtual Optional<Dependency> visit_object(Json::Reader& r, const Json::Object& obj) const override
{
Dependency dep;
for (const auto& el : obj)
{
if (Strings::starts_with(el.first, "$"))
{
dep.extra_info.insert_or_replace(el.first.to_string(), el.second);
}
}
r.required_object_field(type_name(), obj, NAME, dep.name, Json::PackageNameDeserializer::instance);
r.optional_object_field(obj, FEATURES, dep.features, DependencyFeatureArrayDeserializer::instance);
r.optional_object_field(obj, DEFAULT_FEATURES, dep.default_features, Json::BooleanDeserializer::instance);
r.optional_object_field(obj, HOST, dep.host, Json::BooleanDeserializer::instance);
r.optional_object_field(obj, PLATFORM, dep.platform, PlatformExprDeserializer::instance);
auto has_ge_constraint = r.optional_object_field(
obj, VERSION_GE, dep.constraint.value, VersionConstraintStringDeserializer::instance);
if (has_ge_constraint)
{
dep.constraint.type = VersionConstraintKind::Minimum;
const auto& constraint_value = dep.constraint.value;
auto h = constraint_value.find('#');
if (h != std::string::npos)
{
auto opt = Strings::strto<int>(ZStringView{constraint_value}.substr(h + 1));
auto v = opt.get();
if (v && *v >= 0)
{
dep.constraint.port_version = *v;
}
else
{
r.add_generic_error(type_name(),
msg::format(msgVersionConstraintPortVersionMustBePositiveInteger));
}
dep.constraint.value.erase(h);
}
}
return dep;
}
static DependencyDeserializer instance;
};
DependencyDeserializer DependencyDeserializer::instance;
struct DependencyArrayDeserializer final : Json::IDeserializer<std::vector<Dependency>>
{
virtual LocalizedString type_name() const override { return msg::format(msgAnArrayOfDependencies); }
virtual Optional<std::vector<Dependency>> visit_array(Json::Reader& r, const Json::Array& arr) const override
{
return r.array_elements(arr, DependencyDeserializer::instance);
}
static const DependencyArrayDeserializer instance;
};
const DependencyArrayDeserializer DependencyArrayDeserializer::instance;
constexpr StringLiteral DependencyDeserializer::NAME;
constexpr StringLiteral DependencyDeserializer::HOST;
constexpr StringLiteral DependencyDeserializer::FEATURES;
constexpr StringLiteral DependencyDeserializer::DEFAULT_FEATURES;
constexpr StringLiteral DependencyDeserializer::PLATFORM;
constexpr StringLiteral DependencyDeserializer::VERSION_GE;
struct DependencyOverrideDeserializer final : Json::IDeserializer<DependencyOverride>
{
virtual LocalizedString type_name() const override { return msg::format(msgAnOverride); }
constexpr static StringLiteral NAME = "name";
virtual Span<const StringView> valid_fields() const override
{
static constexpr StringView u[] = {NAME};
static const auto t = Util::Vectors::concat<StringView>(schemed_deserializer_fields(), u);
return t;
}
virtual Optional<DependencyOverride> visit_object(Json::Reader& r, const Json::Object& obj) const override
{
DependencyOverride dep;
for (const auto& el : obj)
{
if (Strings::starts_with(el.first, "$"))
{
dep.extra_info.insert_or_replace(el.first.to_string(), el.second);
}
}
const auto type_name = this->type_name();
r.required_object_field(type_name, obj, NAME, dep.name, Json::PackageNameDeserializer::instance);
auto schemed_version = visit_required_schemed_deserializer(type_name, r, obj, true);
dep.version = std::move(schemed_version.version);
dep.scheme = schemed_version.scheme;
return dep;
}
static const DependencyOverrideDeserializer instance;
};
const DependencyOverrideDeserializer DependencyOverrideDeserializer::instance;
constexpr StringLiteral DependencyOverrideDeserializer::NAME;
struct DependencyOverrideArrayDeserializer : Json::ArrayDeserializer<DependencyOverrideDeserializer>
{
LocalizedString type_name() const override { return msg::format(msgAnArrayOfDependencyOverrides); }
static const DependencyOverrideArrayDeserializer instance;
};
const DependencyOverrideArrayDeserializer DependencyOverrideArrayDeserializer::instance;
static constexpr StringLiteral VALID_LICENSES[] = {
#include "spdx-licenses.inc"
};
static constexpr StringLiteral VALID_EXCEPTIONS[] = {
#include "spdx-exceptions.inc"
};
// The "license" field; either:
// * a string, which must be an SPDX license expression.
// EBNF located at: https://learn.microsoft.com/vcpkg/reference/vcpkg-json#license
// * `null`, for when the license of the package cannot be described by an SPDX expression
struct SpdxLicenseExpressionParser : ParserBase
{
SpdxLicenseExpressionParser(StringView sv, StringView origin) : ParserBase(sv, origin) { }
static const StringLiteral* case_insensitive_find(View<StringLiteral> lst, StringView id)
{
return Util::find_if(lst,
[id](StringLiteral el) { return Strings::case_insensitive_ascii_equals(id, el); });
}
static constexpr bool is_idstring_element(char32_t ch) { return is_alphanumdash(ch) || ch == '.'; }
enum class Expecting
{
License, // at the beginning, or after a compound (AND, OR)
Exception, // after a WITH
CompoundOrWith, // after a license
Compound, // after an exception (only one WITH is allowed), or after a close paren
};
void eat_idstring(std::string& result, Expecting& expecting)
{
auto loc = cur_loc();
auto token = match_while(is_idstring_element);
if (Strings::starts_with(token, "DocumentRef-"))
{
add_error(msg::format(msgLicenseExpressionDocumentRefUnsupported), loc);
if (cur() == ':')
{
next();
}
return;
}
else if (token == "AND" || token == "OR" || token == "WITH")
{
if (expecting == Expecting::License)
{
add_error(msg::format(msgLicenseExpressionExpectLicenseFoundCompound, msg::value = token), loc);
}
if (expecting == Expecting::Exception)
{
add_error(msg::format(msgLicenseExpressionExpectExceptionFoundCompound, msg::value = token), loc);
}
if (token == "WITH")
{
if (expecting == Expecting::Compound)
{
add_error(msg::format(msgLicenseExpressionExpectCompoundFoundWith), loc);
}
expecting = Expecting::Exception;
}
else
{
expecting = Expecting::License;
}
result.push_back(' ');
result.append(token.begin(), token.end());
result.push_back(' ');
return;
}
switch (expecting)
{
case Expecting::Compound:
add_error(msg::format(msgLicenseExpressionExpectCompoundFoundWord, msg::value = token), loc);
break;
case Expecting::CompoundOrWith:
add_error(msg::format(msgLicenseExpressionExpectCompoundOrWithFoundWord, msg::value = token), loc);
break;
case Expecting::License:
if (Strings::starts_with(token, "LicenseRef-"))
{
result.append(token.begin(), token.end());
}
else
{
auto it = case_insensitive_find(VALID_LICENSES, token);
if (it != std::end(VALID_LICENSES))
{
result.append(it->begin(), it->end());
}
else
{
add_warning(msg::format(msgLicenseExpressionUnknownLicense, msg::value = token), loc);
result.append(token.begin(), token.end());
}
if (cur() == '+')
{
next();
result.push_back('+');
}
}
expecting = Expecting::CompoundOrWith;
break;
case Expecting::Exception:
auto it = case_insensitive_find(VALID_EXCEPTIONS, token);
if (it != std::end(VALID_EXCEPTIONS))
{
// case normalization
result.append(it->begin(), it->end());
}
else
{
add_warning(msg::format(msgLicenseExpressionUnknownException, msg::value = token), loc);
result.append(token.begin(), token.end());
}
expecting = Expecting::Compound;
break;
}
}
std::string parse()
{
if (cur() == Unicode::end_of_file)
{
add_error(msg::format(msgEmptyLicenseExpression));
return {};
}
Expecting expecting = Expecting::License;
std::string result;
size_t open_parens = 0;
while (!at_eof())
{
skip_whitespace();
switch (cur())
{
case '(':
if (expecting == Expecting::Compound || expecting == Expecting::CompoundOrWith)
{
add_error(msg::format(msgLicenseExpressionExpectCompoundFoundParen));
}
if (expecting == Expecting::Exception)
{
add_error(msg::format(msgLicenseExpressionExpectExceptionFoundParen));
}
result.push_back('(');
expecting = Expecting::License;
++open_parens;
next();
break;
case ')':
if (expecting == Expecting::License)
{
add_error(msg::format(msgLicenseExpressionExpectLicenseFoundParen));
}
else if (expecting == Expecting::Exception)
{
add_error(msg::format(msgLicenseExpressionExpectExceptionFoundParen));
}
if (open_parens == 0)
{
add_error(msg::format(msgLicenseExpressionImbalancedParens));
}
result.push_back(')');
expecting = Expecting::Compound;
--open_parens;
next();
break;
case '+':
add_error(msg::format(msgLicenseExpressionContainsExtraPlus));
next();
break;
default:
if (cur() > 0x7F)
{
auto ch = cur();
auto first = it().pointer_to_current();
next();
auto last = it().pointer_to_current();
add_error(msg::format(msgLicenseExpressionContainsUnicode,
msg::value = static_cast<uint32_t>(ch),
msg::pretty_value = StringView{first, last}));
break;
}
if (!is_idstring_element(cur()))
{
add_error(msg::format(msgLicenseExpressionContainsInvalidCharacter,
msg::value = static_cast<char>(cur())));
next();
break;
}
eat_idstring(result, expecting);
break;
}
}
if (expecting == Expecting::License)
{