feat(features): additive C++ feature flags (G2)
VoidBuildSystem CI / 1 · Requirements (push) Successful in 8s
VoidBuildSystem CI / 2A · Linux build (debug) (push) Successful in 2m47s
VoidBuildSystem CI / 2A · Linux build (coverage) (push) Successful in 2m53s
VoidBuildSystem CI / 2A · Linux build (sanitizers) (push) Successful in 3m36s
VoidBuildSystem CI / 2B · Windows Wine build (debug) (push) Successful in 1m57s
VoidBuildSystem CI / 2B · Windows Wine build (relwithdebinfo) (push) Successful in 3m23s
VoidBuildSystem CI / 3B · Windows Wine test (debug) (push) Successful in 25s
VoidBuildSystem CI / 3B · Windows Wine test (relwithdebinfo) (push) Successful in 24s
VoidBuildSystem CI / 2A · Linux build (relwithdebinfo) (push) Successful in 6m1s
VoidBuildSystem CI / 3A · Linux test (coverage) (push) Failing after 2m2s
VoidBuildSystem CI / 3A · Linux test (sanitizers) (push) Successful in 2m2s
VoidBuildSystem CI / 3A · Linux test (relwithdebinfo) (push) Failing after 6m54s
VoidBuildSystem CI / 3A · Linux test (debug) (push) Failing after 7m4s
VoidBuildSystem CI / 4A2 · VBS installers (linux target) (push) Skipped
VoidBuildSystem CI / 4A2 · VBS installers (windows target) (push) Skipped
VoidBuildSystem CI / 5 · Release (push) Skipped

- Project::feature(name, default_on) / FeatureDescription, Target's
  require_feature() (the `.requires` spelling is a C++20 keyword), and
  Dependency::when(feature) for optional dependencies.
- --features a,b / --no-default-features on build, test, run, explain and
  deps resolve/update. Unification is additive: defaults (unless
  disabled) + features required by targets + requested features.
- Enabled features add VBS_FEATURE_<NAME> compile definitions to every
  target, so action keys and the cache are feature-aware; switching the
  set invalidates cached actions.
- Optional dependencies are filtered before resolution, so vbs.lock
  contains only active ones.
- vbs explain --show-features prints the unified set with reasons
  (default/requested/required by target); vbs metadata lists declared
  features with their enabled state; the descriptor JSON carries features
  and target requirements.

API note: features provide preprocessor-level configuration (defines), not
ABI/layout changes across a feature boundary.

Verified: features_unify_defines_and_invalidate_action_keys and
optional_dependencies_follow_feature_selection pass; vbs_core_tests 8/8;
CLI contracts pass; clang-format clean.
This commit is contained in:
faumaray committed 2026-12-06 20:01:29 +03:00
1 parent 8b21583eeb
commit f03524628b
10 files changed
+512 -3

No files matched your search

