xref: /linux/scripts/package/install-extmod-build (revision c297aa7d3fb6755890b78b483e82c9cf07370d50)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3
4set -eu
5
6destdir=${1}
7
8is_enabled() {
9	grep -q "^$1=y" include/config/auto.conf
10}
11
12find_in_scripts() {
13	find scripts \
14		\( -name atomic -o -name dtc -o -name kconfig -o -name package \) -prune -o \
15		! -name unifdef -a ! -name mk_elfconfig -a \( -type f -o -type l \) -print
16}
17
18mkdir -p "${destdir}"
19
20(
21	cd "${srctree}"
22	echo Makefile
23	find "arch/${SRCARCH}" -maxdepth 1 -name 'Makefile*'
24	find "arch/${SRCARCH}" -name generated -prune -o -name include -type d -print
25	find "arch/${SRCARCH}" -name Kbuild.platforms -o -name Platform
26	find include \( -name config -o -name generated \) -prune -o \( -type f -o -type l \) -print
27	find_in_scripts
28) | tar -c -f - -C "${srctree}" -T - | tar -xf - -C "${destdir}"
29
30{
31	if is_enabled CONFIG_OBJTOOL; then
32		echo tools/objtool/objtool
33	fi
34
35	echo Module.symvers
36	echo "arch/${SRCARCH}/include/generated"
37	echo include/config/auto.conf
38	echo include/config/kernel.release
39	echo include/generated
40	find_in_scripts
41
42	if is_enabled CONFIG_GCC_PLUGINS; then
43		find scripts/gcc-plugins -name '*.so'
44	fi
45} | tar -c -f - -T - | tar -xf - -C "${destdir}"
46
47# When ${CC} and ${HOSTCC} differ, we are likely cross-compiling. Rebuild host
48# programs using ${CC}. This assumes CC=${CROSS_COMPILE}gcc, which is usually
49# the case for package building. It does not cross-compile when CC=clang.
50#
51# This caters to host programs that participate in Kbuild. objtool and
52# resolve_btfids are out of scope.
53if [ "${CC}" != "${HOSTCC}" ] && is_enabled CONFIG_CC_CAN_LINK; then
54	echo "Rebuilding host programs with ${CC}..."
55
56	cat <<-'EOF' >  "${destdir}/Kbuild"
57	subdir-y := scripts
58	EOF
59
60	# HOSTCXX is not overridden. The C++ compiler is used to build:
61	# - scripts/kconfig/qconf, which is unneeded for external module builds
62	# - GCC plugins, which will not work on the installed system even after
63	#   being rebuilt.
64	#
65	# Use the single-target build to avoid the modpost invocation, which
66	# would overwrite Module.symvers.
67	"${MAKE}" HOSTCC="${CC}" KBUILD_EXTMOD="${destdir}" scripts/
68
69	cat <<-'EOF' >  "${destdir}/scripts/Kbuild"
70	subdir-y := basic
71	hostprogs-always-y := mod/modpost
72	mod/modpost-objs := $(addprefix mod/, modpost.o file2alias.o sumversion.o symsearch.o)
73	EOF
74
75	# Run once again to rebuild scripts/basic/ and scripts/mod/modpost.
76	"${MAKE}" HOSTCC="${CC}" KBUILD_EXTMOD="${destdir}" scripts/
77
78	rm -f "${destdir}/Kbuild" "${destdir}/scripts/Kbuild"
79fi
80
81find "${destdir}" \( -name '.*.cmd' -o -name '*.o' \) -delete
82