sh2-nommu/j2: add better support and add required patches from musl-cross-make
[openadk.git] / scripts / make-module-ipkgs.sh
blobe50340c0ad0d72750436ffcf33b5c6998d24999b
1 #!/usr/bin/env bash
3 # make-module-ipkgs.sh - scan through modules directory and create a package
4 # for each of them automatically.
6 # Copyright (C) 2015 - Phil Sutter <phil@nwl.cc>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # Usage:
23 # $0 <ARCH> <KERNEL_VERSION> <LINUX_BUILD_DIR> <pkg-build-cmd> <PACKAGE_DIR>
25 ARCH="$1"
26 VER="$2"
27 BUILD_DIR="$3"
28 PKG_BUILD="$4"
29 PACKAGE_DIR="$5"
31 # declare associative arrays
32 declare -A modpaths moddeps modlevels
34 # recursively find a level for given module which is high enough so all
35 # dependencies are in a lower level
36 find_modlevel() { # (modname)
37 local dep level=0
38 for dep in ${moddeps[$1]}; do
39 [[ -n "${modlevels[$dep]}" ]] || find_modlevel $dep
40 [[ ${modlevels[$dep]} -lt $level ]] || level=$((modlevels[$dep] + 1))
41 done
42 modlevels[$1]=$level
45 # sanitize modname, ipkg does not allow uppercase or underscores
46 pkgname() { # (modname)
47 tr 'A-Z_' 'a-z-' <<< "kmod-$1"
50 for modpath in $(find ${BUILD_DIR}/modules -name \*.ko | xargs); do
51 modname="$(basename $modpath .ko)"
52 moddep="$(strings $modpath | awk -F= '/^depends=/{print $2}' | sed 's/,/ /g')"
53 modpaths[$modname]="$modpath"
54 moddeps[$modname]="$moddep"
55 done
57 #echo "modpaths:"
58 #for modname in ${!modpaths[@]}; do
59 # echo "$modname: ${modpaths[$modname]}"
60 #done
61 #echo
62 #echo "moddeps:"
63 #for modname in ${!moddeps[@]}; do
64 # echo "$modname: ${moddeps[$modname]}"
65 #done
66 #echo
68 for modname in ${!modpaths[@]}; do
69 find_modlevel $modname
71 ctrlfile=${BUILD_DIR}/kmod-control/kmod-${modname}.control
72 ipkgdir=${BUILD_DIR}/linux-modules/ipkg/$modname
74 cat >$ctrlfile <<-EOF
75 Package: $(pkgname $modname)
76 Priority: optional
77 Section: sys
78 Description: kernel module $modname
79 EOF
80 sh $(dirname $0)/make-ipkg-dir.sh $ipkgdir $ctrlfile $VER $ARCH
82 depline="kernel ($VER)"
83 for m in ${moddeps[$modname]}; do
84 depline+=", $(pkgname ${m})"
85 done
86 echo "Depends: $depline" >>${ipkgdir}/CONTROL/control
87 mkdir -p ${ipkgdir}/lib/modules/${VER}
88 cp ${modpaths[$modname]} ${ipkgdir}/lib/modules/${VER}
89 cat >${ipkgdir}/CONTROL/postinst <<EOF
90 #!/bin/sh
91 if [ -z \${IPKG_INSTROOT} ]; then
92 . /etc/functions.sh
93 load_modules /etc/modules.d/${modlevels[$modname]}-$modname
95 EOF
96 chmod 0755 ${ipkgdir}/CONTROL/postinst
97 mkdir -p ${ipkgdir}/etc/modules.d
98 echo $modname >${ipkgdir}/etc/modules.d/${modlevels[$modname]}-$modname
99 env ${PKG_BUILD} ${ipkgdir} ${PACKAGE_DIR} || exit 1
100 done