Skip to content

Commit

Permalink
text progress report and more filesystem error handling (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy authored Jan 26, 2023
1 parent 2c85e09 commit a51e55b
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 51 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ tmp
.vscode
.github
external
tests
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"--output=${workspaceFolder}/tmp/test_runtime.dll",
"--temp_dir=${workspaceFolder}/tmp",
"--ecsact_sdk=${workspaceFolder}/../ecsact_sdk/dist",
"--debug"
"--debug",
"--report_format=text"
]
},
"args": [
Expand Down
2 changes: 1 addition & 1 deletion cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("//bazel:copts.bzl", "copts")

cc_binary(
name = "ecsact_rtb",
srcs = ["cli.cc"],
srcs = ["cli.cc", "stdout_text_progress_reporter.hh"],
copts = copts,
data = ecsact_rtb_runfiles,
visibility = ["//visibility:public"],
Expand Down
107 changes: 70 additions & 37 deletions cli/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iostream>
#include <mutex>
#include <string>
#include <memory>
#include "docopt.h"
#include "nlohmann/json.hpp"
#include "ecsact/interpret/eval.hh"
Expand All @@ -19,19 +20,28 @@
#include "find_cpp_compiler/find_cpp_compiler.hh"
#include "progress_report/progress_report.hh"

#include "stdout_text_progress_reporter.hh"

namespace fs = std::filesystem;

constexpr auto USAGE = R"(
Usage:
ecsact_rtb <ecsact_file>... --output=<output> [--temp_dir=<temp_dir>]
[--compiler_path=<compiler_path>] [--ecsact_sdk=<path>] [--debug]
[--wasm=<wasm>]
ecsact_rtb <ecsact_file>... --output=<output> [--report_format=<format>]
[--temp_dir=<temp_dir>] [--compiler_path=<compiler_path>]
[--ecsact_sdk=<path>] [--debug] [--wasm=<wasm>]
)";

constexpr auto OPTIONS = R"(
Options:
--output=<output>
Output path for runtime library.
--report_format=<format> [default: json]
Which format to report progress of runtime builder. Allowed the following:
json Progress is reported in JSON format. One JSON object per line to
stdout.
text Progress is reported in human readable text to stdout. Format
should not be relied upon. Only useful when running ecsact_rtb
manually.
--temp_dir=<temp_dir>
Optionally supply a temporary directory to write the generated/fetched
source files. If one is not provided one will be generated.
Expand Down Expand Up @@ -104,9 +114,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(subcommand_end_message, id, exit_code)

} // namespace ecsact_rtb

