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

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

See C.L. 522

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