1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# remove tasks that modify the source tree in case externalsrc is inherited
8SRCTREECOVEREDTASKS += "do_validate_branches do_kernel_configcheck do_kernel_checkout do_fetch do_unpack do_patch"
9PATCH_GIT_USER_EMAIL ?= "kernel-yocto@oe"
10PATCH_GIT_USER_NAME ?= "OpenEmbedded"
11
12# The distro or local.conf should set this, but if nobody cares...
13LINUX_KERNEL_TYPE ??= "standard"
14
15# KMETA ?= ""
16KBRANCH ?= "master"
17KMACHINE ?= "${MACHINE}"
18SRCREV_FORMAT ?= "meta_machine"
19
20# LEVELS:
21#   0: no reporting
22#   1: report options that are specified, but not in the final config
23#   2: report options that are not hardware related, but set by a BSP
24KCONF_AUDIT_LEVEL ?= "1"
25KCONF_BSP_AUDIT_LEVEL ?= "0"
26KMETA_AUDIT ?= "yes"
27KMETA_AUDIT_WERROR ?= ""
28
29# returns local (absolute) path names for all valid patches in the
30# src_uri
31def find_patches(d,subdir):
32    patches = src_patches(d)
33    patch_list=[]
34    for p in patches:
35        _, _, local, _, _, parm = bb.fetch.decodeurl(p)
36        # if patchdir has been passed, we won't be able to apply it so skip
37        # the patch for now, and special processing happens later
38        patchdir = ''
39        if "patchdir" in parm:
40            patchdir = parm["patchdir"]
41        if subdir:
42            if subdir == patchdir:
43                patch_list.append(local)
44        else:
45            # skip the patch if a patchdir was supplied, it won't be handled
46            # properly
47            if not patchdir:
48                patch_list.append(local)
49
50    return patch_list
51
52# returns all the elements from the src uri that are .scc files
53def find_sccs(d):
54    sources=src_patches(d, True)
55    sources_list=[]
56    for s in sources:
57        base, ext = os.path.splitext(os.path.basename(s))
58        if ext and ext in [".scc", ".cfg"]:
59            sources_list.append(s)
60        elif base and 'defconfig' in base:
61            sources_list.append(s)
62
63    return sources_list
64
65# check the SRC_URI for "kmeta" type'd git repositories. Return the name of
66# the repository as it will be found in WORKDIR
67def find_kernel_feature_dirs(d):
68    feature_dirs=[]
69    fetch = bb.fetch2.Fetch([], d)
70    for url in fetch.urls:
71        urldata = fetch.ud[url]
72        parm = urldata.parm
73        type=""
74        if "type" in parm:
75            type = parm["type"]
76        if "destsuffix" in parm:
77            destdir = parm["destsuffix"]
78            if type == "kmeta":
79                feature_dirs.append(destdir)
80
81    return feature_dirs
82
83# find the master/machine source branch. In the same way that the fetcher proceses
84# git repositories in the SRC_URI we take the first repo found, first branch.
85def get_machine_branch(d, default):
86    fetch = bb.fetch2.Fetch([], d)
87    for url in fetch.urls:
88        urldata = fetch.ud[url]
89        parm = urldata.parm
90        if "branch" in parm:
91            branches = urldata.parm.get("branch").split(',')
92            btype = urldata.parm.get("type")
93            if btype != "kmeta":
94                return branches[0]
95
96    return default
97
98# returns a list of all directories that are on FILESEXTRAPATHS (and
99# hence available to the build) that contain .scc or .cfg files
100def get_dirs_with_fragments(d):
101    extrapaths = []
102    extrafiles = []
103    extrapathsvalue = (d.getVar("FILESEXTRAPATHS") or "")
104    # Remove default flag which was used for checking
105    extrapathsvalue = extrapathsvalue.replace("__default:", "")
106    extrapaths = extrapathsvalue.split(":")
107    for path in extrapaths:
108        if path + ":True" not in extrafiles:
109            extrafiles.append(path + ":" + str(os.path.exists(path)))
110
111    return " ".join(extrafiles)
112
113do_kernel_metadata() {
114	set +e
115
116	if [ -n "$1" ]; then
117		mode="$1"
118	else
119		mode="patch"
120	fi
121
122	cd ${S}
123	export KMETA=${KMETA}
124
125	bbnote "do_kernel_metadata: for summary/debug, set KCONF_AUDIT_LEVEL > 0"
126
127	# if kernel tools are available in-tree, they are preferred
128	# and are placed on the path before any external tools. Unless
129	# the external tools flag is set, in that case we do nothing.
130	if [ -f "${S}/scripts/util/configme" ]; then
131		if [ -z "${EXTERNAL_KERNEL_TOOLS}" ]; then
132			PATH=${S}/scripts/util:${PATH}
133		fi
134	fi
135
136	# In a similar manner to the kernel itself:
137	#
138	#   defconfig: $(obj)/conf
139	#   ifeq ($(KBUILD_DEFCONFIG),)
140	#	$< --defconfig $(Kconfig)
141	#   else
142	#	@echo "*** Default configuration is based on '$(KBUILD_DEFCONFIG)'"
143	#	$(Q)$< --defconfig=arch/$(SRCARCH)/configs/$(KBUILD_DEFCONFIG) $(Kconfig)
144	#   endif
145	#
146	# If a defconfig is specified via the KBUILD_DEFCONFIG variable, we copy it
147	# from the source tree, into a common location and normalized "defconfig" name,
148	# where the rest of the process will include and incoroporate it into the build
149	#
150	# If the fetcher has already placed a defconfig in WORKDIR (from the SRC_URI),
151	# we don't overwrite it, but instead warn the user that SRC_URI defconfigs take
152	# precendence.
153	#
154	if [ -n "${KBUILD_DEFCONFIG}" ]; then
155		if [ -f "${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG}" ]; then
156			if [ -f "${WORKDIR}/defconfig" ]; then
157				# If the two defconfig's are different, warn that we overwrote the
158				# one already placed in WORKDIR
159				cmp "${WORKDIR}/defconfig" "${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG}"
160				if [ $? -ne 0 ]; then
161					bbdebug 1 "detected SRC_URI or unpatched defconfig in WORKDIR. ${KBUILD_DEFCONFIG} copied over it"
162				fi
163				cp -f ${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG} ${WORKDIR}/defconfig
164			else
165				cp -f ${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG} ${WORKDIR}/defconfig
166			fi
167			in_tree_defconfig="${WORKDIR}/defconfig"
168		else
169			bbfatal "A KBUILD_DEFCONFIG '${KBUILD_DEFCONFIG}' was specified, but not present in the source tree (${S}/arch/${ARCH}/configs/)"
170		fi
171	fi
172
173	if [ "$mode" = "patch" ]; then
174		# was anyone trying to patch the kernel meta data ?, we need to do
175		# this here, since the scc commands migrate the .cfg fragments to the
176		# kernel source tree, where they'll be used later.
177		check_git_config
178		patches="${@" ".join(find_patches(d,'kernel-meta'))}"
179		if [ -n "$patches" ]; then
180		    (
181			    cd ${WORKDIR}/kernel-meta
182
183			    # take the SRC_URI patches, and create a series file
184			    # this is required to support some better processing
185			    # of issues with the patches
186			    rm -f series
187			    for p in $patches; do
188				cp $p .
189				echo "$(basename $p)" >> series
190			    done
191
192			    # process the series with kgit-s2q, which is what is
193			    # handling the rest of the kernel. This allows us
194			    # more flexibility for handling failures or advanced
195			    # mergeing functinoality
196			    message=$(kgit-s2q --gen -v --patches ${WORKDIR}/kernel-meta 2>&1)
197			    if [ $? -ne 0 ]; then
198				# setup to try the patch again
199				kgit-s2q --prev
200				bberror "Problem applying patches to: ${WORKDIR}/kernel-meta"
201				bbfatal_log "\n($message)"
202			    fi
203			)
204		fi
205	fi
206
207	sccs_from_src_uri="${@" ".join(find_sccs(d))}"
208	patches="${@" ".join(find_patches(d,''))}"
209	feat_dirs="${@" ".join(find_kernel_feature_dirs(d))}"
210
211	# a quick check to make sure we don't have duplicate defconfigs If
212	# there's a defconfig in the SRC_URI, did we also have one from the
213	# KBUILD_DEFCONFIG processing above ?
214	src_uri_defconfig=$(echo $sccs_from_src_uri | awk '(match($0, "defconfig") != 0) { print $0 }' RS=' ')
215	# drop and defconfig's from the src_uri variable, we captured it just above here if it existed
216	sccs_from_src_uri=$(echo $sccs_from_src_uri | awk '(match($0, "defconfig") == 0) { print $0 }' RS=' ')
217
218	if [ -n "$in_tree_defconfig" ]; then
219		sccs_defconfig=$in_tree_defconfig
220		if [ -n "$src_uri_defconfig" ]; then
221			bbwarn "[NOTE]: defconfig was supplied both via KBUILD_DEFCONFIG and SRC_URI. Dropping SRC_URI entry $src_uri_defconfig"
222		fi
223	else
224		# if we didn't have an in-tree one, make our defconfig the one
225		# from the src_uri. Note: there may not have been one from the
226		# src_uri, so this can be an empty variable.
227		sccs_defconfig=$src_uri_defconfig
228	fi
229	sccs="$sccs_from_src_uri"
230
231	# check for feature directories/repos/branches that were part of the
232	# SRC_URI. If they were supplied, we convert them into include directives
233	# for the update part of the process
234	for f in ${feat_dirs}; do
235		if [ -d "${WORKDIR}/$f/kernel-meta" ]; then
236			includes="$includes -I${WORKDIR}/$f/kernel-meta"
237		elif [ -d "${WORKDIR}/../oe-local-files/$f" ]; then
238			includes="$includes -I${WORKDIR}/../oe-local-files/$f"
239	        elif [ -d "${WORKDIR}/$f" ]; then
240			includes="$includes -I${WORKDIR}/$f"
241		fi
242	done
243	for s in ${sccs} ${patches}; do
244		sdir=$(dirname $s)
245		includes="$includes -I${sdir}"
246                # if a SRC_URI passed patch or .scc has a subdir of "kernel-meta",
247                # then we add it to the search path
248                if [ -d "${sdir}/kernel-meta" ]; then
249			includes="$includes -I${sdir}/kernel-meta"
250                fi
251	done
252
253	# expand kernel features into their full path equivalents
254	bsp_definition=$(spp ${includes} --find -DKMACHINE=${KMACHINE} -DKTYPE=${LINUX_KERNEL_TYPE})
255	if [ -z "$bsp_definition" ]; then
256		if [ -z "$sccs_defconfig" ]; then
257			bbfatal_log "Could not locate BSP definition for ${KMACHINE}/${LINUX_KERNEL_TYPE} and no defconfig was provided"
258		fi
259	else
260		# if the bsp definition has "define KMETA_EXTERNAL_BSP t",
261		# then we need to set a flag that will instruct the next
262		# steps to use the BSP as both configuration and patches.
263		grep -q KMETA_EXTERNAL_BSP $bsp_definition
264		if [ $? -eq 0 ]; then
265		    KMETA_EXTERNAL_BSPS="t"
266		fi
267	fi
268	meta_dir=$(kgit --meta)
269
270	KERNEL_FEATURES_FINAL=""
271	if [ -n "${KERNEL_FEATURES}" ]; then
272		for feature in ${KERNEL_FEATURES}; do
273			feature_found=f
274			for d in $includes; do
275				path_to_check=$(echo $d | sed 's/^-I//')
276				if [ "$feature_found" = "f" ] && [ -e "$path_to_check/$feature" ]; then
277				    feature_found=t
278				fi
279			done
280			if [ "$feature_found" = "f" ]; then
281				if [ -n "${KERNEL_DANGLING_FEATURES_WARN_ONLY}" ]; then
282				    bbwarn "Feature '$feature' not found, but KERNEL_DANGLING_FEATURES_WARN_ONLY is set"
283				    bbwarn "This may cause runtime issues, dropping feature and allowing configuration to continue"
284				else
285				    bberror "Feature '$feature' not found, this will cause configuration failures."
286				    bberror "Check the SRC_URI for meta-data repositories or directories that may be missing"
287				    bbfatal_log "Set KERNEL_DANGLING_FEATURES_WARN_ONLY to ignore this issue"
288				fi
289			else
290				KERNEL_FEATURES_FINAL="$KERNEL_FEATURES_FINAL $feature"
291			fi
292		done
293        fi
294
295	if [ "$mode" = "config" ]; then
296		# run1: pull all the configuration fragments, no matter where they come from
297		elements="`echo -n ${bsp_definition} $sccs_defconfig ${sccs} ${patches} $KERNEL_FEATURES_FINAL`"
298		if [ -n "${elements}" ]; then
299			echo "${bsp_definition}" > ${S}/${meta_dir}/bsp_definition
300			scc --force -o ${S}/${meta_dir}:cfg,merge,meta ${includes} $sccs_defconfig $bsp_definition $sccs $patches $KERNEL_FEATURES_FINAL
301			if [ $? -ne 0 ]; then
302				bbfatal_log "Could not generate configuration queue for ${KMACHINE}."
303			fi
304		fi
305	fi
306
307	# if KMETA_EXTERNAL_BSPS has been set, or it has been detected from
308	# the bsp definition, then we inject the bsp_definition into the
309	# patch phase below.  we'll piggy back on the sccs variable.
310	if [ -n "${KMETA_EXTERNAL_BSPS}" ]; then
311		sccs="${bsp_definition} ${sccs}"
312	fi
313
314	if [ "$mode" = "patch" ]; then
315		# run2: only generate patches for elements that have been passed on the SRC_URI
316		elements="`echo -n ${sccs} ${patches} $KERNEL_FEATURES_FINAL`"
317		if [ -n "${elements}" ]; then
318			scc --force -o ${S}/${meta_dir}:patch --cmds patch ${includes} ${sccs} ${patches} $KERNEL_FEATURES_FINAL
319			if [ $? -ne 0 ]; then
320				bbfatal_log "Could not generate configuration queue for ${KMACHINE}."
321			fi
322		fi
323	fi
324
325	if [ ${KCONF_AUDIT_LEVEL} -gt 0 ]; then
326		bbnote "kernel meta data summary for ${KMACHINE} (${LINUX_KERNEL_TYPE}):"
327		bbnote "======================================================================"
328		if [ -n "${KMETA_EXTERNAL_BSPS}" ]; then
329			bbnote "Non kernel-cache (external) bsp"
330		fi
331		bbnote "BSP entry point / definition: $bsp_definition"
332		if [ -n "$in_tree_defconfig" ]; then
333			bbnote "KBUILD_DEFCONFIG: ${KBUILD_DEFCONFIG}"
334		fi
335		bbnote "Fragments from SRC_URI: $sccs_from_src_uri"
336		bbnote "KERNEL_FEATURES: $KERNEL_FEATURES_FINAL"
337		bbnote "Final scc/cfg list: $sccs_defconfig $bsp_definition $sccs $KERNEL_FEATURES_FINAL"
338	fi
339
340	set -e
341}
342
343do_patch() {
344	set +e
345	cd ${S}
346
347	check_git_config
348	meta_dir=$(kgit --meta)
349	(cd ${meta_dir}; ln -sf patch.queue series)
350	if [ -f "${meta_dir}/series" ]; then
351		kgit_extra_args=""
352		if [ "${KERNEL_DEBUG_TIMESTAMPS}" != "1" ]; then
353		    kgit_extra_args="--commit-sha author"
354		fi
355		kgit-s2q --gen -v $kgit_extra_args --patches .kernel-meta/
356		if [ $? -ne 0 ]; then
357			bberror "Could not apply patches for ${KMACHINE}."
358			bbfatal_log "Patch failures can be resolved in the linux source directory ${S})"
359		fi
360	fi
361
362	if [ -f "${meta_dir}/merge.queue" ]; then
363		# we need to merge all these branches
364		for b in $(cat ${meta_dir}/merge.queue); do
365			git show-ref --verify --quiet refs/heads/${b}
366			if [ $? -eq 0 ]; then
367				bbnote "Merging branch ${b}"
368				git merge -q --no-ff -m "Merge branch ${b}" ${b}
369			else
370				bbfatal "branch ${b} does not exist, cannot merge"
371			fi
372		done
373	fi
374
375	set -e
376}
377
378do_kernel_checkout() {
379	set +e
380
381	source_dir=`echo ${S} | sed 's%/$%%'`
382	source_workdir="${WORKDIR}/git"
383	if [ -d "${WORKDIR}/git/" ]; then
384		# case: git repository
385		# if S is WORKDIR/git, then we shouldn't be moving or deleting the tree.
386		if [ "${source_dir}" != "${source_workdir}" ]; then
387			if [ -d "${source_workdir}/.git" ]; then
388				# regular git repository with .git
389				rm -rf ${S}
390				mv ${WORKDIR}/git ${S}
391			else
392				# create source for bare cloned git repository
393				git clone ${WORKDIR}/git ${S}
394				rm -rf ${WORKDIR}/git
395			fi
396		fi
397		cd ${S}
398
399		# convert any remote branches to local tracking ones
400		for i in `git branch -a --no-color | grep remotes | grep -v HEAD`; do
401			b=`echo $i | cut -d' ' -f2 | sed 's%remotes/origin/%%'`;
402			git show-ref --quiet --verify -- "refs/heads/$b"
403			if [ $? -ne 0 ]; then
404				git branch $b $i > /dev/null
405			fi
406		done
407
408		# Create a working tree copy of the kernel by checking out a branch
409		machine_branch="${@ get_machine_branch(d, "${KBRANCH}" )}"
410
411		# checkout and clobber any unimportant files
412		git checkout -f ${machine_branch}
413	else
414		# case: we have no git repository at all.
415		# To support low bandwidth options for building the kernel, we'll just
416		# convert the tree to a git repo and let the rest of the process work unchanged
417
418		# if ${S} hasn't been set to the proper subdirectory a default of "linux" is
419		# used, but we can't initialize that empty directory. So check it and throw a
420		# clear error
421
422	        cd ${S}
423		if [ ! -f "Makefile" ]; then
424			bberror "S is not set to the linux source directory. Check "
425			bbfatal "the recipe and set S to the proper extracted subdirectory"
426		fi
427		rm -f .gitignore
428		git init
429		check_git_config
430		git add .
431		git commit -q -n -m "baseline commit: creating repo for ${PN}-${PV}"
432		git clean -d -f
433	fi
434
435	set -e
436}
437do_kernel_checkout[dirs] = "${S} ${WORKDIR}"
438
439addtask kernel_checkout before do_kernel_metadata after do_symlink_kernsrc
440addtask kernel_metadata after do_validate_branches do_unpack before do_patch
441do_kernel_metadata[depends] = "kern-tools-native:do_populate_sysroot"
442do_kernel_metadata[file-checksums] = " ${@get_dirs_with_fragments(d)}"
443do_validate_branches[depends] = "kern-tools-native:do_populate_sysroot"
444
445do_kernel_configme[depends] += "virtual/${TARGET_PREFIX}binutils:do_populate_sysroot"
446do_kernel_configme[depends] += "virtual/${TARGET_PREFIX}gcc:do_populate_sysroot"
447do_kernel_configme[depends] += "bc-native:do_populate_sysroot bison-native:do_populate_sysroot"
448do_kernel_configme[depends] += "kern-tools-native:do_populate_sysroot"
449do_kernel_configme[dirs] += "${S} ${B}"
450do_kernel_configme() {
451	do_kernel_metadata config
452
453	# translate the kconfig_mode into something that merge_config.sh
454	# understands
455	case ${KCONFIG_MODE} in
456		*allnoconfig)
457			config_flags="-n"
458			;;
459		*alldefconfig)
460			config_flags=""
461			;;
462		*)
463			if [ -f ${WORKDIR}/defconfig ]; then
464				config_flags="-n"
465			fi
466			;;
467	esac
468
469	cd ${S}
470
471	meta_dir=$(kgit --meta)
472	configs="$(scc --configs -o ${meta_dir})"
473	if [ $? -ne 0 ]; then
474		bberror "${configs}"
475		bbfatal_log "Could not find configuration queue (${meta_dir}/config.queue)"
476	fi
477
478	CFLAGS="${CFLAGS} ${TOOLCHAIN_OPTIONS}" HOSTCC="${BUILD_CC} ${BUILD_CFLAGS} ${BUILD_LDFLAGS}" HOSTCPP="${BUILD_CPP}" CC="${KERNEL_CC}" LD="${KERNEL_LD}" OBJCOPY="${KERNEL_OBJCOPY}" STRIP="${KERNEL_STRIP}" ARCH=${ARCH} merge_config.sh -O ${B} ${config_flags} ${configs} > ${meta_dir}/cfg/merge_config_build.log 2>&1
479	if [ $? -ne 0 -o ! -f ${B}/.config ]; then
480		bberror "Could not generate a .config for ${KMACHINE}-${LINUX_KERNEL_TYPE}"
481		if [ ${KCONF_AUDIT_LEVEL} -gt 1 ]; then
482			bbfatal_log "`cat ${meta_dir}/cfg/merge_config_build.log`"
483		else
484			bbfatal_log "Details can be found at: ${S}/${meta_dir}/cfg/merge_config_build.log"
485		fi
486	fi
487
488	if [ ! -z "${LINUX_VERSION_EXTENSION}" ]; then
489		echo "# Global settings from linux recipe" >> ${B}/.config
490		echo "CONFIG_LOCALVERSION="\"${LINUX_VERSION_EXTENSION}\" >> ${B}/.config
491	fi
492}
493
494addtask kernel_configme before do_configure after do_patch
495addtask config_analysis
496
497do_config_analysis[depends] = "virtual/kernel:do_configure"
498do_config_analysis[depends] += "kern-tools-native:do_populate_sysroot"
499
500CONFIG_AUDIT_FILE ?= "${WORKDIR}/config-audit.txt"
501CONFIG_ANALYSIS_FILE ?= "${WORKDIR}/config-analysis.txt"
502
503python do_config_analysis() {
504    import re, string, sys, subprocess
505
506    s = d.getVar('S')
507
508    env = os.environ.copy()
509    env['PATH'] = "%s:%s%s" % (d.getVar('PATH'), s, "/scripts/util/")
510    env['LD'] = d.getVar('KERNEL_LD')
511    env['CC'] = d.getVar('KERNEL_CC')
512    env['OBJCOPY'] = d.getVar('KERNEL_OBJCOPY')
513    env['STRIP'] = d.getVar('KERNEL_STRIP')
514    env['ARCH'] = d.getVar('ARCH')
515    env['srctree'] = s
516
517    # read specific symbols from the kernel recipe or from local.conf
518    # i.e.: CONFIG_ANALYSIS:pn-linux-yocto-dev = 'NF_CONNTRACK LOCALVERSION'
519    config = d.getVar( 'CONFIG_ANALYSIS' )
520    if not config:
521       config = [ "" ]
522    else:
523       config = config.split()
524
525    for c in config:
526        for action in ["analysis","audit"]:
527            if action == "analysis":
528                try:
529                    analysis = subprocess.check_output(['symbol_why.py', '--dotconfig',  '{}'.format( d.getVar('B') + '/.config' ), '--blame', c], cwd=s, env=env ).decode('utf-8')
530                except subprocess.CalledProcessError as e:
531                    bb.fatal( "config analysis failed when running '%s': %s" % (" ".join(e.cmd), e.output.decode('utf-8')))
532
533                outfile = d.getVar( 'CONFIG_ANALYSIS_FILE' )
534
535            if action == "audit":
536                try:
537                    analysis = subprocess.check_output(['symbol_why.py', '--dotconfig',  '{}'.format( d.getVar('B') + '/.config' ), '--summary', '--extended', '--sanity', c], cwd=s, env=env ).decode('utf-8')
538                except subprocess.CalledProcessError as e:
539                    bb.fatal( "config analysis failed when running '%s': %s" % (" ".join(e.cmd), e.output.decode('utf-8')))
540
541                outfile = d.getVar( 'CONFIG_AUDIT_FILE' )
542
543            if c:
544                outdir = os.path.dirname( outfile )
545                outname = os.path.basename( outfile )
546                outfile = outdir + '/'+ c + '-' + outname
547
548            if config and os.path.isfile(outfile):
549                os.remove(outfile)
550
551            with open(outfile, 'w+') as f:
552                f.write( analysis )
553
554            bb.warn( "Configuration {} executed, see: {} for details".format(action,outfile ))
555            if c:
556                bb.warn( analysis )
557}
558
559python do_kernel_configcheck() {
560    import re, string, sys, subprocess
561
562    s = d.getVar('S')
563
564    # if KMETA isn't set globally by a recipe using this routine, use kgit to
565    # locate or create the meta directory. Otherwise, kconf_check is not
566    # passed a valid meta-series for processing
567    kmeta = d.getVar("KMETA")
568    if not kmeta or not os.path.exists('{}/{}'.format(s,kmeta)):
569        kmeta = subprocess.check_output(['kgit', '--meta'], cwd=d.getVar('S')).decode('utf-8').rstrip()
570
571    env = os.environ.copy()
572    env['PATH'] = "%s:%s%s" % (d.getVar('PATH'), s, "/scripts/util/")
573    env['LD'] = d.getVar('KERNEL_LD')
574    env['CC'] = d.getVar('KERNEL_CC')
575    env['OBJCOPY'] = d.getVar('KERNEL_OBJCOPY')
576    env['STRIP'] = d.getVar('KERNEL_STRIP')
577    env['ARCH'] = d.getVar('ARCH')
578    env['srctree'] = s
579
580    try:
581        configs = subprocess.check_output(['scc', '--configs', '-o', s + '/.kernel-meta'], env=env).decode('utf-8')
582    except subprocess.CalledProcessError as e:
583        bb.fatal( "Cannot gather config fragments for audit: %s" % e.output.decode("utf-8") )
584
585    config_check_visibility = int(d.getVar("KCONF_AUDIT_LEVEL") or 0)
586    bsp_check_visibility = int(d.getVar("KCONF_BSP_AUDIT_LEVEL") or 0)
587    kmeta_audit_werror = d.getVar("KMETA_AUDIT_WERROR") or ""
588    warnings_detected = False
589
590    # if config check visibility is "1", that's the lowest level of audit. So
591    # we add the --classify option to the run, since classification will
592    # streamline the output to only report options that could be boot issues,
593    # or are otherwise required for proper operation.
594    extra_params = ""
595    if config_check_visibility == 1:
596       extra_params = "--classify"
597
598    # category #1: mismatches
599    try:
600        analysis = subprocess.check_output(['symbol_why.py', '--dotconfig',  '{}'.format( d.getVar('B') + '/.config' ), '--mismatches', extra_params], cwd=s, env=env ).decode('utf-8')
601    except subprocess.CalledProcessError as e:
602        bb.fatal( "config analysis failed when running '%s': %s" % (" ".join(e.cmd), e.output.decode('utf-8')))
603
604    if analysis:
605        outfile = "{}/{}/cfg/mismatch.txt".format( s, kmeta )
606        if os.path.isfile(outfile):
607           os.remove(outfile)
608        with open(outfile, 'w+') as f:
609            f.write( analysis )
610
611        if config_check_visibility and os.stat(outfile).st_size > 0:
612            with open (outfile, "r") as myfile:
613                results = myfile.read()
614                bb.warn( "[kernel config]: specified values did not make it into the kernel's final configuration:\n\n%s" % results)
615                warnings_detected = True
616
617    # category #2: invalid fragment elements
618    extra_params = ""
619    if bsp_check_visibility > 1:
620        extra_params = "--strict"
621    try:
622        analysis = subprocess.check_output(['symbol_why.py', '--dotconfig',  '{}'.format( d.getVar('B') + '/.config' ), '--invalid', extra_params], cwd=s, env=env ).decode('utf-8')
623    except subprocess.CalledProcessError as e:
624        bb.fatal( "config analysis failed when running '%s': %s" % (" ".join(e.cmd), e.output.decode('utf-8')))
625
626    if analysis:
627        outfile = "{}/{}/cfg/invalid.txt".format(s,kmeta)
628        if os.path.isfile(outfile):
629           os.remove(outfile)
630        with open(outfile, 'w+') as f:
631            f.write( analysis )
632
633        if bsp_check_visibility and os.stat(outfile).st_size > 0:
634            with open (outfile, "r") as myfile:
635                results = myfile.read()
636                bb.warn( "[kernel config]: This BSP contains fragments with warnings:\n\n%s" % results)
637                warnings_detected = True
638
639    # category #3: redefined options (this is pretty verbose and is debug only)
640    try:
641        analysis = subprocess.check_output(['symbol_why.py', '--dotconfig',  '{}'.format( d.getVar('B') + '/.config' ), '--sanity'], cwd=s, env=env ).decode('utf-8')
642    except subprocess.CalledProcessError as e:
643        bb.fatal( "config analysis failed when running '%s': %s" % (" ".join(e.cmd), e.output.decode('utf-8')))
644
645    if analysis:
646        outfile = "{}/{}/cfg/redefinition.txt".format(s,kmeta)
647        if os.path.isfile(outfile):
648           os.remove(outfile)
649        with open(outfile, 'w+') as f:
650            f.write( analysis )
651
652        # if the audit level is greater than two, we report if a fragment has overriden
653        # a value from a base fragment. This is really only used for new kernel introduction
654        if bsp_check_visibility > 2 and os.stat(outfile).st_size > 0:
655            with open (outfile, "r") as myfile:
656                results = myfile.read()
657                bb.warn( "[kernel config]: This BSP has configuration options defined in more than one config, with differing values:\n\n%s" % results)
658                warnings_detected = True
659
660    if warnings_detected and kmeta_audit_werror:
661        bb.fatal( "configuration warnings detected, werror is set, promoting to fatal" )
662}
663
664# Ensure that the branches (BSP and meta) are on the locations specified by
665# their SRCREV values. If they are NOT on the right commits, the branches
666# are corrected to the proper commit.
667do_validate_branches() {
668	set +e
669	cd ${S}
670
671	machine_branch="${@ get_machine_branch(d, "${KBRANCH}" )}"
672	machine_srcrev="${SRCREV_machine}"
673
674	# if SRCREV is AUTOREV it shows up as AUTOINC there's nothing to
675	# check and we can exit early
676	if [ "${machine_srcrev}" = "AUTOINC" ]; then
677	    linux_yocto_dev='${@oe.utils.conditional("PREFERRED_PROVIDER_virtual/kernel", "linux-yocto-dev", "1", "", d)}'
678	    if [ -n "$linux_yocto_dev" ]; then
679		git checkout -q -f ${machine_branch}
680		ver=$(grep "^VERSION =" ${S}/Makefile | sed s/.*=\ *//)
681		patchlevel=$(grep "^PATCHLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
682		sublevel=$(grep "^SUBLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
683		kver="$ver.$patchlevel"
684		bbnote "dev kernel: performing version -> branch -> SRCREV validation"
685		bbnote "dev kernel: recipe version ${LINUX_VERSION}, src version: $kver"
686		echo "${LINUX_VERSION}" | grep -q $kver
687		if [ $? -ne 0 ]; then
688		    version="$(echo ${LINUX_VERSION} | sed 's/\+.*$//g')"
689		    versioned_branch="v$version/$machine_branch"
690
691		    machine_branch=$versioned_branch
692		    force_srcrev="$(git rev-parse $machine_branch 2> /dev/null)"
693		    if [ $? -ne 0 ]; then
694			bbfatal "kernel version mismatch detected, and no valid branch $machine_branch detected"
695		    fi
696
697		    bbnote "dev kernel: adjusting branch to $machine_branch, srcrev to: $force_srcrev"
698		fi
699	    else
700		bbnote "SRCREV validation is not required for AUTOREV"
701	    fi
702	elif [ "${machine_srcrev}" = "" ]; then
703		if [ "${SRCREV}" != "AUTOINC" ] && [ "${SRCREV}" != "INVALID" ]; then
704		       # SRCREV_machine_<MACHINE> was not set. This means that a custom recipe
705		       # that doesn't use the SRCREV_FORMAT "machine_meta" is being built. In
706		       # this case, we need to reset to the give SRCREV before heading to patching
707		       bbnote "custom recipe is being built, forcing SRCREV to ${SRCREV}"
708		       force_srcrev="${SRCREV}"
709		fi
710	else
711		git cat-file -t ${machine_srcrev} > /dev/null
712		if [ $? -ne 0 ]; then
713			bberror "${machine_srcrev} is not a valid commit ID."
714			bbfatal_log "The kernel source tree may be out of sync"
715		fi
716		force_srcrev=${machine_srcrev}
717	fi
718
719	git checkout -q -f ${machine_branch}
720	if [ -n "${force_srcrev}" ]; then
721		# see if the branch we are about to patch has been properly reset to the defined
722		# SRCREV .. if not, we reset it.
723		branch_head=`git rev-parse HEAD`
724		if [ "${force_srcrev}" != "${branch_head}" ]; then
725			current_branch=`git rev-parse --abbrev-ref HEAD`
726			git branch "$current_branch-orig"
727			git reset --hard ${force_srcrev}
728			# We've checked out HEAD, make sure we cleanup kgit-s2q fence post check
729			# so the patches are applied as expected otherwise no patching
730			# would be done in some corner cases.
731			kgit-s2q --clean
732		fi
733	fi
734
735	set -e
736}
737
738OE_TERMINAL_EXPORTS += "KBUILD_OUTPUT"
739KBUILD_OUTPUT = "${B}"
740
741python () {
742    # If diffconfig is available, ensure it runs after kernel_configme
743    if 'do_diffconfig' in d:
744        bb.build.addtask('do_diffconfig', None, 'do_kernel_configme', d)
745
746    externalsrc = d.getVar('EXTERNALSRC')
747    if externalsrc:
748        # If we deltask do_patch, do_kernel_configme is left without
749        # dependencies and runs too early
750        d.setVarFlag('do_kernel_configme', 'deps', (d.getVarFlag('do_kernel_configme', 'deps', False) or []) + ['do_unpack'])
751}
752
753# extra tasks
754addtask kernel_version_sanity_check after do_kernel_metadata do_kernel_checkout before do_compile
755addtask validate_branches before do_patch after do_kernel_checkout
756addtask kernel_configcheck after do_configure before do_compile
757