class stdout_progress_reporter : public ecsact_rtb::progress_reporter {
std::mutex _mutex;

class stdout_json_progress_reporter : public ecsact_rtb::progress_reporter {
template<typename MessageT>
void _report(MessageT& message) {
auto message_json = "{}"_json;
Expand Down Expand Up @@ -135,23 +143,42 @@ int main(int argc, char* argv[]) {

std::ios_base::sync_with_stdio(false);

stdout_progress_reporter reporter;

std::string runfiles_err;
auto argv0 = executable_path().string();
if(argv0.empty()) {
argv0 = std::string(argv[0]);
}

auto runfiles = Runfiles::Create(argv0, &runfiles_err);
auto args = docopt_workaround(argc, argv);

auto reporter = [&]() -> std::unique_ptr<ecsact_rtb::progress_reporter> {
auto report_format_str = std::string{};
if(!args.contains("--report_format")) {
report_format_str = "json";
} else {
report_format_str = args.at("--report_format").asString();
}

if(report_format_str == "json") {
return std::make_unique<stdout_json_progress_reporter>();
}

if(report_format_str == "text") {
return std::make_unique<ecsact_rtb::stdout_text_progress_reporter>();
}

return nullptr;
}();

assert(reporter);

if(runfiles == nullptr) {
reporter.report(ecsact_rtb::warning_message{
reporter->report(ecsact_rtb::warning_message{
.content = "Cannot load runfiles: "s + runfiles_err,
});
}

auto args = docopt_workaround(argc, argv);

const auto debug_build = args.at("--debug").asBool();
std::string wasm_support_str = "auto";
if(args.at("--wasm").isString()) {
Expand All @@ -177,7 +204,7 @@ int main(int argc, char* argv[]) {
for(const auto& file : files) {
fs::path file_path(file);
if(!fs::exists(file_path)) {
reporter.report(ecsact_rtb::error_message{
reporter->report(ecsact_rtb::error_message{
.content = "File doesn't exist: " + file_path.string(),
});
return 1;
Expand All @@ -197,7 +224,7 @@ int main(int argc, char* argv[]) {
err_msg.line = err.line;
err_msg.character = err.character;

reporter.report(err_msg);
reporter->report(err_msg);
}

return 1;
Expand All @@ -208,15 +235,15 @@ int main(int argc, char* argv[]) {

#if defined(_WIN32)
if(output_path.extension() != ".dll") {
reporter.report(ecsact_rtb::error_message{
reporter->report(ecsact_rtb::error_message{
.content = "Cross compilation not supported. Only allowed to build "s +
".dll runtimes.",
});
return 1;
}
#elif defined(__linux__)
if(output_path.extension() != ".so") {
reporter.report(ecsact_rtb::error_message{
reporter->report(ecsact_rtb::error_message{
.content = "Cross compilation not supported. Only allowed to build "s +
".so runtimes.",
});
Expand Down Expand Up @@ -253,12 +280,12 @@ int main(int argc, char* argv[]) {
}

auto ecsact_cli_path = find_ecsact_cli({
.reporter = reporter,
.reporter = *reporter,
.esact_sdk_path = esact_sdk_path,
});

if(ecsact_cli_path.ecsact_cli_path.empty()) {
reporter.report(ecsact_rtb::error_message{
reporter->report(ecsact_rtb::error_message{
.content = "Could not find ecsact CLI",
});
return 1;
Expand All @@ -269,30 +296,36 @@ int main(int argc, char* argv[]) {

auto wasmer = find_wasmer({
.wasm_support = wasm_support,
.reporter = reporter,
.reporter = *reporter,
.path = {},
});

auto generated_files = generate_files({
.reporter = *reporter,
.temp_dir = temp_dir,
.ecsact_cli_path = ecsact_cli_path.ecsact_cli_path,
.ecsact_file_paths = ecsact_file_paths,
});

auto fetched_sources = fetch_sources({
.reporter = *reporter,
.temp_dir = temp_dir,
.runfiles = runfiles,
.fetch_wasm_related_sources = !wasmer.wasmer_path.empty(),
});

auto cpp_compiler = find_cpp_compiler({
.reporter = *reporter,
.working_directory = working_directory,
.path = compiler_path,
.runfiles = runfiles,
});

runtime_compile({
.reporter = reporter,
.generated_files = generate_files({
.reporter = reporter,
.temp_dir = temp_dir,
.ecsact_cli_path = ecsact_cli_path.ecsact_cli_path,
.ecsact_file_paths = ecsact_file_paths,
}),
.fetched_sources = fetch_sources({
.reporter = reporter,
.temp_dir = temp_dir,
.runfiles = runfiles,
.fetch_wasm_related_sources = !wasmer.wasmer_path.empty(),
}),
.cpp_compiler = find_cpp_compiler({
.reporter = reporter,
.working_directory = working_directory,
.path = compiler_path,
.runfiles = runfiles,
}),
.reporter = *reporter,
.generated_files = generated_files,
.fetched_sources = fetched_sources,
.cpp_compiler = cpp_compiler,
.wasmer = wasmer,
.output_path = output_path,
.working_directory = working_directory,
Expand Down
84 changes: 84 additions & 0 deletions cli/stdout_text_progress_reporter.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

#include <iostream>

#include "progress_report/progress_report.hh"

namespace ecsact_rtb {
class stdout_text_progress_reporter : public ecsact_rtb::progress_reporter {
void _report(alert_message& msg) {
std::cerr << "[ALERT] " << msg.content << "\n";
}

void _report(info_message& msg) {
std::cout << "[INFO] " << msg.content << "\n";
}

void _report(error_message& msg) {
std::cerr << "[ERROR] " << msg.content << "\n";
}

void _report(ecsact_error_message& msg) {
std::cerr //
<< "[Ecsact Error] " << msg.ecsact_source_path << ":" << msg.line << ":"
<< msg.character << "\n"
<< msg.message << "\n";
}

void _report(warning_message& msg) {
std::cout << "[WARNING] " << msg.content << "\n";
}

void _report(success_message& msg) {
std::cout << "[SUCCESS] " << msg.content << "\n";
}

void _report(module_methods_message& msg) {
std::cout << "[Module Methods for " << msg.module_name << "]\n";
for(auto& method : msg.methods) {
std::cout //
<< " " << (method.available ? "YES " : " NO ")
<< method.method_name << "\n";
}
}

void _report(subcommand_start_message& msg) {
std::cout //
<< "[SUBCOMMAND START id=(" << std::to_string(msg.id) << ")] "
<< msg.executable << " ";
for(auto& arg : msg.arguments) {
std::cout << arg << " ";
}
std::cout << "\n";
}

void _report(subcommand_stdout_message& msg) {
std::cout //
<< "[SUBCOMMAND STDOUT id=(" << std::to_string(msg.id) << ")] "
<< msg.line << "\n";
}

void _report(subcommand_stderr_message& msg) {
std::cerr //
<< "[SUBCOMMAND STDERR id=(" << std::to_string(msg.id) << ")] "
<< msg.line << "\n";
}

void _report(subcommand_progress_message& msg) {
std::cout //
<< "[SUBCOMMAND PROG id=(" << std::to_string(msg.id) << ")] "
<< msg.description << "\n";
}

void _report(subcommand_end_message& msg) {
std::cout //
<< "[SUBCOMMAND END id=(" << std::to_string(msg.id) << ")] "
<< "exit code " << msg.exit_code << "\n";
}

public:
void report(ecsact_rtb::message_variant_t message) {
std::visit([this](auto& message) { _report(message); }, message);
}
};
} // namespace ecsact_rtb
1 change: 1 addition & 0 deletions fetch_sources/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//progress_report",
"//util:report_error_code",
"@bazel_tools//tools/cpp/runfiles",
"@boost//libs/process",
"@com_github_biojppm_c4core//:c4core",
Expand Down
Loading

0 comments on commit a51e55b

Please sign in to comment.