Skip to content

Commit

Permalink
Merge pull request #158 from build-cpp/vcpkg-default-features
Browse files Browse the repository at this point in the history
Allow disabling default features with vcpkg
  • Loading branch information
mrexodia authored Nov 27, 2024
2 parents 13a7a87 + e9480a6 commit b52cf5b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/cmake-toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
1 change: 1 addition & 0 deletions include/project_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct Vcpkg {
struct Package {
std::string name;
std::vector<std::string> features;
bool default_features = true;
};

std::vector<Package> packages;
Expand Down
7 changes: 5 additions & 2 deletions src/cmake_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
7 changes: 6 additions & 1 deletion src/project_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b52cf5b

Please sign in to comment.