summaryrefslogtreecommitdiff
path: root/kbuild.sh
blob: 8f950b9689967108e559b0c8a4d47433fd996c7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0+

set -e

kbuild_root=$(dirname $(readlink -f "$0"))
kernel_root="${PWD}"
kernel_output="${kernel_root}/output"

declare -A cross_compile
declare -A extra_kcflags
declare -A target_root

if [[ -r $kbuild_root/config.sh ]] ; then
	source $kbuild_root/config.sh || exit 1
fi

#kbuild_options='CONFIG_DEBUG_SECTION_MISMATCH=y'
#kbuild_options='C=1'
kbuild_options="KALLSYMS_EXTRA_PASS=0"

get_kernel_version() {
	local version=$(cat Makefile | grep '^VERSION = ' | sed 's/.* = //')
	local patchlevel=$(cat Makefile | grep '^PATCHLEVEL = ' | sed 's/.* = //')
	local sublevel=$(cat Makefile | grep '^SUBLEVEL = ' | sed 's/.* = //')
	echo "$version.$patchlevel.$sublevel"
}

version=$(get_kernel_version)

#
# Options parsing
#

usage() {
	echo   "Usage: $(basename $1) [options] platform [make options]"
	echo   ""
	echo   "Supported options:"
	echo   "--bootloader file	Boot loader file (CrOS images only)"
	echo   "--build components	List of components to build: image, modules, dt (default: all)"
	echo   "--cmdline		Command line arguments file (CrOS images only)"
	echo   "--config		Run menuconfig"
	echo   "--deb			Create Debian packages"
	echo   "--defconfig file	Generate a new .config with defaults from file"
	echo   "--doc[=dir]		Compile the documentation (for the specified dir only)"
	echo   "--dt-check file		Run DT bindings checks"
	echo   "-h, --help		Print this help text"
	echo   "--			End options parsing, next argument is the platform"
	echo   ""
	echo   "Supported platforms:"
	for f in $kbuild_root/platforms/*.sh ; do
		echo "- $(basename -s .sh $f)"
	done
}

declare -A do_build
do_menu_config=0
do_package_deb=0
opt_bootloader=
opt_build="image,modules,dt"
opt_cmdline_file=
opt_defconfig=
opt_doc_dirs='.'
opt_dt_bindings=''
opt_make_opts=

while [[ $# != 0 ]] ; do
	option=$1
	shift

	case $option in
		--bootloader)
			opt_bootloader=$1
			if [[ ! -r $opt_bootloader ]] ; then
				echo "Boot loader file $opt_bootloader is not readable"
				exit 1
			fi
			shift
			;;
		--build)
			opt_build="$1"
			shift
			;;
		--cmdline)
			opt_cmdline_file=$1
			if [[ ! -r $opt_cmdline_file ]] ; then
				echo "Command line file $opt_cmdline_file is not readable"
				exit 1
			fi
			shift
			;;
		--config)
			do_menu_config=1
			;;
		--deb)
			do_package_deb=1
			;;
		--defconfig)
			opt_defconfig=$1
			if [[ ! -r $opt_defconfig ]] ; then
				echo "defconfig file $opt_defconfig is not readable"
				exit 1
			fi
			shift
			;;
		--doc)
			do_build[doc]=1
			;;
		--doc=*)
			do_build[doc]=1
			opt_doc_dirs="${option/--doc=/}"
			;;
		--dt-check)
			opt_dt_bindings="$1"
			shift
			;;
		-h|--help)
			usage $0
			exit 1
			;;
		--)
			platform=auto
			;;
		--*)
			echo "Unrecognized option $option"
			exit 1
			;;
		*)
			if [[ -z "$platform" ]] ; then
				platform=$option
			else
				opt_make_opts="$option $opt_make_opts"
			fi
			;;
	esac
done

IFS=',' read -r -a opt_build <<< "$opt_build"
for component in ${opt_build[@]} ; do
	case $component in
	dt|image|modules)
		do_build[$component]=1
		;;
	*)
		echo "Invalid build component ${component}"
		exit 1
		;;
	esac
done

#
# Platform
#

kcflags="-Werror -Wno-error=cpp"

if [[ -z "$platform" || "$platform" == "auto" ]] ; then
	for f in $kbuild_root/platforms/*.sh ; do
		pattern=$(cat $f | grep "dir_pattern=" | head -1)
		pattern=${pattern/#dir_pattern=/}
		if [[ -n "$pattern" ]] ; then
			echo "$kernel_root" | grep -q "/$pattern/" || continue
			platform=$(basename -s .sh $f)
			break
		fi
	done
fi

if [[ ! -r "$kbuild_root/platforms/$platform.sh" ]] ; then
	echo "Unknown platform '$platform'. Supported platforms are"
	for f in $kbuild_root/platforms/*.sh ; do
		echo "- $(basename -s .sh $f)"
	done
	exit 1
fi

source "${kbuild_root}/platforms/$platform.sh" || exit 1

# Apply version-specific kcflags, useful to suppress warnings based on the
# kernel version.
[[ -z "${extra_kcflags[$version]+_}" ]] || kcflags="$kcflags ${extra_kcflags[$version]}"

#
# Architecture
#

if [[ ! -r "$kbuild_root/arch/$arch.sh" ]] ; then
	echo "Unknown architecture '$arch'. Supported architectures are"
	for f in $kbuild_root/arch/*.sh ; do
		echo "- $(basename -s .sh $f)"
	done
	exit 1
fi

source "${kbuild_root}/arch/$arch.sh" || exit 1

# Override the output directory for ARM for historical reasons
case $arch in
	arm)
		output_dir="$kernel_output/arm32"
		;;
	*)
		output_dir="$kernel_output/$arch"
		;;
esac

#
# Build environment and tools
#

num_cpus=$(cat /proc/cpuinfo | grep '^processor\b' | wc -l)
make="make ARCH=$arch O=$output_dir CROSS_COMPILE=${cross_compile[$arch]} ${extra_make_args}"
pmake="$make -j$((num_cpus*2)) $opt_make_opts"

echo "Compiling for platform $platform with $num_cpus CPUs"

mkdir -p $output_dir/

if [[ -n "$opt_defconfig" ]] ; then
	mkdir -p $output_dir/arch/$arch/configs/
	cp $opt_defconfig $output_dir/arch/$arch/configs/build_defconfig
	$make build_defconfig
	rm $output_dir/arch/$arch/configs/build_defconfig
fi

if [[ $do_menu_config == 1 ]] ; then
	$make menuconfig
	exit 0
fi

#
# Compile documentation if requested
#

if [[ ${do_build[doc]} == 1 ]] ; then
	(
	if [[ ! -f "$kernel_output/doc/bin/activate" ]] ; then
		virtualenv "$kernel_output/doc"
		source "$kernel_output/doc/bin/activate"
		pip install -r Documentation/sphinx/requirements.txt
	else
		source "$kernel_output/doc/bin/activate"
	fi

	$pmake SPHINXDIRS="${opt_doc_dirs}" htmldocs
	)
fi

#
# Check DT bindings
#

if [[ ! -z ${opt_dt_bindings} ]] ; then
	$pmake dt_binding_check DT_CHECKER_FLAGS=-m "DT_SCHEMA_FILES=${opt_dt_bindings}"
fi

#
# Compile the kernel, modules and DTBs
#

kernel_dtbs=
if [[ ${do_build[dt]} == 1 ]] ; then
	for dtb in ${dtbs[@]} ; do
		dtb=${dtb/:*/}

		if [[ "${dtb}" == /* ]] ; then
			continue
		fi

		kernel_dtbs="$kernel_dtbs $dtb"
	done
fi

if [[ ${do_build[image]} == 1 ]] ; then
	$pmake $kbuild_options KCFLAGS="$kcflags" $image_kernel $kernel_dtbs
fi

grep 'CONFIG_MODULES=y' $output_dir/.config > /dev/null && modules=modules
if [[ x$modules == xmodules ]] && [[ ${do_build[modules]} == 1 ]]; then
	$pmake $kbuild_options KCFLAGS="$kcflags" $modules
fi

version=$(cat $output_dir/include/generated/utsrelease.h | cut -d '"' -f 2)

#
# Create the image
#

_image_type=${image/:*/}

if [[ -r "$kbuild_root/images/${_image_type}.sh" ]] ; then
	source "${kbuild_root}/images/${_image_type}.sh" || exit 1
	eval make_${_image_type}_image
else
	_image_file=${_image_type}
fi

if echo "${image}" | grep -q ':' ; then
	_image_name=${image/*:/}
else
	_image_name=${_image_file}
fi

_image_file="$output_dir/arch/$arch/boot/${_image_file}"

#
# Copy the files to their destination directories.
#
# The kernel image and DTBs are copied to both $target_dir/boot and $tftp_dir
# (if available). When the image format is CrOS, installation to $tftp_dir is
# skipped as Chrome OS devices can't boot from TFTP.
#
# The modules are installed to $target_dir if available, or to
# $output_dir/modules otherwise.
#

target_dir=${target_dir:-${target_root[$arch]}}
tftp_dir=${tftp_dir:-$tftp_root/$platform}

copy_dtbs() {
	local dest_dir=$1
	local dtb

	if [[ ${do_build[dt]} != 1 ]] ; then
		return
	fi

	if [[ $_image_type == FIT || $_image_type == CrOS ]] ; then
		return
	fi

	for dtb in ${dtbs[@]} ; do
		local src=${dtb/:*/}
		local dst=${dtb/*:/}

		# If a destination is explicitly specified, don't strip
		# directory name. This can be used for instance to store .dtbo
		# files in an overlays/ subdirectory.
		if ! echo ${dtb} | grep -q ':' ; then
			dst=$(basename $dst)
		fi

		# The destination should never be an absolute path.
		if [[ "${dst}" == /* ]] ; then
			echo "Error: DTB destination ${dtb} can't be absolute path"
			exit 1
		fi

		mkdir -p $(dirname $dest_dir/$dst)
		cp $output_dir/arch/${arch}/boot/dts/$src* $dest_dir/$dst
	done
}

if [[ -d "$target_dir" ]] ; then
	mkdir -p $target_dir/boot
	cp ${_image_file} $target_dir/boot/${_image_name}
	copy_dtbs $target_dir/boot/
fi

if [[ $_image_type != CrOS && -d "$tftp_dir" ]] ; then
	cp ${_image_file} $tftp_dir/${_image_name}
	copy_dtbs $tftp_dir/
fi

if [[ x$modules == xmodules ]] && [[ ${do_build[modules]} == 1 ]]; then
	mod_path=${target_dir:-$output_dir/modules}
	mkdir -p $mod_path
	$pmake INSTALL_MOD_PATH=$mod_path modules_install
fi

#
# Create the Debian package if requested
#

if [[ $do_package_deb == 1 ]] ; then
	$pmake $kbuild_options KCFLAGS="$kcflags" DPKG_FLAGS="-d" bindeb-pkg
fi

#
# Print status and terminate
#

echo "Kernel image available in ${_image_file}"
if [[ x$modules == xmodules ]] && [[ ${do_build[modules]} == 1 ]]; then
	echo "Kernel modules installed to $mod_path/lib/modules/$version"
fi
echo "Build $version completed at $(date)"