1 # Copyright © 2013 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
16 package Dpkg::BuildProfiles;
21 our $VERSION = '1.00';
26 evaluate_restriction_formula
29 use Exporter qw(import);
31 use Dpkg::Util qw(:list);
41 Dpkg::BuildProfiles - handle build profiles
45 The Dpkg::BuildProfiles module provides functions to handle the build
52 =item @profiles = get_build_profiles()
54 Get an array with the currently active build profiles, taken from
55 the environment variable B<DEB_BUILD_PROFILES>.
59 sub get_build_profiles {
60 return @build_profiles if $cache_profiles;
62 if (Dpkg::Build::Env::has('DEB_BUILD_PROFILES')) {
63 @build_profiles = split /\s+/, Dpkg::Build::Env::get('DEB_BUILD_PROFILES');
67 return @build_profiles;
70 =item set_build_profiles(@profiles)
72 Set C<@profiles> as the current active build profiles, by setting
73 the environment variable B<DEB_BUILD_PROFILES>.
77 sub set_build_profiles {
81 @build_profiles = @profiles;
82 Dpkg::Build::Env::set('DEB_BUILD_PROFILES', join ' ', @profiles);
85 =item @profiles = parse_build_profiles($string)
87 Parses a build profiles specification, into an array of array references.
91 sub parse_build_profiles {
94 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
96 return map { [ split /\s+/ ] } split /\s*>\s+<\s*/, $string;
99 =item evaluate_restriction_formula(\@formula, \@profiles)
101 Evaluate whether a restriction formula of the form "<foo bar> <baz>", given as
102 a nested array, is true or false, given the array of enabled build profiles.
106 sub evaluate_restriction_formula {
107 my ($formula, $profiles) = @_;
109 # Restriction formulas are in disjunctive normal form:
110 # (foo AND bar) OR (blub AND bla)
111 foreach my $restrlist (@{$formula}) {
112 my $seen_profile = 1;
114 foreach my $restriction (@$restrlist) {
115 next if $restriction !~ m/^(!)?(.+)/;
117 my $negated = defined $1 && $1 eq '!';
119 my $found = any { $_ eq $profile } @{$profiles};
121 # If a negative set profile is encountered, stop processing.
122 # If a positive unset profile is encountered, stop processing.
123 if ($found == $negated) {
129 # This conjunction evaluated to true so we don't have to evaluate
131 return 1 if $seen_profile;
140 =head2 Version 1.00 (dpkg 1.17.17)
142 Mark the module as public.