| 1 |
#!/usr/bin/perl |
| 2 |
# dh_builtusing - set dpkg-gencontrol substitution variables for the Built-Using field |
| 3 |
# SPDX-License-Identifier: GPL-3.0+ |
| 4 |
# (GNU General Public License, version 3 or later at your convenience) |
| 5 |
# Copyright 2023-2026 Nicolas Boulenguez <[email protected]> |
| 6 |
|
| 7 |
# This program is free software: you can redistribute it and/or |
| 8 |
# modify it under the terms of the GNU General Public License as |
| 9 |
# published by the Free Software Foundation, either version 3 of the |
| 10 |
# License, or (at your option) any later version. |
| 11 |
# This program is distributed in the hope that it will be useful, but |
| 12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# General Public License for more details. |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
use feature qw(signatures); |
| 19 |
use re '/amsx'; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
|
| 23 |
use Debian::Debhelper::Dh_Lib; |
| 24 |
use Dpkg::BuildProfiles 'get_build_profiles'; |
| 25 |
use Dpkg::Control::Info; |
| 26 |
use Dpkg::Deps; |
| 27 |
use English '-no_match_vars'; |
| 28 |
use Memoize; |
| 29 |
|
| 30 |
my $control_file = 'debian/control'; |
| 31 |
my $logorrheic_print = sub { }; |
| 32 |
init( |
| 33 |
options => { |
| 34 |
'c=s' => sub { |
| 35 |
warning '-c option is deprecated'; |
| 36 |
$control_file = $_[1]; |
| 37 |
}, |
| 38 |
'logorrheic' => sub { |
| 39 |
$dh{VERBOSE} = 1; |
| 40 |
$logorrheic_print = \&verbose_print; |
| 41 |
}, |
| 42 |
} |
| 43 |
); |
| 44 |
my $control = Dpkg::Control::Info->new($control_file); |
| 45 |
|
| 46 |
# pkg: a binary package that may |
| 47 |
# be produced by the current build, |
| 48 |
# use a dh_builtusing substitution variable. |
| 49 |
# dep: a binary package that may |
| 50 |
# match a dh_builtusing substitution variable, |
| 51 |
# be installed during the build, |
| 52 |
# belong to a Build-Depends field, |
| 53 |
# Variables disabled by restrictions are processed immediately. |
| 54 |
# Normal expansions are stored as [pkg, var, dep, first] in a todo list |
| 55 |
# so that dpkg-query runs at most once. |
| 56 |
# Then a second pass actually sets the variables. |
| 57 |
# $first is true when $dep was the first option in an alternative in |
| 58 |
# Build-Depends. This triggers a warning when it is not installed. |
| 59 |
my @todo; |
| 60 |
|
| 61 |
# Deps in the Build-Depends$suffix control field, filtered by the |
| 62 |
# architecture and profile restrictions. |
| 63 |
# The result of deps_parse is an instance of AND or Simple. |
| 64 |
# AND items are instances of OR or Simple. |
| 65 |
# OR items are instances of Simple. |
| 66 |
# This function returns an array of OR or Simple. |
| 67 |
sub build_depends : prototype($) ($suffix) { |
| 68 |
my $contents = $control->get_source->{"Build-Depends$suffix"}; |
| 69 |
$contents or return (); |
| 70 |
return deps_parse( |
| 71 |
$contents, |
| 72 |
reduce_restrictions => 1, |
| 73 |
build_profiles => [get_build_profiles], |
| 74 |
build_dep => 1, |
| 75 |
)->get_deps; |
| 76 |
} |
| 77 |
memoize('build_depends'); |
| 78 |
|
| 79 |
# A hash mapping an installed dep to a "SOURCE (= VERSION)" relation. |
| 80 |
sub source_version : prototype(@) (@binaries) { |
| 81 |
my $format = |
| 82 |
"\${db:Status-Abbrev}\${Package}:\${source:Package} (= \${source:Version})\n"; |
| 83 |
my @cmd = ( 'dpkg-query', '-Wf', $format, @binaries ); |
| 84 |
my @out = qx_cmd(@cmd); # _; |
| 85 |
$logorrheic_print->(" source_version: @cmd -> @out"); |
| 86 |
my %result; |
| 87 |
for (@out) { |
| 88 |
chomp; |
| 89 |
if (m/ ^ .i. ([^:]*) : (.*) /) { |
| 90 |
if ( exists $result{$1} and $result{$1} ne $2 ) { |
| 91 |
die "$1: distinct versions co-installed?\n"; |
| 92 |
} |
| 93 |
$logorrheic_print->(" $1: $2"); |
| 94 |
$result{$1} = $2; |
| 95 |
} |
| 96 |
} |
| 97 |
return \%result; |
| 98 |
} |
| 99 |
|
| 100 |
sub search_installed_packages : prototype($$$) ( $pkg, $var, $glob ) { |
| 101 |
$glob =~ y/DPS/.+*/; |
| 102 |
my $format = "\${db:Status-Abbrev}\${Package}\n"; |
| 103 |
my @cmd = ( 'dpkg-query', '-Wf', $format, $glob ); |
| 104 |
my @out = qx_cmd(@cmd); # _; |
| 105 |
$logorrheic_print->(" search_installed_packages: @cmd -> @out"); |
| 106 |
for (@out) { |
| 107 |
chomp; |
| 108 |
s/ ^ .i. // or next; |
| 109 |
push @todo, [ $pkg, $var, $_, 1 ]; |
| 110 |
warning "$var: please add $_ to Build-Depends."; |
| 111 |
} |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
my $RE_PATTERN = qr/ [[:lower:]\dS] [[:lower:]\dDPS-] + /; |
| 116 |
my $RE_ARCH = qr/ : [[:lower:]] [[:lower:]\d] + /; |
| 117 |
my $RE_CAPTURE = qr{ |
| 118 |
[$][{] |
| 119 |
( dh- (?:static)? builtusing: # var |
| 120 |
( $RE_PATTERN ) ( $RE_ARCH )? |
| 121 |
) |
| 122 |
[}] |
| 123 |
( [^,|]* ) # restrictions |
| 124 |
}; |
| 125 |
|
| 126 |
sub search_in_dependency_string : prototype($$) ( $pkg, $string ) { |
| 127 |
$logorrheic_print->(" dependency_string=|$string|"); |
| 128 |
while ( $string =~ m/$RE_CAPTURE/g ) { |
| 129 |
my ( $var, $pattern, $arch, $restrictions ) = ( $1, $2, $3, $4 ); |
| 130 |
if ($arch) { |
| 131 |
warning "$var: architecture qualifiers are deprecated"; |
| 132 |
} |
| 133 |
$logorrheic_print->(" v=$var p=$pattern r=|$restrictions|"); |
| 134 |
my $parsed = Dpkg::Deps::Simple->new("fake $restrictions"); |
| 135 |
if ( $parsed->{relation} ) { |
| 136 |
error("$var carries a version relation"); |
| 137 |
} |
| 138 |
|
| 139 |
if ( $parsed->arch_is_concerned(hostarch) |
| 140 |
and $parsed->profile_is_concerned( [get_build_profiles] ) ) |
| 141 |
{ |
| 142 |
my $regex = $pattern; |
| 143 |
$regex =~ s/ D /[.]/g; |
| 144 |
$regex =~ s/ P /[+]/g; |
| 145 |
$regex =~ s/ S /.*/g; |
| 146 |
my $suffix = package_is_arch_all($pkg) ? '-Indep' : '-Arch'; |
| 147 |
my $old_todo = @todo; |
| 148 |
for ( build_depends q{}, build_depends $suffix ) { # OR / Simple |
| 149 |
my $first = 1; # first option? |
| 150 |
for ( $_->get_deps ) { # Simple |
| 151 |
if ( $_->{package} =~ m/^$regex$/ ) { |
| 152 |
push @todo, [ $pkg, $var, $_->{package}, $first ]; |
| 153 |
} |
| 154 |
$first = 0; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
# If no build dependency matches, search installed packages. |
| 159 |
if ( $old_todo eq @todo ) { |
| 160 |
search_installed_packages( $pkg, $var, $pattern ); |
| 161 |
} |
| 162 |
if ( $old_todo eq @todo ) { |
| 163 |
error "$var: no match in (active) Build-Depends (or $suffix)."; |
| 164 |
} |
| 165 |
} |
| 166 |
else { |
| 167 |
verbose_print( |
| 168 |
"In package $pkg, substvar $var += disabled-by-restriction"); |
| 169 |
addsubstvar( $pkg, $var, 'disabled-by-restriction (= 0)' ); |
| 170 |
} |
| 171 |
} |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
# Only search in uncommented right hand sides. |
| 176 |
sub search_in_substvars_file : prototype($) ($pkg) { |
| 177 |
my $path = 'debian/' . pkgext($pkg) . 'substvars'; |
| 178 |
if ( -e $path ) { |
| 179 |
$logorrheic_print->("substvars_file=$path"); |
| 180 |
my $old_todo = @todo; |
| 181 |
open my $file, q{<}, $path or error("open $path failed: $ERRNO"); |
| 182 |
while (<$file>) { |
| 183 |
if (m/ ^ [[:alnum:]] [[:alnum:]:-]* [?]? = (.*) /) { |
| 184 |
search_in_dependency_string( $pkg, $1 ); |
| 185 |
} |
| 186 |
} |
| 187 |
close $file or error("close $path failed: $ERRNO"); |
| 188 |
if ( $old_todo ne @todo ) { |
| 189 |
warning("dh-builtusing variables in $path are deprecated."); |
| 190 |
} |
| 191 |
} |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
for my $pkg ( @{ $dh{DOPACKAGES} } ) { |
| 196 |
|
| 197 |
# Parse the substvars file before extending it. |
| 198 |
search_in_substvars_file($pkg); |
| 199 |
for my $field_name ( 'Built-Using', 'Static-Built-Using' ) { |
| 200 |
$logorrheic_print->("pkg=$pkg field=$field_name"); |
| 201 |
my $field_contents = $control->get_pkg_by_name($pkg)->{$field_name}; |
| 202 |
if ($field_contents) { |
| 203 |
search_in_dependency_string( $pkg, $field_contents ); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
if (@todo) { |
| 208 |
my $relations = source_version( map { ${$_}[2] } @todo ); |
| 209 |
for (@todo) { |
| 210 |
my ( $pkg, $var, $dep, $first ) = @{$_}; |
| 211 |
if ( exists $relations->{$dep} ) { |
| 212 |
$logorrheic_print->("$pkg.$var += $relations->{$dep}"); |
| 213 |
addsubstvar $pkg, $var, $relations->{$dep}; |
| 214 |
} |
| 215 |
elsif ($first) { |
| 216 |
warning "$var: $dep is not installed"; |
| 217 |
} |
| 218 |
else { |
| 219 |
$logorrheic_print->("$var: $dep is not installed"); |
| 220 |
} |
| 221 |
} |
| 222 |
} |