diff --git a/docs/cmake-toml.md b/docs/cmake-toml.md index b2339b8..6e4f23b 100644 --- a/docs/cmake-toml.md +++ b/docs/cmake-toml.md @@ -160,17 +160,18 @@ Variables emit a [`set`](https://cmake.org/cmake/help/latest/command/set.html) a ```toml [vcpkg] -version = "2024.03.25" -url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2024.03.25.tar.gz" +version = "2024.11.16" +url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2024.11.16.tar.gz" packages = ["fmt", "zlib"] overlay-ports = ["my-ports"] +overlay-triplets = ["my-triplets"] ``` -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). +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) or [vcpkg.link](https://vcpkg.link). 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). +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). The `overlay-triplets` feature allows you to customize triplets and change the default behavior (for example always preferring static libraries on Windows). To specify both in one go you can do `[vcpkg].overlay = "my-overlay"`. ## Packages diff --git a/docs/examples/vcpkg.md b/docs/examples/vcpkg.md index 838d66d..6a1c403 100644 --- a/docs/examples/vcpkg.md +++ b/docs/examples/vcpkg.md @@ -9,7 +9,7 @@ nav_order: 4 # Dependencies from vcpkg -Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://vcpkg.io/) and links an `example` target to it: +Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://github.com/microsoft/vcpkg) and links an `example` target to it: ```toml [project] @@ -17,13 +17,12 @@ name = "vcpkg" description = "Dependencies from vcpkg" # See https://github.com/microsoft/vcpkg/releases for vcpkg versions -# See https://vcpkg.io/en/packages.html for available packages +# See https://vcpkg.io/en/packages or https://vcpkg.link for available packages [vcpkg] -version = "2024.03.25" +version = "2024.11.16" packages = ["fmt"] -[find-package] -fmt = {} +[find-package.fmt] [target.example] type = "executable" diff --git a/include/project_parser.hpp b/include/project_parser.hpp index 4de3870..5f37142 100644 --- a/include/project_parser.hpp +++ b/include/project_parser.hpp @@ -49,7 +49,10 @@ struct Vcpkg { }; std::vector packages; + + // https://github.com/Microsoft/vcpkg-docs/blob/main/vcpkg/users/buildsystems/cmake-integration.md std::vector overlay_ports; + std::vector overlay_triplets; bool enabled() const { return !packages.empty(); diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index 436985c..947d7b7 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -861,6 +861,22 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { endl(); } + if (project.vcpkg.enabled()) { + comment("vcpkg settings"); + auto emit_overlay = [&cmd](const std::string &name, const std::vector &overlay) { + if (!overlay.empty()) { + for (const auto &directory : overlay) { + if (!fs::is_directory(directory)) { + throw std::runtime_error("[vcpkg] overlay is not a directory: " + directory); + } + } + cmd("set")(name, overlay); + } + }; + emit_overlay("VCPKG_OVERLAY_PORTS", project.vcpkg.overlay_ports); + emit_overlay("VCPKG_OVERLAY_TRIPLETS", project.vcpkg.overlay_triplets); + } + gen.conditional_includes(project.include_before); gen.conditional_cmake(project.cmake_before); @@ -996,29 +1012,7 @@ 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 << " \"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 << " \"version-string\": \"none\"\n"; ofs << "}\n"; } diff --git a/src/project_parser.cpp b/src/project_parser.cpp index 38a1b04..ec793ee 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -860,7 +860,20 @@ 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); + if (v.contains("overlay")) { + std::string overlay; + v.optional("overlay", overlay); + vcpkg.overlay_triplets = vcpkg.overlay_ports = {overlay}; + if (v.contains("overlay-ports")) { + throw_key_error("[vcpkg].overlay was already specified", "overlay-ports", v.find("overlay-ports")); + } + if (v.contains("overlay-triplets")) { + throw_key_error("[vcpkg].overlay was already specified", "overlay-triplets", v.find("overlay-triplets")); + } + } else { + v.optional("overlay-ports", vcpkg.overlay_ports); + v.optional("overlay-triplets", vcpkg.overlay_triplets); + } } checker.check(conditions, true); diff --git a/tests/vcpkg/cmake.toml b/tests/vcpkg/cmake.toml index 3e573d6..382b9b5 100644 --- a/tests/vcpkg/cmake.toml +++ b/tests/vcpkg/cmake.toml @@ -1,17 +1,16 @@ -# Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://vcpkg.io/) and links an `example` target to it: +# Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://github.com/microsoft/vcpkg) and links an `example` target to it: [project] name = "vcpkg" description = "Dependencies from vcpkg" # See https://github.com/microsoft/vcpkg/releases for vcpkg versions -# See https://vcpkg.io/en/packages.html for available packages +# See https://vcpkg.io/en/packages or https://vcpkg.link for available packages [vcpkg] -version = "2024.03.25" +version = "2024.11.16" packages = ["fmt"] -[find-package] -fmt = {} +[find-package.fmt] [target.example] type = "executable"