Skip to content

Commit a6d5f1c

Browse files
authored
Merge pull request #41 from cursey/vcpkg-package-features
Add support for vcpkg package features
2 parents 83087ff + dc852b9 commit a6d5f1c

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

include/project_parser.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ struct Package {
3333
struct Vcpkg {
3434
std::string version;
3535
std::string url;
36-
std::vector<std::string> packages;
36+
37+
struct Package {
38+
std::string name;
39+
std::vector<std::string> features;
40+
};
41+
42+
std::vector<Package> packages;
3743
};
3844

3945
enum TargetType {

src/cmake_generator.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
579579

580580
// Show a nicer error than vcpkg when specifying an invalid package name
581581
for (const auto &package : project.vcpkg.packages) {
582-
if (!vcpkg_valid_identifier(package)) {
583-
throw std::runtime_error("Invalid [vcpkg].packages name '" + package + "' (needs to be lowercase alphanumeric)");
582+
if (!vcpkg_valid_identifier(package.name)) {
583+
throw std::runtime_error("Invalid [vcpkg].packages name '" + package.name + "' (needs to be lowercase alphanumeric)");
584584
}
585585
}
586586

@@ -611,10 +611,31 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
611611
const auto &packages = project.vcpkg.packages;
612612
for (size_t i = 0; i < packages.size(); i++) {
613613
const auto &package = packages[i];
614-
if (!vcpkg_valid_identifier(package)) {
615-
throw std::runtime_error("Invalid vcpkg package name '" + package + "'");
614+
const auto &features = package.features;
615+
if (!vcpkg_valid_identifier(package.name)) {
616+
throw std::runtime_error("Invalid vcpkg package name '" + package.name + "'");
617+
}
618+
for (const auto &feature : features) {
619+
if (!vcpkg_valid_identifier(feature)) {
620+
throw std::runtime_error("Invalid vcpkg package feature '" + feature + "'");
621+
}
622+
}
623+
if (features.empty()) {
624+
ofs << " \"" << package.name << '\"';
625+
} else {
626+
ofs << " {\n";
627+
ofs << " \"name\": \"" << package.name << "\",\n";
628+
ofs << " \"features\": [";
629+
for (size_t j = 0; j < features.size(); j++) {
630+
const auto &feature = features[j];
631+
ofs << '\"' << feature << '\"';
632+
if (j + 1 < features.size()) {
633+
ofs << ',';
634+
}
635+
}
636+
ofs << "]\n";
637+
ofs << " }";
616638
}
617-
ofs << " \"" << package << '\"';
618639
if (i + 1 < packages.size()) {
619640
ofs << ',';
620641
}

src/project_parser.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,26 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
495495
auto &v = checker.create(toml, "vcpkg");
496496
v.optional("url", vcpkg.url);
497497
v.optional("version", vcpkg.version);
498-
v.required("packages", vcpkg.packages);
498+
for (const auto &p : v.find("packages").as_array()) {
499+
Vcpkg::Package package;
500+
const auto &package_str = p.as_string().str;
501+
const auto open_bracket = package_str.find('[');
502+
const auto close_bracket = package_str.find(']', open_bracket);
503+
if (open_bracket == std::string::npos && close_bracket == std::string::npos) {
504+
package.name = package_str;
505+
} else if (close_bracket != std::string::npos) {
506+
package.name = package_str.substr(0, open_bracket);
507+
const auto features = package_str.substr(open_bracket + 1, close_bracket - open_bracket - 1);
508+
std::istringstream feature_stream{features};
509+
std::string feature;
510+
while (std::getline(feature_stream, feature, ',')) {
511+
package.features.emplace_back(feature);
512+
}
513+
} else {
514+
throw std::runtime_error("Invalid vcpkg package '" + package_str + "'");
515+
}
516+
vcpkg.packages.emplace_back(std::move(package));
517+
}
499518
}
500519

501520
checker.check(conditions);

0 commit comments

Comments
 (0)