ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/BuildSystem/ToolParser.pm
Revision: 1.5.4.3
Committed: Wed Jan 24 13:59:03 2007 UTC (18 years, 3 months ago) by sashby
Content type: text/plain
Branch: v103_with_xml
CVS Tags: V110p1
Changes since 1.5.4.2: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 sashby 1.2 #____________________________________________________________________
2     # File: ToolParser.pm
3     #____________________________________________________________________
4     #
5     # Author: Shaun Ashby <Shaun.Ashby@cern.ch>
6     # Update: 2004-02-09 20:14:55+0100
7 sashby 1.5.4.3 # Revision: $Id: ToolParser.pm,v 1.5.4.2 2007/01/24 13:24:24 sashby Exp $
8 sashby 1.2 #
9     # Copyright: 2004 (C) Shaun Ashby
10     #
11     #--------------------------------------------------------------------
12     package BuildSystem::ToolParser;
13     require 5.004;
14    
15     use Exporter;
16     use ActiveDoc::SimpleDoc;
17     use Utilities::Verbose;
18 sashby 1.5.4.3 #use BuildSystem::ToolTagUtils;
19 sashby 1.2
20     @ISA=qw(Exporter Utilities::Verbose);
21     @EXPORT=qw();
22    
23     #
24     sub new
25     ###############################################################
26     # new #
27     ###############################################################
28     # modified : Thu Nov 13 10:42:08 2003 / SFA #
29     # params : #
30     # : #
31     # function : #
32     # : #
33     ###############################################################
34     {
35     my $proto=shift;
36     my $class=ref($proto) || $proto;
37 sashby 1.5.4.1 $self={};
38 sashby 1.2
39     bless $self,$class;
40    
41     $self->{cache}=shift;
42     $self->{mydoctype}="BuildSystem::ToolParser";
43     $self->{mydocversion}="1.1";
44     $self->{interactive} = 0;
45     $self->{content} = {};
46     $self->{nested} = 0;
47 sashby 1.5.4.1 $self->{scramdoc}=ActiveDoc::SimpleDoc->new();
48     $self->{scramdoc}->newparse("setup", $self->{mydoctype},'Subs');
49    
50     return $self;
51     }
52    
53     ### Tag handler methods ###
54     sub tool()
55     {
56     my ($object,$name,%attributes)=@_;
57     my $hashref = \%attributes;
58     # A way to distinguish the naming of different nested levels:
59     $self->{levels}=['','tag','nexttag'];
60     $$hashref{'name'} =~ tr[A-Z][a-z];
61    
62     # Make sure we only pick up the tool requested:
63     if ( ($self->{tool} eq $$hashref{'name'}) &&
64     ($self->{version} eq $$hashref{'version'} ))
65     {
66     # These variables will be used when expanding settings
67     # in tool variable defs:
68     $ENV{SCRAMToolname} = $$hashref{'name'};
69     $ENV{SCRAMToolversion} = $$hashref{'version'};
70     $self->{content}->{TOOLNAME}=$$hashref{'name'};
71     $self->{content}->{TOOLVERSION}=$$hashref{'version'};
72     }
73     else
74     {
75     print "\n";
76     $::scram->scramerror("Configuration problem! Wanted/actual ".$self->{tool}." tool versions differ (wanted = ".$self->{version}.", downloaded = ".$$hashref{'version'}.")\n");
77     }
78 sashby 1.2
79 sashby 1.5.4.1 # Test to see if this doc defines a
80     # scram-managed project or a compiler:
81     if (exists ($$hashref{'type'}))
82     {
83     $$hashref{'type'} =~ tr[A-Z][a-z];
84     $self->{content}->{SCRAM_PROJECT} = 0;
85    
86     if ($$hashref{'type'} eq 'scram')
87     {
88     $self->{content}->{SCRAM_PROJECT} = 1;
89     }
90     elsif ($$hashref{'type'} eq 'compiler')
91     {
92     # Is tool a compiler? Store this for retrieval from tool manager obj:
93     $self->{content}->{SCRAM_COMPILER} = 1;
94     }
95     else
96     {
97     $::scram->scramwarn("Unknown type \"".$$hashref{'type'}."\" in tool ".$$hashref{'name'}."\n");
98     }
99     }
100     }
101    
102     sub tool_()
103     {
104     delete $self->{levels};
105     delete $self->{id};
106     delete $self->{nested};
107     }
108    
109     sub lib()
110     {
111     my ($object,$name,%attributes)=@_;
112     push(@{$self->{"$self->{levels}->[$self->{nested}]".content}->{LIB}},$attributes{'name'});
113     }
114    
115     sub info()
116     {
117     my ($object,$name,%attributes)=@_;
118     $self->{"$self->{levels}->[$self->{nested}]".content}->{INFO} = \%attributes;
119     }
120    
121     sub use()
122     {
123     my ($object,$name,%attributes)=@_;
124     push(@{$self->{"$self->{levels}->[$self->{nested}]".content}->{USE}},$attributes{'name'});
125     }
126    
127     sub runtime()
128     {
129     my ($object,$name,%attributes)=@_;
130     my $hashref = \%attributes;
131     my $envname;
132     # Break the value/default value into its constituent parts:
133     foreach my $t (qw(value default))
134     {
135     if (exists ($$hashref{$t}))
136     {
137     $hashref->{ELEMENTS} = [];
138     map
139     {
140     # In some cases, we might set a runtime path (e.g. LD_LIBRARY_PATH) to
141     # a proper path value i.e. X:Y. In this case, don't bother adding the string
142     # as a "variable" to ELEMENTS:
143     if ($_ =~ m|\$(.*)?| && $_ !~ /:/)
144     {
145     push(@{$hashref->{ELEMENTS}},$1);
146     }
147     } split("/",$hashref->{$t});
148     }
149     }
150 sashby 1.2
151 sashby 1.5.4.1 # Check to see if we have a "type" arg. If so, we use this to create the key:
152     if (exists ($hashref->{'type'}))
153     {
154     my $type=$hashref->{'type'};
155     # Make the type uppercase:
156     $type =~ tr/[a-z]/[A-Z]/;
157     # Rename the environment as "<type>:<env name>":
158     $envname = $type.":".$$hashref{'name'};
159     }
160     else
161     {
162     $envname = $$hashref{'name'};
163     }
164    
165     # Delete name entry so hash is more tidy
166     delete $$hashref{'name'};
167    
168     # Before we save $hashref we need to know if there are already
169     # any runtime tags with the same name. If there are, we must save all
170     # data to an aray of hashes:
171     if (exists ($self->{"$self->{levels}->[$self->{nested}]".content}->{RUNTIME}->{$envname}))
172     {
173     push(@{$self->{"$self->{levels}->[$self->{nested}]".content}->{RUNTIME}->{$envname}},$hashref);
174     }
175     else
176     {
177     # No entry yet so just store the hashref:
178     $self->{"$self->{levels}->[$self->{nested}]".content}->{RUNTIME}->{$envname} = [ $hashref ];
179     }
180 sashby 1.2 }
181    
182 sashby 1.5.4.1 sub flags()
183 sashby 1.2 {
184 sashby 1.5.4.1 my ($object,$name,%attributes)=@_;
185     # Extract the flag name and its value:
186     my ($flagname,$flagvaluestring) = each %attributes;
187     $flagname =~ tr/[a-z]/[A-Z]/; # Keep flag name uppercase
188     chomp($flagvaluestring);
189     # Split the value on whitespace so we can push all
190     # individual flags into an array:
191     my @flagvalues = split(' ',$flagvaluestring);
192    
193     # Is current tag within another tag block?
194     if ($self->{nested} > 0)
195     {
196     # Check to see if the current flag name is already stored in the hash. If so,
197     # just add the new values to the array of flag values:
198     if (exists ($self->{"$self->{levels}->[$self->{nested}]".content}->{FLAGS}->{$flagname}))
199     {
200     push(@{$self->{"$self->{levels}->[$self->{nested}]".content}->{FLAGS}->{$flagname}},@flagvalues);
201     }
202     else
203     {
204     $self->{"$self->{levels}->[$self->{nested}]".content}->{FLAGS}->{$flagname} = [ @flagvalues ];
205     }
206     }
207     else
208     {
209     if (exists ($self->{content}->{FLAGS}->{$flagname}))
210     {
211     push(@{$self->{content}->{FLAGS}->{$flagname}},@flagvalues);
212     }
213     else
214     {
215     $self->{content}->{FLAGS}->{$flagname} = [ @flagvalues ];
216     }
217     }
218     }
219    
220     sub client()
221     {
222     $self->pushlevel();
223     }
224    
225     sub client_()
226     {
227     if ($self->{isarch} == 1)
228     {
229     # If we already have an architecture tag, we must write to tagcontent hash:
230     $self->{tagcontent}->{CLIENT}=$self->{nexttagcontent};
231     delete $self->{nexttagcontent};
232     }
233     else
234     {
235     $self->{content}->{CLIENT}=$self->{tagcontent};
236     }
237 sashby 1.2
238 sashby 1.5.4.1 $self->poplevel();
239     }
240    
241     sub environment()
242     {
243     my ($object,$name,%attributes)=@_;
244     my $hashref = \%attributes;
245     # Save a copy of the name of this environment:
246     my $envname=$$hashref{'name'};
247     delete $$hashref{'name'}; # Delete name entry so hash is more tidy
248     # Break the value/default value into its constituent parts:
249     foreach my $t (qw(value default))
250     {
251     if (exists ($$hashref{$t}))
252     {
253     $hashref->{ELEMENTS} = [];
254     map
255     {
256     if ($_ =~ m|\$(.*)?|)
257     {
258     push(@{$hashref->{ELEMENTS}},$1);
259     }
260     } split("/",$hashref->{$t});
261     }
262     }
263 sashby 1.2
264 sashby 1.5.4.1 # Before we save $hashref we need to know if there are already
265     # any env tags with the same name. If there are, we must save all
266     # data to an aray of hashes:
267     if (exists ($self->{"$self->{levels}->[$self->{nested}]".content}->{ENVIRONMENT}->{$envname}))
268     {
269     push(@{$self->{"$self->{levels}->[$self->{nested}]".content}->{ENVIRONMENT}->{$envname}},$hashref);
270     }
271     else
272     {
273     # No entry yet so just store the hashref:
274     $self->{"$self->{levels}->[$self->{nested}]".content}->{ENVIRONMENT}->{$envname} = [ $hashref ];
275     }
276 sashby 1.2 }
277    
278 sashby 1.5.4.1 sub makefile()
279     {
280     my ($object,$name,%attributes)=@_;
281     # Set our own Char handler so we can collect the content
282     # of the Makefile tag:
283     $object->setHandlers(Char => \&makefile_content);
284     $self->{makefilecontent} = [];
285     }
286    
287     sub makefile_content()
288     {
289     my ($object, @strings) = @_;
290     push(@{$self->{makefilecontent}},@strings);
291     }
292    
293     sub makefile_()
294     {
295     my ($object,$name)=@_;
296     push(@{$self->{"$self->{levels}->[$self->{nested}]".content}->{MAKEFILE}},
297     join('',@{$self->{makefilecontent}}));
298     delete $self->{makefilecontent};
299     # Unset the Char handler to revert to the default behaviour:
300     $object->setHandlers(Char => 0);
301     }
302    
303     sub architecture()
304     {
305     my ($object,$name,%attributes)=@_;
306     $self->pushlevel(\%attributes,1); # Set nested to 1;
307     }
308    
309     sub architecture_()
310     {
311     # Need to be able to cope with multiple arch blocks with same arch string:
312     if (exists ($self->{content}->{ARCH}->{$self->{id}->{'name'}}))
313     {
314     # Already have an architecture tag for this arch:
315     while (my ($k,$v) = each %{$self->{tagcontent}})
316     {
317     # If this tag (e.g. LIB, USE, MAKEFILE) already exists and (as we know
318     # it should be) its data is an ARRAY, push it to the store:
319     if (exists ($self->{content}->{ARCH}->{$self->{id}->{'name'}}->{$k}) &&
320     ref($v) eq 'ARRAY')
321     {
322     push(@{$self->{content}->{ARCH}->{$self->{id}->{'name'}}->{$k}},@$v);
323     }
324     else
325     {
326     # Otherwise (for HASH data) we just store it. Note that, because we do
327     # not loop over the HASH content and check for already existsing keys,
328     # if two arch blocks with same arch name define the same tag (e.g, ENV),
329     # the last occurrence will be kept (i.e. the two values won't be added
330 sashby 1.5.4.2 # to one ENV hash: //FIXME for later....)
331 sashby 1.5.4.1 $self->{content}->{ARCH}->{$self->{id}->{'name'}}->{$k} = $v;
332     }
333     }
334     }
335     else
336     {
337     $self->{content}->{ARCH}->{$self->{id}->{'name'}}=$self->{tagcontent};
338     }
339    
340     delete $self->{isarch};
341     $self->poplevel();
342     }
343    
344 sashby 1.2 sub parse
345     {
346     my $self=shift;
347 sashby 1.5.4.1 my ($tool,$toolver,$file)=@_;
348 sashby 1.2 $self->{tool}=$tool;
349     $self->{version}=$toolver;
350 sashby 1.5.4.1 $self->{scramdoc}->filetoparse($file);
351 sashby 1.2 $self->verbose("Setup Parse");
352 sashby 1.5.4.1 $self->{scramdoc}->parse("setup");
353     delete $self->{scramdoc};
354 sashby 1.2 return $self;
355     }
356    
357     sub pushlevel
358     {
359     my $self = shift;
360     my ($info, $nextlevel)=@_;
361    
362     $self->{id} = $info if (defined $info);
363    
364     # Check to see if last tag was arch: if so, ceate new level:
365     if ($self->{isarch} == 1)
366     {
367     $self->{nested} = 2;
368     $self->{nexttagcontent}={};
369     }
370     else
371     {
372     $self->{nested} = 1;
373     $self->{tagcontent}={};
374     }
375    
376     # Set something which says "last starter tag was ARCH":
377     if ($nextlevel)
378     {
379     $self->{isarch} = 1;
380     }
381     }
382    
383     sub poplevel
384     {
385     my $self = shift;
386    
387     # Drop level of nesting by one:
388     $self->{nested}--;
389    
390     if ($self->{isarch} != 1)
391     {
392     delete $self->{tagcontent};
393     }
394     }
395    
396     sub rmenvdata
397     {
398     my $self=shift;
399     delete $self->{ENVDATA};
400     }
401    
402     ###################################
403     ## Data Access Methods ##
404     ###################################
405     sub toolname
406     {
407     my $self=shift;
408     # Return tool name:
409     return ($self->{content}->{TOOLNAME});
410     }
411    
412     sub toolversion
413     {
414     my $self=shift;
415     # Return tool version:
416     return ($self->{content}->{TOOLVERSION});
417     }
418    
419     sub toolcontent
420     {
421     my $self=shift;
422     # Return whole of content hash:
423     return $self->{content};
424     }
425    
426     sub getrawdata()
427     {
428     my $self=shift;
429     my ($tagtype)=@_;
430    
431     # Check to see if we have data for this tag:
432     if (! exists ($self->{content}->{$tagtype}))
433     {
434     # If not, return:
435     return 0;
436     }
437    
438     # Check the number of keys for hash referred to by this object.
439     # If 0, return:
440     if (ref($self->{content}->{$tagtype}) eq 'HASH') #
441     {
442     if ((my $nkeys=keys %{$self->{content}->{$tagtype}}) > 0)
443     {
444     # Return the data for the tag $tagtype. ARCH is a bit special because
445     # we want the data for the actual arch (thus, data is on a different level):
446     if ($tagtype eq 'ARCH')
447     {
448     my $archmatch = {};
449     # Check for matching arch key and return hash of relevant data.
450     # Also must take into account the fact that several arch names may match, e.g. Linux, Linux__2 and
451     # Linux__2.4 all match. If we find more than one match, collect ALL matching data and return it:
452     while (my ($k,$v) = each %{$self->{content}->{ARCH}})
453     {
454     # For every matching architecture we snatch the data and squirrel it away:
455     if ( $ENV{SCRAM_ARCH} =~ /$k.*/ )
456     {
457     # Now we check the tags inside the arch block. Note that we do not want to descend
458     # into CLIENT tags, if these exist. We just want to return all data in the ARCH
459     # block while making sure that multiple matches are handled correctly. We assume that
460     # you will only find one CLIENT block inside and ARCH:
461     while (my ($matchtag, $matchval) = each %{$v})
462     {
463     if ($matchtag =~ /CLIENT|FLAGS|RUNTIME/)
464     {
465     $archmatch->{$matchtag} = $matchval;
466     }
467     else
468     {
469     # Treat tags differently according to whether they are HASHes or ARRAYs:
470     if (ref($matchval) =~ /HASH/)
471     {
472     while (my ($t, $val) = each %{$matchval})
473     {
474     if (exists ($archmatch->{$matchtag}->{$t}))
475     {
476     push(@{$archmatch->{$matchtag}->{$t}},@$val);
477     }
478     else
479     {
480     $archmatch->{$matchtag}->{$t} = $val;
481     }
482     }
483     }
484     else # Here we deal with arrays:
485     {
486     if (exists ($archmatch->{$matchtag}))
487     {
488     push(@{$archmatch->{$matchtag}},@$matchval);
489     }
490     else
491     {
492     $archmatch->{$matchtag} = $matchval;
493     }
494     }
495     }
496     }
497     }
498     }
499     # Return the squirrel:
500     return $archmatch;
501    
502     } # End of ARCH tag treatment
503     else
504     {
505     # Return other tag data:
506     return $self->{content}->{$tagtype};
507     }
508     }
509     else
510     {
511     print "Warning: $tagtype tags contain no other tag data!","\n";
512     return undef;
513     }
514     }
515     else
516     {
517     # We have an array of data or a scalar:
518     return $self->{content}->{$tagtype};
519     }
520     }
521    
522     sub processrawtool()
523     {
524     my $self=shift;
525     my ($interactive) = @_;
526     my $data = [];
527     my $environments = {}; # Somewhere to collect our environments
528    
529     # Set interactive mode if required:
530     $self->{interactive} = $interactive;
531    
532     # Somewhere to store the data:
533     use BuildSystem::ToolData;
534     my $tooldataobj = BuildSystem::ToolData->new();
535    
536     # Set the name and version:
537     $tooldataobj->toolname($self->toolname());
538     $tooldataobj->toolversion($self->toolversion());
539    
540     # First, collect all tag data so that we only have non-nested tags.
541     # Check for architecture-dependent data first, followed by client tags:
542     foreach $nested_tag (qw( ARCH CLIENT ))
543     {
544     if (my $thisdata=$self->getrawdata($nested_tag))
545     {
546     foreach my $item (keys %{ $thisdata })
547     {
548     if ($item eq 'CLIENT')
549     {
550     my $clientdata = $thisdata->{$item};
551     foreach my $ckey (keys %{$clientdata})
552     {
553     $environments->{$ckey} = $clientdata->{$ckey};
554     }
555     }
556     elsif ($item eq 'ENVIRONMENT' || $item eq 'RUNTIME')
557     {
558     # Check to see if tag already exists before saving:
559     if (exists($environments->{$item}))
560     {
561     foreach my $ek (keys %{$thisdata})
562     {
563     if (exists($environments->{$item}->{$ek}))
564     {
565     push(@{$environments->{$item}->{$ek}}, @{$thisdata->{$item}->{$ek}});
566     }
567     else
568     {
569     $environments->{$item}->{$ek} = $thisdata->{$item}->{$ek};
570     }
571     }
572     }
573     else
574     {
575     # There isn't an entry yet:
576     $environments->{$item} = $thisdata->{$item};
577     }
578     }
579     else
580     {
581     my $data = $thisdata->{$item};
582    
583     if (ref($data) eq 'HASH')
584     {
585     while (my ($f,$v) = each %$data)
586     {
587     $tooldataobj->flags($f,$v);
588     }
589     }
590     else
591     {
592     my $subname = lc($item);
593     $tooldataobj->$subname($data), if ($#$data != -1);
594     }
595     }
596     }
597     }
598     else
599     {
600     # No entry for this nested tag. Proceed.
601     next;
602     }
603     }
604    
605     # Now handle all other normal tags:
606     foreach my $normal_tag (qw( ENVIRONMENT RUNTIME ))
607     {
608     # Do we have some data for this tag?
609     if (my $thisdata=$self->getrawdata($normal_tag))
610     {
611     # Add the data to our environments hash. We must check to see if
612     # there is an entry already:
613     if (exists($environments->{$normal_tag}))
614     {
615     foreach my $ek (keys %{$thisdata})
616     {
617     if (exists($environments->{$normal_tag}->{$ek}))
618     {
619     push(@{$environments->{$normal_tag}->{$ek}}, @{$thisdata->{$normal_tag}->{$ek}});
620     }
621     else
622     {
623     $environments->{$normal_tag}->{$ek} = $thisdata->{$normal_tag}->{$ek};
624     }
625     }
626     }
627     else
628     {
629     # There isn't an entry yet:
630     $environments->{$normal_tag} = $thisdata;
631     }
632     }
633     else
634     {
635     # No data so proceed:
636     next;
637     }
638     }
639    
640     # Finally, tags that can be stored straight away:
641     foreach my $tag (qw( FLAGS MAKEFILE ))
642     {
643     my $bdata = $self->getrawdata($tag);
644     if (ref($bdata) eq 'HASH')
645     {
646     while (my ($f,$v) = each %$bdata)
647     {
648     $tooldataobj->flags($f,$v);
649     }
650     }
651     else
652     {
653     $tooldataobj->makefile($bdata), if ($#$bdata != -1);
654     }
655     }
656    
657     # Libs and tool dependencise:
658     foreach my $tag (qw( LIB USE ))
659     {
660     my $bdata = $self->getrawdata($tag);
661     my $subname = lc($tag);
662     $tooldataobj->$subname($bdata), if ($#$bdata != -1);
663     }
664    
665     # Also check to see if this tool is a scram-managed project. If
666     # so, set the SCRAM_PROJECT variable in the ToolData object:
667     if (exists ($self->{content}->{SCRAM_PROJECT}))
668     {
669     $tooldataobj->scram_project($self->{content}->{SCRAM_PROJECT});
670     }
671 sashby 1.4
672     # And check to see if this tool is a compiler. If so, set
673     # the SCRAM_COMPILER variable in the ToolData object:
674     if (exists ($self->{content}->{SCRAM_COMPILER}))
675     {
676     $tooldataobj->scram_compiler($self->{content}->{SCRAM_COMPILER});
677     }
678 sashby 1.2
679     # Establish the order of parsing the value strings:
680     my $order = $self->process_environments($environments);
681    
682     if ($self->{interactive})
683     {
684     # Set the values interactively:
685     $self->interactively_find_settings($tooldataobj, $environments, $order);
686     }
687     else
688     {
689     # Set the values:
690     $self->find_settings($tooldataobj, $environments, $order);
691     }
692    
693     # Return a ToolData object:
694     return $tooldataobj;
695     }
696    
697     sub process_environments()
698     {
699     my $self=shift;
700     my ($environments)=@_;
701    
702     use BuildSystem::SCRAMGrapher;
703     my $G = BuildSystem::SCRAMGrapher->new();
704    
705     foreach $envtype (keys %{$environments})
706     {
707     while (my ($envcontent,$envdata) = each %{$environments->{$envtype}})
708     {
709     # Add a vertex for the VARIABLE name:
710     $G->vertex($envcontent);
711    
712     foreach my $element (@$envdata)
713     {
714     if (exists($element->{'ELEMENTS'}))
715     {
716     map
717     {
718     # Add a path for each element in ELEMENTS:
719     $G->edge($envcontent, $_);
720     } @{$element->{'ELEMENTS'}};
721     }
722     }
723     }
724     }
725    
726     my $setup_order = $G->sort();
727     return $setup_order;
728     }
729    
730     sub find_settings()
731     {
732     my $self=shift;
733     my ($tooldataobj, $environments, $ordering)=@_;
734     my $stringtoeval;
735     my $runtime=[];
736     my $path;
737    
738     use BuildSystem::ToolSettingValidator;
739    
740     my $tsv = BuildSystem::ToolSettingValidator->new($environments, $self->toolname());
741    
742     foreach my $envname (@$ordering)
743     {
744     my $type = 'ENVIRONMENT';
745     my $envdata = $tsv->environment($type, $envname);
746    
747     # Handle single-occurrence variables first (i.e. VAR appears once
748     # in array of hashes):
749     if ($envdata != 0 && $#$envdata == 0) # One element only!
750     {
751     print "\nFinding a value for $envname:","\n";
752     print "\n";
753     # We have an environment and only one data element:
754     # Check the lookup DB:
755     if ($tsv->checkDB($envname))
756     {
757     print "\tValidating value for $envname (found in tool DB):","\n";
758     if ($tsv->validatepath())
759     {
760     # Save in TSV and store in ToolData object:
761     $tsv->savevalue($envname,$tsv->pathfromdb());
762     $self->store($tooldataobj, $envname, $tsv->pathfromdb());
763     }
764     else
765     {
766     $path = $tsv->findvalue($envname, $envdata);
767     # Save the value in ToolData object:
768     $self->store($tooldataobj, $envname, $path);
769     }
770     }
771     else
772     {
773     $path = $tsv->findvalue($envname, $envdata);
774     # Save in ToolData object:
775     $self->store($tooldataobj, $envname, $path);
776     }
777     }
778     elsif ($envdata != 0 && $#$envdata > 0)
779     {
780     print "\nFinding a value for $envname:","\n";
781     print "\n";
782     foreach my $elementdata (@$envdata)
783     {
784     $path = $tsv->findvalue($envname, $elementdata);
785     # Save in ToolData object:
786     $self->store($tooldataobj, $envname, $path);
787     }
788     }
789     elsif (exists($ENV{$envname}))
790     {
791     # Nothing to do here:
792 sashby 1.5 push(@$runtime, $envname); # FIX From Shahzad.
793 sashby 1.2 next;
794     }
795     else
796     {
797     push(@$runtime, $envname);
798     }
799     }
800    
801     # Check that the required libraries exist:
802     $self->_lib_validate($tooldataobj);
803    
804     # Now process the runtime settings:
805     print "\n";
806     print "-------------------------------\n";
807    
808     foreach my $rtname (@$runtime)
809     {
810     my $type = 'RUNTIME';
811     my $envdata = $tsv->environment($type, $rtname);
812     my ($rttype,$realrtname) = split(':',$rtname);
813 sashby 1.3
814 sashby 1.2 # Only validate paths:
815     if ($rtname =~ /:/)
816     {
817     # Handle single-occurrence variables first (i.e. VAR appears once
818     # in array of hashes):
819     if ($envdata != 0 && $#$envdata == 0) # One element only!
820     {
821     print "\nRuntime path settings for $realrtname:","\n";
822     print "\n";
823     # We have an environment and only one data element:
824     # Check the lookup DB:
825     if ($tsv->checkDB($rtname))
826     {
827     print "\tValidating value for path $realrtname (found in tool DB):","\n";
828     if ($tsv->validatepath())
829     {
830     # Save in TSV and store in ToolData object:
831     $tsv->savevalue($rtname, $tsv->pathfromdb());
832     $tooldataobj->runtime($rtname, [ $tsv->pathfromdb() ]);
833     }
834     else
835     {
836     $path = $tsv->findvalue($rtname, $envdata);
837     # Save the value in ToolData object:
838     $tooldataobj->runtime($rtname, [ $path ]);
839     }
840     }
841     else
842     {
843     $path = $tsv->findvalue($rtname, $envdata);
844     # Save in ToolData object:
845     $tooldataobj->runtime($rtname, [ $path ]);
846     }
847     }
848     elsif ($envdata != 0 && $#$envdata > 0)
849     {
850     print "\nRuntime path settings for $realrtname:","\n";
851     print "\n";
852     foreach my $elementdata (@$envdata)
853     {
854     $path = $tsv->findvalue($rtname, $elementdata);
855     # Save in ToolData object:
856     $tooldataobj->runtime($rtname, [ $path ]);
857     }
858     }
859     else
860     {
861     next;
862     }
863     }
864     else
865     {
866     # Handle runtime variables:
867     if ($envdata != 0 && $#$envdata == 0) # One element only!
868     {
869     my $value='';
870     $tsv->checkdefaults($envdata, \$value);
871     print "\n";
872    
873     # Chck to see if the value contains a variable that should be evaluated:
874     if ($value =~ /$/)
875     {
876     # If so, find the value and substitute. This should work for all
877     # occurrences of variables because by this point (and because the ordering
878     # was established at the start) all other variables will have real values:
879     my $dvalue = $tsv->_expandvars($value);
880     $value = $dvalue;
881     }
882    
883     print "Runtime variable ",$rtname," set to \"",$value,"\"\n";
884    
885     # Store the variable setting:
886     $tooldataobj->runtime($rtname, [ $value ]);
887     }
888     else
889     {
890     next;
891     }
892     }
893     }
894    
895     print "\n";
896     }
897    
898     sub interactively_find_settings()
899     {
900     my $self=shift;
901     my ($tooldataobj, $environments, $ordering)=@_;
902     my $stringtoeval;
903     my $runtime=[];
904     my ($path, $dpath);
905    
906     use BuildSystem::ToolSettingValidator;
907    
908     my $tsv = BuildSystem::ToolSettingValidator->new($environments, $self->toolname(), $self->{interactive});
909    
910     foreach my $envname (@$ordering)
911     {
912     my $type = 'ENVIRONMENT';
913     my $envdata = $tsv->environment($type, $envname);
914    
915     # Handle single-occurrence variables first (i.e. VAR appears once
916     # in array of hashes):
917     if ($envdata != 0 && $#$envdata == 0) # One element only!
918     {
919     print "\nFinding a value for $envname:","\n";
920     print "\n";
921     # We have an environment and only one data element:
922     # Check the lookup DB:
923     if ($tsv->checkDB($envname))
924     {
925     print "\tValidating value for $envname (found in tool DB):","\n";
926     if ($tsv->validatepath())
927     {
928     # This is our default:
929     $dpath = $tsv->pathfromdb();
930     # Run promptuser() to see if this value can be kept
931     # or should be changed:
932     $path = $tsv->promptuser($envname, $dpath);
933     # Save in TSV and store in ToolData object:
934     $tsv->savevalue($envname,$path);
935     $self->store($tooldataobj, $envname, $path);
936     }
937     else
938     {
939     $path = $tsv->ifindvalue($envname, $envdata);
940     # Save the value in ToolData object:
941     $self->store($tooldataobj, $envname, $path);
942     }
943     }
944     else
945     {
946     $dpath = $tsv->ifindvalue($envname, $envdata);
947     # Save in ToolData object:
948     $self->store($tooldataobj, $envname, $dpath);
949     }
950     }
951     elsif ($envdata != 0 && $#$envdata > 0)
952     {
953     print "\nFinding a value for $envname:","\n";
954     print "\n";
955     foreach my $elementdata (@$envdata)
956     {
957     $path = $tsv->ifindvalue($envname, $elementdata);
958     # Save in ToolData object:
959     $self->store($tooldataobj, $envname, $path);
960     }
961     }
962     elsif (exists($ENV{$envname}))
963     {
964     # Nothing to do here:
965     next;
966     }
967     else
968     {
969     push(@$runtime, $envname);
970     }
971     }
972    
973     # Check that the required libraries exist:
974     $self->_lib_validate($tooldataobj);
975    
976     # Now process the runtime settings:
977     print "\n";
978     print "-------------------------------\n";
979     foreach my $rtname (@$runtime)
980     {
981     my $type = 'RUNTIME';
982     my $envdata = $tsv->environment($type, $rtname);
983     my ($rttype,$realrtname) = split(':',$rtname);
984    
985     # Only validate paths:
986     if ($rtname =~ /:/)
987     {
988     # Handle single-occurrence variables first (i.e. VAR appears once
989     # in array of hashes):
990     if ($envdata != 0 && $#$envdata == 0) # One element only!
991     {
992     print "\nRuntime path settings for $realrtname:","\n";
993     print "\n";
994     # We have an environment and only one data element:
995     # Check the lookup DB:
996     if ($tsv->checkDB($rtname))
997     {
998     print "\tValidating value for path $realrtname (found in tool DB):","\n";
999     if ($tsv->validatepath())
1000     {
1001     $dpath = $tsv->pathfromdb();
1002     # Run promptuser() to see if this value can be kept
1003     # or should be changed:
1004     $path = $tsv->promptuser($rtname, $dpath);
1005     # Save in TSV and store in ToolData object:
1006     $tsv->savevalue($rtname, $path);
1007     $tooldataobj->runtime($rtname, [ $path ]);
1008     }
1009     else
1010     {
1011     $dpath = $tsv->ifindvalue($rtname, $envdata);
1012     # Save the value in ToolData object:
1013     $tooldataobj->runtime($rtname, [ $path ]);
1014     }
1015     }
1016     else
1017     {
1018     $path = $tsv->ifindvalue($rtname, $envdata);
1019     # Save in ToolData object:
1020     $tooldataobj->runtime($rtname, [ $path ]);
1021     }
1022     }
1023     elsif ($envdata != 0 && $#$envdata > 0)
1024     {
1025     print "\nRuntime path settings for $realrtname:","\n";
1026     print "\n";
1027     foreach my $elementdata (@$envdata)
1028     {
1029     $path = $tsv->ifindvalue($rtname, $elementdata);
1030     # Save in ToolData object:
1031     $tooldataobj->runtime($rtname, [ $path ]);
1032     }
1033     }
1034     else
1035     {
1036     next;
1037     }
1038     }
1039     else
1040     {
1041     # Handle runtime variables:
1042     if ($envdata != 0 && $#$envdata == 0) # One element only!
1043     {
1044     my $dvalue='';
1045     $tsv->checkdefaults($envdata, \$dvalue);
1046     print "\n";
1047     my $value = $tsv->promptuserforvar($rtname, $dvalue);
1048     # Store the variable setting:
1049     $tooldataobj->runtime($rtname, [ $value ]);
1050     }
1051     else
1052     {
1053     next;
1054     }
1055     }
1056     }
1057    
1058     print "\n";
1059     }
1060    
1061     sub store()
1062     {
1063     my $self=shift;
1064     my ($tooldataobj, $envname, $path) = @_;
1065     my $subrtn = lc($envname);
1066    
1067     if ($tooldataobj->can($subrtn))
1068     {
1069     $tooldataobj->$subrtn([ $path ]);
1070     }
1071     else
1072     {
1073     $tooldataobj->variable_data($envname, $path);
1074     }
1075     }
1076    
1077     sub _lib_validate()
1078     {
1079     my $self=shift;
1080     my ($toolobj)=@_;
1081     my $errorstatus = { 0 => $main::good."[OK]".$main::normal, 1 => $main::error."[ERROR]".$main::normal };
1082     my $libsfound={};
1083    
1084     # Firstly, we check to see if there are libraries provided by this tool:
1085     my @libraries = $toolobj->lib();
1086     my @libpaths = $toolobj->libdir();
1087    
1088     foreach my $ldir (@libpaths)
1089     {
1090     my $full_libname_glob="lib".$lib."*.*";
1091     # Change to lib dir so we avoid the very long paths in our glob:
1092     chdir($ldir);
1093     # Next we use a glob to get libs matching this string (so we
1094     # can see if there's a shared or archive lib):
1095     my @possible_libs = glob($full_libname_glob);
1096     #
1097     map
1098     {
1099     $_ =~ s/\.so*|\.a*//g; # Remove all endings
1100     # Store in our hash of found libs:
1101     $libsfound->{$_} = 1;
1102     } @possible_libs;
1103     }
1104    
1105     # Next we iterate over the list of libraries in our tool and
1106     # see if it was found in one of the libdirs:
1107     print "\n\n", if ($#libraries != -1);
1108     foreach my $library (@libraries)
1109     {
1110     # Good status:
1111     my $errorid = 0;
1112     if (! exists ($libsfound->{'lib'.$library}))
1113     {
1114     # Check in system library dirs:
1115     if ($self->_check_system_libs($library))
1116     {
1117     $errorid = 0;
1118     }
1119     else
1120     {
1121     $errorid = 1;
1122     }
1123     }
1124     printf("* Library check %-10s for lib%-12s\n",$errorstatus->{$errorid}, $library);
1125     }
1126    
1127     print "\n";
1128     }
1129    
1130     sub _check_system_libs()
1131     {
1132     my $self=shift;
1133     my ($lib)=@_;
1134     my $libsfound = {};
1135     my $systemdirs = [ qw( /lib /usr/lib /usr/local/lib /usr/X11R6/lib ) ];
1136     my $full_libname_glob="lib".$lib."*.*";
1137     my $found = 0;
1138    
1139     foreach my $dir (@$systemdirs)
1140     {
1141     # Change to lib dir so we avoid the very long paths in our glob:
1142     chdir($dir);
1143     # Next we use a glob to get libs matching this string (so we
1144     # can see if there's a shared or archive lib):
1145     my @possible_libs = glob($full_libname_glob);
1146     #
1147     map
1148     {
1149     $_ =~ s/\.so*|\.a*//g; # Remove all endings
1150     # Store in our hash of found libs:
1151     $libsfound->{$_} = 1;
1152     } @possible_libs;
1153     }
1154    
1155     # See if we find the library in the system lib directories:
1156     if (! exists ($libsfound->{'lib'.$library}))
1157     {
1158     $found = 1;
1159     }
1160    
1161     return $found;
1162     }
1163    
1164 sashby 1.5.4.1 sub AUTOLOAD()
1165     {
1166     my ($xmlparser,$name,%attributes)=@_;
1167     return if $AUTOLOAD =~ /::DESTROY$/;
1168     my $name=$AUTOLOAD;
1169     $name =~ s/.*://;
1170     }
1171    
1172 sashby 1.2 1;