source: CMT/HEAD/source/cmt_constituent.cxx @ 627

Last change on this file since 627 was 627, checked in by rybkin, 11 years ago

See C.L. 498

  • 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// See the complete license in cmt_license.txt "http://www.cecill.info".
6//-----------------------------------------------------------
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <ctype.h>
12
13#include "cmt_constituent.h"
14#include "cmt_generator.h"
15#include "cmt_system.h"
16#include "cmt_database.h"
17#include "cmt_log.h"
18
19/*----------------------------------------------------------*/
20/*                                                          */
21/*  Operations on Constituent                               */
22/*                                                          */
23/*----------------------------------------------------------*/
24
25//----------------------------------------------------------
26void Constituent::show (const cmt_string& name)
27{
28  Constituent* cptr = find (name);
29  if (cptr == 0) return;
30
31  const Constituent& constituent = *cptr;
32
33  constituent.show ();
34}
35
36//----------------------------------------------------------
37void Constituent::parse_all ()
38{
39  static ConstituentVector& Constituents = constituents ();
40
41  int number;
42
43  for (number = 0; number < Symbol::symbol_number (); number++)
44    {
45      Symbol& symbol = Symbol::symbol (number);
46
47      if (symbol.type != Symbol::SymbolAction) continue;
48
49      /*
50      if (symbol.value_lists.size () < 1) continue;
51
52      cmt_string value = symbol.build_macro_value ();
53
54      if (value != "")
55        {
56          add_for_action (symbol.name);
57        }
58      */
59      add_for_action (symbol);
60    }
61
62  for (number = 0; number < Constituents.size (); number++)
63    {
64      Constituent& constituent = Constituents[number];
65
66      constituent.parse ();
67    }
68}
69
70//----------------------------------------------------------
71void Constituent::show_all ()
72{
73  static ConstituentVector& Constituents = constituents ();
74
75  int number;
76
77  for (number = 0; number < Constituents.size (); number++)
78    {
79      const Constituent& constituent = Constituents[number];
80
81      constituent.show ();
82    }
83}
84
85//----------------------------------------------------------
86void Constituent::show_names ()
87{
88  static ConstituentVector& Constituents = constituents ();
89
90  int number;
91
92  for (number = 0; number < Constituents.size (); number++)
93    {
94      Constituent& constituent = Constituents[number];
95      cout << constituent.name << endl;
96    }
97}
98
99//----------------------------------------------------------
100Constituent* Constituent::find (const cmt_string& name)
101{
102  static ConstituentVector& Constituents = constituents ();
103
104  int constituent_index;
105
106  if (Constituents.size () == 0) return (0);
107
108  for (constituent_index = 0;
109       constituent_index < Constituents.size ();
110       constituent_index++)
111    {
112      Constituent& constituent = Constituents[constituent_index];
113
114      if (constituent.name == name)
115        {
116          return (&constituent);
117        }
118    }
119
120  return (0);
121}
122
123class constituents_action_iterator
124{
125public:
126  typedef enum
127    {
128      ready,
129      need_include
130    } states;
131
132  constituents_action_iterator (Constituent& c) : m_constituent (c)
133  {
134    m_state = ready;
135  }
136
137  void set (const cmt_string& w)
138  {
139    int equal;
140
141    if (w == "") return;
142
143    if (m_state == need_include)
144      {
145        m_state = ready;
146
147        cmt_string& include = m_constituent.includes.add ();
148        include = w;
149      }
150
151    if (w == "-OS9")
152      {
153        m_constituent.need_OS9 = true;
154      }
155    else if ((w == "-Windows") ||
156             (w == "-windows"))
157      {
158        m_constituent.windows = true;
159      }
160    else if (w == "-no_share")
161      {
162        m_constituent.no_share = true;
163      }
164    else if (w == "-no_static")
165      {
166        m_constituent.no_static = true;
167      }
168    else if (w == "-prototypes")
169      {
170        m_constituent.need_prototypes = true;
171      }
172    else if (w == "-no_prototypes")
173      {
174        m_constituent.need_prototypes = false;
175      }
176    else if (w == "-check")
177      {
178        if (m_constituent.type == Application)
179          {
180            m_constituent.need_check = true;
181          }
182      }
183    else if (w == "-triggers")
184      {
185        if (m_constituent.type == Library)
186          {
187            //m_constituent.build_triggers = true;
188          }
189      }
190    else if (w == "-no_triggers")
191      {
192        if (m_constituent.type == Library)
193          {
194            m_constituent.build_triggers = false;
195          }
196      }
197    else if (w == "-I")
198      {
199        m_state = need_include;
200      }
201    else if (w.substr (0, 3) == "-s=")
202      {
203        w.substr (3, m_subdir);
204      }
205    else if (w.substr (0, 3) == "-x=")
206      {
207        cmt_string& exclude = m_constituent.excludes.add ();
208        w.substr (3, exclude);
209        cmt_regexp& exp = m_constituent.exclude_exprs.add ();
210        exp.set (exclude);
211      }
212    else if (w.substr (0, 3) == "-k=")
213      {
214        cmt_string& select = m_constituent.selects.add ();
215        w.substr (3, select);
216        cmt_regexp& exp = m_constituent.select_exprs.add ();
217        exp.set (select);
218      }
219    else if (w.substr (0, 8) == "-import=")
220      {       
221        cmt_string text_imports;
222        w.substr (8, text_imports);
223
224        CmtSystem::cmt_string_vector imports;
225
226        CmtSystem::split (text_imports, ',', imports);
227
228        for (int i = 0; i < imports.size (); i++)
229          {
230            cmt_string& import = m_constituent.imports.add ();
231            import             = imports[i] ; 
232          }       
233      }
234    else if (w.substr (0, 7) == "-group=")
235      {
236        cmt_string group_name = "";
237
238        w.substr (7, group_name);
239       
240        if (group_name != "")
241          {
242            m_constituent.group = Group::add (group_name);
243          }
244      }
245    else if (w.substr (0, 8) == "-suffix=")
246      {
247        w.substr (8, m_constituent.suffix);
248      }
249    else if (w == "-target_tag")
250      {
251        m_constituent.has_target_tag = true;
252      }
253    else if (w.substr (0, 1) == "-")
254      {
255        //if (!Cmt::get_quiet ())
256        //{
257        CmtMessage::warning ("bad option " + w + " in constituent " +
258                             m_constituent.name);
259        //      cerr << "#CMT> Warning: bad option "
260        //           << w << " in constituent " << m_constituent.name << endl;
261        //CmtError::set (CmtError::execution_error, cmd);
262        //}
263      }
264    else if ((equal = w.find ("=")) != cmt_string::npos)
265      {
266        cmt_string variable_name;
267        cmt_string variable_value;
268       
269        w.substr (0, equal, variable_name);
270        w.substr (equal + 1, variable_value);
271       
272        Variable* v = Variable::find (m_constituent.variables, variable_name);
273        if (v == 0)
274          {
275            v = &(m_constituent.variables.add ());
276            v->set (variable_name);
277          }
278
279        (*v) = variable_value;
280      }
281    else
282      {
283        // We have a normal source module
284 
285        cmt_string& module = m_constituent.modules.add ();
286       
287        module.erase (0);
288         
289        //
290        // The prefix explicitly provided in (w) has priority
291        // over the currently specified (m_subdir) when it is an
292        // absolute path
293        //
294        if (CmtSystem::absolute_path (w))
295          {
296            module += w;
297          }
298        else
299          {
300            cmt_string prefix;
301            cmt_string name = w;
302
303            CmtSystem::dirname (name, prefix);
304            if (prefix == "../src") CmtSystem::basename (name, name);
305
306            module += m_subdir;
307
308            if (module != "")
309              {
310                module += CmtSystem::file_separator ();
311              }
312
313            module += name;
314          }
315      }
316  }
317
318  Constituent& m_constituent;
319  cmt_string m_subdir;
320  states m_state;
321};
322
323//----------------------------------------------------------
324void Constituent::action (ConstituentType type,
325                          const CmtSystem::cmt_string_vector& words)
326{
327  cmt_string generator;
328  cmt_string name;
329  Constituent* constituent;
330
331  int i = 1;
332
333  if (type == Document)
334    {
335      generator = words[i];
336      if (generator == "") return;
337      i++;
338    }
339
340  name = words[i];
341  if (name == "") return;
342  i++;
343
344  constituent = add (type, name, generator);
345
346  for (;i < words.size (); i++)
347    {
348      const cmt_string& w = words[i];
349      cmt_string& parameter = constituent->parameters.add ();
350      parameter = w;
351    }
352}
353
354//----------------------------------------------------------
355void Constituent::parse ()
356{
357  if (parameters.size () == 0) return;
358
359  Constituent& me = *this;
360
361  modules.clear ();
362
363  constituents_action_iterator it (me);
364
365  for (int i = 0; i < parameters.size (); i++)
366    {
367      const cmt_string& w = parameters[i];
368      cmt_string ew = w;
369
370      Symbol::expand (ew);
371
372      CmtSystem::cmt_string_vector ws;
373
374      //cerr << "Constituent " << name << " Analyzing module " << ew << endl;
375
376//       if (ew.substr (0, 13) == "action_value=")
377//      {
378//           it.set (ew);
379//      }
380//       else
381//      {
382          CmtSystem::split (ew, " \t", ws);
383
384          for (int j = 0; j < ws.size (); ++j)
385            {
386              const cmt_string& w = ws[j];
387         
388              //cerr << "Constituent " << name << " Setting module " << w << endl;
389              it.set (w);
390            }
391//      }
392    }
393
394  parameters.clear ();
395
396  if (has_target_tag && (Document != type || "cmt_actions" != group->name ()))
397    {
398      const Tag* tag = Tag::find ("target_" + name);
399      if (!Tag::check_tag_used (tag) && !Symbol::check_tag_used (tag))
400        {
401          // Keep associated tag target_<name>
402          // only if this tag is used somewhere
403          // in tag or symbol expression
404          has_target_tag = false;
405          CmtMessage::verbose ("discarded tag target_" + name + " for "
406                               + (type == Application ? "application " :
407                                  (type == Library ? "library " : "document "))
408                               + name);
409        }
410    }
411}
412
413//----------------------------------------------------------
414Constituent* Constituent::add (ConstituentType type,
415                               const cmt_string& name,
416                               const cmt_string& generator)
417{
418  static ConstituentVector& Constituents = constituents ();
419
420  {
421    Constituent* constituent;
422
423    if (name == "") return (0);
424
425    constituent = find (name);
426    if (constituent != 0) return (constituent);
427  }
428
429  Constituent& constituent = Constituents.add ();
430  constituent.clear ();
431
432  constituent.name      = name;
433  constituent.generator = generator;
434  constituent.type      = type;
435  constituent.need_prototypes = Cmt::need_prototypes ();
436
437  return (&constituent);
438}
439
440//----------------------------------------------------------
441Constituent* Constituent::add_for_action (const Symbol& symbol)
442//Constituent* Constituent::add_for_action (const cmt_string& name)
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 ( "" == value && !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.