6 $Id: config.prl 768 2006-01-28 03:33:28Z marknodine $
7 Copyright (C) 2002-2005 Freescale Semiconductor
8 Distributed under terms of the GNU General Public License (GPL).
12 Description: This program is a generic script to query the user for
13 information related to the system configuration that needs to be taken
14 into account in building or installing a software project. The
15 configuration is data-driven, using data from a separate perl module
16 ``swconfig.pm`` which must be written by the software developer. This
17 perl module should have the following outline::
23 @INSTRUCTIONS = ( ... );
25 sub WRAPUP { # Optional
29 These variables are as follows::
31 @ENVS Array of key/value pairs representing configuration
32 parameters, where the key is a name chosen for the
33 configuration parameter and the value is a reference
34 to a hash with the following keys::
36 default: The default value for the parameter
37 desc: A description to be printed while prompting
38 exec: 1 if the represents a path to an executable
39 with the parameter name. Optional.
40 checkfail: Reference to a subroutine to check for
41 validity of a response. It receives the
42 response as its argument and should return 1
43 if the response is invalid. Optional.
45 %FILE Hash of key/value pairs representing files that will
46 undergo configuration. The key is the name of the
47 file (relative to the configuring directory) and the
48 value is a reference to a hash with the following
51 init: Code to call to initialize text at the top
52 of the file. The values of configuration
53 parameters can be accessed via %CFGS, using
54 the configuration parameter name as key.
55 warn: The prefix to use on lines containing a
56 generated warning after the configuration
57 initialization text. The warning is
58 suppressed if the string is "<NONE>".
59 Optional. Defaults to "#### ".
60 exec: If true, sets the generated file to be
63 The contents of the generated file will be::
66 Warning (unless warn element is "<NONE>")
67 Contents of <filename>.root (if it exists)
69 sub WRAPUP Generates any additional files not fitting the schema
70 accommodated by %FILES.
72 @INSTRUCTIONS A list of the commands that should be run subsequent to
73 configuration to build and install the project.
77 Usage: perl config.prl [options]
80 -f file Keep same values as <file> (in config.log format)
82 -i Print installation instructions (no configuration done)
83 -k Keep same values as config.log
84 -u Print list of files to configure (no configuration done)
85 -v Print current configuration values (no configuration done)
91 Usage
() if ! getopts
("f:hikuv") || $opt_h;
93 # All the configuration is data-driven using config.pm
96 #### Here ends the configuration information for the config.prl file. ####
98 print "\nTo build, do \n", map("\t$_\n", @swconfig::INSTRUCTIONS
) if $opt_i;
99 print "\nFiles to configure:\n",map("\t$_\n",sort keys %swconfig::FILE
)
101 exit if ($opt_i || $opt_u) && ! $opt_v;
103 $opt_k = 1 if $opt_v || defined $opt_f;
105 # Ask a couple of questions about the environment.
107 %ENVS = @swconfig::ENVS
;
108 my $nenvs = keys %ENVS;
109 @envs = @swconfig::ENVS
[map(2*$_, 0 .. ($nenvs-1))];
110 @defaults{@envs} = map($ENVS{$_}{default}, @envs);
112 $DEFAULT_CFG_FILE = "config.log";
113 $CFG_FILE = defined $opt_f ?
$opt_f : $DEFAULT_CFG_FILE;
114 # Default to the current values if there is a config file
115 if (defined $opt_f || -f
$CFG_FILE) {
116 open CF
, $CFG_FILE or die "Cannot open $CFG_FILE.";
117 my %cfg = eval(join('',<CF
>));
118 @defaults{keys %cfg} = values %cfg;
122 # Default to values on the path
123 foreach $exec (@envs) {
124 next unless $ENVS{$exec}{exec};
125 my $bin = `which $exec`;
126 $defaults{$exec} = $1 if $bin =~ m
|(.*)/|;
130 %swconfig::CFGS
= %defaults if $opt_k;
133 print "Please tell us about your environment by answering the following \n";
134 print "questions. The value in the parentheses is the default value. \n";
135 print "Hit the <return> key to accept the default. Otherwise type a \n";
136 print "different value and hit <return> \n\n";
139 foreach $env (@envs) {
141 my $def = " ($defaults{$env})" if $defaults{$env} ne '';
142 print "$ENVS{$env}{desc}$def: ";
143 $swconfig::CFGS
{$env} = <STDIN
>;
144 chomp $swconfig::CFGS
{$env};
145 $swconfig::CFGS
{$env} = $defaults{$env}
146 if $swconfig::CFGS
{$env} eq '';
148 defined $ENVS{$env}{checkfail
} &&
149 &{$ENVS{$env}{checkfail
}}($swconfig::CFGS
{$env}) ||
151 ! -x
"$swconfig::CFGS{$env}/$env" && do {
152 print "There is no executable $swconfig::CFGS{$env}/$env\n"; 1 };
154 $defaults{$env} = $swconfig::CFGS
{$env};
157 foreach (keys %swconfig::CFGS
) {
158 $value = $swconfig::CFGS
{$_};
160 $value =~ s/^\s*(\S*)\s*/$1/;
161 $value = $defaults{$_} unless $value;
162 $swconfig::CFGS
{$_} = $value;
163 $defaults{$_} = $value;
168 print "\nDoes this look right? [y|n] ";
171 last if ($okay =~ m/^[y1]/i);
179 # Dump the configurations into a Config.log file
180 rename "$DEFAULT_CFG_FILE", "$DEFAULT_CFG_FILE.bak";
181 open (CL
, ">$DEFAULT_CFG_FILE") or die "Cannot write to $DEFAULT_CFG_FILE.";
182 print CL
map(qq('$_'=>'$swconfig::CFGS{$_}',\n), sort keys %swconfig::CFGS
);
185 @files = sort keys %swconfig::FILE
;
187 foreach $file (@files) {
188 print "Configuring $file\n";
189 open (RF
, "$file.root") if -r
"$file.root";
191 or die "Failed to modify $file. Please correct the error and try again.";
193 my $routine = $swconfig::FILE
{$file}{init
};
194 print MF
eval("package config; \${\\&\$routine()}");
195 die "Error evaluating initialization for $file: $@" if $@
;
196 if ($swconfig::FILE
{$file}{warn} ne '<NONE>') {
197 my $warn = $swconfig::FILE
{$file}{warn} || "#### ";
198 print MF
"${warn}Text above here was automatically generated during the configuration.\n";
199 print MF
"${warn}Modifications to this file will be lost during configuration.\n";
202 die "Failed to configure $file: $@" if $@
;
203 if (-r
"$file.root") {
209 chmod 0775, $file if $swconfig::FILE
{$file}{exec};
212 swconfig
::WRAPUP
() if defined &swconfig
::WRAPUP
;
214 print "\nEverything has been configured. To build, do \n";
215 print map("\t$_\n", @swconfig::INSTRUCTIONS
);
218 print "\nHere is the summary of the configuration:\n";
219 foreach $env (@envs) {
220 print " $ENVS{$env}{desc}: $swconfig::CFGS{$env}\n";
224 # This subroutine extracts and prints usage information
227 $what = "Usage" if ! $what;
228 my $mark = $what eq 'Description' ?
"($what|Usage)" : $what;
229 if (open(ME
,$0) == 1) {
231 if ((/^=begin $mark/ .. /^=end $mark/) &&
232 ! /^=(begin|end) $mark/) {
233 s/(\$\{[^\}]+\})/eval($1)/ge;
240 print STDERR
"Usage not available.\n";