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

Last change on this file since 561 was 561, checked in by rybkin, 13 years ago

See C.L. 444

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