BuildOptions.pm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Copyright © 2007 Frank Lichtenheld <djpig@debian.org>
  2. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  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 of the License, or
  7. # (at your option) 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 <https://www.gnu.org/licenses/>.
  16. package Dpkg::BuildOptions;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '1.01';
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::BuildEnv;
  23. =encoding utf8
  24. =head1 NAME
  25. Dpkg::BuildOptions - parse and update build options
  26. =head1 DESCRIPTION
  27. The Dpkg::BuildOptions object can be used to manipulate options stored
  28. in environment variables like DEB_BUILD_OPTIONS and
  29. DEB_BUILD_MAINT_OPTIONS.
  30. =head1 FUNCTIONS
  31. =over 4
  32. =item my $bo = Dpkg::BuildOptions->new(%opts)
  33. Create a new Dpkg::BuildOptions object. It will be initialized based
  34. on the value of the environment variable named $opts{envvar} (or
  35. DEB_BUILD_OPTIONS if that option is not set).
  36. =cut
  37. sub new {
  38. my ($this, %opts) = @_;
  39. my $class = ref($this) || $this;
  40. my $self = {
  41. options => {},
  42. source => {},
  43. envvar => $opts{envvar} // 'DEB_BUILD_OPTIONS',
  44. };
  45. bless $self, $class;
  46. $self->merge(Dpkg::BuildEnv::get($self->{envvar}), $self->{envvar});
  47. return $self;
  48. }
  49. =item $bo->reset()
  50. Reset the object to not have any option (it's empty).
  51. =cut
  52. sub reset {
  53. my ($self) = @_;
  54. $self->{options} = {};
  55. $self->{source} = {};
  56. }
  57. =item $bo->merge($content, $source)
  58. Merge the options set in $content and record that they come from the
  59. source $source. $source is mainly used in warning messages currently
  60. to indicate where invalid options have been detected.
  61. $content is a space separated list of options with optional assigned
  62. values like "nocheck parallel=2".
  63. =cut
  64. sub merge {
  65. my ($self, $content, $source) = @_;
  66. return 0 unless defined $content;
  67. my $count = 0;
  68. foreach (split(/\s+/, $content)) {
  69. unless (/^([a-z][a-z0-9_-]*)(?:=(\S*))?$/) {
  70. warning(_g('invalid flag in %s: %s'), $source, $_);
  71. next;
  72. }
  73. $count += $self->set($1, $2, $source);
  74. }
  75. return $count;
  76. }
  77. =item $bo->set($option, $value, [$source])
  78. Store the given option in the objet with the given value. It's legitimate
  79. for a value to be undefined if the option is a simple boolean (its
  80. presence means true, its absence means false). The $source is optional
  81. and indicates where the option comes from.
  82. The known options have their values checked for sanity. Options without
  83. values have their value removed and options with invalid values are
  84. discarded.
  85. =cut
  86. sub set {
  87. my ($self, $key, $value, $source) = @_;
  88. # Sanity checks
  89. if ($key =~ /^(noopt|nostrip|nocheck)$/ && defined($value)) {
  90. $value = undef;
  91. } elsif ($key eq 'parallel') {
  92. $value //= '';
  93. return 0 if $value !~ /^\d*$/;
  94. }
  95. $self->{options}{$key} = $value;
  96. $self->{source}{$key} = $source;
  97. return 1;
  98. }
  99. =item $bo->get($option)
  100. Return the value associated to the option. It might be undef even if the
  101. option exists. You might want to check with $bo->has($option) to verify if
  102. the option is stored in the object.
  103. =cut
  104. sub get {
  105. my ($self, $key) = @_;
  106. return $self->{options}{$key};
  107. }
  108. =item $bo->has($option)
  109. Returns a boolean indicating whether the option is stored in the object.
  110. =cut
  111. sub has {
  112. my ($self, $key) = @_;
  113. return exists $self->{options}{$key};
  114. }
  115. =item $string = $bo->output($fh)
  116. Return a string representation of the build options suitable to be
  117. assigned to an environment variable. Can optionnaly output that string to
  118. the given filehandle.
  119. =cut
  120. sub output {
  121. my ($self, $fh) = @_;
  122. my $o = $self->{options};
  123. my $res = join(' ', map { defined($o->{$_}) ? $_ . '=' . $o->{$_} : $_ } sort keys %$o);
  124. print { $fh } $res if defined $fh;
  125. return $res;
  126. }
  127. =item $bo->export([$var])
  128. Export the build options to the given environment variable. If omitted,
  129. the environment variable defined at creation time is assumed. The value
  130. set to the variable is also returned.
  131. =cut
  132. sub export {
  133. my ($self, $var) = @_;
  134. $var //= $self->{envvar};
  135. my $content = $self->output();
  136. Dpkg::BuildEnv::set($var, $content);
  137. return $content;
  138. }
  139. =back
  140. =head1 CHANGES
  141. =head2 Version 1.01
  142. Enable to use another environment variable instead of DEB_BUILD_OPTIONS.
  143. Thus add support for the "envvar" option at creation time.
  144. =head2 Version 1.00
  145. Mark the module as public.
  146. =head1 AUTHOR
  147. Raphaël Hertzog <hertzog@debian.org>
  148. =cut
  149. 1;