BuildProfiles.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright © 2013 Guillem Jover <guillem@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. package Dpkg::BuildProfiles;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.00';
  19. our @EXPORT_OK = qw(get_build_profiles set_build_profiles parse_build_profiles
  20. evaluate_restriction_formula);
  21. use Exporter qw(import);
  22. use Dpkg::Util qw(:list);
  23. use Dpkg::BuildEnv;
  24. my $cache_profiles;
  25. my @build_profiles;
  26. =encoding utf8
  27. =head1 NAME
  28. Dpkg::BuildProfiles - handle build profiles
  29. =head1 DESCRIPTION
  30. The Dpkg::BuildProfiles module provides functions to handle the build
  31. profiles.
  32. =head1 FUNCTIONS
  33. =over 4
  34. =item my @profiles = get_build_profiles()
  35. Get an array with the currently active build profiles, taken from
  36. the environment variable B<DEB_BUILD_PROFILES>.
  37. =cut
  38. sub get_build_profiles {
  39. return @build_profiles if $cache_profiles;
  40. if (Dpkg::BuildEnv::has('DEB_BUILD_PROFILES')) {
  41. @build_profiles = split /\s+/, Dpkg::BuildEnv::get('DEB_BUILD_PROFILES');
  42. }
  43. $cache_profiles = 1;
  44. return @build_profiles;
  45. }
  46. =item set_build_profiles(@profiles)
  47. Set C<@profiles> as the current active build profiles, by setting
  48. the environment variable B<DEB_BUILD_PROFILES>.
  49. =cut
  50. sub set_build_profiles {
  51. my (@profiles) = @_;
  52. @build_profiles = @profiles;
  53. Dpkg::BuildEnv::set('DEB_BUILD_PROFILES', join ' ', @profiles);
  54. }
  55. =item my @profiles = parse_build_profiles($string)
  56. Parses a build profiles specification, into an array of array references.
  57. =cut
  58. sub parse_build_profiles {
  59. my $string = shift;
  60. $string =~ s/^\s*<(.*)>\s*$/$1/;
  61. return map { [ split /\s+/ ] } split />\s+</, $string;
  62. }
  63. =item evaluate_restriction_formula(\@formula, \@profiles)
  64. Evaluate whether a restriction formula of the form "<foo bar> <baz>", given as
  65. a nested array, is true or false, given the array of enabled build profiles.
  66. =cut
  67. sub evaluate_restriction_formula {
  68. my ($formula, $profiles) = @_;
  69. # Restriction formulas are in disjunctive normal form:
  70. # (foo AND bar) OR (blub AND bla)
  71. foreach my $restrlist (@{$formula}) {
  72. my $seen_profile = 1;
  73. foreach my $restriction (@$restrlist) {
  74. next if $restriction !~ m/^(!)?(.+)/;
  75. my $negated = defined $1 && $1 eq '!';
  76. my $profile = $2;
  77. my $found = any { $_ eq $profile } @{$profiles};
  78. # If a negative set profile is encountered, stop processing.
  79. # If a positive unset profile is encountered, stop processing.
  80. if ($found == $negated) {
  81. $seen_profile = 0;
  82. last;
  83. }
  84. }
  85. # This conjunction evaluated to true so we don't have to evaluate
  86. # the others.
  87. return 1 if $seen_profile;
  88. }
  89. return 0;
  90. }
  91. =back
  92. =head1 CHANGES
  93. =head2 Version 1.00
  94. Mark the module as public.
  95. =cut
  96. 1;