MCPcopy Create free account
hub / github.com/build-cpp/cmkr / Generator

Class Generator

src/cmake_generator.cpp:490–649  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

488}
489
490struct Generator {
491 Generator(const parser::Project &project, fs::path path) : project(project), path(std::move(path)) {
492 }
493 Generator(const Generator &) = delete;
494
495 const parser::Project &project;
496 fs::path path;
497 std::stringstream ss;
498 int indent = 0;
499
500 Command cmd(const std::string &command, const std::string &post_comment = "") {
501 if (command.empty())
502 throw std::invalid_argument("command cannot be empty");
503 if (command == "if") {
504 indent++;
505 return Command(ss, indent - 1, command, post_comment);
506 } else if (command == "else" || command == "elseif") {
507 return Command(ss, indent - 1, command, post_comment);
508 } else if (command == "endif") {
509 indent--;
510 }
511 return Command(ss, indent, command, post_comment);
512 }
513
514 CommandEndl comment(const std::string &comment) {
515 ss << Command::indent(indent) << "# " << comment << '\n';
516 return CommandEndl(ss);
517 }
518
519 void endl() {
520 ss << '\n';
521 }
522
523 void inject_includes(const std::vector<std::string> &includes) {
524 if (!includes.empty()) {
525 for (const auto &file : includes) {
526 if (!fs::exists(path / file)) {
527 throw std::runtime_error("Include not found: " + file);
528 }
529 cmd("include")(file);
530 }
531 }
532 }
533
534 void inject_cmake(const std::string &cmake) {
535 if (!cmake.empty()) {
536 if (cmake.back() == '\"') {
537 throw std::runtime_error("Detected additional \" at the end of cmake block");
538 }
539 auto cmake_lf = tolf(cmake);
540 while (!cmake_lf.empty() && cmake_lf.back() == '\n')
541 cmake_lf.pop_back();
542 bool did_indent = false;
543 for (char ch : cmake_lf) {
544 if (!did_indent) {
545 ss << Command::indent(indent);
546 did_indent = true;
547 } else if (ch == '\n') {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected