1 /******************************************************************************
2 * Copyright (c) 2017-2018, Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
28 #include <signal.h>
29 #include <getopt.h>
30 #include <iostream>
31 #include <iomanip>
32 #include <fstream>
33 #include <cassert>
34 #include <string>
35 #include <cstdlib>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
42 #include "tidl_viewer.h"
44 using namespace tidl;
46 static void ProcessArgs(int argc, char *argv[], std::string& network_file,
47 bool& do_print, std::string& dot_file);
49 static void DisplayHelp();
50 static bool fs_exists(std::string path);
52 int main(int argc, char *argv[])
53 {
54 // Catch ctrl-c to ensure a clean exit
55 signal(SIGABRT, exit);
56 signal(SIGTERM, exit);
58 // Process arguments
59 std::string network_file;
60 std::string dot_file;
61 bool do_print = false;
62 ProcessArgs(argc, argv, network_file, do_print, dot_file);
64 bool status = true;
66 // Dump network to stdout if requested
67 if (do_print)
68 status &= util::PrintNetwork(network_file);
70 // Generate a dot file
71 status &= util::GenerateDotGraphForNetwork(network_file, dot_file);
73 // If the dot utility exists, generate a SVG file
74 const std::string dot_bin("/usr/bin/dot");
75 if (fs_exists(dot_bin))
76 {
77 std::string command = dot_bin;
78 command += " -Tsvg " + dot_file + " -o " + dot_file + ".svg";
79 int x = std::system(command.c_str());
80 if (x != 0) status = false;
82 }
84 if (!status)
85 return EXIT_FAILURE;
87 return EXIT_SUCCESS;
88 }
91 void ProcessArgs(int argc, char *argv[], std::string& network_file,
92 bool& do_print, std::string& dot_file)
93 {
94 if (argc < 4)
95 {
96 DisplayHelp();
97 exit(EXIT_SUCCESS);
98 }
100 const struct option long_options[] =
101 {
102 {"help", no_argument, 0, 'h'},
103 {"dot", required_argument, 0, 'd'},
104 {"print", no_argument, 0, 'p'},
105 {0, 0, 0, 0}
106 };
108 int option_index = 0;
110 while (true)
111 {
112 int this_option_optind = optind ? optind : 1;
113 int c = getopt_long(argc, argv, "-d:ph", long_options, &option_index);
115 if (c == -1)
116 break;
118 switch (c)
119 {
120 case 1: network_file = argv[this_option_optind];
121 break;
123 case 'd': dot_file = optarg;
124 break;
126 case 'p': do_print = true;
127 break;
129 case 'h': DisplayHelp();
130 exit(EXIT_SUCCESS);
131 break;
133 case '?': // Error in getopt_long
134 exit(EXIT_FAILURE);
135 break;
137 default:
138 std::cerr << "Unsupported option: " << c << std::endl;
139 break;
140 }
141 }
143 if (dot_file.empty())
144 {
145 std::cerr << "ERROR: output dot file not specified." << std::endl;
146 DisplayHelp();
147 exit(EXIT_FAILURE);
148 }
149 }
151 #define STRING(S) XSTRING(S)
152 #define XSTRING(S) #S
153 void DisplayHelp()
154 {
155 std::string version = STRING(_BUILD_VER);
156 version += ".";
157 version += STRING(_BUILD_SHA);
159 std::cout << "Usage: tidl_viewer -d <dot file name> <network binary file>\n"
160 << "Version: " << version << std::endl
161 << "Options: \n"
162 " -p Print network layer info\n"
163 " -h Display this help message\n";
164 }
166 bool fs_exists(std::string path)
167 {
168 struct stat statbuf;
169 if (stat(path.c_str(), &statbuf) == 0) return true;
170 else return false;
171 }