[1] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <ctype.h> |
---|
| 5 | |
---|
| 6 | #include "cmt_constituent.h" |
---|
| 7 | #include "cmt_generator.h" |
---|
| 8 | #include "cmt_system.h" |
---|
| 9 | #include "cmt_database.h" |
---|
| 10 | |
---|
| 11 | /*----------------------------------------------------------*/ |
---|
| 12 | /* */ |
---|
| 13 | /* Operations on Constituent */ |
---|
| 14 | /* */ |
---|
| 15 | /*----------------------------------------------------------*/ |
---|
| 16 | |
---|
| 17 | //---------------------------------------------------------- |
---|
| 18 | void Constituent::show (const cmt_string& name) |
---|
| 19 | { |
---|
| 20 | Constituent* cptr = find (name); |
---|
| 21 | if (cptr == 0) return; |
---|
| 22 | |
---|
| 23 | const Constituent& constituent = *cptr; |
---|
| 24 | |
---|
| 25 | constituent.show (); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | //---------------------------------------------------------- |
---|
| 29 | void Constituent::parse_all () |
---|
| 30 | { |
---|
| 31 | static ConstituentVector& Constituents = constituents (); |
---|
| 32 | |
---|
| 33 | int number; |
---|
| 34 | |
---|
| 35 | for (number = 0; number < Constituents.size (); number++) |
---|
| 36 | { |
---|
| 37 | Constituent& constituent = Constituents[number]; |
---|
| 38 | |
---|
| 39 | constituent.parse (); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | //---------------------------------------------------------- |
---|
| 44 | void Constituent::show_all () |
---|
| 45 | { |
---|
| 46 | static ConstituentVector& Constituents = constituents (); |
---|
| 47 | |
---|
| 48 | int number; |
---|
| 49 | |
---|
| 50 | for (number = 0; number < Constituents.size (); number++) |
---|
| 51 | { |
---|
| 52 | const Constituent& constituent = Constituents[number]; |
---|
| 53 | |
---|
| 54 | constituent.show (); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | //---------------------------------------------------------- |
---|
| 59 | void Constituent::show_names () |
---|
| 60 | { |
---|
| 61 | static ConstituentVector& Constituents = constituents (); |
---|
| 62 | |
---|
| 63 | int number; |
---|
| 64 | |
---|
| 65 | for (number = 0; number < Constituents.size (); number++) |
---|
| 66 | { |
---|
| 67 | Constituent& constituent = Constituents[number]; |
---|
| 68 | cout << constituent.name << endl; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | //---------------------------------------------------------- |
---|
| 73 | Constituent* Constituent::find (const cmt_string& name) |
---|
| 74 | { |
---|
| 75 | static ConstituentVector& Constituents = constituents (); |
---|
| 76 | |
---|
| 77 | int constituent_index; |
---|
| 78 | |
---|
| 79 | if (Constituents.size () == 0) return (0); |
---|
| 80 | |
---|
| 81 | for (constituent_index = 0; |
---|
| 82 | constituent_index < Constituents.size (); |
---|
| 83 | constituent_index++) |
---|
| 84 | { |
---|
| 85 | Constituent& constituent = Constituents[constituent_index]; |
---|
| 86 | |
---|
| 87 | if (constituent.name == name) |
---|
| 88 | { |
---|
| 89 | return (&constituent); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | return (0); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | class constituents_action_iterator |
---|
| 97 | { |
---|
| 98 | public: |
---|
| 99 | typedef enum |
---|
| 100 | { |
---|
| 101 | ready, |
---|
| 102 | need_include |
---|
| 103 | } states; |
---|
| 104 | |
---|
| 105 | constituents_action_iterator (Constituent& c) : m_constituent (c) |
---|
| 106 | { |
---|
| 107 | m_state = ready; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void set (const cmt_string& w) |
---|
| 111 | { |
---|
| 112 | int equal; |
---|
| 113 | |
---|
| 114 | if (w == "") return; |
---|
| 115 | |
---|
| 116 | if (m_state == need_include) |
---|
| 117 | { |
---|
| 118 | m_state = ready; |
---|
| 119 | |
---|
| 120 | cmt_string& include = m_constituent.includes.add (); |
---|
| 121 | include = w; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if (w == "-OS9") |
---|
| 125 | { |
---|
| 126 | m_constituent.need_OS9 = true; |
---|
| 127 | } |
---|
| 128 | else if ((w == "-Windows") || |
---|
| 129 | (w == "-windows")) |
---|
| 130 | { |
---|
| 131 | m_constituent.windows = true; |
---|
| 132 | } |
---|
| 133 | else if (w == "-no_share") |
---|
| 134 | { |
---|
| 135 | m_constituent.no_share = true; |
---|
| 136 | } |
---|
| 137 | else if (w == "-no_static") |
---|
| 138 | { |
---|
| 139 | m_constituent.no_static = true; |
---|
| 140 | } |
---|
| 141 | else if (w == "-prototypes") |
---|
| 142 | { |
---|
| 143 | m_constituent.need_prototypes = true; |
---|
| 144 | } |
---|
| 145 | else if (w == "-no_prototypes") |
---|
| 146 | { |
---|
| 147 | m_constituent.need_prototypes = false; |
---|
| 148 | } |
---|
| 149 | else if (w == "-check") |
---|
| 150 | { |
---|
| 151 | m_constituent.need_check = true; |
---|
| 152 | } |
---|
| 153 | else if (w == "-triggers") |
---|
| 154 | { |
---|
| 155 | if (m_constituent.type == Library) |
---|
| 156 | { |
---|
| 157 | //m_constituent.build_triggers = true; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | else if (w == "-no_triggers") |
---|
| 161 | { |
---|
| 162 | if (m_constituent.type == Library) |
---|
| 163 | { |
---|
| 164 | m_constituent.build_triggers = false; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | else if (w == "-I") |
---|
| 168 | { |
---|
| 169 | m_state = need_include; |
---|
| 170 | } |
---|
| 171 | else if (w.substr (0, 3) == "-s=") |
---|
| 172 | { |
---|
| 173 | w.substr (3, m_subdir); |
---|
| 174 | } |
---|
| 175 | else if (w.substr (0, 8) == "-import=") |
---|
| 176 | { |
---|
| 177 | cmt_string& import = m_constituent.imports.add (); |
---|
| 178 | w.substr (8, import); |
---|
| 179 | } |
---|
| 180 | else if (w.substr (0, 7) == "-group=") |
---|
| 181 | { |
---|
| 182 | cmt_string group_name = ""; |
---|
| 183 | |
---|
| 184 | w.substr (7, group_name); |
---|
| 185 | |
---|
| 186 | m_constituent.group = Group::add (group_name); |
---|
| 187 | } |
---|
| 188 | else if (w.substr (0, 8) == "-suffix=") |
---|
| 189 | { |
---|
| 190 | w.substr (8, m_constituent.suffix); |
---|
| 191 | } |
---|
| 192 | else if ((equal = w.find ("=")) != cmt_string::npos) |
---|
| 193 | { |
---|
| 194 | cmt_string variable_name; |
---|
| 195 | cmt_string variable_value; |
---|
| 196 | |
---|
| 197 | w.substr (0, equal, variable_name); |
---|
| 198 | w.substr (equal + 1, variable_value); |
---|
| 199 | |
---|
| 200 | Variable* v = Variable::find (m_constituent.variables, variable_name); |
---|
| 201 | if (v == 0) |
---|
| 202 | { |
---|
| 203 | v = &(m_constituent.variables.add ()); |
---|
| 204 | v->set (variable_name); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | (*v) = variable_value; |
---|
| 208 | } |
---|
| 209 | else |
---|
| 210 | { |
---|
| 211 | // We have a normal source module |
---|
| 212 | |
---|
| 213 | cmt_string& module = m_constituent.modules.add (); |
---|
| 214 | |
---|
| 215 | module.erase (0); |
---|
| 216 | |
---|
| 217 | // |
---|
| 218 | // The prefix explicitly provided in (w) has priority |
---|
| 219 | // over the currently specified (m_subdir) when it is an |
---|
| 220 | // absolute path |
---|
| 221 | // |
---|
| 222 | if (CmtSystem::absolute_path (w)) |
---|
| 223 | { |
---|
| 224 | module += w; |
---|
| 225 | } |
---|
| 226 | else |
---|
| 227 | { |
---|
| 228 | cmt_string prefix; |
---|
| 229 | cmt_string name = w; |
---|
| 230 | |
---|
| 231 | CmtSystem::dirname (name, prefix); |
---|
| 232 | if (prefix == "../src") CmtSystem::basename (name, name); |
---|
| 233 | |
---|
| 234 | module += m_subdir; |
---|
| 235 | |
---|
| 236 | if (module != "") |
---|
| 237 | { |
---|
| 238 | module += CmtSystem::file_separator (); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | module += name; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | Constituent& m_constituent; |
---|
| 247 | cmt_string m_subdir; |
---|
| 248 | states m_state; |
---|
| 249 | }; |
---|
| 250 | |
---|
| 251 | //---------------------------------------------------------- |
---|
| 252 | void Constituent::action (ConstituentType type, |
---|
| 253 | const CmtSystem::cmt_string_vector& words) |
---|
| 254 | { |
---|
| 255 | cmt_string generator; |
---|
| 256 | cmt_string name; |
---|
| 257 | Constituent* constituent; |
---|
| 258 | |
---|
| 259 | int i = 1; |
---|
| 260 | |
---|
| 261 | if (type == Document) |
---|
| 262 | { |
---|
| 263 | generator = words[i]; |
---|
| 264 | if (generator == "") return; |
---|
| 265 | i++; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | name = words[i]; |
---|
| 269 | if (name == "") return; |
---|
| 270 | i++; |
---|
| 271 | |
---|
| 272 | constituent = add (type, name, generator); |
---|
| 273 | |
---|
| 274 | for (;i < words.size (); i++) |
---|
| 275 | { |
---|
| 276 | const cmt_string& w = words[i]; |
---|
| 277 | cmt_string& parameter = constituent->parameters.add (); |
---|
| 278 | parameter = w; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | //---------------------------------------------------------- |
---|
| 283 | void Constituent::parse () |
---|
| 284 | { |
---|
| 285 | if (parameters.size () == 0) return; |
---|
| 286 | |
---|
| 287 | Constituent& me = *this; |
---|
| 288 | |
---|
| 289 | modules.clear (); |
---|
| 290 | |
---|
| 291 | constituents_action_iterator it (me); |
---|
| 292 | |
---|
| 293 | for (int i = 0; i < parameters.size (); i++) |
---|
| 294 | { |
---|
| 295 | const cmt_string& w = parameters[i]; |
---|
| 296 | cmt_string ew = w; |
---|
| 297 | |
---|
| 298 | Symbol::expand (ew); |
---|
| 299 | |
---|
| 300 | CmtSystem::cmt_string_vector ws; |
---|
| 301 | |
---|
| 302 | CmtSystem::split (ew, " \t", ws); |
---|
| 303 | |
---|
| 304 | for (int j = 0; j < ws.size (); ++j) |
---|
| 305 | { |
---|
| 306 | const cmt_string& w = ws[j]; |
---|
| 307 | |
---|
| 308 | //cerr << "Constituent " << name << " Setting module " << w << endl; |
---|
| 309 | it.set (w); |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | parameters.clear (); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | //---------------------------------------------------------- |
---|
| 317 | Constituent* Constituent::add (ConstituentType type, |
---|
| 318 | const cmt_string& name, |
---|
| 319 | const cmt_string& generator) |
---|
| 320 | { |
---|
| 321 | static ConstituentVector& Constituents = constituents (); |
---|
| 322 | |
---|
| 323 | { |
---|
| 324 | Constituent* constituent; |
---|
| 325 | |
---|
| 326 | if (name == "") return (0); |
---|
| 327 | |
---|
| 328 | constituent = find (name); |
---|
| 329 | if (constituent != 0) return (constituent); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | Constituent& constituent = Constituents.add (); |
---|
| 333 | constituent.clear (); |
---|
| 334 | |
---|
| 335 | constituent.name = name; |
---|
| 336 | constituent.generator = generator; |
---|
| 337 | constituent.type = type; |
---|
| 338 | constituent.need_prototypes = Cmt::need_prototypes (); |
---|
| 339 | |
---|
| 340 | return (&constituent); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | |
---|
| 344 | //---------------------------------------------------------- |
---|
| 345 | void Constituent::clear_all () |
---|
| 346 | { |
---|
| 347 | static ConstituentVector& Constituents = constituents (); |
---|
| 348 | |
---|
| 349 | for (int i = 0; i < Constituents.size (); i++) |
---|
| 350 | { |
---|
| 351 | Constituent& c = Constituents[i]; |
---|
| 352 | c.clear (); |
---|
| 353 | } |
---|
| 354 | Constituents.clear (); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | //---------------------------------------------------------- |
---|
| 358 | Constituent::ConstituentVector& Constituent::constituents () |
---|
| 359 | { |
---|
| 360 | static Database& db = Database::instance (); |
---|
| 361 | static ConstituentVector& Constituents = db.constituents (); |
---|
| 362 | |
---|
| 363 | return (Constituents); |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | //---------------------------------------------------------- |
---|
| 367 | Constituent::Constituent () |
---|
| 368 | { |
---|
| 369 | clear (); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | //---------------------------------------------------------- |
---|
| 373 | Constituent::~Constituent () |
---|
| 374 | { |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | //---------------------------------------------------------- |
---|
| 378 | void Constituent::clear () |
---|
| 379 | { |
---|
| 380 | name = ""; |
---|
| 381 | generator = ""; |
---|
| 382 | type = Document; |
---|
| 383 | group = 0; |
---|
| 384 | modules.clear (); |
---|
| 385 | parameters.clear (); |
---|
| 386 | need_OS9 = false; |
---|
| 387 | windows = false; |
---|
| 388 | no_static = false; |
---|
| 389 | no_share = false; |
---|
| 390 | need_prototypes = false; |
---|
| 391 | need_check = false; |
---|
| 392 | build_triggers = false; |
---|
| 393 | includes.clear (); |
---|
| 394 | imports.clear (); |
---|
| 395 | variables.clear (); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | //---------------------------------------------------------- |
---|
| 399 | void Constituent::build_all_makefiles (bool simulation) |
---|
| 400 | { |
---|
| 401 | static ConstituentVector& Constituents = constituents (); |
---|
| 402 | |
---|
| 403 | int i; |
---|
| 404 | |
---|
| 405 | for (i = 0; i < Constituents.size (); i++) |
---|
| 406 | { |
---|
| 407 | Constituent& constituent = Constituents[i]; |
---|
| 408 | |
---|
| 409 | constituent.build_makefile (simulation); |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | //---------------------------------------------------------- |
---|
| 414 | void Constituent::build_all_msdev_files (bool simulation) |
---|
| 415 | { |
---|
| 416 | static ConstituentVector& Constituents = constituents (); |
---|
| 417 | |
---|
| 418 | int i; |
---|
| 419 | |
---|
| 420 | Generator::build_msdev_workspace (Constituents); |
---|
| 421 | |
---|
| 422 | for (i = 0; i < Constituents.size (); i++) |
---|
| 423 | { |
---|
| 424 | Constituent& constituent = Constituents[i]; |
---|
| 425 | |
---|
| 426 | constituent.build_msdev_file (simulation); |
---|
| 427 | } |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | //---------------------------------------------------------- |
---|
| 431 | void Constituent::build_makefile (bool simulation) const |
---|
| 432 | { |
---|
| 433 | if (!simulation) |
---|
| 434 | { |
---|
| 435 | Generator::build_constituent_makefile (*this); |
---|
| 436 | } |
---|
| 437 | //else cout << command << endl; |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | //---------------------------------------------------------- |
---|
| 441 | void Constituent::build_msdev_file (bool simulation) const |
---|
| 442 | { |
---|
| 443 | if (!simulation) |
---|
| 444 | { |
---|
| 445 | Generator::build_msdev (*this); |
---|
| 446 | } |
---|
| 447 | //else cout << command << endl; |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | //---------------------------------------------------------- |
---|
| 451 | void Constituent::show () const |
---|
| 452 | { |
---|
| 453 | int i; |
---|
| 454 | |
---|
| 455 | switch (type) |
---|
| 456 | { |
---|
| 457 | case Library: |
---|
| 458 | cout << "library"; |
---|
| 459 | break; |
---|
| 460 | case Application: |
---|
| 461 | cout << "application"; |
---|
| 462 | break; |
---|
| 463 | case Document: |
---|
| 464 | cout << "document " << generator; |
---|
| 465 | break; |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | cout << " " << name; |
---|
| 469 | |
---|
| 470 | if (group != 0) |
---|
| 471 | { |
---|
| 472 | cout << " -group=" << group->name (); |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | if (suffix != 0) |
---|
| 476 | { |
---|
| 477 | cout << " -suffix=" << suffix; |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | if ((type == Application) && need_check) |
---|
| 481 | { |
---|
| 482 | cout << " -check"; |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | if ((type == Library) && no_share) |
---|
| 486 | { |
---|
| 487 | cout << " -no_share"; |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | if ((type == Library) && no_static) |
---|
| 491 | { |
---|
| 492 | cout << " -no_static"; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | if ((type == Library) && build_triggers) |
---|
| 496 | { |
---|
| 497 | cout << " -triggers"; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | for (i = 0; i < (imports.size ()); i++) |
---|
| 501 | { |
---|
| 502 | const cmt_string& import_name = imports[i]; |
---|
| 503 | |
---|
| 504 | cout << " -import=" << import_name; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | for (i = 0; i < (modules.size ()); i++) |
---|
| 508 | { |
---|
| 509 | const cmt_string& module_name = modules[i]; |
---|
| 510 | |
---|
| 511 | cout << " " << module_name; |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | for (i = 0; i < (variables.size ()); i++) |
---|
| 515 | { |
---|
| 516 | const Variable& v = variables[i]; |
---|
| 517 | |
---|
| 518 | cout << " " << v.name << "=" << v.value; |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | cout << endl; |
---|
| 522 | } |
---|