+# build a complete dependency list for each module and make sure it
+# is kept in order proper order
+my $mod2 = {};
+sub maybe_unshift
+{
+ my ($array, $ele) = @_;
+ # chop off the leading path /lib/modules/<kver>/ as modprobe
+ # will handle relative paths just fine
+ $ele =~ s:^/lib/modules/[^/]*/::;
+ foreach (@{$array}) {
+ if ($_ eq $ele) {
+ return;
+ }
+ }
+ unshift (@{$array}, $ele);
+}
+sub add_mod_deps
+{
+ my ($depth, $mod, $mod2, $module, $this_module) = @_;
+
+ $depth .= " ";
+ warn "${depth}loading deps of module: $this_module\n" if $verbose;
+ if (length($depth) > 50) {
+ die "too much recursion (circular dependencies in modules?)";
+ }
+
+ foreach my $md (keys %{$mod->{$this_module}}) {
+ add_mod_deps ($depth, $mod, $mod2, $module, $md);
+ warn "${depth} outputting $md\n" if $verbose;
+ maybe_unshift (\@{$$mod2->{$module}}, $md);
+ }
+
+ if (!%{$mod->{$this_module}}) {
+ warn "${depth} no deps\n" if $verbose;
+ }
+}
+foreach my $module (keys %$mod) {
+ warn "filling out module: $module\n" if $verbose;
+ @{$mod2->{$module}} = ();
+ add_mod_deps ("", $mod, \$mod2, $module, $module);
+}
+