source: CMT/v1r26/source/cmt_constituent.cxx

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

See C.L. 500

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