| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Copy modules into the right directories in preparation for building udebs. |
| 4 |
# This script is named after the its counterpart in the original |
| 5 |
# kernel-image-di package by Joey Hess <[email protected]>. |
| 6 |
# |
| 7 |
# Copyright (c) 2001-2002 Herbert Xu <[email protected]> |
| 8 |
# |
| 9 |
# Usage: copy-modules version flavour installedname |
| 10 |
use strict; |
| 11 |
use warnings; |
| 12 |
use autodie; |
| 13 |
|
| 14 |
use Cwd qw(getcwd realpath); |
| 15 |
use File::Basename qw(dirname); |
| 16 |
use File::Temp (); |
| 17 |
use FileHandle; |
| 18 |
use List::Util qw(uniqstr); |
| 19 |
|
| 20 |
use lib dirname(__FILE__); |
| 21 |
use KernelWedge qw(read_package_lists for_each_package package_enabled); |
| 22 |
|
| 23 |
my $prefix = '/usr'; |
| 24 |
my $kw_bin_dir = dirname(__FILE__); |
| 25 |
|
| 26 |
my $version = "${ARGV[0]}-${ARGV[1]}"; |
| 27 |
my $flavour = $ARGV[1]; |
| 28 |
my $installedname = $ARGV[2]; |
| 29 |
my $configdir = realpath($ENV{KW_CONFIG_DIR}); |
| 30 |
my $arch = `dpkg-architecture -qDEB_HOST_ARCH`; |
| 31 |
chomp $arch; |
| 32 |
my $home = getcwd(); |
| 33 |
|
| 34 |
# SOURCEDIR must be set externally to control where to copy from. |
| 35 |
my $moddir = "$ENV{SOURCEDIR}/lib/modules/$installedname"; |
| 36 |
exit 0 unless -d $moddir; |
| 37 |
|
| 38 |
# The directory of modules lists to use. |
| 39 |
my $modlistdir; |
| 40 |
if (-d "$configdir/modules/$arch-$flavour") { |
| 41 |
$modlistdir = "$configdir/modules/$arch-$flavour"; |
| 42 |
} elsif (-d "$configdir/modules/$flavour") { |
| 43 |
$modlistdir = "$configdir/modules/$flavour"; |
| 44 |
} else { |
| 45 |
$modlistdir = "$configdir/modules/$arch"; |
| 46 |
} |
| 47 |
|
| 48 |
# get module dependencies from modules.dep |
| 49 |
my $depmod_pipe; |
| 50 |
{ |
| 51 |
local $ENV{'PATH'} = "/usr/sbin:/sbin:${ENV{PATH}}"; |
| 52 |
open($depmod_pipe, |
| 53 |
($ENV{'DEPMOD'} || 'depmod') |
| 54 |
. " -b ${ENV{SOURCEDIR}} $installedname -n |"); |
| 55 |
} |
| 56 |
my %mod_deps; |
| 57 |
while (<$depmod_pipe>) { |
| 58 |
next if /^(#|alias |softdep )/; |
| 59 |
chomp; |
| 60 |
my @words = split(" "); |
| 61 |
if ($words[0] =~ /:$/) { |
| 62 |
$words[0] =~ s/:$//; |
| 63 |
my $module = shift @words; |
| 64 |
push @{$mod_deps{$module}}, @words; |
| 65 |
} elsif (@words == 3 && $words[2] =~ /^[bc]\d+:\d+$/) { |
| 66 |
# Device node dependency; ignore it |
| 67 |
} else { |
| 68 |
STDERR->print("W: Unrecognised depmod line $_\n"); |
| 69 |
} |
| 70 |
} |
| 71 |
close($depmod_pipe); |
| 72 |
|
| 73 |
unless (keys %mod_deps) { |
| 74 |
print STDERR "No module interdependencies found. This probably means your modules.dep is broken.\n"; |
| 75 |
exit 1; |
| 76 |
} |
| 77 |
|
| 78 |
sub add_module_deps { |
| 79 |
my ($mods, $excl_mods) = @_; |
| 80 |
my @work = keys %$mods; |
| 81 |
my $i = 0; |
| 82 |
|
| 83 |
while ($i <= $#work) { |
| 84 |
my @deps = (grep { !$mods->{$_} && !$excl_mods->{$_} } |
| 85 |
@{$mod_deps{$work[$i]}}); |
| 86 |
push @work, @deps; |
| 87 |
$mods->{$_} = 1 for @deps; |
| 88 |
++$i; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
# generate module interrelationships from package-list file |
| 93 |
my $pkg_deps_file = new File::Temp; |
| 94 |
my %pkg_deps_direct; |
| 95 |
my $versions = [[$arch, '-', $flavour]]; |
| 96 |
my $packages = read_package_lists(); |
| 97 |
for_each_package($packages, $versions, sub { |
| 98 |
my (undef, undef, undef, $package) = @_; |
| 99 |
my $pkg_name = $package->("Package"); |
| 100 |
my @depends = split(", ", $package->("Depends") || ""); |
| 101 |
|
| 102 |
$pkg_deps_file->print("$pkg_name\t$pkg_name\n"); |
| 103 |
foreach my $dep (@depends) { |
| 104 |
# Skip depends that are not built for this |
| 105 |
# architecture. |
| 106 |
next unless package_enabled($packages, $dep, $arch, $flavour); |
| 107 |
push @{$pkg_deps_direct{$pkg_name}}, $dep; |
| 108 |
$pkg_deps_file->print("$pkg_name\t$dep\n"); |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
my %pkg_deps_full; |
| 113 |
my %pkg_mods; |
| 114 |
# loop over all udebs, sort that all dependent modules are processed first |
| 115 |
open(my $deps_sort_pipe, "tsort < $pkg_deps_file | tac |"); |
| 116 |
while (<$deps_sort_pipe>) { |
| 117 |
chomp; |
| 118 |
my $i = $_; |
| 119 |
|
| 120 |
# Get all module package dependencies (direct and indirect) |
| 121 |
my @pkg_deps; |
| 122 |
for (@{$pkg_deps_direct{$i}}) { |
| 123 |
push @pkg_deps, $_, @{$pkg_deps_full{$_}}; |
| 124 |
} |
| 125 |
$pkg_deps_full{$i} = [uniqstr(sort(@pkg_deps))]; |
| 126 |
|
| 127 |
# Get all modules present in package dependencies |
| 128 |
my %excl_mods; |
| 129 |
for (@{$pkg_deps_full{$i}}) { |
| 130 |
for (keys %{$pkg_mods{$_}}) { |
| 131 |
$excl_mods{$_} = 1 |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
# preprocess file, handle includes and excludes |
| 136 |
open(my $preproc_pipe, |
| 137 |
"$kw_bin_dir/preprocess $modlistdir $i $moddir |"); |
| 138 |
my %mods; |
| 139 |
while (<$preproc_pipe>) { |
| 140 |
chomp; |
| 141 |
$mods{$_} = 1; |
| 142 |
} |
| 143 |
close($preproc_pipe); |
| 144 |
|
| 145 |
# Add dependent modules which are not in a package dependency |
| 146 |
add_module_deps(\%mods, \%excl_mods); |
| 147 |
|
| 148 |
$pkg_mods{$i} = \%mods; |
| 149 |
|
| 150 |
if (keys %mods) { |
| 151 |
# copy kernel modules to package build dir |
| 152 |
open(my $cpio_pipe, |
| 153 |
"| cd $moddir && cpio -pd --quiet $home/debian/$i-$version-di$prefix/lib/modules/$installedname"); |
| 154 |
$cpio_pipe->print("$_\n") for keys %mods; |
| 155 |
close($cpio_pipe); |
| 156 |
|
| 157 |
open(my $postinst_fh, '>', "debian/$i-$version-di.postinst"); |
| 158 |
$postinst_fh->print(<<~EOF); |
| 159 |
#!/bin/sh -e |
| 160 |
depmod $installedname |
| 161 |
EOF |
| 162 |
close($postinst_fh); |
| 163 |
} |
| 164 |
} |
| 165 |
close($deps_sort_pipe); |
| 166 |
|
| 167 |
exit 0; |