source: CMT/v1r25-branch/source/cmt_constituent.cxx

Last change on this file was 669, checked in by rybkin, 10 years ago

svn merge -r 666:668 HEAD

  • Property svn:eol-style set to native
File size: 17.5 KB
Line 
1//-----------------------------------------------------------
2// Copyright Christian Arnault LAL-Orsay CNRS
3// arnault@lal.in2p3.fr
4// Modified by garonne@lal.in2p3.fr
5// Modified by Grigory Rybkin
6// See the complete license in cmt_license.txt "http://www.cecill.info".
7//-----------------------------------------------------------
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <ctype.h>
13
14#include "cmt_constituent.h"
15#include "cmt_generator.h"
16#include "cmt_system.h"
17#include "cmt_database.h"
18#include "cmt_log.h"
19
20/*----------------------------------------------------------*/
21/*                                                          */
22/*  Operations on Constituent                               */
23/*                                                          */
24/*----------------------------------------------------------*/
25
26//----------------------------------------------------------
27void Constituent::show (const cmt_string& name)
28{
29  Constituent* cptr = find (name);
30  if (cptr == 0) return;
31
32  const Constituent& constituent = *cptr;
33
34  constituent.show ();
35}
36
37//----------------------------------------------------------
38void Constituent::parse_all ()
39{
40  static ConstituentVector& Constituents = constituents ();
41
42  int number;
43
44  for (number = 0; number < Symbol::symbol_number (); number++)
45    {
46      Symbol& symbol = Symbol::symbol (number);
47
48      if (symbol.type != Symbol::SymbolAction) continue;
49
50      /*
51      if (symbol.value_lists.size () < 1) continue;
52
53      cmt_string value = symbol.build_macro_value ();
54
55      if (value != "")
56        {
57          add_for_action (symbol.name);
58        }
59      */
60      add_for_action (symbol);
61    }
62
63  for (number = 0; number < Constituents.size (); number++)
64    {
65      Constituent& constituent = Constituents[number];
66
67      constituent.parse ();
68    }
69}
70
71//----------------------------------------------------------
72void Constituent::show_all ()
73{
74  static ConstituentVector& Constituents = constituents ();
75
76  int number;
77
78  for (number = 0; number < Constituents.size (); number++)
79    {
80      const Constituent& constituent = Constituents[number];
81
82      constituent.show ();
83    }
84}
85
86//----------------------------------------------------------
87void Constituent::show_names ()
88{
89  static ConstituentVector& Constituents = constituents ();
90
91  int number;
92
93  for (number = 0; number < Constituents.size (); number++)
94    {
95      Constituent& constituent = Constituents[number];
96      cout << constituent.name << endl;
97    }
98}
99
100//----------------------------------------------------------
101Constituent* Constituent::find (const cmt_string& name)
102{
103  static ConstituentVector& Constituents = constituents ();
104
105  int constituent_index;
106
107  if (Constituents.size () == 0) return (0);
108
109  for (constituent_index = 0;
110       constituent_index < Constituents.size ();
111       constituent_index++)
112    {
113      Constituent& constituent = Constituents[constituent_index];
114
115      if (constituent.name == name)
116        {
117          return (&constituent);
118        }
119    }
120
121  return (0);
122}
123
124class constituents_action_iterator
125{
126public:
127  typedef enum
128    {
129      ready,
130      need_include
131    } states;
132
133  constituents_action_iterator (Constituent& c) : m_constituent (c)
134  {
135    m_state = ready;
136  }
137
138  void set (const cmt_string& w)
139  {
140    int equal;
141
142    if (w == "") return;
143
144    if (m_state == need_include)
145      {
146        m_state = ready;
147
148        cmt_string& include = m_constituent.includes.add ();
149        include = w;
150      }
151
152    if (w == "-OS9")
153      {
154        m_constituent.need_OS9 = true;
155      }
156    else if ((w == "-Windows") ||
157             (w == "-windows"))
158      {
159        m_constituent.windows = true;
160      }
161    else if (w == "-no_share")
162      {
163        m_constituent.no_share = true;
164      }
165    else if (w == "-no_static")
166      {
167        m_constituent.no_static = true;
168      }
169    else if (w == "-prototypes")
170      {
171        m_constituent.need_prototypes = true;
172      }
173    else if (w == "-no_prototypes")
174      {
175        m_constituent.need_prototypes = false;
176      }
177    else if (w == "-check")
178      {
179        if (m_constituent.type == Application)
180          {
181            m_constituent.need_check = true;
182          }
183      }
184    else if (w == "-triggers")
185      {
186        if (m_constituent.type == Library)
187          {
188            //m_constituent.build_triggers = true;
189          }
190      }
191    else if (w == "-no_triggers")
192      {
193        if (m_constituent.type == Library)
194          {
195            m_constituent.build_triggers = false;
196          }
197      }
198    else if (w == "-I")
199      {
200        m_state = need_include;
201      }
202    else if (w.substr (0, 3) == "-s=")
203      {
204        w.substr (3, m_subdir);
205      }
206    else if (w.substr (0, 3) == "-x=")
207      {
208        cmt_string& exclude = m_constituent.excludes.add ();
209        w.substr (3, exclude);
210        cmt_regexp& exp = m_constituent.exclude_exprs.add ();
211        exp.set (exclude);
212      }
213    else if (w.substr (0, 3) == "-k=")
214      {
215        cmt_string& select = m_constituent.selects.add ();
216        w.substr (3, select);
217        cmt_regexp& exp = m_constituent.select_exprs.add ();
218        exp.set (select);
219      }
220    else if (w.substr (0, 8) == "-import=")
221      {       
222        cmt_string text_imports;
223        w.substr (8, text_imports);
224
225        CmtSystem::cmt_string_vector imports;
226
227        CmtSystem::split (text_imports, ',', imports);
228
229        for (int i = 0; i < imports.size (); i++)
230          {
231            cmt_string& import = m_constituent.imports.add ();
232            import             = imports[i] ; 
233          }       
234      }
235    else if (w.substr (0, 7) == "-group=")
236      {
237        cmt_string group_name = "";
238
239        w.substr (7, group_name);
240       
241        if (group_name != "")
242          {
243            m_constituent.group = Group::add (group_name);
244          }
245      }
246    else if (w.substr (0, 8) == "-suffix=")
247      {
248        w.substr (8, m_constituent.suffix);
249      }
250    else if (w == "-target_tag")
251      {
252        m_constituent.has_target_tag = true;
253      }
254    else if (w.substr (0, 1) == "-")
255      {
256        //if (!Cmt::get_quiet ())
257        //{
258        CmtMessage::warning ("bad option " + w + " in constituent " +
259                             m_constituent.name);
260        //      cerr << "#CMT> Warning: bad option "
261        //           << w << " in constituent " << m_constituent.name << endl;
262        //CmtError::set (CmtError::execution_error, cmd);
263        //}
264      }
265    else if ((equal = w.find ("=")) != cmt_string::npos)
266      {
267        cmt_string variable_name;
268        cmt_string variable_value;
269       
270        w.substr (0, equal, variable_name);
271        w.substr (equal + 1, variable_value);
272       
273        Variable* v = Variable::find (m_constituent.variables, variable_name);
274        if (v == 0)
275          {
276            v = &(m_constituent.variables.add ());
277            v->set (variable_name);
278          }
279
280        (*v) = variable_value;
281      }
282    else
283      {
284        // We have a normal source module
285 
286        cmt_string& module = m_constituent.modules.add ();
287       
288        module.erase (0);
289         
290        //
291        // The prefix explicitly provided in (w) has priority
292        // over the currently specified (m_subdir) when it is an
293        // absolute path
294        //
295        if (CmtSystem::absolute_path (w))
296          {
297            module += w;
298          }
299        else
300          {
301            cmt_string prefix;
302            cmt_string name = w;
303
304            CmtSystem::dirname (name, prefix);
305            if (prefix == "../src") CmtSystem::basename (name, name);
306
307            module += m_subdir;
308
309            if (module != "")
310              {
311                module += CmtSystem::file_separator ();
312              }
313
314            module += name;
315          }
316      }
317  }
318
319  Constituent& m_constituent;
320  cmt_string m_subdir;
321  states m_state;
322};
323
324//----------------------------------------------------------
325void Constituent::action (ConstituentType type,
326                          const CmtSystem::cmt_string_vector& words)
327{
328  cmt_string generator;
329  cmt_string name;
330  Constituent* constituent;
331
332  int i = 1;
333
334  if (type == Document)
335    {
336      generator = words[i];
337      if (generator == "") return;
338      i++;
339    }
340
341  name = words[i];
342  if (name == "") return;
343  i++;
344
345  constituent = add (type, name, generator);
346
347  for (;i < words.size (); i++)
348    {
349      const cmt_string& w = words[i];
350      cmt_string& parameter = constituent->parameters.add ();
351      parameter = w;
352    }
353}
354
355//----------------------------------------------------------
356void Constituent::parse ()
357{
358  if (parameters.size () == 0) return;
359
360  Constituent& me = *this;
361
362  modules.clear ();
363
364  constituents_action_iterator it (me);
365
366  for (int i = 0; i < parameters.size (); i++)
367    {
368      const cmt_string& w = parameters[i];
369      cmt_string ew = w;
370
371      Symbol::expand (ew);
372
373      CmtSystem::cmt_string_vector ws;
374
375      //cerr << "Constituent " << name << " Analyzing module " << ew << endl;
376
377//       if (ew.substr (0, 13) == "action_value=")
378//      {
379//           it.set (ew);
380//      }
381//       else
382//      {
383          CmtSystem::split (ew, " \t", ws);
384
385          for (int j = 0; j < ws.size (); ++j)
386            {
387              const cmt_string& w = ws[j];
388         
389              //cerr << "Constituent " << name << " Setting module " << w << endl;
390              it.set (w);
391            }
392//      }
393    }
394
395  parameters.clear ();
396
397  if (has_target_tag && (Document != type || group->name () != "cmt_actions"))
398    {
399      const Tag* tag = Tag::find ("target_" + name);
400      if (!Tag::check_tag_used (tag) && !Symbol::check_tag_used (tag))
401        {
402          // Keep associated tag target_<name>
403          // only if this tag is used somewhere
404          // in tag or symbol expression
405          has_target_tag = false;
406          CmtMessage::verbose ("discarded tag target_" + name + " for "
407                               + (type == Application ? "application " :
408                                  (type == Library ? "library " : "document "))
409                               + name);
410        }
411    }
412}
413
414//----------------------------------------------------------
415Constituent* Constituent::add (ConstituentType type,
416                               const cmt_string& name,
417                               const cmt_string& generator)
418{
419  static ConstituentVector& Constituents = constituents ();
420
421  {
422    Constituent* constituent;
423
424    if (name == "") return (0);
425
426    constituent = find (name);
427    if (constituent != 0) return (constituent);
428  }
429
430  Constituent& constituent = Constituents.add ();
431  constituent.clear ();
432
433  constituent.name      = name;
434  constituent.generator = generator;
435  constituent.type      = type;
436  constituent.need_prototypes = Cmt::need_prototypes ();
437
438  return (&constituent);
439}
440
441//----------------------------------------------------------
442Constituent* Constituent::add_for_action (const Symbol& symbol)
443{
444  Constituent* constituent (0);
445  if (symbol.value_lists.size () < 1) return constituent;
446
447  const Tag* tag = Tag::find ("target_" + symbol.name);
448  const bool target_tag_used
449    (Tag::check_tag_used (tag) || Symbol::check_tag_used (tag));
450
451  cmt_string value = symbol.build_macro_value ();
452  if ( 0 == value.size () && !target_tag_used) return constituent;
453
454  constituent = add (Document, symbol.name, "cmt_action_runner");
455
456  constituent->group = Group::add ("cmt_actions");
457
458  // Do NOT associate target_<name> by default
459  // but only if this tag is used somewhere
460  // in tag or symbol expression
461  if (target_tag_used)
462    constituent->has_target_tag = true;
463
464  cmt_string& p1 = constituent->parameters.add ();
465  p1 = "action_value=";
466  Symbol::expand (value);
467  // quote since split will later be applied
468  // - in parse
469  p1 += CmtSystem::quote (value, " \t");
470//   p1 += "$(";
471//   p1 += symbol.name;
472//   p1 += ")";
473
474  return (constituent);
475}
476
477//----------------------------------------------------------
478void Constituent::clear_all ()
479{
480  static ConstituentVector& Constituents = constituents ();
481
482  for (int i = 0; i < Constituents.size (); i++)
483    {
484      Constituent& c = Constituents[i];
485      c.clear ();
486    }
487  Constituents.clear ();
488}
489
490//----------------------------------------------------------
491Constituent::ConstituentVector& Constituent::constituents ()
492{
493  static Database& db = Database::instance ();
494  static ConstituentVector& Constituents = db.constituents ();
495
496  return (Constituents);
497}
498
499//----------------------------------------------------------
500Constituent::Constituent ()
501{
502  clear ();
503}
504
505//----------------------------------------------------------
506Constituent::~Constituent ()
507{
508}
509
510//----------------------------------------------------------
511void Constituent::clear ()
512{
513  name      = "";
514  generator = "";
515  type = Document;
516  group     = 0;
517  modules.clear ();
518  parameters.clear ();
519  need_OS9        = false;
520  windows         = false;
521  no_static       = false;
522  no_share        = false;
523  need_prototypes = false;
524  need_check      = false;
525  build_triggers  = false;
526  has_target_tag  = false;
527  excludes.clear ();
528  exclude_exprs.clear ();
529  selects.clear ();
530  select_exprs.clear ();
531  includes.clear ();
532  imports.clear ();
533  variables.clear ();
534}
535
536//----------------------------------------------------------
537void Constituent::build_all_makefiles (bool simulation)
538{
539  static ConstituentVector& Constituents = constituents ();
540
541  int i;
542
543  for (i = 0; i < Constituents.size (); i++)
544    {
545      Constituent& constituent = Constituents[i];
546
547      constituent.build_makefile (simulation);
548    }
549}
550
551//----------------------------------------------------------
552void Constituent::build_all_msdev_files (bool simulation)
553{
554  static ConstituentVector& Constituents = constituents ();
555
556  int i;
557
558  Generator::build_msdev_workspace (Constituents);
559
560  for (i = 0; i < Constituents.size (); i++)
561    {
562      Constituent& constituent = Constituents[i];
563
564      constituent.build_msdev_file (simulation);
565    }
566}
567
568// Visual Studio.Net Support                                     
569//----------------------------------------------------------     
570void Constituent::build_all_vsnet_files (bool simulation)       
571{                                                               
572  static ConstituentVector& Constituents = constituents ();     
573                                                                 
574  int i;                                                         
575                                                                 
576  Generator::build_vsnet_workspace (Constituents);               
577                                                                 
578  for (i = 0; i < Constituents.size (); i++)                     
579    {                                                           
580      Constituent& constituent = Constituents[i];               
581                                                                 
582      constituent.build_vsnet_file (simulation);                 
583    }                                                           
584}                                                               
585                                                                 
586//----------------------------------------------------------
587void Constituent::build_makefile (bool simulation) const
588{
589  if (!simulation)
590    {
591      bool dependencies (false);
592      Generator::build_constituent_makefile (*this, dependencies);
593    }
594}
595
596//----------------------------------------------------------
597void Constituent::build_msdev_file (bool simulation) const
598{
599  if (!simulation)
600    {
601      Generator::build_msdev (*this);
602    }
603}
604
605// Visual Studio.net Support                                 
606//---------------------------------------------------------- 
607void Constituent::build_vsnet_file (bool simulation) const   
608{                                                             
609  if (!simulation)                                           
610    {                                                         
611      Generator::build_vsnet (*this);                         
612    }
613}
614
615//----------------------------------------------------------
616void Constituent::show (ostream& out) const
617//void Constituent::show () const
618{
619  int i;
620
621  switch (type)
622    {
623    case Library:
624      out << "library";
625      break;
626    case Application:
627      out << "application";
628      break;
629    case Document:
630      out << "document " << generator;
631      break;
632    }
633 
634  out << " " << name;
635 
636  if (group != 0)
637    {
638      out << " -group=" << group->name ();
639    }
640 
641  if (suffix != 0)
642    {
643      out << " -suffix=" << suffix;
644    }
645 
646  if ((type == Application) && need_check)
647    {
648      out << " -check";
649    }
650 
651  if ((type == Library) && no_share)
652    {
653      out << " -no_share";
654    }
655 
656  if ((type == Library) && no_static)
657    {
658      out << " -no_static";
659    }
660 
661  if ((type == Library) && build_triggers)
662    {
663      out << " -triggers";
664    }
665 
666  if (type == Application || type == Library)
667    {
668      if (need_prototypes)
669        out << " -prototypes";
670      else
671        out << " -no_prototypes";
672    }
673 
674  if (has_target_tag)
675    {
676      out << " -target_tag";
677    }
678 
679  for (i = 0; i < (imports.size ()); i++)
680    {
681      const cmt_string& import_name = imports[i];
682     
683      out << " -import=" << import_name;
684    }
685 
686  for (i = 0; i < (excludes.size ()); i++)
687    {
688      const cmt_string& exclude = excludes[i];
689     
690      out << " -x=" << exclude;
691    }
692 
693  for (i = 0; i < (selects.size ()); i++)
694    {
695      const cmt_string& select = selects[i];
696     
697      out << " -k=" << select;
698    }
699 
700  for (i = 0; i < (modules.size ()); i++)
701    {
702      const cmt_string& module_name = modules[i];
703     
704      out << " " << module_name;
705    }
706 
707  for (i = 0; i < (variables.size ()); i++)
708    {
709      const Variable& v = variables[i];
710     
711      //      out << " " << v.name << "=" << v.value;
712      // quote (up to) 2 times as split is applied 2 times
713      // - before action
714      // - in parse
715      cmt_string qvalue (CmtSystem::quote (v.value, " \t"));
716      out << " " << v.name << "="
717          << (qvalue == v.value ? v.value : CmtSystem::quote (qvalue, " \t"));
718      //          << CmtSystem::quote (CmtSystem::quote (v.value, " \t"), " \t");
719    }
720 
721  out << endl;
722}
Note: See TracBrowser for help on using the repository browser.