@@ -117,10 +117,10 @@ kmake() {
117117 if gcc-specs-pie; then
118118 kernel_cflags=" -nopie -fstack-check=no ${kernel_cflags} "
119119 fi
120- emake " --directory=${KERNEL_DIR} " \
120+ emake " --directory=${S} /source " \
121121 ARCH=" ${kernel_arch} " \
122122 CROSS_COMPILE=" ${CHOST} -" \
123- KBUILD_OUTPUT=" ${S} /build" \
123+ KBUILD_OUTPUT=" .. /build" \
124124 KCFLAGS=" ${kernel_cflags} " \
125125 LDFLAGS=" " \
126126 " V=1" \
@@ -184,6 +184,85 @@ shred_keys() {
184184 cp certs/modules.pub.pem " ${sig_key} " || die
185185}
186186
187+ # Populate /lib/modules/$(uname -r)/{build,source}
188+ install_build_source () {
189+ local kernel_arch=$( tc-arch-kernel)
190+ local host_kernel_arch=$( tc-ninja_magic_to_arch kern " ${CBUILD} " )
191+
192+ # NOTE: We have to get ${archabspaths} before removing symlinks under
193+ # /usr/lib/modules. However, do not exclude "dt-bindings" for now,
194+ # as it looks architecture-independent.
195+ local archabspaths=($( ls -1d ${D} /usr/lib/modules/${KV_FULL} /source/scripts/dtc/include-prefixes/* \
196+ | grep -v dt-bindings ) )
197+
198+ # remove the broken symlinks referencing $ROOT
199+ rm " ${D} /usr/lib/modules/${KV_FULL} /build" || die
200+
201+ # Compose list of architectures to be excluded from the kernel modules
202+ # tree in the final image. It is an array to be used as a pattern for
203+ # grep command below at the end of "find source/scripts" command for
204+ # fetching kernel modules list, e.g.:
205+ # find source/scripts -follow -print \
206+ # | grep -E -v -w "include-prefixes/arc|include-prefixes/xtensa"
207+ declare -a excarchlist
208+ local excarchstr
209+
210+ for apath in " ${archabspaths[@]} " ; do
211+ local arch
212+ arch=$( basename " ${apath} " )
213+ if [[ " ${arch} " != " ${kernel_arch} " ]]; then
214+ excarchlist+=(" include-prefixes/${arch} " )
215+
216+ # Do not append delimiter '|' in case of the last element.
217+ if [[ " ${apath} " != " ${archabspaths[-1]} " ]]; then
218+ excarchlist+=(" |" )
219+ fi
220+ fi
221+ done
222+
223+ # Remove every whitespace from the grep pattern string, to make pattern
224+ # matching work well.
225+ excarchstr=$( echo " ${excarchlist[@]} " | sed -e ' s/[[:space:]]*//g' )
226+
227+ # Install a stripped source for out-of-tree module builds (Debian-derived)
228+ #
229+ # NOTE: we need to exclude unsupported architectures from source/scripts,
230+ # to prevent the final image from having unnecessary directories under
231+ # /usr/lib/modules/${KV_FULL}/source/scripts/dtc/include-prefixes.
232+ # The grep must run with "-w" to exclude exact patterns like either arm
233+ # or arm64.
234+ {
235+ echo source/Makefile
236+ find source/arch/${host_kernel_arch} -follow -maxdepth 1 -name ' Makefile*' -print
237+ find source/arch/${kernel_arch} -follow -maxdepth 1 -name ' Makefile*' -print
238+ find source/arch/${kernel_arch} -follow \( -name ' module.lds' -o -name ' Kbuild.platforms' -o -name ' Platform' \) -print
239+ find $( find source/arch/${kernel_arch} -follow \( -name include -o -name scripts \) -follow -type d -print) -print
240+ find source/include -follow -print
241+ find source/scripts -follow -print | grep -E -v -w " ${excarchstr} "
242+ find build/ -print
243+ } | cpio -pd \
244+ --preserve-modification-time \
245+ --owner=root:root \
246+ --dereference \
247+ " ${D} /usr/lib/modules/${KV_FULL} " || die
248+ # ./build/source is a symbolic link so cpio ends up creating an empty dir.
249+ # Restore the symlink.
250+ pushd " ${D} /usr/lib/modules/${KV_FULL} "
251+ rmdir build/source || die
252+ ln -sr source build || die
253+ # Symlink includes into the build directory to resemble Ubuntu's /lib/modules
254+ # layout. This lets the Nvidia driver build when passing SYSSRC=/lib/modules/../build
255+ # instead of requiring SYSOUT/SYSSRC.
256+ {
257+ find source/include -mindepth 1 -maxdepth 1 -type d
258+ find source/arch/${kernel_arch} /include -mindepth 1 -maxdepth 1 -type d
259+ } | while read src; do
260+ dst=" ${src/ source/ build} "
261+ ln -sr " ${src} " " ${dst} " || die
262+ done || die
263+ popd
264+ }
265+
187266coreos-kernel_pkg_pretend () {
188267 [[ " ${MERGE_TYPE} " == binary ]] && return
189268
@@ -201,7 +280,10 @@ coreos-kernel_pkg_setup() {
201280}
202281
203282coreos-kernel_src_unpack () {
283+ # we more or less reproduce the layout in /lib/modules/$(uname -r)/
204284 mkdir -p " ${S} /build" || die
285+ mkdir -p " ${S} /source" || die
286+ ln -s " ${KERNEL_DIR} " /* " ${S} /source/" || die
205287}
206288
207289coreos-kernel_src_configure () {
0 commit comments