PEP#: 328
[tpot/pegasus/.git] / SetConfig_EnvVar
1 #!/usr/bin/perl
2 #//%2006////////////////////////////////////////////////////////////////////////
3 #//
4 #// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
5 #// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
6 #// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
7 #// IBM Corp.; EMC Corporation, The Open Group.
8 #// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
9 #// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
10 #// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
11 #// EMC Corporation; VERITAS Software Corporation; The Open Group.
12 #// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
13 #// EMC Corporation; Symantec Corporation; The Open Group.
14 #//
15 #// Permission is hereby granted, free of charge, to any person obtaining a copy
16 #// of this software and associated documentation files (the "Software"), to
17 #// deal in the Software without restriction, including without limitation the
18 #// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 #// sell copies of the Software, and to permit persons to whom the Software is
20 #// furnished to do so, subject to the following conditions:
21 #// 
22 #// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
23 #// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
24 #// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 #// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26 #// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 #// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 #// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 #// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #//
31 #//================================================================================
32 use strict;
33 #use warnings;
34 my $HELP_FILE = "SetConfig_EnvVar.help";
35 my $STATUS_FILE = "env_var.status";
36 my $PC_FILE = "pegasus.pc";
37
38 my %component_hash = ();
39 my %component_value=();
40 my %define_type_hash=();
41 my %depend_on_hash=();
42 my %enable_component_hash=();
43 my %short_descr_hash=();
44 my %long_descr_hash=();
45 my %argument_list=();
46
47 # Load up the info
48 &parse_help_file();
49 # Check for any arguments
50 &parse_arguments();
51 # Check the OS?
52 &determine_platform();
53 # Check the source path
54 &determine_source_path();
55 # Check arguments (if they have a path)
56 &check_arguments();
57 # Start asking the user the questions.
58 &ask_user();
59 # Print out all the options.
60 &write_out_status($STATUS_FILE);
61 # Create the .pc file for PKG-CONFIG
62 &write_out_pkg($PC_FILE);
63 # Write out extra info.
64 &extra_info();
65
66 sub determine_source_path()
67   {
68     my $source_dir = $ENV{PWD};
69     if (-r "$source_dir/$0")
70         {
71         $component_value{"PEGASUS_ROOT"} = $source_dir;
72         }
73   
74   }
75 sub which () {
76
77   my $execname = shift;
78   for (map {"$_/$execname"} split(/:/, $ENV{PATH}))
79     {
80       return $_ if (-e and -x);
81     }
82 }
83 sub determine_platform() {
84
85    my $uname="";
86    my $platform = "";
87    open UNAME,"uname -ms|" or die$!;
88    $uname = <UNAME>;
89    close UNAME;
90
91 #'uname -ms' for IA-64: "HP-UX ia64"
92 #'uname -ms' for PA-RISC: "HP-UX 9000/871"
93 #'uname -ms' for Sun Solaris: "SunOS sun4u"
94 #'uname -ms' for PPC64 Linux: "Linux ppc64"
95 #'uname -ms' for I64 Linux: "Linux ia64"
96 #'iname -ms' for PPC Linux: "Linux ppc"
97 #'uname -ms' for AMD64 Linux: "Linux x86_64"
98
99    if ($uname =~ /SunOS/)
100         {
101           $platform = "SOLARIS_SPARC_CC";
102           if (&which("gcc") =~ /gcc/)
103             {
104               $platform = "SOLARIS_SPARC_GNU";
105             }
106         }
107    if ($uname =~ /Linux/) 
108      {
109        # Default value
110        $platform = "LINUX_IX86_GNU";
111        if ($uname =~ /ppc/i)
112          {
113            $platform = "LINUX_PPC_GNU";
114          }
115        
116        if ($uname =~ /ia64/i)
117          {
118            $platform = "LINUX_IA64_GNU";
119          }
120      }
121    if ($uname =~ /AIX/)
122         {
123           $platform ="AIX_RS_IBMCXX";
124         }
125    if ($uname =~ /HP-UX/)
126         {
127           $platform = "HPUX_ACC";
128           if ($uname =~ /ia64/i)
129             {
130               $platform = "HPUX_IA64_ACC";
131             }
132           if ($uname =~ /9000/)
133             {
134               $platform = "HPUX_PARISC_ACC";
135             }
136         }
137    $component_value{"PEGASUS_PLATFORM"} = $platform;
138  }
139
140 sub extra_info() {
141
142   print "\n\n";
143   print "=================================================\n";
144   print "All your build options have been saved in ",$STATUS_FILE,"\n";
145   print "If you need to modify anything, please do so now.\n";
146   print "\nNOTE:\n";
147   print "The PATH enviroment variables needs to be modified\n";
148   print "to have",$component_value{"PEGASUS_HOME"},"/bin\n";
149   print "if you wish to run the unit tests.\n";
150   print "\n";
151   print "You might also need to set this enviroment variable\n";
152   print "PEGASUS_ROOT=",$component_value{"PEGASUS_ROOT"},"\n";
153   print "\n\nLastly, to compile OpenPegasus, type make\n";
154   print "=================================================\n";
155
156 }
157 sub write_out_pkg() {
158
159 }
160 sub write_out_status() {
161   
162   my $output = shift;
163   my $c="";
164   open OUTPUT, ">$output" or die $!;
165
166   foreach $c (sort keys(%component_value))
167     {
168       print OUTPUT ("# ", $short_descr_hash{$c},"\n");
169           if ($component_value{$c} eq "No")
170                 {
171         print OUTPUT ("# ",$c,"=",$component_value{$c},"\n");
172                 }
173           else
174                 {
175         print OUTPUT ($c,"=",$component_value{$c},"\n");
176                 }
177     }
178   close OUTPUT;
179   
180 }
181 sub ask_user() {
182
183   my $c="";
184   my $d="";
185   my $component_that_has_depend="";
186   my $loop_question=0;
187   my $pass=0;
188   my $skip=0;
189   my $search_for_dependants=0;
190
191   foreach $pass (0..1) {
192      if ($pass == 0) { 
193           print "=================================================\n";
194           print "All your build options will be saved in ",$STATUS_FILE," file.\n";
195           print "If you need to modify anything, you can do it after this program finishes..\n";
196           print "=================================================\n";
197     }
198     foreach $c (sort keys(%component_hash))
199       {
200         
201         # check in the $argument_list to see if this option
202         # has been passed in. But do it only on the first
203         # pass
204         if ($pass == 0) {
205           foreach $d (sort keys(%argument_list))
206             {    
207               if ($d eq $enable_component_hash{$c})
208                 {
209                   print $c," is set\n";
210                   $component_value{$c}="Yes";
211                   next;
212                 }
213           }
214         }
215         $skip = 0;
216         # Does this build option have a dependency?
217         if (!$depend_on_hash{$c} eq "" ) 
218           {
219           # Yes. Has the dependency been set?
220           if ($component_value{$depend_on_hash{$c}} eq "")
221             {
222               # It has not been set. Skip this pass.
223               #print "Skip, this build option - ",$c," is dependent (",$depend_on_hash{$c},")\n";
224               $skip =1;
225             }
226         }
227    # if this is the second pass, ignore the flag options.
228     if ($pass != 0) 
229     {
230           if ($define_type_hash{$c} =~ /flag/)
231       {
232         $skip =1;
233       }
234     }
235         if (($component_value{$c} eq "") && ($skip == 0)) {
236           # Ask the user.
237           print "\n--------------------\n";
238           print $long_descr_hash{$c},"\n";
239           $loop_question=1;
240       $search_for_dependants=0;
241           if ($define_type_hash{$c} =~ /boolean/)
242             {
243               do
244                 {
245                   print $c," [Y/n]: ";
246                   $_ = <STDIN>;
247                   if (/^y/i)
248                     {
249                       $component_value{$c}="Yes";
250               # There are build options that are 'disable.' Meaning that 'yes'
251               # sets all of the dependents to 'Yes' as well.
252                       $loop_question=0;            
253              if ($c =~ /PEGASUS_DISABLE/i)
254              {
255                 $search_for_dependants=1;
256              } 
257                     }
258                   if (/^n/i)
259                     {
260                       # Maybe ask the user again?
261                       $loop_question=0;
262                       $component_value{$c}="No";
263               # If the answer is No and it is a disable option, don't search
264               # the dependatns. Why? B/c the user might want to disable some
265               # more fine-grained options.
266               if ($c =~ /PEGASUS_DISABLE/i)
267               {
268                 $search_for_dependants=0;
269                           } else
270               {
271                 $search_for_dependants=1;
272               }
273              }
274               if ($search_for_dependants==1) {
275               #print "Search for dependants of ",$c,"\n";
276                       # Traverse through all the components to invalidate (set to No)
277                       # the dependants.
278                       foreach $component_that_has_depend (keys(%depend_on_hash))
279                         {
280                           # Filter out all of those build options that do not have any dependents.
281                           if (!$depend_on_hash{$component_that_has_depend} eq "")
282                             {
283                               #print " ",$depend_on_hash{$component_that_has_depend}," ? ",$c,"\n";
284                               # Does the dependant build option equal to the we are searching for?
285                               if ($depend_on_hash{$component_that_has_depend} eq $c) {
286                                 #print "Found: ",$component_that_has_depend," depends on ",$c,"\n"; }
287                                 $component_value{$component_that_has_depend}="No";
288                               }
289                             }
290                         }
291                     }
292                       if ($loop_question==1) 
293                         {
294                           print "Please entry Y or N.\n";
295                         }
296                 } while ($loop_question==1);
297
298             } # if type is boolean
299           
300           if ($define_type_hash{$c} =~ /flag/)
301        {
302          print $c,"[]: ";
303          $_ = <STDIN>;
304          chomp($_);
305          $component_value{$c}=$_; 
306        }
307           if ($define_type_hash{$c} =~ /path/)
308             {
309               do
310                 {
311                   print $c, "[/usr/include/]: ";
312                   $_ = <STDIN>;
313                   chomp($_);
314                   # Is the directory readable?
315                   if (-r $_)
316                     {
317                       $loop_question=0;
318                       $component_value{$c}=$_;
319                     }
320                   else
321                     {
322                       print "Path is not accessible. Please enter again.\n";
323                     }           
324                 } while ($loop_question==1);
325               
326             }
327         } # componet_value eq "";
328       }
329   }
330 }
331   
332 sub check_arguments() {
333     
334   my $c="";
335   foreach $c (sort keys(%argument_list))
336     {
337        if ($argument_list{$c} eq "") {
338         #print $c,"is not a PATH\n";
339       }
340       else 
341         {
342           # Check the path
343           if (!(-r $argument_list{$c}))
344             {
345               print $c,"=",$argument_list{$c}," path cannot be accessed.\n"
346            }
347         }
348      }
349 }
350
351 sub print_help() {
352
353    my $c="";
354    
355    print "Usage: ",$0," [OPTION]...\n";
356    print "\nArguments:\n";
357
358    foreach $c (sort keys(%component_hash)) {
359         print "   ",$enable_component_hash{$c};
360         if ($define_type_hash{$c} =~ /path/i) 
361         {
362                 print "=<directory>";
363         }
364         if ($define_type_hash{$c} =~ /flag/i) 
365         {
366                 print "=<flag>";
367         }
368         print "\t\t";
369         print $short_descr_hash{$c},"\n"
370    }
371 }
372
373 sub parse_arguments {
374
375   my $argnum = 0;
376   my @temp = ();
377   foreach $argnum (0..$#ARGV) {
378   
379     if (lc($ARGV[$argnum]) =~ /--help/i) {
380       &print_help();
381       exit (0);
382     }
383     if ($ARGV[$argnum] =~ /--/) {
384       # Two types:
385       # --<text>=<path>
386       # --text
387       # We need to extract the <text> and <path> to
388       # argument_list with <text> as key and
389       # <path> as value.
390       @temp = split(/=/, $ARGV[$argnum]);
391       $argument_list{$temp[0]}=$temp[1];
392     }
393   }
394
395 }
396 sub parse_help_file  {
397
398    my $component = "";
399    my $token = "";
400    my @tokens = {};
401    my $state = 1;
402    open HELP_FILE, "$HELP_FILE" or die$!;
403
404    while (<HELP_FILE>) {
405         # Find component.
406         if (/[\d\w_]+[\s]?{/) {
407                 s/[\s{]*//g;
408                 $component_hash{$_}=$_;
409                 $component=$_;
410                 $depend_on_hash{$component} = "";
411                 $long_descr_hash{$component} = "";
412                 $state = 1;
413         }
414         if (/,/ || /}/) {
415           #remove the offending }
416            s/}//;
417           #remove any spaces in front of the string
418            s/\s//;
419           #get the tokens seperated by ,
420            @tokens=split (/,/,$_);
421            foreach $token (@tokens) {
422 #               print "[",$state," ",$token, "]";
423
424                 if ($state == 1) { 
425                   #<depend or (Boolean or Path)
426                   #new option: flag
427                   # check for boolean or Path
428                    if (lc($token) =~ /boolean/ || /path/i || /flag/i){
429                         #print "(",$token,")";
430                         $define_type_hash{$component}=lc($token);
431                         $state = 3;
432                     }
433                     else {
434                       # check for "OPENSSL_HOME:<component>
435                       if ($depend_on_hash{$component} eq ""){
436                         $depend_on_hash{$component}=$token;
437                         #print "(",$token,")";
438                       }
439                     }
440                  }
441
442                 elsif ($state == 3) { #--enable-<component>
443
444                   if ($token =~ /--/) {
445                     $enable_component_hash{$component}=$token;
446                     $state =4;
447                   }
448                 }
449                 elsif ($state == 4) {#short_desc
450
451                   if (!($token =~ /\n/)) {
452                     $short_descr_hash{$component}=$token;
453                     $state=5;
454                   }
455                 }
456                 elsif ($state == 5) {#long descr
457
458                   $long_descr_hash{$component}=$long_descr_hash{$component}." ".$token;
459                 }
460               } # foreach token
461         
462          } #if /,/ or /}/
463       }
464    close (HELP_FILE);
465 }
466