Skip to content

Commit 232e49e

Browse files
committed
Add support for vcpkg CRT and library linkage customization
1 parent 1596a81 commit 232e49e

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

docs/cmake-toml.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ include-after = ["cmake/after-subdir.cmake"]
9797
version = "2021.05.12"
9898
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2021.05.12.tar.gz"
9999
packages = ["fmt", "zlib"]
100+
crt-linkage = "dynamic"
101+
library-linkage = "dynamic"
100102
```
101103

102104
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
114116
components = ["mycomponent"]
115117
```
116118

119+
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.
120+
117121
## FetchContent
118122

119123
**Note**: The `[fetch-content]` feature is unpolished and will likely change in a future release.

docs/examples/vcpkg.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ description = "Dependencies from vcpkg"
2121
[vcpkg]
2222
version = "2021.05.12"
2323
packages = ["fmt"]
24+
crt-linkage = "dynamic"
25+
library-linkage = "dynamic"
2426

2527
[find-package]
2628
fmt = {}

include/project_parser.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct Package {
3939
struct Vcpkg {
4040
std::string version;
4141
std::string url;
42+
std::string crt_linkage;
43+
std::string library_linkage;
4244

4345
struct Package {
4446
std::string name;

src/cmake_generator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
634634
cmd("message")("STATUS", "Fetching vcpkg (" + version_name + ")...");
635635
cmd("FetchContent_Declare")("vcpkg", "URL", url);
636636
cmd("FetchContent_MakeAvailable")("vcpkg");
637+
if (!project.vcpkg.crt_linkage.empty()) {
638+
cmd("set")("VCPKG_CRT_LINKAGE", project.vcpkg.crt_linkage);
639+
}
640+
if (!project.vcpkg.library_linkage.empty()) {
641+
cmd("set")("VCPKG_LIBRARY_LINKAGE", project.vcpkg.library_linkage);
642+
}
637643
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
638644
cmd("endif")();
639645
endl();

src/project_parser.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,19 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
627627
auto &v = checker.create(toml, "vcpkg");
628628
v.optional("url", vcpkg.url);
629629
v.optional("version", vcpkg.version);
630+
631+
auto handle_linkage = [&v](const toml::key &ky, std::string &value) {
632+
if (v.contains(ky)) {
633+
v.required(ky, value);
634+
if (value != "dynamic" && value != "static") {
635+
throw std::runtime_error(format_key_error("Unknown linkage, expected dynamic/static", value, v.find(ky)));
636+
}
637+
}
638+
};
639+
640+
handle_linkage("crt-linkage", vcpkg.crt_linkage);
641+
handle_linkage("library-linkage", vcpkg.library_linkage);
642+
630643
for (const auto &p : v.find("packages").as_array()) {
631644
Vcpkg::Package package;
632645
const auto &package_str = p.as_string().str;

tests/vcpkg/cmake.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ description = "Dependencies from vcpkg"
99
[vcpkg]
1010
version = "2021.05.12"
1111
packages = ["fmt"]
12+
crt-linkage = "dynamic"
13+
library-linkage = "dynamic"
1214

1315
[find-package]
1416
fmt = {}

0 commit comments

Comments
 (0)