From 232e49e08780a5f91467b4b59d8fef2b76349d09 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 1 Apr 2022 02:27:41 +0200 Subject: [PATCH] Add support for vcpkg CRT and library linkage customization --- docs/cmake-toml.md | 4 ++++ docs/examples/vcpkg.md | 2 ++ include/project_parser.hpp | 2 ++ src/cmake_generator.cpp | 6 ++++++ src/project_parser.cpp | 13 +++++++++++++ tests/vcpkg/cmake.toml | 2 ++ 6 files changed, 29 insertions(+) diff --git a/docs/cmake-toml.md b/docs/cmake-toml.md index e81b9a7..ea335fa 100644 --- a/docs/cmake-toml.md +++ b/docs/cmake-toml.md @@ -97,6 +97,8 @@ include-after = ["cmake/after-subdir.cmake"] version = "2021.05.12" url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2021.05.12.tar.gz" packages = ["fmt", "zlib"] +crt-linkage = "dynamic" +library-linkage = "dynamic" ``` 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). @@ -114,6 +116,8 @@ config = true components = ["mycomponent"] ``` +The `crt-linkage` specifies the MSVC runtime library variant to use while compiling packages. `library-linkage` is whether libraries built are dynamic (default) or static. + ## FetchContent **Note**: The `[fetch-content]` feature is unpolished and will likely change in a future release. diff --git a/docs/examples/vcpkg.md b/docs/examples/vcpkg.md index 9ebb048..d1e7de1 100644 --- a/docs/examples/vcpkg.md +++ b/docs/examples/vcpkg.md @@ -21,6 +21,8 @@ description = "Dependencies from vcpkg" [vcpkg] version = "2021.05.12" packages = ["fmt"] +crt-linkage = "dynamic" +library-linkage = "dynamic" [find-package] fmt = {} diff --git a/include/project_parser.hpp b/include/project_parser.hpp index c8f8186..4aae28d 100644 --- a/include/project_parser.hpp +++ b/include/project_parser.hpp @@ -39,6 +39,8 @@ struct Package { struct Vcpkg { std::string version; std::string url; + std::string crt_linkage; + std::string library_linkage; struct Package { std::string name; diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index e15eb54..881d0cb 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -634,6 +634,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { cmd("message")("STATUS", "Fetching vcpkg (" + version_name + ")..."); cmd("FetchContent_Declare")("vcpkg", "URL", url); cmd("FetchContent_MakeAvailable")("vcpkg"); + if (!project.vcpkg.crt_linkage.empty()) { + cmd("set")("VCPKG_CRT_LINKAGE", project.vcpkg.crt_linkage); + } + if (!project.vcpkg.library_linkage.empty()) { + cmd("set")("VCPKG_LIBRARY_LINKAGE", project.vcpkg.library_linkage); + } cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake"); cmd("endif")(); endl(); diff --git a/src/project_parser.cpp b/src/project_parser.cpp index 380a723..862f3b4 100644 --- a/src/project_parser.cpp +++ b/src/project_parser.cpp @@ -627,6 +627,19 @@ Project::Project(const Project *parent, const std::string &path, bool build) { auto &v = checker.create(toml, "vcpkg"); v.optional("url", vcpkg.url); v.optional("version", vcpkg.version); + + auto handle_linkage = [&v](const toml::key &ky, std::string &value) { + if (v.contains(ky)) { + v.required(ky, value); + if (value != "dynamic" && value != "static") { + throw std::runtime_error(format_key_error("Unknown linkage, expected dynamic/static", value, v.find(ky))); + } + } + }; + + handle_linkage("crt-linkage", vcpkg.crt_linkage); + handle_linkage("library-linkage", vcpkg.library_linkage); + for (const auto &p : v.find("packages").as_array()) { Vcpkg::Package package; const auto &package_str = p.as_string().str; diff --git a/tests/vcpkg/cmake.toml b/tests/vcpkg/cmake.toml index 9207dbf..50dd0b3 100644 --- a/tests/vcpkg/cmake.toml +++ b/tests/vcpkg/cmake.toml @@ -9,6 +9,8 @@ description = "Dependencies from vcpkg" [vcpkg] version = "2021.05.12" packages = ["fmt"] +crt-linkage = "dynamic" +library-linkage = "dynamic" [find-package] fmt = {}