Skip to content

Commit ffdedbd

Browse files
committed
fixes
1 parent bb7a0bc commit ffdedbd

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

include/cmake.hpp

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

3535
struct Vcpkg {
36+
std::string url;
3637
std::vector<std::string> packages;
3738
};
3839

src/cmake.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ CMake::CMake(const std::string &path, bool build) {
192192
get_optional(t, "link-options", target.link_options);
193193
get_optional(t, "precompile-headers", target.precompile_headers);
194194

195+
if (!target.headers.empty()) {
196+
auto &sources = target.sources.nth(0).value();
197+
const auto &headers = target.headers.nth(0)->second;
198+
sources.insert(sources.end(), headers.begin(), headers.end());
199+
}
200+
195201
if (t.contains("alias")) {
196202
target.alias = toml::find(t, "alias").as_string();
197203
}
@@ -251,6 +257,7 @@ CMake::CMake(const std::string &path, bool build) {
251257

252258
if (toml.contains("vcpkg")) {
253259
const auto &v = toml::find(toml, "vcpkg");
260+
vcpkg.url = toml::find(v, "url").as_string();
254261
vcpkg.packages = toml::find<decltype(vcpkg.packages)>(v, "packages");
255262
}
256263

src/gen.cpp

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <iomanip>
1212
#include <new>
1313
#include <nlohmann/json.hpp>
14+
#include <regex>
1415
#include <sstream>
1516
#include <stdexcept>
1617
#include <string>
@@ -522,26 +523,35 @@ int generate_cmake(const char *path, bool root) {
522523
}
523524
}
524525

525-
if (!cmake.vcpkg.packages.empty()) {
526-
cmd("include")("FetchContent");
527-
cmd("message")("STATUS", "Fetching vcpkg...");
528-
cmd("FetchContent_Declare")("vcpkg", "URL", "https://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");
526+
if (!cmake.vcpkg.packages.empty() && !cmake.vcpkg.url.empty()) {
527+
auto vcpkg_escape_identifier = [](const std::string &name) -> std::string {
528+
const std::regex ok("[a-z0-9]+(-[a-z0-9]+)*");
529+
const std::regex reserved("prn|aux|nul|con|lpt[1-9]|com[1-9]|core|default");
530+
std::cmatch m;
531+
if (!std::regex_match(name.c_str(), m, reserved) && std::regex_match(name.c_str(), m, ok)) {
532+
return name;
533+
} else {
534+
// should probably throw!
535+
return "project-name";
536+
}
537+
};
531538
using namespace nlohmann;
532539
json j;
533540
j["$schema"] = "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json";
534-
j["name"] = cmake.project_name;
535-
if (cmake.project_version.empty())
536-
throw std::runtime_error("vcpkg manifest mode requires that the project have a version string!");
537-
j["version"] = cmake.project_version;
541+
j["name"] = vcpkg_escape_identifier(cmake.project_name);
542+
if (!cmake.project_version.empty())
543+
j["version"] = cmake.project_version;
538544
j["dependencies"] = cmake.vcpkg.packages;
545+
cmd("include")("FetchContent");
546+
cmd("message")("STATUS", "Fetching vcpkg...");
547+
cmd("FetchContent_Declare")("vcpkg", "URL", cmake.vcpkg.url);
548+
cmd("FetchContent_MakeAvailable")("vcpkg");
549+
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
539550
std::ofstream ofs("vcpkg.json");
540551
if (!ofs) {
541552
throw std::runtime_error("Failed to create a vcpkg.json manifest file!");
542553
}
543-
ofs << std::setw(4) << j << std::endl;
544-
ofs.close();
554+
ofs << std::setw(2) << j << std::endl;
545555
}
546556

547557
if (!cmake.packages.empty()) {
@@ -616,21 +626,8 @@ int generate_cmake(const char *path, bool root) {
616626
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
617627
gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
618628

619-
auto headers_var = target.name + "_HEADERS";
620629
auto sources_var = target.name + "_SOURCES";
621630

622-
if (!target.headers.empty()) {
623-
cmd("set")(headers_var, RawArg("\"\"")).endl();
624-
gen.handle_condition(target.headers, [&](const std::string &condition, const std::vector<std::string> &condition_headers) {
625-
auto headers = expand_cmake_paths(condition_headers, path);
626-
if (headers.empty()) {
627-
auto header_key = condition.empty() ? "headers" : (condition + ".headers");
628-
throw std::runtime_error(target.name + " " + header_key + " wildcard found 0 files");
629-
}
630-
cmd("list")("APPEND", headers_var, headers);
631-
});
632-
}
633-
634631
bool added_toml = false;
635632
cmd("set")(sources_var, RawArg("\"\"")).endl();
636633
gen.handle_condition(target.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
@@ -708,10 +705,6 @@ int generate_cmake(const char *path, bool root) {
708705
// clang-format on
709706
}
710707

711-
if (!target.headers.empty()) {
712-
cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + headers_var + "}").endl();
713-
}
714-
715708
if (!target.sources.empty()) {
716709
cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + sources_var + "}").endl();
717710
}

0 commit comments

Comments
 (0)