dist: deprecated shar and tar+compress formats
[platform/upstream/automake.git] / lib / Automake / Options.pm
1 # Copyright (C) 2003-2013 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 2, 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 5.006;
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.
75 use vars '%_options';        # From AUTOMAKE_OPTIONS
76 use vars '%_global_options'; # From AM_INIT_AUTOMAKE or the command line.
77
78 # Whether process_option_list has already been called for the current
79 # Makefile.am.
80 use vars '$_options_processed';
81 # Whether process_global_option_list has already been called.
82 use vars '$_global_options_processed';
83
84 =head2 Constants
85
86 =over 4
87
88 =item FOREIGN
89
90 =item GNU
91
92 =item GNITS
93
94 Strictness constants used as values for C<$strictness>.
95
96 =back
97
98 =cut
99
100 # Constants to define the "strictness" level.
101 use constant FOREIGN => 0;
102 use constant GNU     => 1;
103 use constant GNITS   => 2;
104
105 =head2 Variables
106
107 =over 4
108
109 =item C<$strictness>
110
111 The current strictness.  One of C<FOREIGN>, C<GNU>, or C<GNITS>.
112
113 =item C<$strictness_name>
114
115 The current strictness name.  One of C<'foreign'>, C<'gnu'>, or C<'gnits'>.
116
117 =back
118
119 =cut
120
121 # Strictness levels.
122 use vars qw ($strictness $strictness_name);
123
124 # Strictness level as set on command line.
125 use vars qw ($_default_strictness $_default_strictness_name);
126
127
128 =head2 Functions
129
130 =over 4
131
132 =item C<Automake::Options::reset>
133
134 Reset the options variables for the next F<Makefile.am>.
135
136 In other words, this gets rid of all local options in use by the
137 previous F<Makefile.am>.
138
139 =cut
140
141 sub reset ()
142 {
143   $_options_processed = 0;
144   %_options = %_global_options;
145   # The first time we are run,
146   # remember the current setting as the default.
147   if (defined $_default_strictness)
148     {
149       $strictness = $_default_strictness;
150       $strictness_name = $_default_strictness_name;
151     }
152   else
153     {
154       $_default_strictness = $strictness;
155       $_default_strictness_name = $strictness_name;
156     }
157 }
158
159 =item C<$value = option ($name)>
160
161 =item C<$value = global_option ($name)>
162
163 Query the state of an option.  If the option is unset, this
164 returns the empty list.  Otherwise it returns the option's value,
165 as set by C<set_option> or C<set_global_option>.
166
167 Note that C<global_option> should be used only when it is
168 important to make sure an option hasn't been set locally.
169 Otherwise C<option> should be the standard function to
170 check for options (be they global or local).
171
172 =cut
173
174 sub option ($)
175 {
176   my ($name) = @_;
177   return () unless defined $_options{$name};
178   return $_options{$name};
179 }
180
181 sub global_option ($)
182 {
183   my ($name) = @_;
184   return () unless defined $_global_options{$name};
185   return $_global_options{$name};
186 }
187
188 =item C<set_option ($name, $value)>
189
190 =item C<set_global_option ($name, $value)>
191
192 Set an option.  By convention, C<$value> is usually the location
193 of the option definition.
194
195 =cut
196
197 sub set_option ($$)
198 {
199   my ($name, $value) = @_;
200   $_options{$name} = $value;
201 }
202
203 sub set_global_option ($$)
204 {
205   my ($name, $value) = @_;
206   $_global_options{$name} = $value;
207 }
208
209
210 =item C<unset_option ($name)>
211
212 =item C<unset_global_option ($name)>
213
214 Unset an option.
215
216 =cut
217
218 sub unset_option ($)
219 {
220   my ($name) = @_;
221   delete $_options{$name};
222 }
223
224 sub unset_global_option ($)
225 {
226   my ($name) = @_;
227   delete $_global_options{$name};
228 }
229
230
231 =item C<process_option_list (@list)>
232
233 =item C<process_global_option_list (@list)>
234
235 Process Automake's option lists.  C<@list> should be a list of hash
236 references with keys C<option> and C<where>, where C<option> is an
237 option as they occur in C<AUTOMAKE_OPTIONS> or C<AM_INIT_AUTOMAKE>,
238 and C<where> is the location where that option occurred.
239
240 These functions should be called at most once for each set of options
241 having the same precedence; i.e., do not call it twice for two options
242 from C<AM_INIT_AUTOMAKE>.
243
244 Return 1 on error, 0 otherwise.
245
246 =cut
247
248 # _option_must_be_from_configure ($OPTION, $WHERE)
249 # ----------------------------------------------
250 # Check that the $OPTION given in location $WHERE is specified with
251 # AM_INIT_AUTOMAKE, not with AUTOMAKE_OPTIONS.
252 sub _option_must_be_from_configure ($$)
253 {
254   my ($opt, $where)= @_;
255   return
256     if $where->get =~ /^configure\./;
257   error $where,
258         "option '$opt' can only be used as argument to AM_INIT_AUTOMAKE\n" .
259         "but not in AUTOMAKE_OPTIONS makefile statements";
260 }
261
262 # _is_valid_easy_option ($OPTION)
263 # -------------------------------
264 # Explicitly recognize valid automake options that require no
265 # special handling by '_process_option_list' below.
266 sub _is_valid_easy_option ($)
267 {
268   my $opt = shift;
269   return scalar grep { $opt eq $_ } qw(
270     check-news
271     color-tests
272     dejagnu
273     dist-bzip2
274     dist-lzip
275     dist-xz
276     dist-zip
277     info-in-builddir
278     no-define
279     no-dependencies
280     no-dist
281     no-dist-gzip
282     no-exeext
283     no-installinfo
284     no-installman
285     no-texinfo.tex
286     nostdinc
287     readme-alpha
288     serial-tests
289     parallel-tests
290     silent-rules
291     std-options
292     subdir-objects
293   );
294 }
295
296 # $BOOL
297 # _process_option_list (\%OPTIONS, @LIST)
298 # ------------------------------------------
299 # Process a list of options.  \%OPTIONS is the hash to fill with options
300 # data.  @LIST is a list of options as get passed to public subroutines
301 # process_option_list() and process_global_option_list() (see POD
302 # documentation above).
303 sub _process_option_list (\%@)
304 {
305   my ($options, @list) = @_;
306   my @warnings = ();
307
308   foreach my $h (@list)
309     {
310       local $_ = $h->{'option'};
311       my $where = $h->{'where'};
312       $options->{$_} = $where;
313       if ($_ eq 'gnits' || $_ eq 'gnu' || $_ eq 'foreign')
314         {
315           set_strictness ($_);
316         }
317       # TODO: Remove this special check in Automake 3.0.
318       elsif (/^(.*\/)?ansi2knr$/)
319         {
320           # Obsolete (and now removed) de-ANSI-fication support.
321           error ($where,
322                  "automatic de-ANSI-fication support has been removed");
323         }
324       # TODO: Remove this special check in Automake 3.0.
325       elsif ($_ eq 'cygnus')
326         {
327           error $where, "support for Cygnus-style trees has been removed";
328         }
329       # TODO: Remove this special check in Automake 3.0.
330       elsif ($_ eq 'dist-lzma')
331         {
332           error ($where, "support for lzma-compressed distribution " .
333                          "archives has been removed");
334         }
335       # TODO: Make this a fatal error in Automake 2.0.
336       elsif ($_ eq 'dist-shar')
337         {
338           msg ('obsolete', $where,
339                "support for shar distribution archives is deprecated.\n" .
340                "  It will be removed in Automake 2.0");
341         }
342       # TODO: Make this a fatal error in Automake 2.0.
343       elsif ($_ eq 'dist-tarZ')
344         {
345           msg ('obsolete', $where,
346                "support for distribution archives compressed with " .
347                "legacy program 'compress' is deprecated.\n" .
348                "  It will be removed in Automake 2.0");
349         }
350       elsif (/^filename-length-max=(\d+)$/)
351         {
352           delete $options->{$_};
353           $options->{'filename-length-max'} = [$_, $1];
354         }
355       elsif ($_ eq 'tar-v7' || $_ eq 'tar-ustar' || $_ eq 'tar-pax')
356         {
357           _option_must_be_from_configure ($_, $where);
358           for my $opt ('tar-v7', 'tar-ustar', 'tar-pax')
359             {
360               next
361                 if $opt eq $_ or ! exists $options->{$opt};
362               error ($where,
363                      "options '$_' and '$opt' are mutually exclusive");
364               last;
365             }
366         }
367       elsif (/^\d+\.\d+(?:\.\d+)?[a-z]?(?:-[A-Za-z0-9]+)?$/)
368         {
369           # Got a version number.
370           if (Automake::Version::check ($VERSION, $&))
371             {
372               error ($where, "require Automake $_, but have $VERSION",
373                      uniq_scope => US_GLOBAL);
374               return 1;
375             }
376         }
377       elsif (/^(?:--warnings=|-W)(.*)$/)
378         {
379           my @w = map { { cat => $_, loc => $where} } split (',', $1);
380           push @warnings, @w;
381         }
382       elsif (! _is_valid_easy_option $_)
383         {
384           error ($where, "option '$_' not recognized",
385                  uniq_scope => US_GLOBAL);
386           return 1;
387         }
388     }
389   # We process warnings here, so that any explicitly-given warning setting
390   # will take precedence over warning settings defined implicitly by the
391   # strictness.
392   foreach my $w (@warnings)
393     {
394       msg 'unsupported', $w->{'loc'},
395           "unknown warning category '$w->{'cat'}'"
396         if switch_warning $w->{cat};
397     }
398   return 0;
399 }
400
401 sub process_option_list (@)
402 {
403   prog_error "local options already processed"
404     if $_options_processed;
405   return _process_option_list (%_options, @_);
406   $_options_processed = 1;
407 }
408
409 sub process_global_option_list (@)
410 {
411   prog_error "global options already processed"
412     if $_global_options_processed;
413   return _process_option_list (%_global_options, @_);
414   $_global_options_processed = 1;
415 }
416
417 =item C<set_strictness ($name)>
418
419 Set the current strictness level.
420 C<$name> should be one of C<'foreign'>, C<'gnu'>, or C<'gnits'>.
421
422 =cut
423
424 # Set strictness.
425 sub set_strictness ($)
426 {
427   $strictness_name = $_[0];
428
429   Automake::ChannelDefs::set_strictness ($strictness_name);
430
431   if ($strictness_name eq 'gnu')
432     {
433       $strictness = GNU;
434     }
435   elsif ($strictness_name eq 'gnits')
436     {
437       $strictness = GNITS;
438     }
439   elsif ($strictness_name eq 'foreign')
440     {
441       $strictness = FOREIGN;
442     }
443   else
444     {
445       prog_error "level '$strictness_name' not recognized";
446     }
447 }
448
449 1;
450
451 ### Setup "GNU" style for perl-mode and cperl-mode.
452 ## Local Variables:
453 ## perl-indent-level: 2
454 ## perl-continued-statement-offset: 2
455 ## perl-continued-brace-offset: 0
456 ## perl-brace-offset: 0
457 ## perl-brace-imaginary-offset: 0
458 ## perl-label-offset: -2
459 ## cperl-indent-level: 2
460 ## cperl-brace-offset: 0
461 ## cperl-continued-brace-offset: 0
462 ## cperl-label-offset: -2
463 ## cperl-extra-newline-before-brace: t
464 ## cperl-merge-trailing-else: nil
465 ## cperl-continued-statement-offset: 2
466 ## End: