yosys/passes/cmds/tee.cc

128 lines
4.1 KiB
C++
Raw Normal View History

2014-06-03 09:23:31 +02:00
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2014 Claire Xenia Wolf <claire@yosyshq.com>
2014-06-03 09:23:31 +02:00
* Copyright (C) 2014 Johann Glaser <Johann.Glaser@gmx.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "kernel/yosys.h"
#include "kernel/log_help.h"
2014-06-03 09:23:31 +02:00
2014-09-27 16:17:53 +02:00
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
2014-06-03 09:23:31 +02:00
struct TeePass : public Pass {
TeePass() : Pass("tee", "redirect command output to file") { }
bool formatted_help() override {
auto *help = PrettyHelp::get_current();
help->set_group("passes/status");
return false;
}
2020-06-19 01:34:52 +02:00
void help() override
2014-06-03 09:23:31 +02:00
{
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
log("\n");
log(" tee [-q] [-o logfile|-a logfile] cmd\n");
log("\n");
log("Execute the specified command, optionally writing the commands output to the\n");
log("specified logfile(s).\n");
log("\n");
log(" -q\n");
log(" Do not print output to the normal destination (console and/or log file).\n");
2014-06-03 09:23:31 +02:00
log("\n");
log(" -o logfile\n");
log(" Write output to this file, truncate if exists.\n");
log("\n");
log(" -a logfile\n");
log(" Write output to this file, append if exists.\n");
log("\n");
log(" -s scratchpad\n");
log(" Write output to this scratchpad value, truncate if it exists.\n");
log("\n");
2016-09-06 17:43:24 +02:00
log(" +INT, -INT\n");
log(" Add/subtract INT from the -v setting for this command.\n");
2016-09-06 17:43:24 +02:00
log("\n");
2014-06-03 09:23:31 +02:00
}
2020-06-19 01:34:52 +02:00
void execute(std::vector<std::string> args, RTLIL::Design *design) override
2014-06-03 09:23:31 +02:00
{
std::vector<FILE*> backup_log_files, files_to_close;
std::vector<std::ostream*> backup_log_streams;
std::vector<std::string> backup_log_scratchpads;
2016-09-06 17:43:24 +02:00
int backup_log_verbose_level = log_verbose_level;
backup_log_streams = log_streams;
2014-06-03 09:23:31 +02:00
backup_log_files = log_files;
backup_log_scratchpads = log_scratchpads;
2014-06-03 09:23:31 +02:00
size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++)
{
if (args[argidx] == "-q" && files_to_close.empty()) {
log_files.clear();
log_streams.clear();
2014-06-03 09:23:31 +02:00
continue;
}
if ((args[argidx] == "-o" || args[argidx] == "-a") && argidx+1 < args.size()) {
2014-07-24 15:06:45 +02:00
const char *open_mode = args[argidx] == "-o" ? "w" : "a+";
auto path = args[++argidx];
rewrite_filename(path);
FILE *f = fopen(path.c_str(), open_mode);
yosys_input_files.insert(args[argidx]);
2014-06-03 09:23:31 +02:00
if (f == NULL) {
for (auto cf : files_to_close)
fclose(cf);
log_cmd_error("Can't create file %s.\n", args[argidx]);
2014-06-03 09:23:31 +02:00
}
log_files.push_back(f);
files_to_close.push_back(f);
continue;
}
if (args[argidx] == "-s" && argidx+1 < args.size()) {
auto name = args[++argidx];
design->scratchpad[name] = "";
log_scratchpads.push_back(name);
continue;
}
2016-09-06 17:43:24 +02:00
if (GetSize(args[argidx]) >= 2 && (args[argidx][0] == '-' || args[argidx][0] == '+') && args[argidx][1] >= '0' && args[argidx][1] <= '9') {
log_verbose_level += atoi(args[argidx].c_str());
continue;
}
2014-06-03 09:23:31 +02:00
break;
}
try {
std::vector<std::string> new_args(args.begin() + argidx, args.end());
Pass::call(design, new_args);
2015-01-25 22:57:09 +01:00
} catch (...) {
2014-06-03 09:23:31 +02:00
for (auto cf : files_to_close)
fclose(cf);
log_files = backup_log_files;
log_streams = backup_log_streams;
log_scratchpads = backup_log_scratchpads;
2015-01-25 22:57:09 +01:00
throw;
2014-06-03 09:23:31 +02:00
}
for (auto cf : files_to_close)
fclose(cf);
2016-09-06 17:43:24 +02:00
log_verbose_level = backup_log_verbose_level;
2014-06-03 09:23:31 +02:00
log_files = backup_log_files;
log_streams = backup_log_streams;
log_scratchpads = backup_log_scratchpads;
2014-06-03 09:23:31 +02:00
}
} TeePass;
2014-09-27 16:17:53 +02:00
PRIVATE_NAMESPACE_END