|
11 | 11 | #include <iomanip> |
12 | 12 | #include <new> |
13 | 13 | #include <nlohmann/json.hpp> |
| 14 | +#include <regex> |
14 | 15 | #include <sstream> |
15 | 16 | #include <stdexcept> |
16 | 17 | #include <string> |
@@ -522,26 +523,35 @@ int generate_cmake(const char *path, bool root) { |
522 | 523 | } |
523 | 524 | } |
524 | 525 |
|
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 | + }; |
531 | 538 | using namespace nlohmann; |
532 | 539 | json j; |
533 | 540 | 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; |
538 | 544 | 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"); |
539 | 550 | std::ofstream ofs("vcpkg.json"); |
540 | 551 | if (!ofs) { |
541 | 552 | throw std::runtime_error("Failed to create a vcpkg.json manifest file!"); |
542 | 553 | } |
543 | | - ofs << std::setw(4) << j << std::endl; |
544 | | - ofs.close(); |
| 554 | + ofs << std::setw(2) << j << std::endl; |
545 | 555 | } |
546 | 556 |
|
547 | 557 | if (!cmake.packages.empty()) { |
@@ -616,21 +626,8 @@ int generate_cmake(const char *path, bool root) { |
616 | 626 | [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); }); |
617 | 627 | gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); |
618 | 628 |
|
619 | | - auto headers_var = target.name + "_HEADERS"; |
620 | 629 | auto sources_var = target.name + "_SOURCES"; |
621 | 630 |
|
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 | | - |
634 | 631 | bool added_toml = false; |
635 | 632 | cmd("set")(sources_var, RawArg("\"\"")).endl(); |
636 | 633 | 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) { |
708 | 705 | // clang-format on |
709 | 706 | } |
710 | 707 |
|
711 | | - if (!target.headers.empty()) { |
712 | | - cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + headers_var + "}").endl(); |
713 | | - } |
714 | | - |
715 | 708 | if (!target.sources.empty()) { |
716 | 709 | cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + sources_var + "}").endl(); |
717 | 710 | } |
|
0 commit comments