blob: fb30c7cc055e6c65b22ce6e9c6eeeb22d754287c [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# remove tasks that modify the source tree in case externalsrc is inherited
Andrew Geissler82c905d2020-04-13 13:39:40 -05002SRCTREECOVEREDTASKS += "do_validate_branches do_kernel_configcheck do_kernel_checkout do_fetch do_unpack do_patch"
Patrick Williamsc0f7c042017-02-23 20:41:17 -06003PATCH_GIT_USER_EMAIL ?= "kernel-yocto@oe"
4PATCH_GIT_USER_NAME ?= "OpenEmbedded"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005
Andrew Geissler82c905d2020-04-13 13:39:40 -05006# The distro or local.conf should set this, but if nobody cares...
7LINUX_KERNEL_TYPE ??= "standard"
8
9# KMETA ?= ""
10KBRANCH ?= "master"
11KMACHINE ?= "${MACHINE}"
12SRCREV_FORMAT ?= "meta_machine"
13
14# LEVELS:
15# 0: no reporting
16# 1: report options that are specified, but not in the final config
17# 2: report options that are not hardware related, but set by a BSP
18KCONF_AUDIT_LEVEL ?= "1"
19KCONF_BSP_AUDIT_LEVEL ?= "0"
20KMETA_AUDIT ?= "yes"
Andrew Geisslerc9f78652020-09-18 14:11:35 -050021KMETA_AUDIT_WERROR ?= ""
Andrew Geissler82c905d2020-04-13 13:39:40 -050022
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023# returns local (absolute) path names for all valid patches in the
24# src_uri
Brad Bishop19323692019-04-05 15:28:33 -040025def find_patches(d,subdir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 patches = src_patches(d)
27 patch_list=[]
28 for p in patches:
Brad Bishop19323692019-04-05 15:28:33 -040029 _, _, local, _, _, parm = bb.fetch.decodeurl(p)
30 # if patchdir has been passed, we won't be able to apply it so skip
31 # the patch for now, and special processing happens later
32 patchdir = ''
33 if "patchdir" in parm:
34 patchdir = parm["patchdir"]
35 if subdir:
36 if subdir == patchdir:
37 patch_list.append(local)
38 else:
Andrew Geisslereff27472021-10-29 15:35:00 -050039 # skip the patch if a patchdir was supplied, it won't be handled
40 # properly
41 if not patchdir:
42 patch_list.append(local)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043
44 return patch_list
45
46# returns all the elements from the src uri that are .scc files
47def find_sccs(d):
48 sources=src_patches(d, True)
49 sources_list=[]
50 for s in sources:
51 base, ext = os.path.splitext(os.path.basename(s))
52 if ext and ext in [".scc", ".cfg"]:
53 sources_list.append(s)
Andrew Geissler82c905d2020-04-13 13:39:40 -050054 elif base and 'defconfig' in base:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 sources_list.append(s)
56
57 return sources_list
58
59# check the SRC_URI for "kmeta" type'd git repositories. Return the name of
60# the repository as it will be found in WORKDIR
61def find_kernel_feature_dirs(d):
62 feature_dirs=[]
63 fetch = bb.fetch2.Fetch([], d)
64 for url in fetch.urls:
65 urldata = fetch.ud[url]
66 parm = urldata.parm
67 type=""
68 if "type" in parm:
69 type = parm["type"]
70 if "destsuffix" in parm:
71 destdir = parm["destsuffix"]
72 if type == "kmeta":
73 feature_dirs.append(destdir)
74
75 return feature_dirs
76
77# find the master/machine source branch. In the same way that the fetcher proceses
78# git repositories in the SRC_URI we take the first repo found, first branch.
79def get_machine_branch(d, default):
80 fetch = bb.fetch2.Fetch([], d)
81 for url in fetch.urls:
82 urldata = fetch.ud[url]
83 parm = urldata.parm
84 if "branch" in parm:
85 branches = urldata.parm.get("branch").split(',')
Patrick Williamsf1e5d692016-03-30 15:21:19 -050086 btype = urldata.parm.get("type")
87 if btype != "kmeta":
88 return branches[0]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089
90 return default
91
Andrew Geisslerc9f78652020-09-18 14:11:35 -050092# returns a list of all directories that are on FILESEXTRAPATHS (and
93# hence available to the build) that contain .scc or .cfg files
94def get_dirs_with_fragments(d):
95 extrapaths = []
96 extrafiles = []
97 extrapathsvalue = (d.getVar("FILESEXTRAPATHS") or "")
98 # Remove default flag which was used for checking
99 extrapathsvalue = extrapathsvalue.replace("__default:", "")
100 extrapaths = extrapathsvalue.split(":")
101 for path in extrapaths:
102 if path + ":True" not in extrafiles:
103 extrafiles.append(path + ":" + str(os.path.exists(path)))
104
105 return " ".join(extrafiles)
106
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107do_kernel_metadata() {
108 set +e
Andrew Geissler635e0e42020-08-21 15:58:33 -0500109
110 if [ -n "$1" ]; then
111 mode="$1"
112 else
113 mode="patch"
114 fi
115
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 cd ${S}
117 export KMETA=${KMETA}
118
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700119 bbnote "do_kernel_metadata: for summary/debug, set KCONF_AUDIT_LEVEL > 0"
120
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 # if kernel tools are available in-tree, they are preferred
122 # and are placed on the path before any external tools. Unless
123 # the external tools flag is set, in that case we do nothing.
124 if [ -f "${S}/scripts/util/configme" ]; then
125 if [ -z "${EXTERNAL_KERNEL_TOOLS}" ]; then
126 PATH=${S}/scripts/util:${PATH}
127 fi
128 fi
129
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130 # In a similar manner to the kernel itself:
131 #
132 # defconfig: $(obj)/conf
133 # ifeq ($(KBUILD_DEFCONFIG),)
134 # $< --defconfig $(Kconfig)
135 # else
136 # @echo "*** Default configuration is based on '$(KBUILD_DEFCONFIG)'"
137 # $(Q)$< --defconfig=arch/$(SRCARCH)/configs/$(KBUILD_DEFCONFIG) $(Kconfig)
138 # endif
139 #
140 # If a defconfig is specified via the KBUILD_DEFCONFIG variable, we copy it
141 # from the source tree, into a common location and normalized "defconfig" name,
142 # where the rest of the process will include and incoroporate it into the build
143 #
144 # If the fetcher has already placed a defconfig in WORKDIR (from the SRC_URI),
145 # we don't overwrite it, but instead warn the user that SRC_URI defconfigs take
146 # precendence.
147 #
148 if [ -n "${KBUILD_DEFCONFIG}" ]; then
149 if [ -f "${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG}" ]; then
150 if [ -f "${WORKDIR}/defconfig" ]; then
Andrew Geissler635e0e42020-08-21 15:58:33 -0500151 # If the two defconfig's are different, warn that we overwrote the
152 # one already placed in WORKDIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 cmp "${WORKDIR}/defconfig" "${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG}"
154 if [ $? -ne 0 ]; then
Andrew Geissler635e0e42020-08-21 15:58:33 -0500155 bbdebug 1 "detected SRC_URI or unpatched defconfig in WORKDIR. ${KBUILD_DEFCONFIG} copied over it"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 fi
Andrew Geissler635e0e42020-08-21 15:58:33 -0500157 cp -f ${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG} ${WORKDIR}/defconfig
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158 else
159 cp -f ${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG} ${WORKDIR}/defconfig
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 fi
Andrew Geissler475cb722020-07-10 16:00:51 -0500161 in_tree_defconfig="${WORKDIR}/defconfig"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162 else
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500163 bbfatal "A KBUILD_DEFCONFIG '${KBUILD_DEFCONFIG}' was specified, but not present in the source tree (${S}/arch/${ARCH}/configs/)"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164 fi
165 fi
166
Andrew Geissler635e0e42020-08-21 15:58:33 -0500167 if [ "$mode" = "patch" ]; then
168 # was anyone trying to patch the kernel meta data ?, we need to do
169 # this here, since the scc commands migrate the .cfg fragments to the
170 # kernel source tree, where they'll be used later.
171 check_git_config
172 patches="${@" ".join(find_patches(d,'kernel-meta'))}"
173 for p in $patches; do
174 (
175 cd ${WORKDIR}/kernel-meta
176 git am -s $p
177 )
178 done
179 fi
Brad Bishop19323692019-04-05 15:28:33 -0400180
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500181 sccs_from_src_uri="${@" ".join(find_sccs(d))}"
Brad Bishop19323692019-04-05 15:28:33 -0400182 patches="${@" ".join(find_patches(d,''))}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 feat_dirs="${@" ".join(find_kernel_feature_dirs(d))}"
184
Andrew Geissler475cb722020-07-10 16:00:51 -0500185 # a quick check to make sure we don't have duplicate defconfigs If
186 # there's a defconfig in the SRC_URI, did we also have one from the
187 # KBUILD_DEFCONFIG processing above ?
188 src_uri_defconfig=$(echo $sccs_from_src_uri | awk '(match($0, "defconfig") != 0) { print $0 }' RS=' ')
189 # drop and defconfig's from the src_uri variable, we captured it just above here if it existed
190 sccs_from_src_uri=$(echo $sccs_from_src_uri | awk '(match($0, "defconfig") == 0) { print $0 }' RS=' ')
191
192 if [ -n "$in_tree_defconfig" ]; then
193 sccs_defconfig=$in_tree_defconfig
194 if [ -n "$src_uri_defconfig" ]; then
195 bbwarn "[NOTE]: defconfig was supplied both via KBUILD_DEFCONFIG and SRC_URI. Dropping SRC_URI defconfig"
196 fi
197 else
198 # if we didn't have an in-tree one, make our defconfig the one
199 # from the src_uri. Note: there may not have been one from the
200 # src_uri, so this can be an empty variable.
201 sccs_defconfig=$src_uri_defconfig
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500202 fi
Andrew Geissler475cb722020-07-10 16:00:51 -0500203 sccs="$sccs_from_src_uri"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500204
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 # check for feature directories/repos/branches that were part of the
206 # SRC_URI. If they were supplied, we convert them into include directives
207 # for the update part of the process
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600208 for f in ${feat_dirs}; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500209 if [ -d "${WORKDIR}/$f/meta" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600210 includes="$includes -I${WORKDIR}/$f/kernel-meta"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800211 elif [ -d "${WORKDIR}/../oe-local-files/$f" ]; then
212 includes="$includes -I${WORKDIR}/../oe-local-files/$f"
Brad Bishop19323692019-04-05 15:28:33 -0400213 elif [ -d "${WORKDIR}/$f" ]; then
214 includes="$includes -I${WORKDIR}/$f"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500215 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600216 done
217 for s in ${sccs} ${patches}; do
218 sdir=$(dirname $s)
219 includes="$includes -I${sdir}"
220 # if a SRC_URI passed patch or .scc has a subdir of "kernel-meta",
221 # then we add it to the search path
222 if [ -d "${sdir}/kernel-meta" ]; then
223 includes="$includes -I${sdir}/kernel-meta"
224 fi
225 done
226
227 # expand kernel features into their full path equivalents
228 bsp_definition=$(spp ${includes} --find -DKMACHINE=${KMACHINE} -DKTYPE=${LINUX_KERNEL_TYPE})
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500229 if [ -z "$bsp_definition" ]; then
Andrew Geissler475cb722020-07-10 16:00:51 -0500230 if [ -z "$sccs_defconfig" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500231 bbfatal_log "Could not locate BSP definition for ${KMACHINE}/${LINUX_KERNEL_TYPE} and no defconfig was provided"
232 fi
Andrew Geissler475cb722020-07-10 16:00:51 -0500233 else
Andrew Geissler82c905d2020-04-13 13:39:40 -0500234 # if the bsp definition has "define KMETA_EXTERNAL_BSP t",
235 # then we need to set a flag that will instruct the next
236 # steps to use the BSP as both configuration and patches.
237 grep -q KMETA_EXTERNAL_BSP $bsp_definition
238 if [ $? -eq 0 ]; then
239 KMETA_EXTERNAL_BSPS="t"
240 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500241 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600242 meta_dir=$(kgit --meta)
243
Andrew Geissler635e0e42020-08-21 15:58:33 -0500244 KERNEL_FEATURES_FINAL=""
245 if [ -n "${KERNEL_FEATURES}" ]; then
246 for feature in ${KERNEL_FEATURES}; do
247 feature_found=f
248 for d in $includes; do
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500249 path_to_check=$(echo $d | sed 's/^-I//')
Andrew Geissler635e0e42020-08-21 15:58:33 -0500250 if [ "$feature_found" = "f" ] && [ -e "$path_to_check/$feature" ]; then
251 feature_found=t
252 fi
253 done
254 if [ "$feature_found" = "f" ]; then
255 if [ -n "${KERNEL_DANGLING_FEATURES_WARN_ONLY}" ]; then
256 bbwarn "Feature '$feature' not found, but KERNEL_DANGLING_FEATURES_WARN_ONLY is set"
257 bbwarn "This may cause runtime issues, dropping feature and allowing configuration to continue"
258 else
259 bberror "Feature '$feature' not found, this will cause configuration failures."
260 bberror "Check the SRC_URI for meta-data repositories or directories that may be missing"
261 bbfatal_log "Set KERNEL_DANGLING_FEATURES_WARN_ONLY to ignore this issue"
262 fi
263 else
264 KERNEL_FEATURES_FINAL="$KERNEL_FEATURES_FINAL $feature"
265 fi
266 done
267 fi
268
269 if [ "$mode" = "config" ]; then
270 # run1: pull all the configuration fragments, no matter where they come from
271 elements="`echo -n ${bsp_definition} $sccs_defconfig ${sccs} ${patches} $KERNEL_FEATURES_FINAL`"
272 if [ -n "${elements}" ]; then
273 echo "${bsp_definition}" > ${S}/${meta_dir}/bsp_definition
274 scc --force -o ${S}/${meta_dir}:cfg,merge,meta ${includes} $sccs_defconfig $bsp_definition $sccs $patches $KERNEL_FEATURES_FINAL
275 if [ $? -ne 0 ]; then
276 bbfatal_log "Could not generate configuration queue for ${KMACHINE}."
277 fi
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500278 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279 fi
280
Andrew Geissler82c905d2020-04-13 13:39:40 -0500281 # if KMETA_EXTERNAL_BSPS has been set, or it has been detected from
282 # the bsp definition, then we inject the bsp_definition into the
283 # patch phase below. we'll piggy back on the sccs variable.
284 if [ -n "${KMETA_EXTERNAL_BSPS}" ]; then
285 sccs="${bsp_definition} ${sccs}"
286 fi
287
Andrew Geissler635e0e42020-08-21 15:58:33 -0500288 if [ "$mode" = "patch" ]; then
289 # run2: only generate patches for elements that have been passed on the SRC_URI
290 elements="`echo -n ${sccs} ${patches} $KERNEL_FEATURES_FINAL`"
291 if [ -n "${elements}" ]; then
292 scc --force -o ${S}/${meta_dir}:patch --cmds patch ${includes} ${sccs} ${patches} $KERNEL_FEATURES_FINAL
293 if [ $? -ne 0 ]; then
294 bbfatal_log "Could not generate configuration queue for ${KMACHINE}."
295 fi
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500296 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500297 fi
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700298
299 if [ ${KCONF_AUDIT_LEVEL} -gt 0 ]; then
300 bbnote "kernel meta data summary for ${KMACHINE} (${LINUX_KERNEL_TYPE}):"
301 bbnote "======================================================================"
302 if [ -n "${KMETA_EXTERNAL_BSPS}" ]; then
303 bbnote "Non kernel-cache (external) bsp"
304 fi
305 bbnote "BSP entry point / definition: $bsp_definition"
306 if [ -n "$in_tree_defconfig" ]; then
307 bbnote "KBUILD_DEFCONFIG: ${KBUILD_DEFCONFIG}"
308 fi
309 bbnote "Fragments from SRC_URI: $sccs_from_src_uri"
310 bbnote "KERNEL_FEATURES: $KERNEL_FEATURES_FINAL"
311 bbnote "Final scc/cfg list: $sccs_defconfig $bsp_definition $sccs $KERNEL_FEATURES_FINAL"
312 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500313}
314
315do_patch() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500316 set +e
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500317 cd ${S}
318
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600319 check_git_config
320 meta_dir=$(kgit --meta)
321 (cd ${meta_dir}; ln -sf patch.queue series)
322 if [ -f "${meta_dir}/series" ]; then
Patrick Williams03907ee2022-05-01 06:28:52 -0500323 kgit_extra_args=""
324 if [ "${KERNEL_DEBUG_TIMESTAMPS}" != "1" ]; then
325 kgit_extra_args="--commit-sha author"
326 fi
327 kgit-s2q --gen -v $kgit_extra_args --patches .kernel-meta/
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600328 if [ $? -ne 0 ]; then
329 bberror "Could not apply patches for ${KMACHINE}."
330 bbfatal_log "Patch failures can be resolved in the linux source directory ${S})"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500331 fi
332 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500333
334 if [ -f "${meta_dir}/merge.queue" ]; then
335 # we need to merge all these branches
336 for b in $(cat ${meta_dir}/merge.queue); do
337 git show-ref --verify --quiet refs/heads/${b}
338 if [ $? -eq 0 ]; then
339 bbnote "Merging branch ${b}"
340 git merge -q --no-ff -m "Merge branch ${b}" ${b}
341 else
342 bbfatal "branch ${b} does not exist, cannot merge"
343 fi
344 done
345 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500346}
347
348do_kernel_checkout() {
349 set +e
350
351 source_dir=`echo ${S} | sed 's%/$%%'`
352 source_workdir="${WORKDIR}/git"
353 if [ -d "${WORKDIR}/git/" ]; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500354 # case: git repository
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500355 # if S is WORKDIR/git, then we shouldn't be moving or deleting the tree.
356 if [ "${source_dir}" != "${source_workdir}" ]; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500357 if [ -d "${source_workdir}/.git" ]; then
358 # regular git repository with .git
359 rm -rf ${S}
360 mv ${WORKDIR}/git ${S}
361 else
362 # create source for bare cloned git repository
363 git clone ${WORKDIR}/git ${S}
364 rm -rf ${WORKDIR}/git
365 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500366 fi
367 cd ${S}
Patrick Williams213cb262021-08-07 19:21:33 -0500368
369 # convert any remote branches to local tracking ones
370 for i in `git branch -a --no-color | grep remotes | grep -v HEAD`; do
371 b=`echo $i | cut -d' ' -f2 | sed 's%remotes/origin/%%'`;
372 git show-ref --quiet --verify -- "refs/heads/$b"
373 if [ $? -ne 0 ]; then
374 git branch $b $i > /dev/null
375 fi
376 done
377
378 # Create a working tree copy of the kernel by checking out a branch
379 machine_branch="${@ get_machine_branch(d, "${KBRANCH}" )}"
380
381 # checkout and clobber any unimportant files
382 git checkout -f ${machine_branch}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383 else
384 # case: we have no git repository at all.
385 # To support low bandwidth options for building the kernel, we'll just
386 # convert the tree to a git repo and let the rest of the process work unchanged
387
388 # if ${S} hasn't been set to the proper subdirectory a default of "linux" is
389 # used, but we can't initialize that empty directory. So check it and throw a
390 # clear error
391
392 cd ${S}
393 if [ ! -f "Makefile" ]; then
394 bberror "S is not set to the linux source directory. Check "
395 bbfatal "the recipe and set S to the proper extracted subdirectory"
396 fi
397 rm -f .gitignore
398 git init
Brad Bishop316dfdd2018-06-25 12:45:53 -0400399 check_git_config
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500400 git add .
401 git commit -q -m "baseline commit: creating repo for ${PN}-${PV}"
402 git clean -d -f
403 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500404}
Andrew Geisslerc926e172021-05-07 16:11:35 -0500405do_kernel_checkout[dirs] = "${S} ${WORKDIR}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406
Andrew Geissler82c905d2020-04-13 13:39:40 -0500407addtask kernel_checkout before do_kernel_metadata after do_symlink_kernsrc
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500408addtask kernel_metadata after do_validate_branches do_unpack before do_patch
409do_kernel_metadata[depends] = "kern-tools-native:do_populate_sysroot"
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500410do_kernel_metadata[file-checksums] = " ${@get_dirs_with_fragments(d)}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500411do_validate_branches[depends] = "kern-tools-native:do_populate_sysroot"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500412
William A. Kennington III47649fa2018-06-28 12:34:29 -0700413do_kernel_configme[depends] += "virtual/${TARGET_PREFIX}binutils:do_populate_sysroot"
414do_kernel_configme[depends] += "virtual/${TARGET_PREFIX}gcc:do_populate_sysroot"
415do_kernel_configme[depends] += "bc-native:do_populate_sysroot bison-native:do_populate_sysroot"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500416do_kernel_configme[depends] += "kern-tools-native:do_populate_sysroot"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500417do_kernel_configme[dirs] += "${S} ${B}"
418do_kernel_configme() {
Andrew Geissler635e0e42020-08-21 15:58:33 -0500419 do_kernel_metadata config
420
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600421 # translate the kconfig_mode into something that merge_config.sh
422 # understands
423 case ${KCONFIG_MODE} in
424 *allnoconfig)
425 config_flags="-n"
426 ;;
427 *alldefconfig)
428 config_flags=""
429 ;;
Andrew Geissler3b8a17c2021-04-15 15:55:55 -0500430 *)
431 if [ -f ${WORKDIR}/defconfig ]; then
432 config_flags="-n"
433 fi
434 ;;
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600435 esac
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500436
437 cd ${S}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600438
439 meta_dir=$(kgit --meta)
440 configs="$(scc --configs -o ${meta_dir})"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500441 if [ $? -ne 0 ]; then
442 bberror "${configs}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600443 bbfatal_log "Could not find configuration queue (${meta_dir}/config.queue)"
444 fi
445
Andrew Geissler82c905d2020-04-13 13:39:40 -0500446 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
447 if [ $? -ne 0 -o ! -f ${B}/.config ]; then
448 bberror "Could not generate a .config for ${KMACHINE}-${LINUX_KERNEL_TYPE}"
449 if [ ${KCONF_AUDIT_LEVEL} -gt 1 ]; then
450 bbfatal_log "`cat ${meta_dir}/cfg/merge_config_build.log`"
451 else
452 bbfatal_log "Details can be found at: ${S}/${meta_dir}/cfg/merge_config_build.log"
453 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500454 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600455
Andrew Geissler82c905d2020-04-13 13:39:40 -0500456 if [ ! -z "${LINUX_VERSION_EXTENSION}" ]; then
457 echo "# Global settings from linux recipe" >> ${B}/.config
458 echo "CONFIG_LOCALVERSION="\"${LINUX_VERSION_EXTENSION}\" >> ${B}/.config
459 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500460}
461
462addtask kernel_configme before do_configure after do_patch
Andrew Geissler635e0e42020-08-21 15:58:33 -0500463addtask config_analysis
464
465do_config_analysis[depends] = "virtual/kernel:do_configure"
466do_config_analysis[depends] += "kern-tools-native:do_populate_sysroot"
467
468CONFIG_AUDIT_FILE ?= "${WORKDIR}/config-audit.txt"
469CONFIG_ANALYSIS_FILE ?= "${WORKDIR}/config-analysis.txt"
470
471python do_config_analysis() {
472 import re, string, sys, subprocess
473
474 s = d.getVar('S')
475
476 env = os.environ.copy()
477 env['PATH'] = "%s:%s%s" % (d.getVar('PATH'), s, "/scripts/util/")
478 env['LD'] = d.getVar('KERNEL_LD')
479 env['CC'] = d.getVar('KERNEL_CC')
480 env['ARCH'] = d.getVar('ARCH')
481 env['srctree'] = s
482
483 # read specific symbols from the kernel recipe or from local.conf
Patrick Williams213cb262021-08-07 19:21:33 -0500484 # i.e.: CONFIG_ANALYSIS:pn-linux-yocto-dev = 'NF_CONNTRACK LOCALVERSION'
Andrew Geissler635e0e42020-08-21 15:58:33 -0500485 config = d.getVar( 'CONFIG_ANALYSIS' )
486 if not config:
487 config = [ "" ]
488 else:
489 config = config.split()
490
491 for c in config:
492 for action in ["analysis","audit"]:
493 if action == "analysis":
494 try:
495 analysis = subprocess.check_output(['symbol_why.py', '--dotconfig', '{}'.format( d.getVar('B') + '/.config' ), '--blame', c], cwd=s, env=env ).decode('utf-8')
496 except subprocess.CalledProcessError as e:
497 bb.fatal( "config analysis failed: %s" % e.output.decode('utf-8'))
498
499 outfile = d.getVar( 'CONFIG_ANALYSIS_FILE' )
500
501 if action == "audit":
502 try:
503 analysis = subprocess.check_output(['symbol_why.py', '--dotconfig', '{}'.format( d.getVar('B') + '/.config' ), '--summary', '--extended', '--sanity', c], cwd=s, env=env ).decode('utf-8')
504 except subprocess.CalledProcessError as e:
505 bb.fatal( "config analysis failed: %s" % e.output.decode('utf-8'))
506
507 outfile = d.getVar( 'CONFIG_AUDIT_FILE' )
508
509 if c:
510 outdir = os.path.dirname( outfile )
511 outname = os.path.basename( outfile )
512 outfile = outdir + '/'+ c + '-' + outname
513
514 if config and os.path.isfile(outfile):
515 os.remove(outfile)
516
517 with open(outfile, 'w+') as f:
518 f.write( analysis )
519
520 bb.warn( "Configuration {} executed, see: {} for details".format(action,outfile ))
521 if c:
522 bb.warn( analysis )
523}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500524
525python do_kernel_configcheck() {
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800526 import re, string, sys, subprocess
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500527
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800528 s = d.getVar('S')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600529
Patrick Williams03907ee2022-05-01 06:28:52 -0500530 # if KMETA isn't set globally by a recipe using this routine, use kgit to
531 # locate or create the meta directory. Otherwise, kconf_check is not
532 # passed a valid meta-series for processing
533 kmeta = d.getVar("KMETA")
534 if not kmeta or not os.path.exists('{}/{}'.format(s,kmeta)):
535 kmeta = subprocess.check_output(['kgit', '--meta'], cwd=d.getVar('S')).decode('utf-8').rstrip()
536
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800537 env = os.environ.copy()
538 env['PATH'] = "%s:%s%s" % (d.getVar('PATH'), s, "/scripts/util/")
Andrew Geissler635e0e42020-08-21 15:58:33 -0500539 env['LD'] = d.getVar('KERNEL_LD')
540 env['CC'] = d.getVar('KERNEL_CC')
541 env['ARCH'] = d.getVar('ARCH')
542 env['srctree'] = s
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600543
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800544 try:
545 configs = subprocess.check_output(['scc', '--configs', '-o', s + '/.kernel-meta'], env=env).decode('utf-8')
546 except subprocess.CalledProcessError as e:
547 bb.fatal( "Cannot gather config fragments for audit: %s" % e.output.decode("utf-8") )
548
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500549 config_check_visibility = int(d.getVar("KCONF_AUDIT_LEVEL") or 0)
550 bsp_check_visibility = int(d.getVar("KCONF_BSP_AUDIT_LEVEL") or 0)
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500551 kmeta_audit_werror = d.getVar("KMETA_AUDIT_WERROR") or ""
552 warnings_detected = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500553
Andrew Geissler635e0e42020-08-21 15:58:33 -0500554 # if config check visibility is "1", that's the lowest level of audit. So
555 # we add the --classify option to the run, since classification will
556 # streamline the output to only report options that could be boot issues,
557 # or are otherwise required for proper operation.
558 extra_params = ""
559 if config_check_visibility == 1:
560 extra_params = "--classify"
561
562 # category #1: mismatches
563 try:
564 analysis = subprocess.check_output(['symbol_why.py', '--dotconfig', '{}'.format( d.getVar('B') + '/.config' ), '--mismatches', extra_params], cwd=s, env=env ).decode('utf-8')
565 except subprocess.CalledProcessError as e:
566 bb.fatal( "config analysis failed: %s" % e.output.decode('utf-8'))
567
568 if analysis:
569 outfile = "{}/{}/cfg/mismatch.txt".format( s, kmeta )
570 if os.path.isfile(outfile):
571 os.remove(outfile)
572 with open(outfile, 'w+') as f:
573 f.write( analysis )
574
575 if config_check_visibility and os.stat(outfile).st_size > 0:
576 with open (outfile, "r") as myfile:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500577 results = myfile.read()
578 bb.warn( "[kernel config]: specified values did not make it into the kernel's final configuration:\n\n%s" % results)
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500579 warnings_detected = True
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800580
Andrew Geissler635e0e42020-08-21 15:58:33 -0500581 # category #2: invalid fragment elements
582 extra_params = ""
583 if bsp_check_visibility > 1:
584 extra_params = "--strict"
585 try:
586 analysis = subprocess.check_output(['symbol_why.py', '--dotconfig', '{}'.format( d.getVar('B') + '/.config' ), '--invalid', extra_params], cwd=s, env=env ).decode('utf-8')
587 except subprocess.CalledProcessError as e:
588 bb.fatal( "config analysis failed: %s" % e.output.decode('utf-8'))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800589
Andrew Geissler635e0e42020-08-21 15:58:33 -0500590 if analysis:
591 outfile = "{}/{}/cfg/invalid.txt".format(s,kmeta)
592 if os.path.isfile(outfile):
593 os.remove(outfile)
594 with open(outfile, 'w+') as f:
595 f.write( analysis )
596
597 if bsp_check_visibility and os.stat(outfile).st_size > 0:
598 with open (outfile, "r") as myfile:
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500599 results = myfile.read()
600 bb.warn( "[kernel config]: This BSP contains fragments with warnings:\n\n%s" % results)
601 warnings_detected = True
Andrew Geissler635e0e42020-08-21 15:58:33 -0500602
603 # category #3: redefined options (this is pretty verbose and is debug only)
604 try:
605 analysis = subprocess.check_output(['symbol_why.py', '--dotconfig', '{}'.format( d.getVar('B') + '/.config' ), '--sanity'], cwd=s, env=env ).decode('utf-8')
606 except subprocess.CalledProcessError as e:
607 bb.fatal( "config analysis failed: %s" % e.output.decode('utf-8'))
608
609 if analysis:
610 outfile = "{}/{}/cfg/redefinition.txt".format(s,kmeta)
611 if os.path.isfile(outfile):
612 os.remove(outfile)
613 with open(outfile, 'w+') as f:
614 f.write( analysis )
615
616 # if the audit level is greater than two, we report if a fragment has overriden
617 # a value from a base fragment. This is really only used for new kernel introduction
618 if bsp_check_visibility > 2 and os.stat(outfile).st_size > 0:
619 with open (outfile, "r") as myfile:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800620 results = myfile.read()
621 bb.warn( "[kernel config]: This BSP has configuration options defined in more than one config, with differing values:\n\n%s" % results)
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500622 warnings_detected = True
623
624 if warnings_detected and kmeta_audit_werror:
625 bb.fatal( "configuration warnings detected, werror is set, promoting to fatal" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500626}
627
628# Ensure that the branches (BSP and meta) are on the locations specified by
629# their SRCREV values. If they are NOT on the right commits, the branches
630# are corrected to the proper commit.
631do_validate_branches() {
632 set +e
633 cd ${S}
634
635 machine_branch="${@ get_machine_branch(d, "${KBRANCH}" )}"
636 machine_srcrev="${SRCREV_machine}"
637
638 # if SRCREV is AUTOREV it shows up as AUTOINC there's nothing to
639 # check and we can exit early
640 if [ "${machine_srcrev}" = "AUTOINC" ]; then
Andrew Geissler09036742021-06-25 14:25:14 -0500641 linux_yocto_dev='${@oe.utils.conditional("PREFERRED_PROVIDER_virtual/kernel", "linux-yocto-dev", "1", "", d)}'
642 if [ -n "$linux_yocto_dev" ]; then
643 git checkout -q -f ${machine_branch}
644 ver=$(grep "^VERSION =" ${S}/Makefile | sed s/.*=\ *//)
645 patchlevel=$(grep "^PATCHLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
646 sublevel=$(grep "^SUBLEVEL =" ${S}/Makefile | sed s/.*=\ *//)
647 kver="$ver.$patchlevel"
648 bbnote "dev kernel: performing version -> branch -> SRCREV validation"
649 bbnote "dev kernel: recipe version ${LINUX_VERSION}, src version: $kver"
650 echo "${LINUX_VERSION}" | grep -q $kver
651 if [ $? -ne 0 ]; then
652 version="$(echo ${LINUX_VERSION} | sed 's/\+.*$//g')"
653 versioned_branch="v$version/$machine_branch"
654
655 machine_branch=$versioned_branch
656 force_srcrev="$(git rev-parse $machine_branch 2> /dev/null)"
657 if [ $? -ne 0 ]; then
658 bbfatal "kernel version mismatch detected, and no valid branch $machine_branch detected"
659 fi
660
661 bbnote "dev kernel: adjusting branch to $machine_branch, srcrev to: $force_srcrev"
662 fi
663 else
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500664 bbnote "SRCREV validation is not required for AUTOREV"
Andrew Geissler09036742021-06-25 14:25:14 -0500665 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500666 elif [ "${machine_srcrev}" = "" ]; then
667 if [ "${SRCREV}" != "AUTOINC" ] && [ "${SRCREV}" != "INVALID" ]; then
668 # SRCREV_machine_<MACHINE> was not set. This means that a custom recipe
669 # that doesn't use the SRCREV_FORMAT "machine_meta" is being built. In
670 # this case, we need to reset to the give SRCREV before heading to patching
671 bbnote "custom recipe is being built, forcing SRCREV to ${SRCREV}"
672 force_srcrev="${SRCREV}"
673 fi
674 else
675 git cat-file -t ${machine_srcrev} > /dev/null
676 if [ $? -ne 0 ]; then
677 bberror "${machine_srcrev} is not a valid commit ID."
678 bbfatal_log "The kernel source tree may be out of sync"
679 fi
680 force_srcrev=${machine_srcrev}
681 fi
682
683 git checkout -q -f ${machine_branch}
684 if [ -n "${force_srcrev}" ]; then
685 # see if the branch we are about to patch has been properly reset to the defined
686 # SRCREV .. if not, we reset it.
687 branch_head=`git rev-parse HEAD`
688 if [ "${force_srcrev}" != "${branch_head}" ]; then
689 current_branch=`git rev-parse --abbrev-ref HEAD`
690 git branch "$current_branch-orig"
691 git reset --hard ${force_srcrev}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500692 # We've checked out HEAD, make sure we cleanup kgit-s2q fence post check
693 # so the patches are applied as expected otherwise no patching
694 # would be done in some corner cases.
695 kgit-s2q --clean
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500696 fi
697 fi
698}
699
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500700OE_TERMINAL_EXPORTS += "KBUILD_OUTPUT"
701KBUILD_OUTPUT = "${B}"
702
703python () {
704 # If diffconfig is available, ensure it runs after kernel_configme
705 if 'do_diffconfig' in d:
706 bb.build.addtask('do_diffconfig', None, 'do_kernel_configme', d)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500707
708 externalsrc = d.getVar('EXTERNALSRC')
709 if externalsrc:
710 # If we deltask do_patch, do_kernel_configme is left without
711 # dependencies and runs too early
712 d.setVarFlag('do_kernel_configme', 'deps', (d.getVarFlag('do_kernel_configme', 'deps', False) or []) + ['do_unpack'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500713}
Andrew Geissler82c905d2020-04-13 13:39:40 -0500714
715# extra tasks
716addtask kernel_version_sanity_check after do_kernel_metadata do_kernel_checkout before do_compile
717addtask validate_branches before do_patch after do_kernel_checkout
718addtask kernel_configcheck after do_configure before do_compile