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