BuildOptions.pm 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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)$/ && length($v)) {
  31. $v = '';
  32. } elsif ($k eq 'parallel' && $v !~ /^-?\d+$/) {
  33. next;
  34. }
  35. $opts{$k} = $v;
  36. }
  37. return \%opts;
  38. }
  39. sub set {
  40. my ($opts, $overwrite) = @_;
  41. $overwrite = 1 if not defined($overwrite);
  42. my $new = {};
  43. $new = parse() unless $overwrite;
  44. while (my ($k, $v) = each %$opts) {
  45. $new->{$k} = $v;
  46. }
  47. my $env = join(" ", map { $new->{$_} ? $_ . "=" . $new->{$_} : $_ } sort keys %$new);
  48. $ENV{DEB_BUILD_OPTIONS} = $env;
  49. return $env;
  50. }
  51. 1;