CompileDES  3.13
Executable-Code Generation from Synchronised libFAUDES Automata
compiledes.cpp
Go to the documentation of this file.
1 
12 #include "libfaudes.h"
13 #include "cgp_codegenerator.h"
14 
15 
16 using namespace faudes;
17 
18 
19 
20 // print usage info and exit
21 void usage_exit(const std::string& message="") {
22  if(message!="") {
23  std::cout << "compiledes: " << message << std::endl;
24  std::cout << "" << std::endl;
25  exit(-1);
26  }
27  std::cout << "compiledes: version " << CodeGenerator::VersionString() << std::endl;
28  std::cout << "" << std::endl;
29  std::cout << "compiledes: usage: " << std::endl;
30  std::cout << "compiledes [-q][-v] -t <target> -o <outfile> <srcfile>"<< std::endl;
31  std::cout << "where " << std::endl;
32  std::cout << " <scrfile>: code generator source file, typically \"*.cgc\"" << std::endl;
33  std::cout << "" << std::endl;
34  std::cout << " -q: less console output " << std::endl;
35  std::cout << " -qq: absolutely no console output " << std::endl;
36  std::cout << " -v: more console output" << std::endl;
37  std::cout << " -vv: even more console output" << std::endl;
38  std::cout << "" << std::endl;
39  std::cout << " -t <target>: target platform, see also below " << std::endl;
40  std::cout << " -o <outfile>: (base)name of output file(s), defaults to console output" << std::endl;
41  std::cout << "" << std::endl;
42  std::cout << "compiledes: target platforms: ";
43  std::vector< std::string > registry = CodeGenerator::Registry();
44  for(size_t i=0; i<registry.size(); ++i)
45  std::cout << registry[i] << " ";
46  std::cout << "" << std::endl;
47  std::cout << "" << std::endl;
48  exit(-1);
49 }
50 
51 // parse commandline, read executor and run executor
52 int main(int argc, char* argv[]) {
53 
54  // helper
55  std::string mMark="% compiledes: ========================================= ";
56 
57  // default behaviour
58  int mConsoleOut=0;
59  std::string mSrcFile="";
60  std::string mOutFile="std::cout";
61  std::string mTarget="";
62 
63  // primitive commad line parsing
64  for(int i=1; i<argc; i++) {
65  std::string option(argv[i]);
66  // option: quiet
67  if((option=="-q") || (option=="--quiet")) {
68  mConsoleOut=-1;
69  continue;
70  }
71  // option: quiet
72  if(option=="-qq"){
73  mConsoleOut=-2;
74  continue;
75  }
76  // option: verbose
77  if((option=="-v") || (option=="--verbose")) {
78  mConsoleOut=1;
79  continue;
80  }
81  // option: verbose
82  if(option=="-vv") {
83  mConsoleOut=2;
84  continue;
85  }
86  // option: out file
87  if((option=="-o") || (option=="--out")) {
88  i++; if(i>=argc) usage_exit();
89  mOutFile=argv[i];
90  continue;
91  }
92  // option: target
93  if((option=="-t") || (option=="--target")) {
94  i++; if(i>=argc) usage_exit();
95  mTarget=argv[i];
96  continue;
97  }
98  // option: help
99  if((option=="-?") || (option=="--help")) {
100  usage_exit();
101  continue;
102  }
103  // option: unknown
104  if(option.c_str()[0]=='-') {
105  usage_exit("unknown option "+ option);
106  continue;
107  }
108  // filename
109  if(mSrcFile!="")
110  usage_exit("more than one source file specified" );
111  mSrcFile=option;
112  }
113 
114  // insist in filename
115  if(mSrcFile=="")
116  usage_exit("you must specify a source file" );
117 
118  // prepare code generator object
119  CodeGenerator* mpCodeGenerator = CodeGenerator::New(mTarget);
120 
121  // test target
122  if(!mpCodeGenerator)
123  usage_exit("unknown target platform");
124 
125  // set verbosity
126  mpCodeGenerator->Verbose(mConsoleOut);
127 
128  // read configuration
129  try{
130  mpCodeGenerator->Read(mSrcFile);
131  }
132  catch(const Exception& fe) {
133  std::cout << std::flush;
134  std::cerr << fe.Message() << std::endl;
135  std::cerr << "CompileDES: caught libFAUDES exception --- missing/missformed configuration file" << std::endl;
136  return 1;
137  }
138 
139  // set output file
140  try{
141  mpCodeGenerator->OutputMode(mOutFile);
142  }
143  catch(const Exception& fe) {
144  std::cout << std::flush;
145  std::cerr << fe.Message() << std::endl;
146  std::cerr << "CompileDES: caught libFAUDES exception --- failed to setup output file/console or output filters" << std::endl;
147  return 1;
148  }
149 
150  // compile
151  try{
152  mpCodeGenerator->Compile();
153  }
154  catch(const Exception& fe) {
155  std::cout << std::flush;
156  std::cerr << fe.Message() << std::endl;
157  std::cerr << "CompileDES: caught libFAUDES exception --- failed to compile configuration data to internal representation" << std::endl;
158  return 1;
159  }
160 
161  // generate code
162  try{
163  mpCodeGenerator->Generate();
164  }
165  catch(const Exception& fe) {
166  std::cout << std::flush;
167  std::cerr << fe.Message() << std::endl;
168  std::cerr << "CompileDES: caught libFAUDES exception --- failed to generate target code for specified options" << std::endl;
169  return 1;
170  }
171 
172  // done
173  return 0;
174 }
Code-generator common base class.
Code-generation common base.
static std::vector< std::string > Registry(void)
Access registry contents.
virtual void OutputMode(const std::string &mode)
Set code output mode.
static CodeGenerator * New(const std::string &type)
Instantiate by identifier (returns 0 on unknown class)
void Verbose(int level, std::ostream *altout=0)
Set verbosity level.
static std::string VersionString(void)
Version (refers to macro COMPILEDES_VERSION, defined in cgp_codegenerator.h)
virtual void Compile(void)
Compile input data for alternative representation.
virtual void Generate(void)
Generate code.