ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/BuildSystem/ToolManager.pm
Revision: 1.13
Committed: Fri Oct 7 16:05:44 2005 UTC (19 years, 7 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_0_2, V1_0_2_p1
Branch point for: v103_branch
Changes since 1.12: +25 -10 lines
Log Message:
Some more bugfixes.

File Contents

# Content
1 #____________________________________________________________________
2 # File: ToolManager.pm
3 #____________________________________________________________________
4 #
5 # Author: Shaun Ashby <Shaun.Ashby@cern.ch>
6 # Update: 2003-11-12 15:04:16+0100
7 # Revision: $Id: ToolManager.pm,v 1.12 2005/07/20 13:33:48 sashby Exp $
8 #
9 # Copyright: 2003 (C) Shaun Ashby
10 #
11 #--------------------------------------------------------------------
12 package BuildSystem::ToolManager;
13 require 5.004;
14
15 use Exporter;
16 use BuildSystem::ToolCache;
17 use BuildSystem::ToolParser;
18 use Utilities::AddDir;
19 use URL::URLhandler;
20 use Utilities::Verbose;
21
22 @ISA=qw(BuildSystem::ToolCache Utilities::Verbose);
23 @EXPORT_OK=qw( );
24 #
25
26 sub new
27 ###############################################################
28 # new #
29 ###############################################################
30 # modified : Wed Nov 12 10:34:10 2003 / SFA #
31 # params : #
32 # : #
33 # function : #
34 # : #
35 ###############################################################
36 {
37 my $proto=shift;
38 my $class=ref($proto) || $proto;
39 my $self=$class->SUPER::new(); # Inherit from ToolCache
40 my $projectarea=shift;
41
42 bless $self,$class;
43
44 $self->{arch}=shift;
45 $self->{topdir}=$projectarea->location();
46 $self->{configdir}=$self->{topdir}."/".$projectarea->configurationdir();
47 $self->{cache}=$projectarea->cache(); # Download tool cache
48 $self->{toolfiledir}=$self->{topdir}."/.SCRAM/InstalledTools";
49 $self->{datastore}=$self->{topdir}."/.SCRAM";
50 $self->{archstore}=$self->{topdir}."/.SCRAM/".$ENV{SCRAM_ARCH};
51
52 # Make sure our tool download dir exists:
53 AddDir::adddir($self->{toolfiledir});
54 AddDir::adddir($self->{archstore});
55
56 # Set the tool cache file to read/write:
57 $self->name($projectarea->toolcachename());
58
59 # Check for the downloaded tools cache:
60 if (exists($self->{cache}))
61 {
62 $self->{urlhandler}=URL::URLhandler->new($self->{cache});
63 }
64
65 return $self;
66 }
67
68 sub clone()
69 {
70 my $self=shift;
71 my $projectarea=shift;
72
73 # Change cache settings to reflect the new location:
74 $self->{topdir}=$projectarea->location();
75
76 $self->{configdir}=$self->{topdir}."/".$projectarea->configurationdir();
77 $self->{toolfiledir}=$self->{topdir}."/.SCRAM/InstalledTools";
78 $self->{datastore}=$self->{topdir}."/.SCRAM";
79 $self->{archstore}=$self->{topdir}."/.SCRAM/".$ENV{SCRAM_ARCH};
80
81 # Change the cache name:
82 $self->name($projectarea->toolcachename());
83 $self->cloned_tm(1);
84
85 return $self;
86 }
87
88 sub arch_change_after_copy()
89 {
90 my $self=shift;
91 my ($newarch, $cachename)=@_;
92 # Make changes to arch-specific settings when copying tool manager
93 # object to another arch during setup:
94 $self->{arch} = $newarch;
95 $self->{archstore} = $self->{topdir}."/.SCRAM/".$newarch;
96 # Change the name of the cache to reflect new (arch-specific) location:
97 $self->name($cachename);
98 }
99
100 sub interactive()
101 {
102 my $self=shift;
103 # Interactive mode on/off:
104 @_ ? $self->{interactive} = shift
105 : ((defined $self->{interactive}) ? $self->{interactive} : 0);
106 }
107
108 sub setupalltools()
109 {
110 my $self = shift;
111 my ($arealocation,$setupopt) = @_;
112 my (@localtools);
113 my $selected;
114
115 # Get the selected tool list. Handle the case where there might not be
116 # any selected tools: //FIXME: need to handle case where there are no
117 # selected tools (not very often but a possibility):
118 my $sel = $self->selected();
119
120 if (defined ($sel))
121 {
122 $selected = [ keys %{$sel} ];
123 }
124
125 # Setup option "setupopt" directs the setup: 1 is for booting from
126 # scratch, 0 is when just doing "scram setup" (in this case we don't
127 # want to pick up everything from any scram-managed projects):
128 if ($setupopt == 1) # We're booting from scratch
129 {
130 # Check to see if there are any SCRAM-managed projects in our local requirements:
131 my $scramprojects = $::scram->_loadscramdb();
132
133 # Look for a match in the scram db:
134 foreach my $S (@$selected)
135 {
136 if (exists ($scramprojects->{$S}))
137 {
138 # Now check the version required exists in
139 # list of scram projects with this name:
140 while (my ($pdata,$plocation) = each %{$scramprojects->{$S}})
141 {
142 # Split the $pdata string to get the real name and the version:
143 my ($pname,$pversion) = split(":",$pdata);
144 if ($pversion eq $self->defaultversion($S))
145 {
146 # Get the tool manager for the scram project:
147 my $sa=$::scram->scramfunctions()->scramprojectdb()->getarea($pname,$pversion);
148 # Load the tool cache:
149 if ( -r $sa->toolcachename())
150 {
151 use Cache::CacheUtilities;
152 my $satoolmanager=&Cache::CacheUtilities::read($sa->toolcachename());
153 # Copy needed content from toolmanager for scram-managed project:
154 $self->inheritcontent($satoolmanager);
155 }
156 }
157 }
158 # Also add this scram-managed project to list of tools to set up:
159 push(@localtools,$S);
160 }
161 else
162 {
163 # Store other tools in ReqDoc in separate array. We will set up these tools later:
164 push(@localtools,$S);
165 }
166 }
167
168 # Set up extra tools required in this project, in addition to
169 # any scram-managed projects
170 foreach my $localtool (@localtools)
171 {
172 # First check to see if it's already set up (i.e., was contained
173 # in list of requirements for scram project):
174 if (! $self->definedtool($localtool))
175 {
176 $self->toolsetup($arealocation,$localtool,$self->defaultversion($localtool));
177 $self->addtoselected($localtool);
178 }
179 else
180 {
181 print $localtool," already set up.","\n",if ($ENV{SCRAM_DEBUG});
182 }
183 }
184 }
185 else
186 {
187 # Just loop over all tools and setup again:
188 foreach my $localtool (@{$selected})
189 {
190 $self->toolsetup($arealocation,$localtool,$self->defaultversion($localtool));
191 }
192 }
193
194 print "\n";
195 }
196
197 sub coresetup()
198 {
199 my $self=shift;
200 my ($toolname, $toolversion, $toolfile, $force) = @_;
201 my ($toolcheck, $toolparser);
202
203 print "\n";
204 print $::bold."Setting up ",$toolname," version ",$toolversion,": ".$::normal,"\n";
205
206 # New ToolParser object for this tool if there isn't one already.
207 # Look in array of raw tools to see if this tool has a ToolParser object:
208 $toolcheck=0;
209
210 map
211 {
212 if ($_->toolname() eq $toolname) {$toolcheck = 1; $toolparser = $_;}
213 } $self->rawtools();
214
215 # Tool not known so we create a new ToolParser object and parse it:
216 if ($toolcheck != 1 || $force == 1)
217 {
218 $toolparser = BuildSystem::ToolParser->new();
219 # We only want to store the stuff relevant for one particular version:
220 $toolparser->parse($toolname, $toolversion, $toolfile);
221 # Store the ToolParser object in the cache:
222 $self->store($toolparser);
223 print "\nFile $toolfile reparsed (modified)","\n",if ($ENV{SCRAM_DEBUG});
224 }
225
226 # Next, set up the tool:
227 my $store = $toolparser->processrawtool($self->interactive());
228 # Make sure that we have this tool in the list of selected tools (just in case this tool was
229 # set up by hand afterwards):
230 $self->addtoselected($toolname);
231
232 # Check to see if this tool is a compiler. If so, store it.
233 # Also store the language that this compiler supprots, and a
234 # compiler name (e.g. gcc323) which, in conjunction with a stem
235 # architecture name like slc3_ia32_, can be used to build a complete arch string:
236 if ($store->scram_compiler() == 1)
237 {
238 my @supported_language = $store->flags("SCRAM_LANGUAGE_TYPE");
239 my @compilername = $store->flags("SCRAM_COMPILER_NAME");
240 $self->scram_compiler($supported_language[0],$toolname,$compilername[0]);
241 }
242
243 # Store the ToolData object in the cache:
244 $self->storeincache($toolparser->toolname(),$store);
245 return $self;
246 }
247
248 sub toolsetup()
249 {
250 my $self=shift;
251 my ($arealocation, $toolname, $toolversion, $toolurl) = @_;
252 my ($urlcache, $url, $filename, $tfname);
253 my $toolfile;
254 my $force = 0; # we may have to force a reparse of a tool file
255
256 $toolname =~ tr[A-Z][a-z];
257 $toolversion ||= $self->defaultversion($toolname);
258 $urlcache=URL::URLcache->new($arealocation."/.SCRAM/cache"); # Download tool cache
259
260 # Check for the downloaded tools cache:
261 if (defined($urlcache))
262 {
263 $self->{urlhandler}=URL::URLhandler->new($urlcache);
264 }
265
266 $url = $self->toolurls()->{$toolname};
267 $filename = $self->{toolfiledir}."/".$toolname;
268
269 # If .SCRAM/InstalledTools doesn't exist, create it:
270 if (! -d $self->{toolfiledir})
271 {
272 AddDir::adddir($self->{toolfiledir});
273 }
274
275 # First, check to see if there was a tool URL given. If so, we might need to read
276 # from http or from a file: type URL:
277 if (my ($proto, $urlv) = ($toolurl =~ /(.*):(.*)/))
278 {
279 # See what kind of URL (file:, http:, cvs:, svn:, .. ):
280 if ($proto eq 'file')
281 {
282 # Check to see if there is a ~ and substitute the user
283 # home directory if there is (file:~/xyz):
284 if (my ($urlpath) = ($urlv =~ m|^\~/(.*)$|))
285 {
286 $urlv = $ENV{HOME}."/".$urlpath;
287 }
288 elsif (my ($urlpath) = ($urlv =~ m|^\./(.*)$|))
289 {
290 # Relative to current directory (file:./xyz):
291 use Cwd qw(&cwd);
292 $urlv = cwd()."/".$urlpath;
293 }
294
295 # If the tool url is a file and the file exists,
296 # copy it to .SCRAM/InstalledTools and set the
297 # filename accordingly:
298 if ( -f $urlv)
299 {
300 use File::Copy;
301 copy($urlv, $filename);
302 my $mode = 0644; chmod $mode, $filename;
303 $toolfile=$filename;
304 # Here we must account for the fact that the file tool doc may be
305 # a modified version of an existing tool in the current config. we
306 # make sure that this file is reparsed, even if there is already a
307 # ToolParser object for the tool:
308 $force = 1;
309 }
310 else
311 {
312 $::scram->scramerror("Unable to set up $toolname from URL \"$toolurl\" - $urlv does not exist!");
313 }
314 }
315 elsif ($proto eq 'http')
316 {
317 print "SCRAM: downloading $toolname from $toolurl","\n";
318 # Download from WWW first:
319 use LWP::Simple qw(&getstore);
320 my $http_response_val = &getstore($toolurl, $filename);
321
322 # Check the HTTP status. If doc not found, exit:
323 if ($http_response_val != 200)
324 {
325 my ($server,$doc) = ($urlv =~ m|//(.*?)/(.*)|);
326 $::scram->scramerror("Unable to set up $toolname: $doc not found on $server!");
327 }
328 else
329 {
330 $toolfile=$filename;
331 }
332 }
333 elsif ($proto eq 'cvs')
334 {
335 print "SCRAM: downloading $toolname from $urlv using protocol $proto.","\n";
336 print "[ not yet supported ]","\n";
337 exit(0);
338 }
339 elsif ($proto eq 'svn')
340 {
341 print "SCRAM: downloading $toolname from $urlv using protocol $proto.","\n";
342 print "[ not yet supported ]","\n";
343 exit(0);
344 }
345 else
346 {
347 $::scram->scramerror("Unable to download $urlv! Unknown protocol \"$proto\". Bye.");
348 }
349 }
350 else
351 {
352 # Copy the downloaded tool file to InstalledTools directory:
353 if ( ! -f $filename )
354 {
355 # If the URL is empty, the chances are that this tool was not downloaded to .SCRAM/InstalledTools.
356 # We signal an error and exit:
357 if ($url eq '')
358 {
359 $::scram->scramerror("$toolname was selected in project requirements but is not in the configuration!");
360 }
361 else
362 {
363 # Otherwise, we try to download it:
364 $self->verbose("Attempting Download of $url");
365 # Get file from download cache:
366 ($url,$filename)=$self->{urlhandler}->get($url);
367 use File::Copy;
368 $tfname=$self->{toolfiledir}."/".$toolname;
369 copy($filename, $tfname);
370 my $mode = 0644; chmod $mode, $tfname;
371 $toolfile=$tfname;
372 }
373 }
374 else
375 {
376 # File already exists in the .SCRAM/InstallTools directory:
377 $toolfile=$filename;
378 }
379 }
380
381 # Run the core setup routine:
382 $self->coresetup($toolname, $toolversion, $toolfile,$force);
383 return $self;
384 }
385
386 sub setupself()
387 {
388 my $self=shift;
389 my ($location)=@_;
390 # Process the file "Self" in local config directory. This is used to
391 # set all the paths/runtime settings for this project:
392 my $filename=$location."/config/Self";
393
394 if ( -f $filename )
395 {
396 print "\n";
397 print $::bold."Setting up SELF:".$::normal,"\n";
398 # Self file exists so process it:
399 $selfparser = BuildSystem::ToolParser->new();
400 $selfparser->parse('self','SELF',$filename);
401
402 # Next, set up the tool:
403 $store = $selfparser->processrawtool($self->interactive());
404
405 # If we are in a developer area, also add RELEASETOP paths:
406 if (exists($ENV{RELEASETOP}))
407 {
408 print "\nAdding RELEASE area settings to self....OK","\n", if ($ENV{SCRAM_DEBUG});
409 $store->addreleasetoself();
410 }
411
412 # Store the ToolData object in the cache:
413 $self->storeincache($selfparser->toolname(),$store);
414 print "\n";
415 }
416 else
417 {
418 print "\n";
419 print "SCRAM: No file config/Self...nothing to do.";
420 print "\n";
421 return;
422 }
423 }
424
425 sub defaultversion()
426 {
427 my $self = shift;
428 my ($tool) = @_;
429 # Return default versions as taken from configuration:
430 return (%{$self->defaultversions()}->{$tool});
431 }
432
433 sub storeincache()
434 {
435 my $self=shift;
436 my ($toolname,$dataobject)=@_;
437
438 # Store ToolData object (for a set-up tool) in cache:
439 if (ref($dataobject) eq 'BuildSystem::ToolData')
440 {
441 $self->{SETUP}->{$toolname} = $dataobject;
442 }
443 else
444 {
445 $::scram->scramerror("ToolManager: BuildSystem::ToolData object reference expected.")
446 }
447 }
448
449 sub tools()
450 {
451 my $self = shift;
452 my @tools;
453
454 map
455 {
456 if ($_ ne "self")
457 {
458 push(@tools, $_);
459 }
460 } keys %{$self->{SETUP}};
461
462 # Return list of set-up tools:
463 return @tools;
464 }
465
466 sub toolsdata()
467 {
468 my $self = shift;
469 my $tooldata = [];
470 my $rawsel = $self->selected();
471
472 foreach my $tool ( sort { %{$rawsel}->{$a}
473 <=> %{$rawsel}->{$b}}
474 keys %{$rawsel} )
475 {
476 # Return tool data objects of all set-up tools, skipping the tool "self":
477 if ($_ ne "self")
478 {
479 # Keep only tools that have really been set up:
480 if (exists $self->{SETUP}->{$tool})
481 {
482 push(@tooldata,$self->{SETUP}->{$tool});
483 }
484 }
485 }
486
487 # Return the array of tools, in order that they appear in RequirementsDoc:
488 return @tooldata;
489 }
490
491 sub definedtool()
492 {
493 my $self=shift;
494 my ($tool)=@_;
495
496 # Check to see if tool X is an external tool:
497 grep ($_ eq $tool, keys %{$self->{SETUP}}) ? return 1
498 : return 0;
499 }
500
501 sub checkifsetup()
502 {
503 my $self=shift;
504 my ($tool)=@_;
505 # Return the ToolData object if the tool has been set up:
506 (exists $self->{SETUP}->{$tool}) ? return ($self->{SETUP}->{$tool})
507 : return undef;
508 }
509
510 sub cloned_tm()
511 {
512 my $self=shift;
513 # Has this area already been cloned and brought in-line with current location:
514 @_ ? $self->{CLONED} = $_[0]
515 : $self->{CLONED};
516 }
517
518 sub remove_tool()
519 {
520 my $self=shift;
521 my ($toolname)=@_;
522 my $tools = $self->{SETUP};
523 my $newtlist = {};
524
525 while (my ($tool, $tooldata) = each %$tools)
526 {
527 if ($tool ne $toolname)
528 {
529 $newtlist->{$tool} = $tooldata;
530 }
531 else
532 {
533 # Is this tool a compiler?
534 if ($tooldata->scram_compiler() == 1)
535 {
536 # Also remove this from the compiler info if there happens to be an entry:
537 while (my ($langtype, $ctool) = each %{$self->{SCRAM_COMPILER}})
538 {
539 if ($toolname eq $ctool->[0])
540 {
541 delete $self->{SCRAM_COMPILER}->{$langtype};
542 print "Deleting compiler $toolname from cache.","\n";
543 }
544 }
545 }
546 else
547 {
548 print "Deleting $toolname from cache.","\n";
549 }
550 }
551 }
552
553 $self->{SETUP} = $newtlist;
554
555 # Now remove from the RAW tool list:
556 $self->cleanup_raw($toolname);
557 print "ToolManager: Updating tool cache.","\n";
558 $self->writecache();
559 }
560
561 sub scram_projects()
562 {
563 my $self=shift;
564 my $scram_projects={};
565
566 foreach my $t ($self->tools())
567 {
568 # Get the ToolData object:
569 my $td=$self->{SETUP}->{$t};
570 $scram_projects->{$t}=$td->variable_data(uc($t)."_BASE"), if ($td->scram_project());
571 }
572
573 return $scram_projects;
574 }
575
576 sub scram_compiler()
577 {
578 my $self=shift;
579 my ($langtype, $toolname, $compilername)=@_;
580
581 if ($langtype)
582 {
583 # Store the compiler info according to supported
584 # language types.
585 #
586 # ---------------------- e.g C++ cxxcompiler gcc323
587 $self->{SCRAM_COMPILER}->{$langtype}=[ $toolname, $compilername ];
588 }
589 else
590 {
591 return $self->{SCRAM_COMPILER};
592 }
593 }
594
595 sub updatetool()
596 {
597 my $self=shift;
598 my ($name, $obj) = @_;
599
600 # Replace the existing copy of the tool with the new one:
601 if (exists $self->{SETUP}->{$name})
602 {
603 # Check to make sure that we were really passed a compiler with
604 # the desired name:
605 if ($obj->toolname() eq $name)
606 {
607 print "ToolManager: Updating the cached copy of ".$name."\n";
608 delete $self->{SETUP}->{$name};
609 $self->{SETUP}->{$name} = $obj;
610 $self->writecache();
611 }
612 else
613 {
614 print "WARNING: Tool name (".$name.") and tool obj name (".$obj->toolname().") don't match!","\n";
615 print " Not making any changes.","\n";
616 }
617 }
618 else
619 {
620 print "WARNING: No entry in cache for ".$name.". Not making any updates.\n";
621 }
622 }
623
624 1;