summaryrefslogtreecommitdiff
path: root/lib/make/setup_configuration.pl
blob: bb0272823816c898def651d24abb2dbe92ecdc14 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#! /usr/bin/perl -sw
#/*@@
#  @file    setup_configuration.pl
#  @date    Fri Jan  8 13:48:48 1999
#  @author  Tom Goodale
#  @desc
#           Setup file for a new configuration in the CCTK
#           Invocation is
#           setup_configuration [-reconfig] [-config_file=<options>] config_name
#  @enddesc
#  @version $Header$
#@@*/

chop ($top = `pwd`);

$configure = "sh $top/lib/make/configure";

if($#ARGV > -1)
{
  $config = shift(@ARGV);
}
else
{
  chop ($config = `uname`);
}
# Replace slashes with underscores.
$config =~ s:[/\\]:_:g;

# Work out if there is a user default file
if($ENV{CACTUSRC_DIR})
{
  if (-e "$ENV{CACTUSRC_DIR}/.cactus/config")
  {
    $default_file = "$ENV{CACTUSRC_DIR}/.cactus/config";
  }
}
elsif (-e "$ENV{HOME}/.cactus/config")
{
  $default_file = "$ENV{HOME}/.cactus/config";
}

# Work out where the config directory is
$configs_dir = $ENV{CONFIGS_DIR};
$configs_dir = 'configs' if (! $configs_dir);

# The configs directory doesn't exist.
if (! -d "$configs_dir" && ! -l "$configs_dir")
{
  print "Completely new cactus build.  Creating config database\n";

  mkdir("$configs_dir", 0755) ||
    die "Internal error - couldn't create '$configs_dir'\n";
}

chdir "$configs_dir" ||
  die "Internal error - couldn't enter '$configs_dir'\n";

# The specified configuration doesn't exist
if (! -d "$config" && ! -l "$config")
{
  print "Creating new configuration $config.\n";

  for $dir ("$config", "$config/build", "$config/lib", "$config/scratch", "$config/config-data")
  {
    mkdir("$dir",0755) || die "Internal error - couldn't create $dir";
  }
}
else
{
  print "Reconfiguring $config.\n";
}


%CONFIGURED = &SetConfigureEnv();

$configure_command = &DetermineConfigureCommand($configure);

chdir "$config/config-data" ||
  die "Internal error - couldn't enter '$configs_dir/$config/config-data'\n";

# remove cached configure options
unlink 'config.cache' if (-f 'config.cache');

system("$configure_command");
$retcode = $? >> 8;

chdir '..';

open(INFO, ">config-info") ||
  die "Internal error - couldn't create '$configs_dir/$config/config-info'\n";

print INFO "# CONFIGURATION  : $config\n";
print INFO "# CONFIG-DATE    : " . gmtime(time()) . " (GMT)\n";
chop ($hostname = `hostname`);
print INFO "# CONFIG-HOST    : $hostname\n";
print INFO "# CONFIG-STATUS  : $retcode\n";
print INFO "# CONFIG-OPTIONS :\n";
foreach $setting (sort keys %CONFIGURED)
{
  print INFO "$setting=$CONFIGURED{$setting}\n";
}

close(INFO);

exit $retcode;


#/*@@
#  @routine
#  @date       Fri Feb 19 19:53:48 1999
#  @author     Tom Goodale
#  @desc
#  Sets the environment for running the configure script.
#  @enddesc
#@@*/
sub SetConfigureEnv
{
  local($line_number, $commandline);
  # Set a default name for the configuration
  $ENV{"EXE"} = "cactus_$config";

  # Set variables from makefile command line first
  $commandline = $ENV{"MAKEFLAGS"};
  $line_number = 0;
#  while ($commandline =~ /^(.*)\s+(\w+)\s*=\s*([_+\-\.\w\\\/\s]*)\s*/)
  while ($commandline =~ /^(.*)\s*\b(\w+)\s*=\s*(.*?)\s*$/)
  {
    if ($2 ne 'options' && $2 ne 'VERBOSE')
    {
      print "Using configuration options from configure line\n"
        if (!$line_number);
      $line_number++;
      # Remember it for writing to config-info
      $option = AddQuotes($3);
      $CONFIGURED{$2} = $option;

      print "  Setting $2 to '$option'\n";
    }
    $commandline=$1;
    #  print "New commandline = <$commandline>\n";
  }
  print "End of options from configure line\n"
    if ($line_number);

  # Add variables from user configuration options file
  if($config_file)
  {
    # Turn path to options file to an absolute path
    $filename = $config_file;
    if($config_file =~ m:^/:)
    {
      # Do nothing
    }
    elsif($config_file =~ m:^~:)
    {
      $filename =~ s/^~/$ENV{"HOME"}/
    }
    else
    {
      $filename = "$top/$config_file";
    }

    # The user has specified a configuration file
    print "Adding configuration options from '$config_file'...\n";
    ParseOptionsFile ($filename, \%ENV, \%CONFIGURED);
    print "End of options from '$config_file'.\n";
  }

  # Add variables from either ${CACTUS_CONFIG_FILES} or a user default file
  if (defined $ENV{CACTUS_CONFIG_FILES})
  {
    foreach $file (split (':', $ENV{CACTUS_CONFIG_FILES}))
    {
      print "Adding configuration options from '$file'...\n";
      ParseOptionsFile ($file, \%ENV, \%CONFIGURED);
      print "End of options from '$file'.\n";
    }
  }
  elsif ($default_file)
  {
    print "Adding configuration options from user defaults in $default_file...\n";
    ParseOptionsFile ($default_file, \%ENV, \%CONFIGURED);
    print "End of options from user defaults.\n";
  }

  return %CONFIGURED;
}

sub ParseOptionsFile
{
  my($file, $env, $options) = @_;
  my($line_number);


  open(INFILE, "< $file") || die "Cannot open configuration file '$file'\n";

  $line_number = 0;
  while(<INFILE>)
  {
    $line_number++;

    #Ignore comments.
    s/\#(.*)$//g;

    #Remove spaces at end of lines
    s/\s*$//;
    s/\n//g;                # Different from chop...

    #Ignore blank lines
    next if (m:^\s*$:);

    # Match lines of the form
    #     keyword value
    # or  keyword = value
    if (/^\s*(\w+)[=\s]+(.*)\s*/)
    {
      # only set it if it wasn't already
      if(! $options->{$1})
      {
        print "  Setting $1 to '$2'\n";
        $env->{$1} = $2;
        # Remember it for writing to config-info
        $options->{$1} = AddQuotes($2);
      }
    }
    else
    {
      print "Could not parse configuration line $line_number...\n'$_'\n";
    }
  }
  close(INFILE);
}

sub DetermineConfigureCommand
{
  my($configure_command) = @_;


  $configure_command .= ' --build='  . $ENV{BUILD_MACHINE}
    if($ENV{BUILD_MACHINE});
  $configure_command .= ' --target=' . $ENV{TARGET_MACHINE}
    if($ENV{TARGET_MACHINE});
  $configure_command .= ' --host='   . $ENV{HOST_MACHINE}
    if($ENV{HOST_MACHINE});

  return $configure_command;
}

sub AddQuotes
{
  local($arg) = @_;

  if ($arg =~ /\\/)
  {
    $arg =~ s:\\::g;
#    $arg = "\'$arg\'";
  }

  # When we grab an arg off the MAKEFLAGS it has $s doubled.
  $arg =~ s/\$\$/\$/g;

  return $arg;
}