Crafter.Build/Crafter.Build-Bounce.cppm
2025-09-09 23:04:05 +02:00

89 lines
No EOL
2.7 KiB
C++

/*
Crafter.Build
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module;
#include <iostream>
#include <memory>
#include <stdexcept>
#include <array>
#include <string>
#include <cstdio>
#include <string_view>
#include <regex>
export module Crafter.Build:Bounce;
namespace Crafter::Build {
export std::string RunCommand(const std::string_view cmd) {
std::array<char, 128> buffer;
std::string result;
std::string with = std::string(cmd) + " 2>&1";
// Open pipe to file
FILE* pipe = popen(with.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
// Read till end of process:
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
// Close pipe
pclose(pipe);
return result;
}
export void RunCommandIgnore(const std::string_view cmd) {
std::string with = std::string(cmd) + " > /dev/null 2>&1";
FILE* pipe = popen(with.c_str(), "r");
if (!pipe) throw std::runtime_error("popen() failed!");
pclose(pipe);
}
export struct ClangError {
std::string filename;
uint32_t line_number;
std::string error_message;
std::string code;
};
export std::vector<ClangError> RunClang(const std::string_view cmd) {
std::string result = RunCommand(cmd);
std::vector<ClangError> errors;
std::regex error_regex(R"((/[^:]+\.cpp):(\d+):\d+: error: (.*)\n\s*[0-9| ]*\s*(.*))");
std::smatch match;
while (std::regex_search(result, match, error_regex)) {
ClangError error;
error.filename = match[1].str();
error.line_number = std::stoi(match[2].str());
error.error_message = match[3].str();
error.code = match[4].str();
errors.push_back(error);
result = match.suffix().str();
}
if(result != "" && errors.size() == 0) {
throw std::runtime_error(result);
}
return errors;
}
}