]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - debian/scripts/dkms-build
67566a47df9a676e0e76805413fefd247c7afcc1
[mirror_ubuntu-kernels.git] / debian / scripts / dkms-build
1 #!/bin/sh
2 set -e
3
4 dkms_dir="$1"
5 abi_flavour="$2"
6 sign="$3"
7 pkgname="$4"
8 pkgdir="$5"
9 dbgpkgdir="$6"
10 package="$7"
11 shift 7
12
13 here=$(dirname "$(readlink -f "${0}")")
14
15 srcdir=$(pwd)
16 cd "$dkms_dir" || exit 1
17
18 built_using_record()
19 {
20 local subst="$1"
21 local built_using="$2"
22 if [ ! -f "$subst" ]; then
23 touch "$subst"
24 fi
25 if ! grep -q -s "^linux:BuiltUsing=" "$subst"; then
26 echo "linux:BuiltUsing=" >>"$subst"
27 fi
28 sed -i -e "s/^\(linux:BuiltUsing=.*\)/\1$built_using, /" "$subst"
29 }
30
31 # ABI: returns present in $? and located path in lpackage_path when found.
32 package_present()
33 {
34 for lpackage_path in "$1"_*.deb
35 do
36 break
37 done
38 [ -f "$lpackage_path" ]
39 }
40
41 # Download and extract the DKMS package -- note there may be more
42 # than one package to install.
43 for package_path in "$@"
44 do
45 package_file=$(basename "$package_path")
46 echo "II: dkms-build downloading $package ($package_file)"
47 rpackage=$( echo "$package_path" | sed -e 's@.*/@@' -e 's@_.*@@' )
48 lpackage=$( echo "$rpackage" | sed -e 's@=.*@@' )
49
50 while true
51 do
52 if package_present "$lpackage"; then
53 break
54 fi
55 case "$package_path" in
56 pool/*)
57 # Attempt download from the launchpad librarian first.
58 "$here/file-downloader" "https://launchpad.net/ubuntu/+archive/primary/+files/$package_file" || true
59 if package_present "$lpackage"; then
60 break
61 fi
62
63 # Download from the available pools.
64 for pool in $( grep -h '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/*.list | awk '{print $2}' | sort -u )
65 do
66 if package_present "$lpackage"; then
67 break
68 fi
69 url="$pool/$package_path"
70 "$here/file-downloader" "$url" && break || true
71 # No components in PPAs.
72 url=$(echo "$url" | sed -e 's@/pool/[^/]*/@/pool/main/@')
73 "$here/file-downloader" "$url" && break || true
74 done
75 ;;
76 http*:*)
77 "$here/file-downloader" "$package_path"
78 ;;
79 */*)
80 cp -p "$package_path" .
81 ;;
82 *)
83 apt-get download "$rpackage"
84 ;;
85 esac
86 break
87 done
88 if ! package_present "$lpackage"; then
89 echo "EE: $lpackage not found"
90 exit 1
91 fi
92
93 dpkg -x "$lpackage"_*.deb "$package"
94
95 lversion=$( echo "$lpackage_path" | sed -e 's@.*/@@' -e 's@_[^_]*$@@' -e 's@.*_@@')
96 #built_using_record "$srcdir/debian/$pkgname.substvars" "$built_using$lpackage (= $lversion)"
97 done
98
99 # Pick out the package/version from the dkms.conf.
100 for dkms_conf in "$package/usr/src"/*/"dkms.conf"
101 do
102 break
103 done
104
105 # It seems some packages have a # in the name which works fine if the
106 # package is installed directly, but not so much if we build it out
107 # of the normal location.
108 sed -i -e '/^PACKAGE_NAME=/ s/#//g' "$dkms_conf"
109
110 # Run any dkms-package specfic configuration steps
111 dkms_config_specific="$srcdir/$0-configure--$package"
112 dkms_config_generic=$(echo "$dkms_config_specific" | sed -e 's/-[0-9][0-9]*$/-N/')
113 for dkms_config in "$dkms_config_specific" "$dkms_config_generic"
114 do
115 if [ -z "$dkms_config" -o ! -e "$dkms_config" ]; then
116 continue
117 fi
118 echo "II: dkms-build-configure $(basename "$dkms_config") found, executing"
119 "$dkms_config" \
120 "$srcdir" \
121 "$dkms_conf" \
122 "$dkms_dir" \
123 "$abi_flavour" \
124 "$sign" \
125 "$pkgname" \
126 "$pkgdir" \
127 "$dbgpkgdir" \
128 "$package" \
129 "$@" || exit 1
130 break
131 done
132
133 cat - <<'EOF' >>"$dkms_conf"
134 POST_BUILD="ubuntu-save-objects ${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build ${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/objects $POST_BUILD"
135 EOF
136 ubuntu_script="$(dirname "$dkms_conf")/ubuntu-save-objects"
137 cat - <<'EOF' >"$ubuntu_script"
138 #!/bin/sh
139 from="$1"
140 to="$2"
141 script="$3"
142 shift 2
143
144 # Copy the objects.
145 echo "II: copying objects to '$to'"
146 mkdir -p "$to"
147 (cd "$from" && find -name \*.o -o -name \*.o.ur-\* -o -name \*.mod | cpio -Lpd "$to")
148
149 # Call the original post_install script if there is one.
150 [ "$script" = '' ] && exit 0
151
152 shift
153 exec "$(dirname "$0")/$script" "$@"
154 EOF
155 chmod +x "$ubuntu_script"
156 dkms_package=$( sed -ne 's/PACKAGE_NAME="\(.*\)"/\1/p' "$dkms_conf" )
157 dkms_version=$( sed -ne 's/PACKAGE_VERSION="\(.*\)"/\1/p' "$dkms_conf" )
158
159 # Build the DKMS binaries.
160 echo "II: dkms-build building $package"
161 fakeroot=""
162 [ $(id -u) -ne 0 ] && fakeroot="/usr/bin/fakeroot"
163 rc=0
164 $fakeroot /usr/sbin/dkms build --no-prepare-kernel --no-clean-kernel \
165 -k "$abi_flavour" ${ARCH:+-a $ARCH} \
166 --sourcetree "$dkms_dir/source" \
167 --dkmstree "$dkms_dir/build" \
168 --kernelsourcedir "$dkms_dir/headers/linux-headers-$abi_flavour" \
169 "$dkms_conf" || rc=1
170
171 # Find the log and add it to our own.
172 for log in "$dkms_dir/build/$dkms_package/$dkms_version/$abi_flavour"/*/"log/make.log" "$dkms_dir/build/$dkms_package/$dkms_version/build/make.log"
173 do
174 [ -f "$log" ] && break
175 done
176 sed -e "s@$dkms_dir@<<DKMSDIR>>@g" <"$log"
177
178 # If this build failed then exit here.
179 [ "$rc" != 0 ] && exit "$rc"
180
181 # Install the modules with debug symbols we possibly built,
182 # and strip the original modules for the next install step.
183 if [ -n "$dbgpkgdir" ]; then
184 dbgpkgdir="$dbgpkgdir/$package"
185 echo "II: dkms-build installing $package into $dbgpkgdir (debug symbols)"
186 install -d "$dbgpkgdir"
187 find "$dkms_dir/build/$dkms_package/$dkms_version/$abi_version" -name \*.ko |
188 while read module; do
189 vmodule=$( basename "$module" )
190
191 # Check for '.debug_info' section in order to copy module.
192 # Useful if debug symbols are requested but not built for
193 # any reason (including not yet supported by DKMS package).
194 # Strip module just in case even if section isn't present.
195 if ${CROSS_COMPILE}objdump -h -j '.debug_info' "$module" >/dev/null 2>&1
196 then
197 echo "copying $vmodule"
198 cp "$module" "$dbgpkgdir"
199 else
200 echo "ignoring $vmodule (missing debug symbols)"
201 fi
202
203 # Just 'strip -g' as '/usr/sbin/dkms' does.
204 echo "stripping $vmodule"
205 strip -g "$module"
206 done
207 fi
208
209 # Install and optionally sign the modules we have built.
210 pkgdir="$pkgdir/$package"
211 echo "II: dkms-build installing $package into $pkgdir"
212 install -d "$pkgdir"
213 find "$dkms_dir/build/$dkms_package/$dkms_version/$abi_version" -name \*.ko |
214 while read module; do
215 vmodule=$( basename "$module" )
216 case "$sign" in
217 --*)
218 echo "copying $vmodule"
219 cp "$module" "$pkgdir"
220 ;;
221 *)
222 echo "signing $vmodule"
223 $sign "$module" "$pkgdir/$vmodule"
224 ;;
225 esac
226 done
227
228 find "$dkms_dir/build/$dkms_package/$dkms_version/objects" -name \*.o -print | \
229 while read object
230 do
231 "$srcdir/debian/scripts/fix-filenames" "$object" "$dkms_dir"
232 done
233
234 # This assumes that .mod files are in the top level build tree
235 # If there are ever .mod files in sub-directories, the dirname of objectlist needs to be stripped as well
236 find "$dkms_dir/build/$dkms_package/$dkms_version/objects" -name \*.mod -print | \
237 while read objectlist
238 do
239 sed "s|^$dkms_dir/build/$dkms_package/$dkms_version/build/||" -i $objectlist
240 done
241
242 # Finally see if there is a dkms-package specific post processor present. Hand
243 # it the original source directory, destination package directory, the objects
244 # as squirreled away, and the log in case it is useful. Finally pass a formed
245 # signing command line in case we need to do that.
246 dkms_build_specific="$srcdir/$0--$package"
247 dkms_build_generic=$(echo "$dkms_build_specific" | sed -n -e 's/-[0-9][0-9]*[a-z]*$/-N/p')
248 for dkms_build in "$dkms_build_specific" "$dkms_build_generic"
249 do
250 if [ -z "$dkms_build" -o ! -e "$dkms_build" ]; then
251 continue
252 fi
253 echo "II: dkms-build override $(basename "$dkms_build") found, executing"
254 "$dkms_build" \
255 "$srcdir" \
256 "$dkms_dir/build/$dkms_package/$dkms_version/objects" \
257 "$log" \
258 "$dkms_dir" \
259 "$abi_flavour" \
260 "$sign" \
261 "$pkgname" \
262 "$pkgdir" \
263 "$dbgpkgdir" \
264 "$package" \
265 "$@" || exit 1
266 break
267 done
268
269 echo "II: dkms-build build $package complete"