Skip to content

Commit 78bcf2a

Browse files
committed
Add support for [vcpkg].overlay-triplets
1 parent 83559cc commit 78bcf2a

File tree

6 files changed

+47
-38
lines changed

6 files changed

+47
-38
lines changed

docs/cmake-toml.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,18 @@ Variables emit a [`set`](https://cmake.org/cmake/help/latest/command/set.html) a
160160

161161
```toml
162162
[vcpkg]
163-
version = "2024.03.25"
164-
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2024.03.25.tar.gz"
163+
version = "2024.11.16"
164+
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/2024.11.16.tar.gz"
165165
packages = ["fmt", "zlib"]
166166
overlay-ports = ["my-ports"]
167+
overlay-triplets = ["my-triplets"]
167168
```
168169

169-
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).
170+
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).
170171

171172
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]`
172173

173-
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).
174+
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"`.
174175

175176
## Packages
176177

docs/examples/vcpkg.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@ nav_order: 4
99

1010
# Dependencies from vcpkg
1111

12-
Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://vcpkg.io/) and links an `example` target to it:
12+
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:
1313

1414
```toml
1515
[project]
1616
name = "vcpkg"
1717
description = "Dependencies from vcpkg"
1818

1919
# See https://github.com/microsoft/vcpkg/releases for vcpkg versions
20-
# See https://vcpkg.io/en/packages.html for available packages
20+
# See https://vcpkg.io/en/packages or https://vcpkg.link for available packages
2121
[vcpkg]
22-
version = "2024.03.25"
22+
version = "2024.11.16"
2323
packages = ["fmt"]
2424

25-
[find-package]
26-
fmt = {}
25+
[find-package.fmt]
2726

2827
[target.example]
2928
type = "executable"

include/project_parser.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ struct Vcpkg {
4949
};
5050

5151
std::vector<Package> packages;
52+
53+
// https://github.com/Microsoft/vcpkg-docs/blob/main/vcpkg/users/buildsystems/cmake-integration.md
5254
std::vector<std::string> overlay_ports;
55+
std::vector<std::string> overlay_triplets;
5356

5457
bool enabled() const {
5558
return !packages.empty();

src/cmake_generator.cpp

+17-23
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,22 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
861861
endl();
862862
}
863863

864+
if (project.vcpkg.enabled()) {
865+
comment("vcpkg settings");
866+
auto emit_overlay = [&cmd](const std::string &name, const std::vector<std::string> &overlay) {
867+
if (!overlay.empty()) {
868+
for (const auto &directory : overlay) {
869+
if (!fs::is_directory(directory)) {
870+
throw std::runtime_error("[vcpkg] overlay is not a directory: " + directory);
871+
}
872+
}
873+
cmd("set")(name, overlay);
874+
}
875+
};
876+
emit_overlay("VCPKG_OVERLAY_PORTS", project.vcpkg.overlay_ports);
877+
emit_overlay("VCPKG_OVERLAY_TRIPLETS", project.vcpkg.overlay_triplets);
878+
}
879+
864880
gen.conditional_includes(project.include_before);
865881
gen.conditional_cmake(project.cmake_before);
866882

@@ -996,29 +1012,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
9961012
ofs << " ],\n";
9971013
ofs << " \"description\": \"" << escape(project.project_description) << "\",\n";
9981014
ofs << " \"name\": \"" << escape(vcpkg_escape_identifier(project.project_name)) << "\",\n";
999-
ofs << " \"version-string\": \"none\"";
1000-
const auto &overlay_ports = project.vcpkg.overlay_ports;
1001-
if (!overlay_ports.empty()) {
1002-
ofs << ",\n";
1003-
// Reference: https://learn.microsoft.com/en-us/vcpkg/reference/vcpkg-json#vcpkg-configuration
1004-
ofs << " \"vcpkg-configuration\": {\n";
1005-
ofs << " \"overlay-ports\": [\n";
1006-
for (size_t i = 0; i < overlay_ports.size(); i++) {
1007-
const auto &directory = overlay_ports[i];
1008-
if (!fs::is_directory(directory)) {
1009-
throw std::runtime_error("[vcpkg].overlay-ports is not a directory: " + directory);
1010-
}
1011-
ofs << " \"" << escape(directory) << "\"";
1012-
if (i > 0) {
1013-
ofs << ",";
1014-
}
1015-
ofs << "\n";
1016-
}
1017-
ofs << " ]\n";
1018-
ofs << " }\n";
1019-
} else {
1020-
ofs << "\n";
1021-
}
1015+
ofs << " \"version-string\": \"none\"\n";
10221016
ofs << "}\n";
10231017
}
10241018

src/project_parser.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,20 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
860860
vcpkg.packages.emplace_back(std::move(package));
861861
}
862862

863-
v.optional("overlay-ports", vcpkg.overlay_ports);
863+
if (v.contains("overlay")) {
864+
std::string overlay;
865+
v.optional("overlay", overlay);
866+
vcpkg.overlay_triplets = vcpkg.overlay_ports = {overlay};
867+
if (v.contains("overlay-ports")) {
868+
throw_key_error("[vcpkg].overlay was already specified", "overlay-ports", v.find("overlay-ports"));
869+
}
870+
if (v.contains("overlay-triplets")) {
871+
throw_key_error("[vcpkg].overlay was already specified", "overlay-triplets", v.find("overlay-triplets"));
872+
}
873+
} else {
874+
v.optional("overlay-ports", vcpkg.overlay_ports);
875+
v.optional("overlay-triplets", vcpkg.overlay_triplets);
876+
}
864877
}
865878

866879
checker.check(conditions, true);

tests/vcpkg/cmake.toml

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
# Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://vcpkg.io/) and links an `example` target to it:
1+
# 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:
22

33
[project]
44
name = "vcpkg"
55
description = "Dependencies from vcpkg"
66

77
# See https://github.com/microsoft/vcpkg/releases for vcpkg versions
8-
# See https://vcpkg.io/en/packages.html for available packages
8+
# See https://vcpkg.io/en/packages or https://vcpkg.link for available packages
99
[vcpkg]
10-
version = "2024.03.25"
10+
version = "2024.11.16"
1111
packages = ["fmt"]
1212

13-
[find-package]
14-
fmt = {}
13+
[find-package.fmt]
1514

1615
[target.example]
1716
type = "executable"

0 commit comments

Comments
 (0)