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