Merge pull request #755 from Icenowy/anlogic-dram-init
[yosys.git] / passes / cmds / tee.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2014 Clifford Wolf <clifford@clifford.at>
5 * Copyright (C) 2014 Johann Glaser <Johann.Glaser@gmx.at>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 */
20
21 #include "kernel/register.h"
22 #include "kernel/rtlil.h"
23 #include "kernel/log.h"
24
25 USING_YOSYS_NAMESPACE
26 PRIVATE_NAMESPACE_BEGIN
27
28 struct TeePass : public Pass {
29 TeePass() : Pass("tee", "redirect command output to file") { }
30 void help() YS_OVERRIDE
31 {
32 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
33 log("\n");
34 log(" tee [-q] [-o logfile|-a logfile] cmd\n");
35 log("\n");
36 log("Execute the specified command, optionally writing the commands output to the\n");
37 log("specified logfile(s).\n");
38 log("\n");
39 log(" -q\n");
40 log(" Do not print output to the normal destination (console and/or log file).\n");
41 log("\n");
42 log(" -o logfile\n");
43 log(" Write output to this file, truncate if exists.\n");
44 log("\n");
45 log(" -a logfile\n");
46 log(" Write output to this file, append if exists.\n");
47 log("\n");
48 log(" +INT, -INT\n");
49 log(" Add/subtract INT from the -v setting for this command.\n");
50 log("\n");
51 }
52 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
53 {
54 std::vector<FILE*> backup_log_files, files_to_close;
55 int backup_log_verbose_level = log_verbose_level;
56 backup_log_files = log_files;
57
58 size_t argidx;
59 for (argidx = 1; argidx < args.size(); argidx++)
60 {
61 if (args[argidx] == "-q" && files_to_close.empty()) {
62 log_files.clear();
63 continue;
64 }
65 if ((args[argidx] == "-o" || args[argidx] == "-a") && argidx+1 < args.size()) {
66 const char *open_mode = args[argidx] == "-o" ? "w" : "a+";
67 FILE *f = fopen(args[++argidx].c_str(), open_mode);
68 yosys_input_files.insert(args[argidx]);
69 if (f == NULL) {
70 for (auto cf : files_to_close)
71 fclose(cf);
72 log_cmd_error("Can't create file %s.\n", args[argidx].c_str());
73 }
74 log_files.push_back(f);
75 files_to_close.push_back(f);
76 continue;
77 }
78 if (GetSize(args[argidx]) >= 2 && (args[argidx][0] == '-' || args[argidx][0] == '+') && args[argidx][1] >= '0' && args[argidx][1] <= '9') {
79 log_verbose_level += atoi(args[argidx].c_str());
80 continue;
81 }
82 break;
83 }
84
85 try {
86 std::vector<std::string> new_args(args.begin() + argidx, args.end());
87 Pass::call(design, new_args);
88 } catch (...) {
89 for (auto cf : files_to_close)
90 fclose(cf);
91 log_files = backup_log_files;
92 throw;
93 }
94
95 for (auto cf : files_to_close)
96 fclose(cf);
97
98 log_verbose_level = backup_log_verbose_level;
99 log_files = backup_log_files;
100 }
101 } TeePass;
102
103 PRIVATE_NAMESPACE_END