00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <ctype.h>
00005
00006 #include "cmt_constituent.h"
00007 #include "cmt_generator.h"
00008 #include "cmt_system.h"
00009 #include "cmt_database.h"
00010
00011
00012
00013
00014
00015
00016
00017
00018 void Constituent::show (const cmt_string& name)
00019 {
00020 Constituent* cptr = find (name);
00021 if (cptr == 0) return;
00022
00023 const Constituent& constituent = *cptr;
00024
00025 constituent.show ();
00026 }
00027
00028
00029 void Constituent::parse_all ()
00030 {
00031 static ConstituentVector& Constituents = constituents ();
00032
00033 int number;
00034
00035 for (number = 0; number < Constituents.size (); number++)
00036 {
00037 Constituent& constituent = Constituents[number];
00038
00039 constituent.parse ();
00040 }
00041 }
00042
00043
00044 void Constituent::show_all ()
00045 {
00046 static ConstituentVector& Constituents = constituents ();
00047
00048 int number;
00049
00050 for (number = 0; number < Constituents.size (); number++)
00051 {
00052 const Constituent& constituent = Constituents[number];
00053
00054 constituent.show ();
00055 }
00056 }
00057
00058
00059 void Constituent::show_names ()
00060 {
00061 static ConstituentVector& Constituents = constituents ();
00062
00063 int number;
00064
00065 for (number = 0; number < Constituents.size (); number++)
00066 {
00067 Constituent& constituent = Constituents[number];
00068 cout << constituent.name << endl;
00069 }
00070 }
00071
00072
00073 Constituent* Constituent::find (const cmt_string& name)
00074 {
00075 static ConstituentVector& Constituents = constituents ();
00076
00077 int constituent_index;
00078
00079 if (Constituents.size () == 0) return (0);
00080
00081 for (constituent_index = 0;
00082 constituent_index < Constituents.size ();
00083 constituent_index++)
00084 {
00085 Constituent& constituent = Constituents[constituent_index];
00086
00087 if (constituent.name == name)
00088 {
00089 return (&constituent);
00090 }
00091 }
00092
00093 return (0);
00094 }
00095
00096 class constituents_action_iterator
00097 {
00098 public:
00099 typedef enum
00100 {
00101 ready,
00102 need_include
00103 } states;
00104
00105 constituents_action_iterator (Constituent& c) : m_constituent (c)
00106 {
00107 m_state = ready;
00108 }
00109
00110 void set (const cmt_string& w)
00111 {
00112 int equal;
00113
00114 if (w == "") return;
00115
00116 if (m_state == need_include)
00117 {
00118 m_state = ready;
00119
00120 cmt_string& include = m_constituent.includes.add ();
00121 include = w;
00122 }
00123
00124 if (w == "-OS9")
00125 {
00126 m_constituent.need_OS9 = true;
00127 }
00128 else if ((w == "-Windows") ||
00129 (w == "-windows"))
00130 {
00131 m_constituent.windows = true;
00132 }
00133 else if (w == "-no_share")
00134 {
00135 m_constituent.no_share = true;
00136 }
00137 else if (w == "-no_static")
00138 {
00139 m_constituent.no_static = true;
00140 }
00141 else if (w == "-prototypes")
00142 {
00143 m_constituent.need_prototypes = true;
00144 }
00145 else if (w == "-no_prototypes")
00146 {
00147 m_constituent.need_prototypes = false;
00148 }
00149 else if (w == "-check")
00150 {
00151 m_constituent.need_check = true;
00152 }
00153 else if (w == "-triggers")
00154 {
00155 if (m_constituent.type == Library)
00156 {
00157
00158 }
00159 }
00160 else if (w == "-no_triggers")
00161 {
00162 if (m_constituent.type == Library)
00163 {
00164 m_constituent.build_triggers = false;
00165 }
00166 }
00167 else if (w == "-I")
00168 {
00169 m_state = need_include;
00170 }
00171 else if (w.substr (0, 3) == "-s=")
00172 {
00173 w.substr (3, m_subdir);
00174 }
00175 else if (w.substr (0, 8) == "-import=")
00176 {
00177 cmt_string& import = m_constituent.imports.add ();
00178 w.substr (8, import);
00179 }
00180 else if (w.substr (0, 7) == "-group=")
00181 {
00182 cmt_string group_name = "";
00183
00184 w.substr (7, group_name);
00185
00186 m_constituent.group = Group::add (group_name);
00187 }
00188 else if (w.substr (0, 8) == "-suffix=")
00189 {
00190 w.substr (8, m_constituent.suffix);
00191 }
00192 else if ((equal = w.find ("=")) != cmt_string::npos)
00193 {
00194 cmt_string variable_name;
00195 cmt_string variable_value;
00196
00197 w.substr (0, equal, variable_name);
00198 w.substr (equal + 1, variable_value);
00199
00200 Variable* v = Variable::find (m_constituent.variables, variable_name);
00201 if (v == 0)
00202 {
00203 v = &(m_constituent.variables.add ());
00204 v->set (variable_name);
00205 }
00206
00207 (*v) = variable_value;
00208 }
00209 else
00210 {
00211
00212
00213 cmt_string& module = m_constituent.modules.add ();
00214
00215 module.erase (0);
00216
00217
00218
00219
00220
00221
00222 if (CmtSystem::absolute_path (w))
00223 {
00224 module += w;
00225 }
00226 else
00227 {
00228 cmt_string prefix;
00229 cmt_string name = w;
00230
00231 CmtSystem::dirname (name, prefix);
00232 if (prefix == "../src") CmtSystem::basename (name, name);
00233
00234 module += m_subdir;
00235
00236 if (module != "")
00237 {
00238 module += CmtSystem::file_separator ();
00239 }
00240
00241 module += name;
00242 }
00243 }
00244 }
00245
00246 Constituent& m_constituent;
00247 cmt_string m_subdir;
00248 states m_state;
00249 };
00250
00251
00252 void Constituent::action (ConstituentType type,
00253 const CmtSystem::cmt_string_vector& words)
00254 {
00255 cmt_string generator;
00256 cmt_string name;
00257 Constituent* constituent;
00258
00259 int i = 1;
00260
00261 if (type == Document)
00262 {
00263 generator = words[i];
00264 if (generator == "") return;
00265 i++;
00266 }
00267
00268 name = words[i];
00269 if (name == "") return;
00270 i++;
00271
00272 constituent = add (type, name, generator);
00273
00274 for (;i < words.size (); i++)
00275 {
00276 const cmt_string& w = words[i];
00277 cmt_string& parameter = constituent->parameters.add ();
00278 parameter = w;
00279 }
00280 }
00281
00282
00283 void Constituent::parse ()
00284 {
00285 if (parameters.size () == 0) return;
00286
00287 Constituent& me = *this;
00288
00289 modules.clear ();
00290
00291 constituents_action_iterator it (me);
00292
00293 for (int i = 0; i < parameters.size (); i++)
00294 {
00295 const cmt_string& w = parameters[i];
00296 cmt_string ew = w;
00297
00298 Symbol::expand (ew);
00299
00300 CmtSystem::cmt_string_vector ws;
00301
00302 CmtSystem::split (ew, " \t", ws);
00303
00304 for (int j = 0; j < ws.size (); ++j)
00305 {
00306 const cmt_string& w = ws[j];
00307
00308
00309 it.set (w);
00310 }
00311 }
00312
00313 parameters.clear ();
00314 }
00315
00316
00317 Constituent* Constituent::add (ConstituentType type,
00318 const cmt_string& name,
00319 const cmt_string& generator)
00320 {
00321 static ConstituentVector& Constituents = constituents ();
00322
00323 {
00324 Constituent* constituent;
00325
00326 if (name == "") return (0);
00327
00328 constituent = find (name);
00329 if (constituent != 0) return (constituent);
00330 }
00331
00332 Constituent& constituent = Constituents.add ();
00333 constituent.clear ();
00334
00335 constituent.name = name;
00336 constituent.generator = generator;
00337 constituent.type = type;
00338 constituent.need_prototypes = Cmt::need_prototypes ();
00339
00340 return (&constituent);
00341 }
00342
00343
00344
00345 void Constituent::clear_all ()
00346 {
00347 static ConstituentVector& Constituents = constituents ();
00348
00349 for (int i = 0; i < Constituents.size (); i++)
00350 {
00351 Constituent& c = Constituents[i];
00352 c.clear ();
00353 }
00354 Constituents.clear ();
00355 }
00356
00357
00358 Constituent::ConstituentVector& Constituent::constituents ()
00359 {
00360 static Database& db = Database::instance ();
00361 static ConstituentVector& Constituents = db.constituents ();
00362
00363 return (Constituents);
00364 }
00365
00366
00367 Constituent::Constituent ()
00368 {
00369 clear ();
00370 }
00371
00372
00373 Constituent::~Constituent ()
00374 {
00375 }
00376
00377
00378 void Constituent::clear ()
00379 {
00380 name = "";
00381 generator = "";
00382 type = Document;
00383 group = 0;
00384 modules.clear ();
00385 parameters.clear ();
00386 need_OS9 = false;
00387 windows = false;
00388 no_static = false;
00389 no_share = false;
00390 need_prototypes = false;
00391 need_check = false;
00392 build_triggers = false;
00393 includes.clear ();
00394 imports.clear ();
00395 variables.clear ();
00396 }
00397
00398
00399 void Constituent::build_all_makefiles (bool simulation)
00400 {
00401 static ConstituentVector& Constituents = constituents ();
00402
00403 int i;
00404
00405 for (i = 0; i < Constituents.size (); i++)
00406 {
00407 Constituent& constituent = Constituents[i];
00408
00409 constituent.build_makefile (simulation);
00410 }
00411 }
00412
00413
00414 void Constituent::build_all_msdev_files (bool simulation)
00415 {
00416 static ConstituentVector& Constituents = constituents ();
00417
00418 int i;
00419
00420 Generator::build_msdev_workspace (Constituents);
00421
00422 for (i = 0; i < Constituents.size (); i++)
00423 {
00424 Constituent& constituent = Constituents[i];
00425
00426 constituent.build_msdev_file (simulation);
00427 }
00428 }
00429
00430
00431 void Constituent::build_makefile (bool simulation) const
00432 {
00433 if (!simulation)
00434 {
00435 Generator::build_constituent_makefile (*this);
00436 }
00437
00438 }
00439
00440
00441 void Constituent::build_msdev_file (bool simulation) const
00442 {
00443 if (!simulation)
00444 {
00445 Generator::build_msdev (*this);
00446 }
00447
00448 }
00449
00450
00451 void Constituent::show () const
00452 {
00453 int i;
00454
00455 switch (type)
00456 {
00457 case Library:
00458 cout << "library";
00459 break;
00460 case Application:
00461 cout << "application";
00462 break;
00463 case Document:
00464 cout << "document " << generator;
00465 break;
00466 }
00467
00468 cout << " " << name;
00469
00470 if (group != 0)
00471 {
00472 cout << " -group=" << group->name ();
00473 }
00474
00475 if (suffix != 0)
00476 {
00477 cout << " -suffix=" << suffix;
00478 }
00479
00480 if ((type == Application) && need_check)
00481 {
00482 cout << " -check";
00483 }
00484
00485 if ((type == Library) && no_share)
00486 {
00487 cout << " -no_share";
00488 }
00489
00490 if ((type == Library) && no_static)
00491 {
00492 cout << " -no_static";
00493 }
00494
00495 if ((type == Library) && build_triggers)
00496 {
00497 cout << " -triggers";
00498 }
00499
00500 for (i = 0; i < (imports.size ()); i++)
00501 {
00502 const cmt_string& import_name = imports[i];
00503
00504 cout << " -import=" << import_name;
00505 }
00506
00507 for (i = 0; i < (modules.size ()); i++)
00508 {
00509 const cmt_string& module_name = modules[i];
00510
00511 cout << " " << module_name;
00512 }
00513
00514 for (i = 0; i < (variables.size ()); i++)
00515 {
00516 const Variable& v = variables[i];
00517
00518 cout << " " << v.name << "=" << v.value;
00519 }
00520
00521 cout << endl;
00522 }