From f8c34e5545f905d0f74f642c88dae7ae63286faa Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 24 Mar 2023 10:43:17 +0000 Subject: [PATCH] Error when [vcpkg] is used from a non-root project --- include/project_parser.hpp | 4 ++++ src/cmake_generator.cpp | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/project_parser.hpp b/include/project_parser.hpp index 0fc5e9f..2e6b07b 100644 --- a/include/project_parser.hpp +++ b/include/project_parser.hpp @@ -47,6 +47,10 @@ struct Vcpkg { }; std::vector packages; + + bool enabled() const { + return !packages.empty(); + } }; enum TargetType { diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index f99244c..cfa9807 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -784,7 +784,11 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { gen.conditional_includes(project.include_after); gen.conditional_cmake(project.cmake_after); - if (!project.vcpkg.packages.empty()) { + if (project.vcpkg.enabled()) { + if (!is_root_project) { + throw std::runtime_error("[vcpkg] is only supported in the root project"); + } + // Allow the user to specify a url or derive it from the version auto url = project.vcpkg.url; auto version_name = url; @@ -797,7 +801,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) { + const auto &packages = project.vcpkg.packages; + for (const auto &package : packages) { if (!vcpkg_valid_identifier(package.name)) { throw std::runtime_error("Invalid [vcpkg].packages name '" + package.name + "' (needs to be lowercase alphanumeric)"); } @@ -831,7 +836,6 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { "dependencies": [ )"; - const auto &packages = project.vcpkg.packages; for (size_t i = 0; i < packages.size(); i++) { const auto &package = packages[i]; const auto &features = package.features;