BuildOptions.pm 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. our $VERSION = "0.01";
  17. use Dpkg::Gettext;
  18. use Dpkg::ErrorHandling;
  19. sub parse {
  20. my ($env) = @_;
  21. $env ||= $ENV{DEB_BUILD_OPTIONS};
  22. unless ($env) { return {}; }
  23. my %opts;
  24. foreach (split(/\s+/, $env)) {
  25. unless (/^([a-z][a-z0-9_-]*)(=(\S*))?$/) {
  26. warning(_g("invalid flag in DEB_BUILD_OPTIONS: %s"), $_);
  27. next;
  28. }
  29. my ($k, $v) = ($1, $3);
  30. # Sanity checks
  31. if ($k =~ /^(noopt|nostrip|nocheck)$/ && defined($v)) {
  32. $v = undef;
  33. } elsif ($k eq 'parallel') {
  34. $v = "" if not defined($v);
  35. next if $v !~ /^\d*$/;
  36. }
  37. $opts{$k} = $v;
  38. }
  39. return \%opts;
  40. }
  41. sub set {
  42. my ($opts, $overwrite) = @_;
  43. $overwrite = 1 if not defined($overwrite);
  44. my $new = {};
  45. $new = parse() unless $overwrite;
  46. while (my ($k, $v) = each %$opts) {
  47. $new->{$k} = $v;
  48. }
  49. my $env = join(" ", map { defined($new->{$_}) ? $_ . "=" . $new->{$_} : $_ } sort keys %$new);
  50. $ENV{DEB_BUILD_OPTIONS} = $env;
  51. return $env;
  52. }
  53. 1;