Commit | Line | Data |
---|---|---|
81fa42ce MJ |
1 | #!/bin/sh -exu |
2 | # | |
3 | # Copyright (C) 2016 - Michael Jeanson <mjeanson@efficios.com> | |
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | ## FUNCTIONS ## | |
19 | ||
20 | # Kernel version compare functions | |
21 | verlte() { | |
22 | [ "$1" = "`printf '%s\n%s' $1 $2 | sort -V | head -n1`" ] | |
23 | } | |
24 | ||
25 | verlt() { | |
26 | [ "$1" = "$2" ] && return 1 || verlte $1 $2 | |
27 | } | |
28 | ||
29 | vergte() { | |
30 | [ "$1" = "`printf '%s\n%s' $1 $2 | sort -V | tail -n1`" ] | |
31 | } | |
32 | ||
33 | vergt() { | |
34 | [ "$1" = "$2" ] && return 1 || vergte $1 $2 | |
35 | } | |
36 | ||
37 | ||
38 | prepare_lnx_sources() { | |
39 | ||
40 | outdir=$1 | |
41 | ||
42 | if [ "$outdir" = "." ]; then | |
43 | koutput="" | |
44 | else | |
45 | koutput="O=\"${outdir}\"" | |
46 | fi | |
47 | ||
48 | # Generate kernel configuration | |
49 | case "$kversion" in | |
50 | Ubuntu*) | |
51 | fakeroot debian/rules clean | |
52 | fakeroot debian/rules genconfigs | |
53 | cp CONFIGS/${ubuntu_config} "${outdir}"/.config | |
54 | ;; | |
55 | *) | |
56 | # Que sera sera | |
57 | make ${koutput} allyesconfig CC=$CC | |
58 | ;; | |
59 | esac | |
60 | ||
61 | # GCC 4.8 | |
62 | sed -i "s/CONFIG_CC_STACKPROTECTOR_STRONG=y/# CONFIG_CC_STACKPROTECTOR_STRONG is not set/g" "${outdir}"/.config | |
63 | ||
64 | # Don't try to sign modules | |
65 | sed -i "s/CONFIG_MODULE_SIG=y/# CONFIG_MODULE_SIG is not set/g" "${outdir}"/.config | |
66 | ||
67 | # Disable kernel stack frame correctness validation, introduced in 4.6.0 and currently fails | |
68 | sed -i "s/CONFIG_STACK_VALIDATION=y/# CONFIG_STACK_VALIDATION is not set/g" "${outdir}"/.config | |
69 | ||
70 | # Enable CONFIG_KALLSYMS_ALL | |
71 | echo "CONFIG_KPROBES=y" >> "${outdir}"/.config | |
72 | echo "CONFIG_FTRACE=y" >> "${outdir}"/.config | |
73 | echo "CONFIG_BLK_DEV_IO_TRACE=y" >> "${outdir}"/.config | |
74 | echo "CONFIG_TRACEPOINTS=y" >> "${outdir}"/.config | |
75 | echo "CONFIG_KALLSYMS_ALL=y" >> "${outdir}"/.config | |
76 | ||
77 | ||
78 | make ${koutput} silentoldconfig CC=$CC | |
79 | make ${koutput} modules_prepare CC=$CC | |
80 | ||
81 | # Version specific tasks | |
82 | case "$kversion" in | |
83 | Ubuntu*) | |
84 | # Add Ubuntu ABI number to kernel headers, this is normally done by the packaging code | |
85 | ABINUM=$(echo $kversion | grep -P -o 'Ubuntu-(lts-)?.*-\K\d+(?=\..*)') | |
86 | echo "#define UTS_UBUNTU_RELEASE_ABI $ABINUM" >> ${outdir}/include/generated/utsrelease.h | |
87 | ;; | |
88 | esac | |
89 | ||
90 | # On powerpc this object is required to link modules | |
91 | if [ "${karch}" = "powerpc" ]; then | |
92 | make ${koutput} arch/powerpc/lib/crtsavres.o CC=$CC | |
93 | fi | |
94 | } | |
95 | ||
96 | ||
97 | ||
98 | build_modules() { | |
99 | ||
100 | kdir="$1" | |
101 | bdir="$2" | |
102 | ||
103 | # Get kernel version from source tree | |
104 | cd "${kdir}" | |
105 | kversion=$(make kernelversion) | |
106 | ||
107 | # Enter latency-tracker source dir | |
108 | cd "${LTTSRCDIR}" | |
109 | ||
110 | # kernels 3.10 to 3.10.13 and 3.11 to 3.11.2 introduce a deadlock in the | |
111 | # timekeeping subsystem. We want those build to fail. | |
112 | if { vergte "$kversion" "3.10" && verlte "$kversion" "3.10.13"; } || \ | |
113 | { vergte "$kversion" "3.11" && verlte "$kversion" "3.11.2"; }; then | |
114 | ||
115 | set +e | |
116 | ||
117 | # Build modules | |
118 | KERNELDIR="${kdir}" make -j${NPROC} V=1 CC=$CC | |
119 | ||
120 | # We expect this build to fail, if it doesn't, fail the job. | |
121 | if [ "$?" -eq 0 ]; then | |
122 | exit 1 | |
123 | fi | |
124 | ||
125 | # We have to publish at least one file or the build will fail | |
126 | echo "This kernel is broken, there is a deadlock in the timekeeping subsystem." > "${bdir}/BROKEN.txt.ko" | |
127 | ||
128 | set -e | |
129 | ||
130 | KERNELDIR="${kdir}" make clean CC=$CC | |
131 | ||
132 | else # Regular build | |
133 | ||
134 | # Build modules against full kernel sources | |
135 | KERNELDIR="${kdir}" make -j${NPROC} V=1 CC=$CC | |
136 | ||
137 | # Install modules to build dir | |
138 | KERNELDIR="${kdir}" make INSTALL_MOD_PATH="${bdir}" modules_install CC=$CC | |
139 | ||
140 | # Clean build dir | |
141 | KERNELDIR="${kdir}" make clean CC=$CC | |
142 | fi | |
143 | } | |
144 | ||
145 | ||
146 | ## MAIN ## | |
147 | ||
148 | # Use gcc 4.9, older kernel don't build with gcc 5 | |
149 | export CC=gcc-4.9 | |
150 | ||
151 | # Use all CPU cores | |
152 | NPROC=$(nproc) | |
153 | ||
154 | LTTSRCDIR="${WORKSPACE}/src/latency-tracker" | |
155 | LNXSRCDIR="${WORKSPACE}/src/linux" | |
156 | ||
157 | LNXBUILDDIR="${WORKSPACE}/build/linux" | |
158 | LNXHDRDIR="${WORKSPACE}/build/linux-headers" | |
159 | ||
160 | LTTBUILDKSRCDIR="${WORKSPACE}/build/latency-tracker-ksrc" | |
161 | LTTBUILDKHDRDIR="${WORKSPACE}/build/latency-tracker-khdr" | |
162 | ||
163 | ||
164 | # Setup cross compile env if available | |
165 | if [ "x${cross_arch:-}" != "x" ]; then | |
166 | ||
167 | case "$cross_arch" in | |
168 | "armhf") | |
169 | karch="arm" | |
170 | cross_compile="arm-linux-gnueabihf-" | |
171 | ubuntu_config="armhf-config.flavour.generic" | |
172 | ;; | |
173 | ||
174 | "arm64") | |
175 | karch="arm64" | |
176 | cross_compile="aarch64-linux-gnu-" | |
177 | ubuntu_config="arm64-config.flavour.generic" | |
178 | ;; | |
179 | ||
180 | "powerpc") | |
181 | karch="powerpc" | |
182 | cross_compile="powerpc-linux-gnu-" | |
183 | ubuntu_config="powerpc-config.flavour.powerpc-smp" | |
184 | ;; | |
185 | ||
186 | "ppc64el") | |
187 | karch="powerpc" | |
188 | cross_compile="powerpc64le-linux-gnu-" | |
189 | ubuntu_config="ppc64el-config.flavour.generic" | |
190 | ;; | |
191 | ||
192 | *) | |
193 | echo "Unsupported cross arch $arch" | |
194 | exit 1 | |
195 | ;; | |
196 | esac | |
197 | ||
198 | # Export variables used by Kbuild for cross compilation | |
199 | export ARCH="${karch}" | |
200 | export CROSS_COMPILE="${cross_compile}" | |
201 | ||
202 | ||
203 | # Set arch specific values if we are not cross compiling | |
204 | elif [ "x${arch:-}" != "x" ]; then | |
205 | case "$arch" in | |
206 | "x86-32") | |
207 | karch="x86" | |
208 | ubuntu_config="i386-config.flavour.generic" | |
209 | ;; | |
210 | ||
211 | "x86-64") | |
212 | karch="x86" | |
213 | ubuntu_config="amd64-config.flavour.generic" | |
214 | ;; | |
215 | ||
216 | "armhf") | |
217 | karch="arm" | |
218 | ubuntu_config="armhf-config.flavour.generic" | |
219 | ;; | |
220 | ||
221 | "arm64") | |
222 | karch="arm64" | |
223 | ubuntu_config="arm64-config.flavour.generic" | |
224 | ;; | |
225 | ||
226 | "powerpc") | |
227 | karch="powerpc" | |
228 | ubuntu_config="powerpc-config.flavour.powerpc-smp" | |
229 | ;; | |
230 | ||
231 | "ppc64el") | |
232 | karch="powerpc" | |
233 | ubuntu_config="ppc64el-config.flavour.generic" | |
234 | ;; | |
235 | ||
236 | *) | |
237 | echo "Unsupported arch $arch" | |
238 | exit 1 | |
239 | ;; | |
240 | esac | |
241 | else | |
242 | echo "Not arch or cross_arch specified" | |
243 | exit 1 | |
244 | fi | |
245 | ||
246 | ||
247 | ||
248 | ||
249 | # Create build directories | |
250 | mkdir -p "${LNXBUILDDIR}" "${LNXHDRDIR}" "${LTTBUILDKSRCDIR}" "${LTTBUILDKHDRDIR}" | |
251 | ||
252 | ||
253 | ||
254 | ## PREPARE DISTRO STYLE KERNEL HEADERS / DEVEL | |
255 | ||
256 | # Enter linux source dir | |
257 | cd "${LNXSRCDIR}" | |
258 | ||
259 | prepare_lnx_sources "." | |
260 | ||
261 | # For RT kernels, copy version file | |
262 | if [ -s localversion-rt ]; then | |
263 | cp -a localversion-rt "${LNXHDRDIR}" | |
264 | fi | |
265 | ||
266 | # Copy all Makefile related stuff | |
267 | find . -path './include/*' -prune \ | |
268 | -o -path './scripts/*' -prune -o -type f \ | |
269 | \( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \ | |
270 | -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \ | |
271 | -print | cpio -pd --preserve-modification-time "${LNXHDRDIR}" | |
272 | ||
273 | # Copy base scripts and include dirs | |
274 | cp -a scripts include "${LNXHDRDIR}" | |
275 | ||
276 | # Copy arch includes | |
277 | (find arch -name include -type d -print | \ | |
278 | xargs -n1 -i: find : -type f) | \ | |
279 | cpio -pd --preserve-modification-time "${LNXHDRDIR}" | |
280 | ||
281 | # Copy arch scripts | |
282 | (find arch -name scripts -type d -print | \ | |
283 | xargs -n1 -i: find : -type f) | \ | |
284 | cpio -pd --preserve-modification-time "${LNXHDRDIR}" | |
285 | ||
286 | # Cleanup scripts | |
287 | rm -f "${LNXHDRDIR}/scripts/*.o" | |
288 | rm -f "${LNXHDRDIR}/scripts/*/*.o" | |
289 | ||
290 | # On powerpc this object is required to link modules | |
291 | if [ "${karch}" = "powerpc" ]; then | |
292 | cp -a --parents arch/powerpc/lib/crtsavres.[So] "${LNXHDRDIR}/" | |
293 | fi | |
294 | ||
295 | # Copy modules related stuff, if available | |
296 | if [ -s Module.symvers ]; then | |
297 | cp Module.symvers "${LNXHDRDIR}" | |
298 | fi | |
299 | ||
300 | if [ -s System.map ]; then | |
301 | cp System.map "${LNXHDRDIR}" | |
302 | fi | |
303 | ||
304 | if [ -s Module.markers ]; then | |
305 | cp Module.markers "${LNXHDRDIR}" | |
306 | fi | |
307 | ||
308 | # Copy config file | |
309 | cp .config "${LNXHDRDIR}" | |
310 | ||
311 | # Make sure the Makefile and version.h have a matching timestamp so that | |
312 | # external modules can be built | |
313 | if [ -s "${LNXHDRDIR}/include/generated/uapi/linux/version.h" ]; then | |
314 | touch -r "${LNXHDRDIR}/Makefile" "${LNXHDRDIR}/include/generated/uapi/linux/version.h" | |
315 | elif [ -s "${LNXHDRDIR}/include/linux/version.h" ]; then | |
316 | touch -r "${LNXHDRDIR}/Makefile" "${LNXHDRDIR}/include/linux/version.h" | |
317 | else | |
318 | echo "Missing version.h" | |
319 | exit 1 | |
320 | fi | |
321 | touch -r "${LNXHDRDIR}/.config" "${LNXHDRDIR}/include/generated/autoconf.h" | |
322 | ||
323 | # Copy .config to include/config/auto.conf so "make prepare" is unnecessary. | |
324 | cp "${LNXHDRDIR}/.config" "${LNXHDRDIR}/include/config/auto.conf" | |
325 | ||
326 | ||
327 | ||
328 | ||
329 | ## PREPARE FULL LINUX SOURCE TREE | |
330 | ||
331 | # Enter linux source dir | |
332 | cd "${LNXSRCDIR}" | |
333 | ||
334 | # Make sure linux source dir is clean | |
335 | git clean -xdf | |
336 | ||
337 | prepare_lnx_sources "${LNXBUILDDIR}" | |
338 | ||
339 | ||
340 | ## BUILD modules | |
341 | ||
342 | # Build modules against full kernel sources | |
343 | build_modules "${LNXBUILDDIR}" "${LTTBUILDKSRCDIR}" | |
344 | ||
345 | # Build modules against kernel headers | |
346 | build_modules "${LNXHDRDIR}" "${LTTBUILDKHDRDIR}" | |
347 | ||
348 | # Make sure modules were built | |
349 | tree "${LTTBUILDKSRCDIR}" | |
350 | if [ "x$(find "${LTTBUILDKSRCDIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then | |
351 | echo "No modules built!" | |
352 | exit 1 | |
353 | fi | |
354 | ||
355 | tree "${LTTBUILDKHDRDIR}" | |
356 | if [ "x$(find "${LTTBUILDKHDRDIR}" -name '*.ko*' -printf yes -quit)" != "xyes" ]; then | |
357 | echo "No modules built!" | |
358 | exit 1 | |
359 | fi | |
360 | ||
361 | # EOF |