+204
View File
@@ -4982,3 +4982,207 @@ VOID_TEST(FullV1Tests, audit_matches_osv_advisories_against_lock) {
advisories.string()});
Void::Testing::expect(clean.has_value() && clean->succeeded());
}
// === Feature flags: additive unification, defines and feature-aware action keys (G2) ===
VOID_TEST(FullV1Tests, features_unify_defines_and_invalidate_action_keys) {
const auto vbs_bin = locate_vbs_binary();
Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin));
if (vbs_bin.empty() || !std::filesystem::exists(vbs_bin)) {
return;
}
Void::FileSystem::TempDir tmp{"vbs_features_"};
const auto proj = tmp / "proj";
write_file(proj / "build.cpp", R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &p) {
p.feature("ssl", true);
p.feature("simd", false);
p.executable("app")
.cxx_standard(Void::Build::CxxStandard::Cxx26)
.sources({"src/main.cpp"})
.require_feature("simd");
}
)cpp");
write_file(proj / "src" / "main.cpp", R"cpp(#include <cstdio>
int main() {
#ifdef VBS_FEATURE_SSL
std::puts("ssl");
#endif
#ifdef VBS_FEATURE_SIMD
std::puts("simd");
#endif
return 0;
}
)cpp");
auto run_vbs = [&](std::vector<std::string> args) {
Platform::ProcessRequest req;
req.executable = vbs_bin;
req.arguments = std::move(args);
req.timeout = std::chrono::seconds{600};
return Platform::run_supervised(req);
};
auto default_run = run_vbs({"run", "--project", proj.string(), "--build-dir",
(tmp / "build-default").string(), "--target", "app"});
Void::Testing::expect(default_run.has_value() && default_run->succeeded());
if (default_run) {
Void::Testing::expect(default_run->stdout_data.find("ssl") != std::string::npos);
Void::Testing::expect(default_run->stdout_data.find("simd") != std::string::npos);
}
auto no_default = run_vbs({"run", "--project", proj.string(), "--build-dir",
(tmp / "build-nodefault").string(), "--target", "app",
"--no-default-features"});
Void::Testing::expect(no_default.has_value() && no_default->succeeded());
if (no_default) {
Void::Testing::expect(no_default->stdout_data.find("ssl") == std::string::npos);
Void::Testing::expect(no_default->stdout_data.find("simd") != std::string::npos);
}
auto explicit_ssl = run_vbs({"run", "--project", proj.string(), "--build-dir",
(tmp / "build-ssl").string(), "--target", "app",
"--no-default-features", "--features", "ssl"});
Void::Testing::expect(explicit_ssl.has_value() && explicit_ssl->succeeded());
if (explicit_ssl) {
Void::Testing::expect(explicit_ssl->stdout_data.find("ssl") != std::string::npos);
Void::Testing::expect(explicit_ssl->stdout_data.find("simd") != std::string::npos);
}
auto unknown = run_vbs({"build", "--project", proj.string(), "--build-dir",
(tmp / "build-unknown").string(), "--features", "nope"});
Void::Testing::expect(unknown.has_value() && unknown->exit_code == 2);
if (unknown) {
Void::Testing::expect(unknown->stderr_data.find("unknown feature 'nope'") !=
std::string::npos);
}
// Feature selection changes the action keys: a cached default build is stale for
// --no-default-features.
auto explained_default = run_vbs({"explain", "--project", proj.string(), "--build-dir",
(tmp / "build-default").string(), "--format", "json"});
Void::Testing::expect(explained_default.has_value() && explained_default->succeeded());
auto explained_other = run_vbs({"explain", "--project", proj.string(), "--build-dir",
(tmp / "build-default").string(), "--no-default-features",
"--format", "json"});
Void::Testing::expect(explained_other.has_value() && explained_other->succeeded());
const auto would_run = [](const Platform::ProcessResult &result) -> long long {
auto parsed = Void::Json::Strict::parse(result.stdout_data);
if (!parsed) {
return -1;
}
const auto *root = std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
if (root == nullptr) {
return -1;
}
const auto *value = std::get_if<std::int64_t>(&root->at("would_run").value);
return value ? *value : -1;
};
Void::Testing::expect_eq(would_run(*explained_default), 0LL);
Void::Testing::expect(would_run(*explained_other) >= 1);
// `explain --show-features` reports the unification reasons.
auto shown = run_vbs({"explain", "--project", proj.string(), "--build-dir",
(tmp / "build-default").string(), "--show-features"});
Void::Testing::expect(shown.has_value() && shown->succeeded());
if (shown) {
Void::Testing::expect(shown->stdout_data.find("ssl: enabled (default)") !=
std::string::npos);
Void::Testing::expect(shown->stdout_data.find("simd: enabled (required by app)") !=
std::string::npos);
}
// `vbs metadata` exposes the declared features and their enabled state.
auto metadata = run_vbs({"metadata", "--project", proj.string(), "--build-dir",
(tmp / "build-default").string()});
Void::Testing::expect(metadata.has_value() && metadata->succeeded());
if (metadata) {
auto parsed = Void::Json::Strict::parse(metadata->stdout_data);
Void::Testing::require(parsed.has_value());
const auto *root = std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
Void::Testing::require(root != nullptr);
const auto *features =
std::get_if<Void::Json::Strict::Value::Array>(&root->at("features").value);
Void::Testing::require(features != nullptr);
Void::Testing::expect_eq(features->size(), std::size_t{2});
bool ssl_enabled = false;
bool simd_enabled = false;
for (const auto &feature : *features) {
const auto *object = std::get_if<Void::Json::Strict::Value::Object>(&feature.value);
if (object == nullptr) {
continue;
}
const auto *name = std::get_if<std::string>(&object->at("name").value);
const auto *enabled = std::get_if<bool>(&object->at("enabled").value);
if (name != nullptr && enabled != nullptr && *enabled) {
if (*name == "ssl") {
ssl_enabled = true;
}
if (*name == "simd") {
simd_enabled = true;
}
}
}
Void::Testing::expect(ssl_enabled);
Void::Testing::expect(simd_enabled);
}
}
// === Feature flags: optional dependencies are resolved only when enabled ===
VOID_TEST(FullV1Tests, optional_dependencies_follow_feature_selection) {
const auto vbs_bin = locate_vbs_binary();
Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin));
if (vbs_bin.empty() || !std::filesystem::exists(vbs_bin)) {
return;
}
Void::FileSystem::TempDir tmp{"vbs_optdep_"};
const auto dep = tmp / "dep";
const auto app = tmp / "app";
write_file(dep / "build.cpp", R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &p) { p.static_library("deplib").sources({"src/dep.cpp"}); }
)cpp");
write_file(dep / "src" / "dep.cpp", "int depfn() { return 1; }\n");
write_file(app / "build.cpp", R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &p) {
p.feature("extra", false);
p.dependency("dep").path("../dep").when("extra");
p.executable("app").sources({"src/main.cpp"});
}
)cpp");
write_file(app / "src" / "main.cpp", "int main() { return 0; }\n");
auto run_vbs = [&](std::vector<std::string> args) {
Platform::ProcessRequest req;
req.executable = vbs_bin;
req.arguments = std::move(args);
req.timeout = std::chrono::seconds{600};
return Platform::run_supervised(req);
};
const auto entry_count = [&]() -> long long {
auto text = Void::FileSystem::read_file(app / "vbs.lock");
if (!text) {
return -1;
}
auto parsed = Void::Json::Strict::parse(*text);
if (!parsed) {
return -1;
}
const auto *root = std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
if (root == nullptr) {
return -1;
}
const auto *contexts =
std::get_if<Void::Json::Strict::Value::Object>(&root->at("contexts").value);
if (contexts == nullptr || contexts->empty()) {
return -1;
}
const auto *context =
std::get_if<Void::Json::Strict::Value::Object>(&contexts->begin()->second.value);
if (context == nullptr) {
return -1;
}
const auto *entries =
std::get_if<Void::Json::Strict::Value::Array>(&context->at("entries").value);
return entries ? static_cast<long long>(entries->size()) : -1;
};
auto resolved = run_vbs({"deps", "resolve", "--project", app.string()});
Void::Testing::expect(resolved.has_value() && resolved->succeeded());
Void::Testing::expect_eq(entry_count(), 0LL);
auto with_feature = run_vbs({"deps", "resolve", "--project", app.string(), "--features",
"extra"});
Void::Testing::expect(with_feature.has_value() && with_feature->succeeded());
Void::Testing::expect_eq(entry_count(), 1LL);
}