BuildProfiles.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 = '0.01';
  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::BuildEnv;
  23. my $cache_profiles;
  24. my @build_profiles;
  25. =encoding utf8
  26. =head1 NAME
  27. Dpkg::BuildProfiles - handle build profiles
  28. =head1 DESCRIPTION
  29. The Dpkg::BuildProfiles module provides functions to handle the build
  30. profiles.
  31. =head1 FUNCTIONS
  32. =over 4
  33. =item my @profiles = get_build_profiles()
  34. Get an array with the currently active build profiles, taken from
  35. the environment variable B<DEB_BUILD_PROFILES>.
  36. =cut
  37. sub get_build_profiles {
  38. return @build_profiles if $cache_profiles;
  39. if (Dpkg::BuildEnv::has('DEB_BUILD_PROFILES')) {
  40. @build_profiles = split / /, Dpkg::BuildEnv::get('DEB_BUILD_PROFILES');
  41. }
  42. $cache_profiles = 1;
  43. return @build_profiles;
  44. }
  45. =item set_build_profiles(@profiles)
  46. Set C<@profiles> as the current active build profiles, by setting
  47. the environment variable B<DEB_BUILD_PROFILES>.
  48. =cut
  49. sub set_build_profiles {
  50. my (@profiles) = @_;
  51. @build_profiles = @profiles;
  52. Dpkg::BuildEnv::set('DEB_BUILD_PROFILES', join ' ', @profiles);
  53. }
  54. =item my @profiles = parse_build_profiles($string)
  55. Parses a build profiles specification, into an array.
  56. =cut
  57. sub parse_build_profiles {
  58. my $string = shift;
  59. return map { lc } split /\s+/, $string;
  60. }
  61. =item evaluate_restriction_formula(\@formula, \@profiles)
  62. Evaluate whether a restriction list, is true or false, given the array of
  63. enabled build profiles.
  64. =cut
  65. sub evaluate_restriction_formula {
  66. my ($restrictions, $build_profiles) = @_;
  67. my $seen_profile = 0;
  68. foreach my $restriction (@{$restrictions}) {
  69. # Determine if this restriction is negated, and within the "profile"
  70. # namespace, otherwise it does not concern this check.
  71. next if $restriction !~ m/^(!)?profile\.(.*)/;
  72. my $negated = defined $1 && $1 eq '!';
  73. my $profile = $2;
  74. # Determine if the restriction matches any of the specified profiles.
  75. my $found = any { $_ eq $profile } @{$build_profiles};
  76. if ($negated) {
  77. if ($found) {
  78. $seen_profile = 0;
  79. last;
  80. } else {
  81. # "!profile.this" includes by default all other profiles
  82. # unless they also appear in a "!profile.other".
  83. $seen_profile = 1;
  84. }
  85. } elsif ($found) {
  86. $seen_profile = 1;
  87. last;
  88. }
  89. }
  90. return $seen_profile;
  91. }
  92. =back
  93. =cut
  94. 1;