| 1 |
#!/usr/bin/perl -w |
| 2 |
use Text::Trim qw(trim); |
| 3 |
use List::MoreUtils qw(uniq); |
| 4 |
|
| 5 |
undef $/; |
| 6 |
my $DEB_HOST_MULTIARCH = |
| 7 |
( split /\n/, qx/dpkg-architecture -qDEB_HOST_MULTIARCH/ )[0]; |
| 8 |
my $DEB_HOST_ARCH = ( split /\n/, qx/dpkg-architecture -qDEB_HOST_ARCH/ )[0]; |
| 9 |
my $DEB_HOST_ARCH_BITS = |
| 10 |
( split /\n/, qx/dpkg-architecture -qDEB_HOST_ARCH_BITS/ )[0]; |
| 11 |
my $DEB_HOST_ARCH_CPU = |
| 12 |
( split /\n/, qx/dpkg-architecture -qDEB_HOST_ARCH_CPU/ )[0]; |
| 13 |
my $DEB_HOST_MULTIARCH_CPU = $DEB_HOST_MULTIARCH; |
| 14 |
$DEB_HOST_MULTIARCH_CPU =~ s/^([^-]+?)-.*$/$1/g; |
| 15 |
|
| 16 |
my $version = ( split /\n/, qx/dpkg-parsechangelog --show-field Version/ )[0]; |
| 17 |
|
| 18 |
sub file_replace { |
| 19 |
my ( $f, $name, $lcname ) = @_; |
| 20 |
my $foutname = $f; |
| 21 |
$foutname =~ s/[@]NAME[@]/$name/; |
| 22 |
$foutname =~ s/[@]LCNAME[@]/$lcname/; |
| 23 |
open IN, "<debian/$f.in" or die "Can't read debian/$f.in: $!\n"; |
| 24 |
local $_ = <IN>; |
| 25 |
close IN; |
| 26 |
|
| 27 |
s/[@]NAME[@]/$name/g; |
| 28 |
s/[@]LCNAME[@]/$lcname/g; |
| 29 |
s/[@]DEB_HOST_MULTIARCH[@]/$DEB_HOST_MULTIARCH/g; |
| 30 |
open OUT, ">debian/$foutname" |
| 31 |
or die "Can't write debian/$foutname: $!\n"; |
| 32 |
print OUT; |
| 33 |
close OUT; |
| 34 |
} |
| 35 |
|
| 36 |
my $control; |
| 37 |
|
| 38 |
my $ctemplate; |
| 39 |
my $cfilename = 'test-@[email protected]'; |
| 40 |
open( my $fctemplate, '<', $cfilename ) or die "cannot open file $cfilename"; |
| 41 |
{ |
| 42 |
local $/ = undef; |
| 43 |
$ctemplate = <$fctemplate>; |
| 44 |
} |
| 45 |
close($fctemplate); |
| 46 |
|
| 47 |
# dpkg-shlibdeps now looks inside, but is ok with an empty file. |
| 48 |
system("touch debian/control"); |
| 49 |
|
| 50 |
my @control_in; |
| 51 |
open IN, "<debian/control.d/control.in" |
| 52 |
or die "Can't read debian/control.d/control.in: $!\n"; |
| 53 |
$_ = <IN>; |
| 54 |
@control_in = grep !/^\s*$/s, split /\n\s*\n/s; |
| 55 |
close IN; |
| 56 |
|
| 57 |
my @controltest_in; |
| 58 |
open IN, "<debian/tests/control.in" |
| 59 |
or die "Can't read debian/tests/control.in: $!\n"; |
| 60 |
$_ = <IN>; |
| 61 |
@controltest_in = grep !/^\s*$/s, split /\n\s*\n/s; |
| 62 |
close IN; |
| 63 |
|
| 64 |
open ISA_LIST, "<isa-list" or die "Can't read isa-list: $!\n"; |
| 65 |
$_ = <ISA_LIST>; |
| 66 |
close ISA_LIST; |
| 67 |
my %archlist; |
| 68 |
|
| 69 |
my @structs; |
| 70 |
foreach my $block ( split /\n\s*\n/s ) { |
| 71 |
my $lastfield = undef; |
| 72 |
my %data = (); |
| 73 |
foreach ( split /\n/s, $block ) { |
| 74 |
next if /^#.*/; |
| 75 |
next if /^\s*$/; |
| 76 |
if (/^([^\s:]+?)\s*:\s*(.*?)\s*$/) { |
| 77 |
$key = $1; |
| 78 |
$value = $2 // ''; |
| 79 |
$data{"$key"} = "$value"; |
| 80 |
$lastfield = "$key"; |
| 81 |
} |
| 82 |
elsif (/^(\s+\S.*?)\s*$/) { |
| 83 |
$data{$lastfield} .= "\n$1"; |
| 84 |
} |
| 85 |
|
| 86 |
next; |
| 87 |
} |
| 88 |
if (%data) { |
| 89 |
$data{"Package"} = "no" if !exists( $data{"Package"} ); |
| 90 |
push @structs, \%data; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
my $controltest = ''; |
| 95 |
|
| 96 |
for (@structs) { |
| 97 |
my %entry = %$_; |
| 98 |
$archlist{ trim($entry{'Architecture'}) } = 1; |
| 99 |
my $name = $entry{'Name'}; |
| 100 |
my $lcname = lc($name); |
| 101 |
$name =~ /^[a-zA-Z0-9\.+_-]+$/ or die "Bad package/isa name: \"$name\".\n"; |
| 102 |
my $description = $entry{'Description'}; |
| 103 |
$description =~ s/\A\s*\n+//m; |
| 104 |
$description =~ s/\A\s+//m; |
| 105 |
$provides = join ',', map { $_=trim(lc($_)); "$_-support (= $version)" } split /,/, ($entry{'Provides'} // ''); |
| 106 |
$renamed = join ',', map { $_=trim($_); "$_" } split /,/, ($entry{'Renamed'} // ''); |
| 107 |
|
| 108 |
if ( $entry{'Package'} =~ /yes/i ) { |
| 109 |
my @c = @control_in; |
| 110 |
foreach (@c) { |
| 111 |
s/[@]NAME[@]/$name/g; |
| 112 |
s/[@]LCNAME[@]/$lcname/g; |
| 113 |
s/[@]ARCHITECTURE[@]/$entry{'Architecture'}/g; |
| 114 |
s/[@]DEB_HOST_MULTIARCH[@]/$DEB_HOST_MULTIARCH/g; |
| 115 |
s/[@]DESCRIPTION[@]/$description/g; |
| 116 |
s/[@]PROVIDES[@]/$provides/g; |
| 117 |
s/[@]BREAKS[@]/$renamed/g; |
| 118 |
s/[@]REPLACES[@]/$renamed/g; |
| 119 |
s/[@]CONFLICTS[@]/$renamed/g; |
| 120 |
$control .= "\n" . $_; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
open C, '>', "test-$name.c"; |
| 125 |
my $test = $entry{'Test'} // "return __builtin_cpu_supports(\"$lcname\");"; |
| 126 |
my $cfile = $ctemplate; |
| 127 |
$cfile =~ s/[@]TEST[@]/$test/gm; |
| 128 |
$cfile =~ s/[@]NAME[@]/$name/g; |
| 129 |
$cfile =~ s/[@]VERSION[@]/$version/g; |
| 130 |
my $cdescription = $description; |
| 131 |
$cdescription =~ s/"/\"/gm; |
| 132 |
$cdescription =~ s/^\s//gm; |
| 133 |
$cdescription =~ s/^[.]\n/\n/gm; |
| 134 |
$cdescription =~ s/\n/\\n\"\n \"/gm; |
| 135 |
$cfile =~ s/[@]DESCRIPTION[@]/$description/g; |
| 136 |
$cfile =~ s/[@]CDESCRIPTION[@]/$cdescription/g; |
| 137 |
|
| 138 |
print C $cfile; |
| 139 |
close C; |
| 140 |
|
| 141 |
for my $qemutype (qw(good bad)) { |
| 142 |
my $qemu = $qemutype; |
| 143 |
if ( exists $entry{ 'qemu-' . $qemu } ) { |
| 144 |
my $finname = 'qemu-' . '@QEMUTYPE@-@NAME@'; |
| 145 |
my $foutname = $finname; |
| 146 |
$foutname =~ s/[@]NAME[@]/$name/; |
| 147 |
$foutname =~ s/[@]QEMUTYPE[@]/$qemutype/; |
| 148 |
open IN, "<$finname" or die "Can't read $finname: $!\n"; |
| 149 |
local $_ = <IN>; |
| 150 |
close IN; |
| 151 |
|
| 152 |
s/[@]NAME[@]/$name/g; |
| 153 |
s/[@]DEB_HOST_MULTIARCH[@]/$DEB_HOST_MULTIARCH/g; |
| 154 |
s/[@]DEB_HOST_MULTIARCH_CPU[@]/$DEB_HOST_MULTIARCH_CPU/g; |
| 155 |
s/[@]DEB_HOST_ARCH[@]/$DEB_HOST_ARCH/g; |
| 156 |
s/[@]DEB_HOST_ARCH_BITS[@]/$DEB_HOST_ARCH_BITS/g; |
| 157 |
s/[@]DEB_HOST_ARCH_CPU[@]/$DEB_HOST_ARCH_CPU/g; |
| 158 |
if ( $qemutype =~ 'good$' ) { |
| 159 |
s/[@]QEMU[@]/$entry{'qemu-good'}/g; |
| 160 |
} |
| 161 |
else { |
| 162 |
s/[@]QEMU[@]/$entry{'qemu-bad'}/g; |
| 163 |
} |
| 164 |
|
| 165 |
open OUT, ">$foutname" |
| 166 |
or die "Can't write $foutname: $!\n"; |
| 167 |
print OUT; |
| 168 |
close OUT; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if ( exists $entry{'qemu-good'} and exists $entry{'qemu-bad'} ) { |
| 173 |
my $qemu_arch = $entry{'qemu-Architecture'} // $entry{'Architecture'}; |
| 174 |
my @c = @controltest_in; |
| 175 |
foreach (@c) { |
| 176 |
s/[@]NAME[@]/$name/g; |
| 177 |
s/[@]LCNAME[@]/$lcname/g; |
| 178 |
s/[@]ARCHITECTURE[@]/$qemu_arch/g; |
| 179 |
s/[@]DEB_HOST_MULTIARCH[@]/$DEB_HOST_MULTIARCH/g; |
| 180 |
s/[@]DESCRIPTION[@]/$description/g; |
| 181 |
$controltest .= $_ . "\n"; |
| 182 |
} |
| 183 |
file_replace( 'tests/test-@NAME@', $name, $lcname ); |
| 184 |
chmod 0777, "debian/tests/test-$name"; |
| 185 |
} |
| 186 |
|
| 187 |
foreach ( |
| 188 |
qw(@[email protected] @[email protected] @[email protected] @[email protected]) |
| 189 |
) |
| 190 |
{ |
| 191 |
file_replace( $_, $name, $lcname ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
my %all_architectures_hash = map {$_ => 1} uniq(sort(split(/ /,join( ' ', sort( keys %archlist ))))); |
| 196 |
if (exists $all_architectures_hash{'any-arm'}) { |
| 197 |
$all_architectures_hash{'armel'} = 0; |
| 198 |
$all_architectures_hash{'armhf'} = 0; |
| 199 |
} |
| 200 |
while ( my ( $key, $val ) = each %all_architectures_hash ) { |
| 201 |
delete $all_architectures_hash{$key} if not ($val // 0); |
| 202 |
} |
| 203 |
my $all_architectures = join(' ',sort(keys(%all_architectures_hash))); |
| 204 |
|
| 205 |
my $control_base; |
| 206 |
my $filename = "debian/control.d/control-base.in"; |
| 207 |
open( my $fh, '<', $filename ) or die "cannot open file $filename"; |
| 208 |
{ |
| 209 |
local $/ = undef; |
| 210 |
$control_base = <$fh>; |
| 211 |
} |
| 212 |
close($fh); |
| 213 |
$control_base =~ s/[@]ALL_ARCHITECTURES[@]/$all_architectures/g; |
| 214 |
$control = $control_base . $control; |
| 215 |
|
| 216 |
open CONTROL, ">debian/control" or die "Can't write to debian/control: $!\n"; |
| 217 |
print CONTROL $control; |
| 218 |
close CONTROL; |
| 219 |
|
| 220 |
open CONTROLTEST, ">debian/tests/control" |
| 221 |
or die "Can't write to debian/tests/control: $!\n"; |
| 222 |
print CONTROLTEST $controltest; |
| 223 |
close CONTROLTEST; |