summaryrefslogtreecommitdiff
path: root/lib/sbin/ProcessConfiguration.pl
blob: 9dffcba9c58dcba5b35f927ec981e2f039f85c0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#/*@@
#  @file      ProcessConfiguration.pl
#  @date      Mon May  8 15:52:08 2000
#  @author    Tom Goodale
#  @desc 
#  
#  @enddesc 
#@@*/

#/*@@
#  @routine    SplitThorns
#  @date       Mon May  8 16:04:59 2000
#  @author     Tom Goodale
#  @desc 
#  Splits the thorns hash into those with source and those without source
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#
#@@*/
sub SplitThorns
{
  my ($configuration_database, $thorns, $source_thorns, $nosource_thorns) = @_;

  foreach $thorn (sort keys %$thorns)
  {
    if($configuration_database->{"\U$thorn OPTIONS\E"} =~ m/NO_SOURCE/i)
    {
      $nosource_thorns->{"$thorn"} = $thorns->{"$thorn"};
    }
    else
    {
      $source_thorns->{"$thorn"} = $thorns->{"$thorn"};
    }
  }
}

#/*@@
#  @routine    ProcessConfiguration
#  @date       Thu Aug 26 22:09:26 2004
#  @author     Tom Goodale
#  @desc 
#  Runs all the configuration scripts belonging to thorns.
#  Has to setup the environment correctly, and as a result
#  modifies the config-info file for future reference.
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#
#@@*/
sub ProcessConfiguration
{
  my($config_dir,$config_database, $thorns, $config_file) = @_;

  my $thorn;
  my $provides;
  my @allowed_opts;

  # Find the master list of allowed options
  foreach $thorn (sort keys %thorns)
  {
#    print "DEBUG: Thorn $thorn\n";

    foreach $provides (split(' ',$config_database->{"\U$thorn\E PROVIDES"}))
    {
#      print "DEBUG: Provides $provides\n";

      if(@{$config_database->{"\U$thorn\E PROVIDES \U$provides\E OPTIONS"}} != 0)
      {
#        print "DEBUG: $thorn provides $provides with options\n";
        push(@allowed_opts, @{$config_database->{"\U$thorn\E PROVIDES \U$provides\E OPTIONS"}});
      }
    }
  }

#  print "DEBUG: allowed options are @allowed_opts\n";

  # Now get all the configuration options.
  my $env;
  my $optfile;
  my $configinfo;
  my $headers;

  ($configinfo,$headers) = ParseConfigInfo($config_file);

  if($ENV{"options"})
  {
    $optfile = ParseOptionsFile($ENV{"options"})
  }
  else
  {
    $optfile = {};
  }

  $env = GetOptionsFromEnv(\%ENV, \@allowed_opts);

  my $modified = AmalgamateOptions($env,$optfile,$configinfo,\@allowed_opts);

  # Write a new config-info file if anything has changed
  if($modified)
  {
    WriteNewConfigInfo($config_file,$headers,$configinfo);
  }

  # Now setup the environment
  # FIXME: Would like to restrict this to just the @allowed_opts, but then
  # flesh configuration options like MPI or arch specific ones like
  # IRIX_BITS would not be propogated 8-(

  foreach $option (keys %$configinfo)
  {
    $ENV{$option} = $configinfo->{$option};
  }

  # Ok, can now run the configuration scripts.

  my %thorns_todo = ();
  map { $thorns_todo{"\U$_\E"} = 1; } keys %thorns;
  my %requirements_done = ();

  my $made_progress = 1;
#  print "DEBUG: Processing thorn provisions:\n";
  while (keys %thorns_todo and $made_progress)
  {
#    print "DEBUG:    Thorns left to do: " . scalar(%thorns_todo) . "\n";
#    map { print "DEBUG:       - $_\n"; } sort keys %thorns_todo;
    $made_progress = 0;
    THORN: foreach $thorn (sort keys %thorns_todo)
    {
#      print "DEBUG:       Checking thorn $thorn\n";
      my @provides_list = split(' ',$config_database->{"\U$thorn\E PROVIDES"});
#      print "DEBUG:          Provides: @provides_list\n";
      my @requires_list = split(' ',$config_database->{"\U$thorn\E REQUIRES"});
#      print "DEBUG:          Requires: @requires_list\n";

      my %need = ();
      map { $need{"\U$_\E"} = 1; } @requires_list;
      map { delete $need{"\U$_\E"}; } @provides_list;
      map { next THORN unless exists $requirements_done{"\U$_\E"}; } keys %need;

#      print "DEBUG:       Processing thorn $thorn\n";
      foreach my $provides (@provides_list)
      {
#        print "DEBUG:          Processing provision $provides\n";
        my $script = $config_database->{"\U$thorn\E PROVIDES \U$provides\E SCRIPT"};
        my $lang   = $config_database->{"\U$thorn\E PROVIDES \U$provides\E LANG"};

        if ($script)
        {
#          print "DEBUG: Running configuration script '$script'\n";
  
          &ParseConfigScript($config_dir, $provides, $lang, $script,
                             $thorn, $config_database);
#          print "DEBUG: \n";
        }

        # Add make definitions to the environment, so that they are
        # available to the following scripts
        my $config = $config_database->{"\U$thorn $provides\E MAKE_DEFINITION"};
        my %options = $config =~ /^\s*(\w+)\s*=(.*)$/mg;
        foreach my $option (keys %options)
        {
          my $value = $options{$option};
          $value =~ s/^\s*//;
          $value =~ s/\s*$//;
#          print "DEBUG: Thorn $thorn, providing $provides, setting \$ENV{$option}=\"$value\"\n";
          $ENV{$option} = $value;
        }

        $requirements_done{"\U$provides\E"} = 1;
      }

      delete $thorns_todo{"\U$thorn\E"};
      $made_progress = 1;
    }
#    if (! $made_progress)
#    {
#      print "DEBUG:    Provided requirements: " . scalar($requirements_done) . "\n";
#      map { print "DEBUG:       - $_\n"; } sort keys %requirements_done;
#    }
#    die unless $made_progress;
  }
}

1;