]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - debian/scripts/dkms-build
3aabf94be4925d0c7575d3b47a45b278d06c3a04
[mirror_ubuntu-jammy-kernel.git] / debian / scripts / dkms-build
1 #!/bin/bash
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 $( apt-cache policy | grep '^ [^ ]' | sort -r -n -k 1,1 -s | \
65 awk '
66 ($2 ~ /^http/) {
67 if (!($2 in E)) {
68 E[$2]=1;
69 print $2;
70 }
71 }
72 ')
73 do
74 if package_present "$lpackage"; then
75 break
76 fi
77 url="$pool/$package_path"
78 "$here/file-downloader" "$url" && break || true
79 # No components in PPAs.
80 url=$(echo "$url" | sed -e 's@/pool/[^/]*/@/pool/main/@')
81 "$here/file-downloader" "$url" && break || true
82 done
83 ;;
84 http*:*)
85 "$here/file-downloader" "$package_path"
86 ;;
87 */*)
88 cp -p "$package_path" .
89 ;;
90 *)
91 apt-get download "$rpackage"
92 ;;
93 esac
94 break
95 done
96 if ! package_present "$lpackage"; then
97 echo "EE: $lpackage not found"
98 exit 1
99 fi
100
101 dpkg -x "$lpackage"_*.deb "$package"
102
103 lversion=$( echo "$lpackage_path" | sed -e 's@.*/@@' -e 's@_[^_]*$@@' -e 's@.*_@@')
104 #built_using_record "$srcdir/debian/$pkgname.substvars" "$built_using$lpackage (= $lversion)"
105 done
106
107 # Pick out the package/version from the dkms.conf.
108 for dkms_conf in "$package/usr/src"/*/"dkms.conf"
109 do
110 break
111 done
112
113 # It seems some packages have a # in the name which works fine if the
114 # package is installed directly, but not so much if we build it out
115 # of the normal location.
116 sed -i -e '/^PACKAGE_NAME=/ s/#//g' "$dkms_conf"
117
118 # Run any dkms-package specfic configuration steps
119 dkms_config_specific="$srcdir/$0-configure--$package"
120 dkms_config_generic=$(echo "$dkms_config_specific" | sed -e 's/-[0-9][0-9]*$/-N/')
121 for dkms_config in "$dkms_config_specific" "$dkms_config_generic"
122 do
123 if [ -z "$dkms_config" -o ! -e "$dkms_config" ]; then
124 continue
125 fi
126 echo "II: dkms-build-configure $(basename "$dkms_config") found, executing"
127 $SHELL "$dkms_config" \
128 "$srcdir" \
129 "$dkms_conf" \
130 "$dkms_dir" \
131 "$abi_flavour" \
132 "$sign" \
133 "$pkgname" \
134 "$pkgdir" \
135 "$dbgpkgdir" \
136 "$package" \
137 "$@" || exit 1
138 break
139 done
140
141 cat - <<'EOF' >>"$dkms_conf"
142 POST_BUILD="ubuntu-save-objects ${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build ${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/objects $POST_BUILD"
143 EOF
144 ubuntu_script="$(dirname "$dkms_conf")/ubuntu-save-objects"
145 cat - <<'EOF' >"$ubuntu_script"
146 #!/bin/sh
147 from="$1"
148 to="$2"
149 script="$3"
150 shift 2
151
152 # Copy the objects.
153 echo "II: copying objects to '$to'"
154 mkdir -p "$to"
155 (cd "$from" && find -name \*.o -o -name \*.o.ur-\* | cpio -Lpd "$to")
156
157 # Call the original post_install script if there is one.
158 [ "$script" = '' ] && exit 0
159
160 shift
161 exec "$(dirname "$0")/$script" "$@"
162 EOF
163 chmod +x "$ubuntu_script"
164 dkms_package=$( sed -ne 's/PACKAGE_NAME="\(.*\)"/\1/p' "$dkms_conf" )
165 dkms_version=$( sed -ne 's/PACKAGE_VERSION="\(.*\)"/\1/p' "$dkms_conf" )
166
167 # Build the DKMS binaries.
168 echo "II: dkms-build building $package"
169 rc=0
170 /usr/sbin/dkms build --no-prepare-kernel --no-clean-kernel \
171 -k "$abi_flavour" \
172 --sourcetree "$dkms_dir/source" \
173 --dkmstree "$dkms_dir/build" \
174 --kernelsourcedir "$dkms_dir/headers/linux-headers-$abi_flavour" \
175 "$dkms_conf" || rc=1
176
177 # Find the log and add it to our own.
178 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"
179 do
180 [ -f "$log" ] && break
181 done
182 sed -e "s@$dkms_dir@<<DKMSDIR>>@g" <"$log"
183
184 # If this build failed then exit here.
185 [ "$rc" != 0 ] && exit "$rc"
186
187 # Install the modules with debug symbols we possibly built,
188 # and strip the original modules for the next install step.
189 if [ -n "$dbgpkgdir" ]; then
190 dbgpkgdir="$dbgpkgdir/$package"
191 echo "II: dkms-build installing $package into $dbgpkgdir (debug symbols)"
192 install -d "$dbgpkgdir"
193 find "$dkms_dir/build/$dkms_package/$dkms_version/$abi_version" -name \*.ko |
194 while read module; do
195 vmodule=$( basename "$module" )
196
197 # Check for '.debug_info' section in order to copy module.
198 # Useful if debug symbols are requested but not built for
199 # any reason (including not yet supported by DKMS package).
200 # Strip module just in case even if section isn't present.
201 if ${CROSS_COMPILE}objdump -h -j '.debug_info' "$module" >/dev/null 2>&1
202 then
203 echo "copying $vmodule"
204 cp "$module" "$dbgpkgdir"
205 else
206 echo "ignoring $vmodule (missing debug symbols)"
207 fi
208
209 # Just 'strip -g' as '/usr/sbin/dkms' does.
210 echo "stripping $vmodule"
211 strip -g "$module"
212 done
213 fi
214
215 # Install and optionally sign the modules we have built.
216 pkgdir="$pkgdir/$package"
217 echo "II: dkms-build installing $package into $pkgdir"
218 install -d "$pkgdir"
219 find "$dkms_dir/build/$dkms_package/$dkms_version/$abi_version" -name \*.ko |
220 while read module; do
221 vmodule=$( basename "$module" )
222 case "$sign" in
223 --*)
224 echo "copying $vmodule"
225 cp "$module" "$pkgdir"
226 ;;
227 *)
228 echo "signing $vmodule"
229 $sign "$module" "$pkgdir/$vmodule"
230 ;;
231 esac
232 done
233
234 find "$dkms_dir/build/$dkms_package/$dkms_version/objects" -name \*.o -print | \
235 while read object
236 do
237 "$srcdir/debian/scripts/fix-filenames" "$object" "$dkms_dir"
238 done
239
240 # Finally see if there is a dkms-package specific post processor present. Hand
241 # it the original source directory, destination package directory, the objects
242 # as squirreled away, and the log in case it is useful. Finally pass a formed
243 # signing command line in case we need to do that.
244 dkms_build_specific="$srcdir/$0--$package"
245 dkms_build_generic=$(echo "$dkms_build_specific" | sed -n -e 's/-[0-9][0-9]*[a-z]*$/-N/p')
246 for dkms_build in "$dkms_build_specific" "$dkms_build_generic"
247 do
248 if [ -z "$dkms_build" -o ! -e "$dkms_build" ]; then
249 continue
250 fi
251 echo "II: dkms-build override $(basename "$dkms_build") found, executing"
252 $SHELL "$dkms_build" \
253 "$srcdir" \
254 "$dkms_dir/build/$dkms_package/$dkms_version/objects" \
255 "$log" \
256 "$dkms_dir" \
257 "$abi_flavour" \
258 "$sign" \
259 "$pkgname" \
260 "$pkgdir" \
261 "$dbgpkgdir" \
262 "$package" \
263 "$@" || exit 1
264 break
265 done
266
267 echo "II: dkms-build build $package complete"