frosted: add support for stm32f407 discovery board
[openadk.git] / scripts / make-module-ipkgs.sh
blob9255d79d7b3060af6de706ce4a80f6e2dd34afc2
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 # start with empty directory, avoid leftovers after version change
69 rm -rf ${BUILD_DIR}/linux-modules
71 for modname in ${!modpaths[@]}; do
72 find_modlevel $modname
74 ctrlfile=${BUILD_DIR}/kmod-control/kmod-${modname}.control
75 ipkgdir=${BUILD_DIR}/linux-modules/ipkg/$modname
77 cat >$ctrlfile <<-EOF
78 Package: $(pkgname $modname)
79 Priority: optional
80 Section: sys
81 Description: kernel module $modname
82 EOF
83 bash $(dirname $0)/make-ipkg-dir.sh $ipkgdir $ctrlfile $VER $ARCH
85 depline="kernel ($VER)"
86 for m in ${moddeps[$modname]}; do
87 depline+=", $(pkgname ${m})"
88 done
89 echo "Depends: $depline" >>${ipkgdir}/CONTROL/control
90 mkdir -p ${ipkgdir}/lib/modules/${VER}
91 cp ${modpaths[$modname]} ${ipkgdir}/lib/modules/${VER}
92 cat >${ipkgdir}/CONTROL/postinst <<EOF
93 #!/bin/sh
94 if [ -z \${IPKG_INSTROOT} ]; then
95 . /etc/functions.sh
96 load_modules /etc/modules.d/${modlevels[$modname]}-$modname
98 EOF
99 chmod 0755 ${ipkgdir}/CONTROL/postinst
100 mkdir -p ${ipkgdir}/etc/modules.d
101 echo $modname >${ipkgdir}/etc/modules.d/${modlevels[$modname]}-$modname
102 ${PKG_BUILD} ${ipkgdir} ${PACKAGE_DIR} || exit 1
103 done