From e9480a625f34ec96130d6854def38406ecebcafb Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Wed, 27 Nov 2024 14:13:37 +0100 Subject: [PATCH] Allow disabling default features with vcpkg --- docs/cmake-toml.md | 2 +- include/project_parser.hpp | 1 + src/cmake_generator.cpp | 7 +++++-- src/project_parser.cpp | 7 ++++++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/cmake-toml.md b/docs/cmake-toml.md index 8d9355a..ff242d1 100644 --- a/docs/cmake-toml.md +++ b/docs/cmake-toml.md @@ -168,7 +168,7 @@ overlay-ports = ["my-ports"] The vcpkg `version` will automatically generate the `url` from the [official repository](https://github.com/microsoft/vcpkg/releases). For a custom registry you can specify your own `url` (and omit the `version`). You can browse available packages on [vcpkg.io](https://vcpkg.io/en/packages.html). -To specify package features you can use the following syntax: `imgui[docking-experimental,freetype,sdl2-binding,opengl3-binding]`. +To specify package features you can use the following syntax: `imgui[docking-experimental,freetype,sdl2-binding,opengl3-binding]`. To disable the [default features](https://learn.microsoft.com/en-us/vcpkg/concepts/default-features) you can do: `cpp-httplib[core,openssl]` The `overlay-ports` feature allows you to embed vcpkg ports inside your project, without having to fork the main vcpkg registry or creating a custom registry. You can find more information in the relevant [documentation](https://learn.microsoft.com/en-us/vcpkg/concepts/overlay-ports). diff --git a/include/project_parser.hpp b/include/project_parser.hpp index debaab1..4de3870 100644 --- a/include/project_parser.hpp +++ b/include/project_parser.hpp @@ -45,6 +45,7 @@ struct Vcpkg { struct Package { std::string name; std::vector features; + bool default_features = true; }; std::vector packages; diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index 69b34fa..436985c 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -957,17 +957,20 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { throw std::runtime_error("Invalid vcpkg package feature '" + feature + "', name is reserved"); } } - if (features.empty()) { + if (features.empty() && package.default_features) { ofs << " \"" << package.name << '\"'; } else { ofs << " {\n"; ofs << " \"name\": \"" << package.name << "\",\n"; + if (!package.default_features) { + ofs << " \"default-features\": false,\n"; + } ofs << " \"features\": ["; for (size_t j = 0; j < features.size(); j++) { const auto &feature = features[j]; ofs << '\"' << feature << '\"'; if (j + 1 < features.size()) { - ofs << ','; + ofs << ", "; } } ofs << "]\n"; diff --git a/src/project_parser.cpp b/src/project_parser.cpp index 822ca53..38a1b04 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -847,7 +847,12 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p std::istringstream feature_stream{features}; std::string feature; while (std::getline(feature_stream, feature, ',')) { - package.features.emplace_back(feature); + // Disable default features with package-name[core,feature1] + if (feature == "core") { + package.default_features = false; + } else { + package.features.emplace_back(feature); + } } } else { throw_key_error("Invalid package name '" + package_str + "'", "packages", p);