BuildOptions.pm 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; either version 2 of the License, or
  4. # (at your option) any later version.
  5. #
  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. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. package Dpkg::BuildOptions;
  14. use strict;
  15. use warnings;
  16. use Dpkg::Gettext;
  17. use Dpkg::ErrorHandling;
  18. sub parse {
  19. my ($env) = @_;
  20. $env ||= $ENV{DEB_BUILD_OPTIONS};
  21. unless ($env) { return {}; }
  22. my %opts;
  23. foreach (split(/\s+/, $env)) {
  24. unless (/^([a-z][a-z0-9_-]*)(=(\S*))?$/) {
  25. warning(_g("invalid flag in DEB_BUILD_OPTIONS: %s"), $_);
  26. next;
  27. }
  28. my ($k, $v) = ($1, $3);
  29. # Sanity checks
  30. if ($k =~ /^(noopt|nostrip|nocheck)$/ && defined($v)) {
  31. $v = undef;
  32. } elsif ($k eq 'parallel') {
  33. $v = "" if not defined($v);
  34. next if $v !~ /^\d*$/;
  35. }
  36. $opts{$k} = $v;
  37. }
  38. return \%opts;
  39. }
  40. sub set {
  41. my ($opts, $overwrite) = @_;
  42. $overwrite = 1 if not defined($overwrite);
  43. my $new = {};
  44. $new = parse() unless $overwrite;
  45. while (my ($k, $v) = each %$opts) {
  46. $new->{$k} = $v;
  47. }
  48. my $env = join(" ", map { defined($new->{$_}) ? $_ . "=" . $new->{$_} : $_ } sort keys %$new);
  49. $ENV{DEB_BUILD_OPTIONS} = $env;
  50. return $env;
  51. }
  52. 1;