Options.pm 12 KB

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