Skip to content

Commit dc4b59f

Browse files
committed
prelim vcpkg manifest support
1 parent 4988f14 commit dc4b59f

File tree

7 files changed

+25510
-20
lines changed

7 files changed

+25510
-20
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/cmake.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ struct Package {
3333
};
3434

3535
struct Vcpkg {
36-
std::string version;
3736
std::vector<std::string> packages;
3837
};
3938

@@ -64,6 +63,7 @@ struct Target {
6463
ConditionVector link_libraries;
6564
ConditionVector link_options;
6665
ConditionVector precompile_headers;
66+
ConditionVector headers;
6767
ConditionVector sources;
6868

6969
std::string alias;

src/cmake.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ CMake::CMake(const std::string &path, bool build) {
181181
target.name = itr.first;
182182
target.type = to_enum<TargetType>(toml::find(t, "type").as_string(), "target type");
183183

184+
get_optional(t, "headers", target.headers);
184185
get_optional(t, "sources", target.sources);
185186
get_optional(t, "compile-definitions", target.compile_definitions);
186187
get_optional(t, "compile-features", target.compile_features);
@@ -250,15 +251,7 @@ CMake::CMake(const std::string &path, bool build) {
250251

251252
if (toml.contains("vcpkg")) {
252253
const auto &v = toml::find(toml, "vcpkg");
253-
vcpkg.version = toml::find(v, "version").as_string();
254254
vcpkg.packages = toml::find<decltype(vcpkg.packages)>(v, "packages");
255-
256-
// This allows the user to use a custom pmm version if desired
257-
if (contents.count("pmm") == 0) {
258-
contents["pmm"]["url"] = "https://github.com/vector-of-bool/pmm/archive/refs/tags/1.5.1.tar.gz";
259-
// Hack to not execute pmm's example CMakeLists.txt
260-
contents["pmm"]["SOURCE_SUBDIR"] = "pmm";
261-
}
262255
}
263256

264257
// Reasonable default conditions (you can override these if you desire)

src/gen.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <cstdio>
99
#include <cstring>
1010
#include <fstream>
11+
#include <iomanip>
12+
#include <nlohmann/json.hpp>
1113
#include <new>
1214
#include <sstream>
1315
#include <stdexcept>
@@ -294,7 +296,7 @@ struct Command {
294296
}
295297

296298
template <class... Ts>
297-
CommandEndl operator()(Ts &&...values) {
299+
CommandEndl operator()(Ts &&... values) {
298300
generated = true;
299301
ss << indent(depth) << command << '(';
300302
(void)std::initializer_list<bool>{print_arg(values)...};
@@ -520,15 +522,20 @@ int generate_cmake(const char *path, bool root) {
520522
}
521523
}
522524

523-
if (!cmake.vcpkg.version.empty()) {
524-
assert("pmm is required in fetch-content for vcpkg to work" && cmake.contents.count("pmm") != 0);
525-
comment("Bootstrap vcpkg");
526-
cmd("include")("${pmm_SOURCE_DIR}/pmm.cmake");
527-
tsl::ordered_map<std::string, std::vector<std::string>> vcpkg_args;
528-
vcpkg_args["REVISION"] = {cmake.vcpkg.version};
529-
vcpkg_args["REQUIRES"] = cmake.vcpkg.packages;
530-
auto vcpkg = std::make_pair("VCPKG", vcpkg_args);
531-
cmd("pmm")(vcpkg).endl();
525+
if (!cmake.vcpkg.packages.empty()) {
526+
cmd("include")("FetchContent");
527+
cmd("message")("STATUS \"Fetching vcpkg...\"");
528+
cmd("FetchContent_Declare")("vcpkg\n\tURL\thttps://github.com/microsoft/vcpkg/archive/refs/tags/2021.05.12.tar.gz");
529+
cmd("FetchContent_MakeAvailable")("vcpkg");
530+
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
531+
using namespace nlohmann;
532+
json j;
533+
j["$schema"] = "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json";
534+
j["name"] = cmake.project_name;
535+
j["version"] = cmake.project_version;
536+
j["dependencies"] = cmake.vcpkg.packages;
537+
std::ofstream ofs("vcpkg.json");
538+
ofs << std::setw(4) << j << std::endl;
532539
}
533540

534541
if (!cmake.packages.empty()) {
@@ -603,8 +610,21 @@ int generate_cmake(const char *path, bool root) {
603610
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
604611
gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
605612

613+
auto headers_var = target.name + "_HEADERS";
606614
auto sources_var = target.name + "_SOURCES";
607615

616+
if (!target.headers.empty()) {
617+
cmd("set")(headers_var, RawArg("\"\"")).endl();
618+
gen.handle_condition(target.headers, [&](const std::string &condition, const std::vector<std::string> &condition_headers) {
619+
auto headers = expand_cmake_paths(condition_headers, path);
620+
if (headers.empty()) {
621+
auto header_key = condition.empty() ? "headers" : (condition + ".headers");
622+
throw std::runtime_error(target.name + " " + header_key + " wildcard found 0 files");
623+
}
624+
cmd("list")("APPEND", headers_var, headers);
625+
});
626+
}
627+
608628
bool added_toml = false;
609629
cmd("set")(sources_var, RawArg("\"\"")).endl();
610630
gen.handle_condition(target.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
@@ -682,8 +702,12 @@ int generate_cmake(const char *path, bool root) {
682702
// clang-format on
683703
}
684704

705+
if (!target.headers.empty()) {
706+
cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + headers_var + "}").endl();
707+
}
708+
685709
if (!target.sources.empty()) {
686-
cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + target.name + "_SOURCES}").endl();
710+
cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + sources_var + "}").endl();
687711
}
688712

689713
if (!target.alias.empty()) {

third_party/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

third_party/nlohmann-3.9.1/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2013-2021 Niels Lohmann
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)