Merge branch 'msvc'
[platform/upstream/automake.git] / lib / Automake / Options.pm
1 # Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software
2 # Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package Automake::Options;
18
19 use strict;
20 use Exporter;
21 use Automake::Config;
22 use Automake::ChannelDefs;
23 use Automake::Channels;
24 use Automake::Version;
25
26 use vars qw (@ISA @EXPORT);
27
28 @ISA = qw (Exporter);
29 @EXPORT = qw (option global_option
30               set_option set_global_option
31               unset_option unset_global_option
32               process_option_list process_global_option_list
33               set_strictness $strictness $strictness_name
34               &FOREIGN &GNU &GNITS);
35
36 =head1 NAME
37
38 Automake::Options - keep track of Automake options
39
40 =head1 SYNOPSIS
41
42   use Automake::Options;
43
44   # Option lookup and setting.
45   $opt = option 'name';
46   $opt = global_option 'name';
47   set_option 'name', 'value';
48   set_global_option 'name', 'value';
49   unset_option 'name';
50   unset_global_option 'name';
51
52   # Batch option setting.
53   process_option_list $location, @names;
54   process_global_option_list $location, @names;
55
56   # Strictness lookup and setting.
57   set_strictness 'foreign';
58   set_strictness 'gnu';
59   set_strictness 'gnits';
60   if ($strictness >= GNU) { ... }
61   print "$strictness_name\n";
62
63 =head1 DESCRIPTION
64
65 This packages manages Automake's options and strictness settings.
66 Options can be either local or global.  Local options are set using an
67 C<AUTOMAKE_OPTIONS> variable in a F<Makefile.am> and apply only to
68 this F<Makefile.am>.  Global options are set from the command line or
69 passed as an argument to C<AM_INIT_AUTOMAKE>, they apply to all
70 F<Makefile.am>s.
71
72 =cut
73
74 # Values are the Automake::Location of the definition, except
75 # for 'ansi2knr' whose value is a pair [filename, Location].
76 use vars '%_options';           # From AUTOMAKE_OPTIONS
77 use vars '%_global_options';    # from AM_INIT_AUTOMAKE or the command line.
78
79 =head2 Constants
80
81 =over 4
82
83 =item FOREIGN
84
85 =item GNU
86
87 =item GNITS
88
89 Strictness constants used as values for C<$strictness>.
90
91 =back
92
93 =cut
94
95 # Constants to define the "strictness" level.
96 use constant FOREIGN => 0;
97 use constant GNU     => 1;
98 use constant GNITS   => 2;
99
100 =head2 Variables
101
102 =over 4
103
104 =item C<$strictness>
105
106 The current strictness.  One of C<FOREIGN>, C<GNU>, or C<GNITS>.
107
108 =item C<$strictness_name>
109
110 The current strictness name.  One of C<'foreign'>, C<'gnu'>, or C<'gnits'>.
111
112 =back
113
114 =cut
115
116 # Strictness levels.
117 use vars qw ($strictness $strictness_name);
118
119 # Strictness level as set on command line.
120 use vars qw ($_default_strictness $_default_strictness_name);
121
122
123 =head2 Functions
124
125 =over 4
126
127 =item C<Automake::Options::reset>
128
129 Reset the options variables for the next F<Makefile.am>.
130
131 In other words, this gets rid of all local options in use by the
132 previous F<Makefile.am>.
133
134 =cut
135
136 sub reset ()
137 {
138   %_options = %_global_options;
139   # The first time we are run,
140   # remember the current setting as the default.
141   if (defined $_default_strictness)
142     {
143       $strictness = $_default_strictness;
144       $strictness_name = $_default_strictness_name;
145     }
146   else
147     {
148       $_default_strictness = $strictness;
149       $_default_strictness_name = $strictness_name;
150     }
151 }
152
153 =item C<$value = option ($name)>
154
155 =item C<$value = global_option ($name)>
156
157 Query the state of an option.  If the option is unset, this
158 returns the empty list.  Otherwise it returns the option's value,
159 as set by C<set_option> or C<set_global_option>.
160
161 Note that C<global_option> should be used only when it is
162 important to make sure an option hasn't been set locally.
163 Otherwise C<option> should be the standard function to
164 check for options (be they global or local).
165
166 =cut
167
168 sub option ($)
169 {
170   my ($name) = @_;
171   return () unless defined $_options{$name};
172   return $_options{$name};
173 }
174
175 sub global_option ($)
176 {
177   my ($name) = @_;
178   return () unless defined $_global_options{$name};
179   return $_global_options{$name};
180 }
181
182 =item C<set_option ($name, $value)>
183
184 =item C<set_global_option ($name, $value)>
185
186 Set an option.  By convention, C<$value> is usually the location
187 of the option definition.
188
189 =cut
190
191 sub set_option ($$)
192 {
193   my ($name, $value) = @_;
194   $_options{$name} = $value;
195 }
196
197 sub set_global_option ($$)
198 {
199   my ($name, $value) = @_;
200   $_global_options{$name} = $value;
201 }
202
203
204 =item C<unset_option ($name)>
205
206 =item C<unset_global_option ($name)>
207
208 Unset an option.
209
210 =cut
211
212 sub unset_option ($)
213 {
214   my ($name) = @_;
215   delete $_options{$name};
216 }
217
218 sub unset_global_option ($)
219 {
220   my ($name) = @_;
221   delete $_global_options{$name};
222 }
223
224
225 =item C<process_option_list ($where, @options)>
226
227 =item C<process_global_option_list ($where, @options)>
228
229 Process Automake's option lists.  C<@options> should be a list of
230 words, as they occur in C<AUTOMAKE_OPTIONS> or C<AM_INIT_AUTOMAKE>.
231
232 Return 1 on error, 0 otherwise.
233
234 =cut
235
236 # $BOOL
237 # _process_option_list (\%OPTIONS, $WHERE, @OPTIONS)
238 # --------------------------------------------------
239 # Process a list of options.  Return 1 on error, 0 otherwise.
240 # \%OPTIONS is the hash to fill with options data, $WHERE is
241 # the location where @OPTIONS occurred.
242 sub _process_option_list (\%$@)
243 {
244   my ($options, $where, @list) = @_;
245
246   foreach (@list)
247     {
248       $options->{$_} = $where;
249       if ($_ eq 'gnits' || $_ eq 'gnu' || $_ eq 'foreign')
250         {
251           set_strictness ($_);
252         }
253       elsif (/^(.*\/)?ansi2knr$/)
254         {
255           # An option like "../lib/ansi2knr" is allowed.  With no
256           # path prefix, we assume the required programs are in this
257           # directory.  We save the actual option for later.
258           $options->{'ansi2knr'} = [$_, $where];
259         }
260       elsif ($_ eq 'no-installman' || $_ eq 'no-installinfo'
261              || $_ eq 'dist-shar' || $_ eq 'dist-zip'
262              || $_ eq 'dist-tarZ' || $_ eq 'dist-bzip2'
263              || $_ eq 'dist-lzip' || $_ eq 'dist-lzma' || $_ eq 'dist-xz'
264              || $_ eq 'no-dist-gzip' || $_ eq 'no-dist'
265              || $_ eq 'dejagnu' || $_ eq 'no-texinfo.tex'
266              || $_ eq 'readme-alpha' || $_ eq 'check-news'
267              || $_ eq 'subdir-objects' || $_ eq 'nostdinc'
268              || $_ eq 'no-exeext' || $_ eq 'no-define'
269              || $_ eq 'std-options'
270              || $_ eq 'color-tests' || $_ eq 'parallel-tests'
271              || $_ eq 'cygnus' || $_ eq 'no-dependencies')
272         {
273           # Explicitly recognize these.
274         }
275       elsif ($_ =~ /^filename-length-max=(\d+)$/)
276         {
277           delete $options->{$_};
278           $options->{'filename-length-max'} = [$_, $1];
279         }
280       elsif ($_ eq  'silent-rules')
281         {
282           error ($where,
283                  "option `$_' can only be used as argument to AM_INIT_AUTOMAKE\n"
284                  . "but not in AUTOMAKE_OPTIONS makefile statements")
285             if $where->get !~ /^configure\./;
286         }
287       elsif ($_ eq 'tar-v7' || $_ eq 'tar-ustar' || $_ eq 'tar-pax')
288         {
289           error ($where,
290                  "option `$_' can only be used as argument to AM_INIT_AUTOMAKE\n"
291                  . "but not in AUTOMAKE_OPTIONS makefile statements")
292             if $where->get !~ /^configure\./;
293           for my $opt ('tar-v7', 'tar-ustar', 'tar-pax')
294             {
295               next if $opt eq $_;
296               if (exists $options->{$opt})
297                 {
298                   error ($where,
299                          "options `$_' and `$opt' are mutually exclusive");
300                   last;
301                 }
302             }
303         }
304       elsif (/^\d+\.\d+(?:\.\d+)?[a-z]?(?:-[A-Za-z0-9]+)?$/)
305         {
306           # Got a version number.
307           if (Automake::Version::check ($VERSION, $&))
308             {
309               error ($where, "require Automake $_, but have $VERSION",
310                      uniq_scope => US_GLOBAL);
311               return 1;
312             }
313         }
314       elsif (/^(?:--warnings=|-W)(.*)$/)
315         {
316           foreach my $cat (split (',', $1))
317             {
318               msg 'unsupported', $where, "unknown warning category `$cat'"
319                 if switch_warning $cat;
320             }
321         }
322       else
323         {
324           error ($where, "option `$_' not recognized",
325                  uniq_scope => US_GLOBAL);
326           return 1;
327         }
328     }
329   return 0;
330 }
331
332 sub process_option_list ($@)
333 {
334   my ($where, @list) = @_;
335   return _process_option_list (%_options, $where, @list);
336 }
337
338 sub process_global_option_list ($@)
339 {
340   my ($where, @list) = @_;
341   return _process_option_list (%_global_options, $where, @list);
342 }
343
344 =item C<set_strictness ($name)>
345
346 Set the current strictness level.
347 C<$name> should be one of C<'foreign'>, C<'gnu'>, or C<'gnits'>.
348
349 =cut
350
351 # Set strictness.
352 sub set_strictness ($)
353 {
354   $strictness_name = $_[0];
355
356   Automake::ChannelDefs::set_strictness ($strictness_name);
357
358   if ($strictness_name eq 'gnu')
359     {
360       $strictness = GNU;
361     }
362   elsif ($strictness_name eq 'gnits')
363     {
364       $strictness = GNITS;
365     }
366   elsif ($strictness_name eq 'foreign')
367     {
368       $strictness = FOREIGN;
369     }
370   else
371     {
372       prog_error "level `$strictness_name' not recognized";
373     }
374 }
375
376 1;
377
378 ### Setup "GNU" style for perl-mode and cperl-mode.
379 ## Local Variables:
380 ## perl-indent-level: 2
381 ## perl-continued-statement-offset: 2
382 ## perl-continued-brace-offset: 0
383 ## perl-brace-offset: 0
384 ## perl-brace-imaginary-offset: 0
385 ## perl-label-offset: -2
386 ## cperl-indent-level: 2
387 ## cperl-brace-offset: 0
388 ## cperl-continued-brace-offset: 0
389 ## cperl-label-offset: -2
390 ## cperl-extra-newline-before-brace: t
391 ## cperl-merge-trailing-else: nil
392 ## cperl-continued-statement-offset: 2
393 ## End: