From ae3d73896e6e5c2109781b34693a33e5c3bbb089 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Tue, 26 Nov 2024 12:33:29 +0100 Subject: [PATCH] Add [vcpkg].overlay-ports feature --- docs/cmake-toml.md | 3 +++ include/project_parser.hpp | 1 + src/cmake_generator.cpp | 26 ++++++++++++++++++++++++-- src/project_parser.cpp | 2 ++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docs/cmake-toml.md b/docs/cmake-toml.md index 607d11b..8d9355a 100644 --- a/docs/cmake-toml.md +++ b/docs/cmake-toml.md @@ -163,12 +163,15 @@ Variables emit a [`set`](https://cmake.org/cmake/help/latest/command/set.html) a version = "2024.03.25" url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2024.03.25.tar.gz" packages = ["fmt", "zlib"] +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]`. +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). + ## Packages ```toml diff --git a/include/project_parser.hpp b/include/project_parser.hpp index 941736d..debaab1 100644 --- a/include/project_parser.hpp +++ b/include/project_parser.hpp @@ -48,6 +48,7 @@ struct Vcpkg { }; std::vector packages; + std::vector overlay_ports; bool enabled() const { return !packages.empty(); diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index efcd7aa..69b34fa 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -936,7 +936,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { ofs << R"({ "$cmkr": "This file is automatically generated from cmake.toml - DO NOT EDIT", "$cmkr-url": "https://github.com/build-cpp/cmkr", - "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", "dependencies": [ )"; @@ -993,7 +993,29 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { ofs << " ],\n"; ofs << " \"description\": \"" << escape(project.project_description) << "\",\n"; ofs << " \"name\": \"" << escape(vcpkg_escape_identifier(project.project_name)) << "\",\n"; - ofs << R"( "version-string": "")" << '\n'; + ofs << " \"version-string\": \"none\""; + const auto &overlay_ports = project.vcpkg.overlay_ports; + if (!overlay_ports.empty()) { + ofs << ",\n"; + // Reference: https://learn.microsoft.com/en-us/vcpkg/reference/vcpkg-json#vcpkg-configuration + ofs << " \"vcpkg-configuration\": {\n"; + ofs << " \"overlay-ports\": [\n"; + for (size_t i = 0; i < overlay_ports.size(); i++) { + const auto &directory = overlay_ports[i]; + if (!fs::is_directory(directory)) { + throw std::runtime_error("[vcpkg].overlay-ports is not a directory: " + directory); + } + ofs << " \"" << escape(directory) << "\""; + if (i > 0) { + ofs << ","; + } + ofs << "\n"; + } + ofs << " ]\n"; + ofs << " }\n"; + } else { + ofs << "\n"; + } ofs << "}\n"; } diff --git a/src/project_parser.cpp b/src/project_parser.cpp index a9be666..822ca53 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -854,6 +854,8 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p } vcpkg.packages.emplace_back(std::move(package)); } + + v.optional("overlay-ports", vcpkg.overlay_ports); } checker.check(conditions, true);