Add support for vcpkg package features

main
cursey 2 years ago
parent 83087ff06e
commit 3201fd052e
No known key found for this signature in database
GPG Key ID: A6A7163A1B8FD42C

@ -33,7 +33,13 @@ struct Package {
struct Vcpkg {
std::string version;
std::string url;
std::vector<std::string> packages;
struct Package {
std::string name;
std::vector<std::string> features;
};
std::vector<Package> packages;
};
enum TargetType {

@ -579,8 +579,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
// Show a nicer error than vcpkg when specifying an invalid package name
for (const auto &package : project.vcpkg.packages) {
if (!vcpkg_valid_identifier(package)) {
throw std::runtime_error("Invalid [vcpkg].packages name '" + package + "' (needs to be lowercase alphanumeric)");
if (!vcpkg_valid_identifier(package.name)) {
throw std::runtime_error("Invalid [vcpkg].packages name '" + package.name + "' (needs to be lowercase alphanumeric)");
}
}
@ -611,13 +611,28 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
const auto &packages = project.vcpkg.packages;
for (size_t i = 0; i < packages.size(); i++) {
const auto &package = packages[i];
if (!vcpkg_valid_identifier(package)) {
throw std::runtime_error("Invalid vcpkg package name '" + package + "'");
if (!vcpkg_valid_identifier(package.name)) {
throw std::runtime_error("Invalid vcpkg package name '" + package.name + "'");
}
ofs << " \"" << package << '\"';
if (i + 1 < packages.size()) {
ofs << ',';
if (package.features.empty()) {
ofs << " \"" << package.name << '\"';
} else {
ofs << " {\n";
ofs << " \"name\": \"" << package.name << "\",\n";
ofs << " \"features\": [";
for (size_t j = 0; j < package.features.size(); j++) {
const auto &feature = package.features[j];
ofs << '\"' << feature << '\"';
if (j + 1 < package.features.size()) {
ofs << ',';
}
}
ofs << "]\n";
ofs << " }";
}
if (i + 1 < packages.size()) {
ofs << ',';
}
ofs << '\n';
}

@ -495,7 +495,26 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
auto &v = checker.create(toml, "vcpkg");
v.optional("url", vcpkg.url);
v.optional("version", vcpkg.version);
v.required("packages", vcpkg.packages);
for (const auto &p : v.find("packages").as_array()) {
Vcpkg::Package package;
const auto &package_str = p.as_string().str;
const auto open_bracket = package_str.find('[');
const auto close_bracket = package_str.find(']', open_bracket);
if (open_bracket == std::string::npos && close_bracket == std::string::npos) {
package.name = package_str;
} else if (close_bracket != std::string::npos) {
package.name = package_str.substr(0, open_bracket);
auto features = package_str.substr(open_bracket + 1, close_bracket - open_bracket - 1);
std::istringstream feature_stream{features};
std::string feature;
while (std::getline(feature_stream, feature, ',')) {
package.features.emplace_back(feature);
}
} else {
throw std::runtime_error("Badly formed vcpkg package name");
}
vcpkg.packages.emplace_back(std::move(package));
}
}
checker.check(conditions);

Loading…
Cancel
Save