File: BuildDeps.pm

package info (click to toggle)
dh-buildinfo 0.11+nmu2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 124 kB
  • sloc: perl: 685; makefile: 49; sh: 24
file content (188 lines) | stat: -rwxr-xr-x 4,814 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package BuildDeps;

# This function checks the build dependencies passed in as the first
# parameter. If they are satisfied, returns false. If they are unsatisfied,
# an list of the unsatisfied depends is returned.
#
# Additional parameters that must be passed:
# * A reference to a hash of all "ok installed" the packages on the system,
#   with the hash key being the package name, and the value being the 
#   installed version.
# * A reference to a hash, where the keys are package names, and the
#   value is a true value iff some package installed on the system provides
#   that package (all installed packages provide themselves)
#
# Optionally, the architecture the package is to be built for can be passed
# in as the 4th parameter. If not set, dpkg will be queried for the build
# architecture.
sub depends {
	return check_line(1, @_);
}

# This function is exactly like unmet_build_depends, except it
# checks for build conflicts, and returns a list of the packages
# that are installed and are conflicted with.
sub conflicts {
	return check_line(0, @_);
}

# This function does all the work. The first parameter is 1 to check build
# deps, and 0 to check build conflicts.
sub check_line {
	my $build_depends=shift;
	my $line=shift;
	my %version=%{shift()};
	my %providers=%{shift()};
	my $dump_met=shift();
	my $build_arch=shift || `dpkg --print-architecture`;
	chomp $build_arch;

	my @unmet=();
	my @met=();
	foreach my $dep (split(/,\s*/, $line)) {
		my $ok=0;
		my @possibles=();
		my @effectives=();
ALTERNATE:	foreach my $alternate (split(/\s*\|\s*/, $dep)) {
			my ($package, $rest)=split(/\s*(?=[\[\(])/, $alternate, 2);
			$package =~ s/\s*$//;

			# Check arch specifications.
			if (defined $rest && $rest=~m/\[(.*?)\]/) {
				my $arches=lc($1);
				my $seen_arch='';
				foreach my $arch (split(' ', $arches)) {
					if ($arch eq $build_arch) {
						$seen_arch=1;
						next;
					}
					elsif ($arch eq "!$build_arch") {
						next ALTERNATE;
					}
					elsif ($arch =~ /!/) {
						# This is equivilant to
						# having seen the current arch,
						# unless the current arch
						# is also listed..
						$seen_arch=1;
					}
				}
				if (! $seen_arch) {
					next;
				}
			}
			
			# This is a possible way to meet the dependency.
			# Remove the arch stuff from $alternate.
			$alternate=~s/\s+\[.*?\]//;
			push @possibles, $alternate;
	
			# Check version.
			if (defined $rest && $rest=~m/\(\s*([<>=]{1,2})\s*(.*?)\s*\)/) {
				my $relation=$1;
				my $version=$2;
				
				if (! exists $version{$package}) {
					# Not installed at all, so fail.
					next;
				}
				else {
					# Compare installed and needed
					# version number.
					system("dpkg", "--compare-versions",
						$version{$package}, $relation,
						 $version);
					if (($? >> 8) != 0) {
						next; # fail
					}
				}
			}
			elsif (! defined $providers{$package}) {
				# It's not a versioned dependency, and
				# nothing provides it, so fail.
				next;
			}
	
			# If we get to here, the dependency was met.
			$ok=1;

			if ($dump_met) {
				# keep it
#				push (@effectives, @{$providers{$package}});
				push (@effectives, $package);
			}
		}
	
		if (@possibles && (($build_depends && ! $ok) ||
		                   (! $build_depends && $ok))) {
			# TODO: this could return a more complex
			# data structure instead to save re-parsing.
			push @unmet, join (" | ", @possibles);
		} else {
			push @met, @effectives;
		}
	}

	if ($dump_met) {
	  return @met;
	} else {
	  return @unmet;
	}
}

sub pkginfo {
  my $package = shift;
  my %version=%{shift()};
  my %providers=%{shift()};

  my @result;

  if (ref $package eq 'ARRAY') {
    foreach my $pkg (@{$package}) {
      push @result, pkginfo ($pkg, \%version, \%providers);
    }
  } else {
    # there is at least one provider, as I assume builddeps are satisfied
    foreach my $provider (@{$providers{$package}}) {
      my $ver = $version{$provider};
      if (defined $ver) {
	if ($package eq $provider) {
	  push (@result, [ $provider, $ver ]);
	} else {
	  # pseudo package
	  push (@result, [ $provider, $ver, $package ]);
	}
      }
    }
  }

  return @result;
}

sub parse_control {
	my $control=shift || "debian/control";
	my %fields = ();

	open (CONTROL, $control) || die "$control: $!\n";
	local $/='';
	my $cdata=<CONTROL>;
	close CONTROL;

	my $dep_regex=qr/\s*((.|\n\s+)*)\s/; # allow multi-line
	if ($cdata =~ /^Build-Depends:$dep_regex/mi) {
		$fields{'Build-Depends'} = $1;
	}
	if ($cdata =~ /^Build-Conflicts:$dep_regex/mi) {
		$fields{'Build-Conflicts'} = $1;
	}
	if (! $binary_only && $cdata =~ /^Build-Depends-Indep:$dep_regex/mi) {
		$fields{'Build-Depends-Indep'} = $1;
	}
	if (! $binary_only && $cdata =~ /^Build-Conflicts-Indep:$dep_regex/mi) {
		$fields{'Build-Conflicts-Indep'} = $1;
	}

	return %fields;
}

1;