blob: 82d80741a951f1e4c94a815d3ffde09fa8600ea8 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# remove tasks that modify the source tree in case externalsrc is inherited
Patrick Williamsc0f7c042017-02-23 20:41:17 -06002SRCTREECOVEREDTASKS += "do_kernel_configme do_validate_branches do_kernel_configcheck do_kernel_checkout do_fetch do_unpack do_patch"
3PATCH_GIT_USER_EMAIL ?= "kernel-yocto@oe"
4PATCH_GIT_USER_NAME ?= "OpenEmbedded"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005
6# returns local (absolute) path names for all valid patches in the
7# src_uri
8def find_patches(d):
9 patches = src_patches(d)
10 patch_list=[]
11 for p in patches:
12 _, _, local, _, _, _ = bb.fetch.decodeurl(p)
13 patch_list.append(local)
14
15 return patch_list
16
17# returns all the elements from the src uri that are .scc files
18def find_sccs(d):
19 sources=src_patches(d, True)
20 sources_list=[]
21 for s in sources:
22 base, ext = os.path.splitext(os.path.basename(s))
23 if ext and ext in [".scc", ".cfg"]:
24 sources_list.append(s)
25 elif base and base in 'defconfig':
26 sources_list.append(s)
27
28 return sources_list
29
30# check the SRC_URI for "kmeta" type'd git repositories. Return the name of
31# the repository as it will be found in WORKDIR
32def find_kernel_feature_dirs(d):
33 feature_dirs=[]
34 fetch = bb.fetch2.Fetch([], d)
35 for url in fetch.urls:
36 urldata = fetch.ud[url]
37 parm = urldata.parm
38 type=""
39 if "type" in parm:
40 type = parm["type"]
41 if "destsuffix" in parm:
42 destdir = parm["destsuffix"]
43 if type == "kmeta":
44 feature_dirs.append(destdir)
45
46 return feature_dirs
47
48# find the master/machine source branch. In the same way that the fetcher proceses
49# git repositories in the SRC_URI we take the first repo found, first branch.
50def get_machine_branch(d, default):
51 fetch = bb.fetch2.Fetch([], d)
52 for url in fetch.urls:
53 urldata = fetch.ud[url]
54 parm = urldata.parm
55 if "branch" in parm:
56 branches = urldata.parm.get("branch").split(',')
Patrick Williamsf1e5d692016-03-30 15:21:19 -050057 btype = urldata.parm.get("type")
58 if btype != "kmeta":
59 return branches[0]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060
61 return default
62
63do_kernel_metadata() {
64 set +e
65 cd ${S}
66 export KMETA=${KMETA}
67
68 # if kernel tools are available in-tree, they are preferred
69 # and are placed on the path before any external tools. Unless
70 # the external tools flag is set, in that case we do nothing.
71 if [ -f "${S}/scripts/util/configme" ]; then
72 if [ -z "${EXTERNAL_KERNEL_TOOLS}" ]; then
73 PATH=${S}/scripts/util:${PATH}
74 fi
75 fi
76
77 machine_branch="${@ get_machine_branch(d, "${KBRANCH}" )}"
78 machine_srcrev="${SRCREV_machine}"
79 if [ -z "${machine_srcrev}" ]; then
80 # fallback to SRCREV if a non machine_meta tree is being built
81 machine_srcrev="${SRCREV}"
82 fi
83
84 # In a similar manner to the kernel itself:
85 #
86 # defconfig: $(obj)/conf
87 # ifeq ($(KBUILD_DEFCONFIG),)
88 # $< --defconfig $(Kconfig)
89 # else
90 # @echo "*** Default configuration is based on '$(KBUILD_DEFCONFIG)'"
91 # $(Q)$< --defconfig=arch/$(SRCARCH)/configs/$(KBUILD_DEFCONFIG) $(Kconfig)
92 # endif
93 #
94 # If a defconfig is specified via the KBUILD_DEFCONFIG variable, we copy it
95 # from the source tree, into a common location and normalized "defconfig" name,
96 # where the rest of the process will include and incoroporate it into the build
97 #
98 # If the fetcher has already placed a defconfig in WORKDIR (from the SRC_URI),
99 # we don't overwrite it, but instead warn the user that SRC_URI defconfigs take
100 # precendence.
101 #
102 if [ -n "${KBUILD_DEFCONFIG}" ]; then
103 if [ -f "${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG}" ]; then
104 if [ -f "${WORKDIR}/defconfig" ]; then
105 # If the two defconfig's are different, warn that we didn't overwrite the
106 # one already placed in WORKDIR by the fetcher.
107 cmp "${WORKDIR}/defconfig" "${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG}"
108 if [ $? -ne 0 ]; then
109 bbwarn "defconfig detected in WORKDIR. ${KBUILD_DEFCONFIG} skipped"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110 else
111 cp -f ${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG} ${WORKDIR}/defconfig
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 fi
113 else
114 cp -f ${S}/arch/${ARCH}/configs/${KBUILD_DEFCONFIG} ${WORKDIR}/defconfig
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116 sccs="${WORKDIR}/defconfig"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 else
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118 bbfatal "A KBUILD_DEFCONFIG '${KBUILD_DEFCONFIG}' was specified, but not present in the source tree"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119 fi
120 fi
121
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500122 sccs_from_src_uri="${@" ".join(find_sccs(d))}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123 patches="${@" ".join(find_patches(d))}"
124 feat_dirs="${@" ".join(find_kernel_feature_dirs(d))}"
125
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126 # a quick check to make sure we don't have duplicate defconfigs
127 # If there's a defconfig in the SRC_URI, did we also have one from
128 # the KBUILD_DEFCONFIG processing above ?
129 if [ -n "$sccs" ]; then
130 # we did have a defconfig from above. remove any that might be in the src_uri
131 sccs_from_src_uri=$(echo $sccs_from_src_uri | awk '{ if ($0!="defconfig") { print $0 } }' RS=' ')
132 fi
133 sccs="$sccs $sccs_from_src_uri"
134
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 # check for feature directories/repos/branches that were part of the
136 # SRC_URI. If they were supplied, we convert them into include directives
137 # for the update part of the process
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600138 for f in ${feat_dirs}; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500139 if [ -d "${WORKDIR}/$f/meta" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600140 includes="$includes -I${WORKDIR}/$f/kernel-meta"
141 elif [ -d "${WORKDIR}/$f" ]; then
142 includes="$includes -I${WORKDIR}/$f"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500143 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600144 done
145 for s in ${sccs} ${patches}; do
146 sdir=$(dirname $s)
147 includes="$includes -I${sdir}"
148 # if a SRC_URI passed patch or .scc has a subdir of "kernel-meta",
149 # then we add it to the search path
150 if [ -d "${sdir}/kernel-meta" ]; then
151 includes="$includes -I${sdir}/kernel-meta"
152 fi
153 done
154
155 # expand kernel features into their full path equivalents
156 bsp_definition=$(spp ${includes} --find -DKMACHINE=${KMACHINE} -DKTYPE=${LINUX_KERNEL_TYPE})
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500157 if [ -z "$bsp_definition" ]; then
158 echo "$sccs" | grep -q defconfig
159 if [ $? -ne 0 ]; then
160 bbfatal_log "Could not locate BSP definition for ${KMACHINE}/${LINUX_KERNEL_TYPE} and no defconfig was provided"
161 fi
162 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600163 meta_dir=$(kgit --meta)
164
165 # run1: pull all the configuration fragments, no matter where they come from
166 elements="`echo -n ${bsp_definition} ${sccs} ${patches} ${KERNEL_FEATURES}`"
167 if [ -n "${elements}" ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500168 echo "${bsp_definition}" > ${S}/${meta_dir}/bsp_definition
169 scc --force -o ${S}/${meta_dir}:cfg,merge,meta ${includes} ${bsp_definition} ${sccs} ${patches} ${KERNEL_FEATURES}
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500170 if [ $? -ne 0 ]; then
171 bbfatal_log "Could not generate configuration queue for ${KMACHINE}."
172 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173 fi
174
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600175 # run2: only generate patches for elements that have been passed on the SRC_URI
176 elements="`echo -n ${sccs} ${patches} ${KERNEL_FEATURES}`"
177 if [ -n "${elements}" ]; then
178 scc --force -o ${S}/${meta_dir}:patch --cmds patch ${includes} ${sccs} ${patches} ${KERNEL_FEATURES}
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500179 if [ $? -ne 0 ]; then
180 bbfatal_log "Could not generate configuration queue for ${KMACHINE}."
181 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182 fi
183}
184
185do_patch() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500186 set +e
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187 cd ${S}
188
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600189 check_git_config
190 meta_dir=$(kgit --meta)
191 (cd ${meta_dir}; ln -sf patch.queue series)
192 if [ -f "${meta_dir}/series" ]; then
193 kgit-s2q --gen -v --patches .kernel-meta/
194 if [ $? -ne 0 ]; then
195 bberror "Could not apply patches for ${KMACHINE}."
196 bbfatal_log "Patch failures can be resolved in the linux source directory ${S})"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 fi
198 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500199
200 if [ -f "${meta_dir}/merge.queue" ]; then
201 # we need to merge all these branches
202 for b in $(cat ${meta_dir}/merge.queue); do
203 git show-ref --verify --quiet refs/heads/${b}
204 if [ $? -eq 0 ]; then
205 bbnote "Merging branch ${b}"
206 git merge -q --no-ff -m "Merge branch ${b}" ${b}
207 else
208 bbfatal "branch ${b} does not exist, cannot merge"
209 fi
210 done
211 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500212}
213
214do_kernel_checkout() {
215 set +e
216
217 source_dir=`echo ${S} | sed 's%/$%%'`
218 source_workdir="${WORKDIR}/git"
219 if [ -d "${WORKDIR}/git/" ]; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500220 # case: git repository
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 # if S is WORKDIR/git, then we shouldn't be moving or deleting the tree.
222 if [ "${source_dir}" != "${source_workdir}" ]; then
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500223 if [ -d "${source_workdir}/.git" ]; then
224 # regular git repository with .git
225 rm -rf ${S}
226 mv ${WORKDIR}/git ${S}
227 else
228 # create source for bare cloned git repository
229 git clone ${WORKDIR}/git ${S}
230 rm -rf ${WORKDIR}/git
231 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232 fi
233 cd ${S}
234 else
235 # case: we have no git repository at all.
236 # To support low bandwidth options for building the kernel, we'll just
237 # convert the tree to a git repo and let the rest of the process work unchanged
238
239 # if ${S} hasn't been set to the proper subdirectory a default of "linux" is
240 # used, but we can't initialize that empty directory. So check it and throw a
241 # clear error
242
243 cd ${S}
244 if [ ! -f "Makefile" ]; then
245 bberror "S is not set to the linux source directory. Check "
246 bbfatal "the recipe and set S to the proper extracted subdirectory"
247 fi
248 rm -f .gitignore
249 git init
Brad Bishop316dfdd2018-06-25 12:45:53 -0400250 check_git_config
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500251 git add .
252 git commit -q -m "baseline commit: creating repo for ${PN}-${PV}"
253 git clean -d -f
254 fi
255
256 # convert any remote branches to local tracking ones
257 for i in `git branch -a --no-color | grep remotes | grep -v HEAD`; do
258 b=`echo $i | cut -d' ' -f2 | sed 's%remotes/origin/%%'`;
259 git show-ref --quiet --verify -- "refs/heads/$b"
260 if [ $? -ne 0 ]; then
261 git branch $b $i > /dev/null
262 fi
263 done
264
265 # Create a working tree copy of the kernel by checking out a branch
266 machine_branch="${@ get_machine_branch(d, "${KBRANCH}" )}"
267
268 # checkout and clobber any unimportant files
269 git checkout -f ${machine_branch}
270}
271do_kernel_checkout[dirs] = "${S}"
272
273addtask kernel_checkout before do_kernel_metadata after do_unpack
274addtask kernel_metadata after do_validate_branches do_unpack before do_patch
275do_kernel_metadata[depends] = "kern-tools-native:do_populate_sysroot"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500276do_validate_branches[depends] = "kern-tools-native:do_populate_sysroot"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277
William A. Kennington III47649fa2018-06-28 12:34:29 -0700278do_kernel_configme[depends] += "virtual/${TARGET_PREFIX}binutils:do_populate_sysroot"
279do_kernel_configme[depends] += "virtual/${TARGET_PREFIX}gcc:do_populate_sysroot"
280do_kernel_configme[depends] += "bc-native:do_populate_sysroot bison-native:do_populate_sysroot"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281do_kernel_configme[dirs] += "${S} ${B}"
282do_kernel_configme() {
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600283 set +e
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500284
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600285 # translate the kconfig_mode into something that merge_config.sh
286 # understands
287 case ${KCONFIG_MODE} in
288 *allnoconfig)
289 config_flags="-n"
290 ;;
291 *alldefconfig)
292 config_flags=""
293 ;;
294 *)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500295 if [ -f ${WORKDIR}/defconfig ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600296 config_flags="-n"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500297 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600298 ;;
299 esac
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500300
301 cd ${S}
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600302
303 meta_dir=$(kgit --meta)
304 configs="$(scc --configs -o ${meta_dir})"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500305 if [ $? -ne 0 ]; then
306 bberror "${configs}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600307 bbfatal_log "Could not find configuration queue (${meta_dir}/config.queue)"
308 fi
309
William A. Kennington III47649fa2018-06-28 12:34:29 -0700310 CFLAGS="${CFLAGS} ${TOOLCHAIN_OPTIONS}" HOSTCC="${BUILD_CC} ${BUILD_CFLAGS} ${BUILD_LDFLAGS}" HOSTCPP="${BUILD_CPP}" CC="${KERNEL_CC}" ARCH=${ARCH} merge_config.sh -O ${B} ${config_flags} ${configs} > ${meta_dir}/cfg/merge_config_build.log 2>&1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500311 if [ $? -ne 0 ]; then
312 bbfatal_log "Could not configure ${KMACHINE}-${LINUX_KERNEL_TYPE}"
313 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600314
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500315 echo "# Global settings from linux recipe" >> ${B}/.config
316 echo "CONFIG_LOCALVERSION="\"${LINUX_VERSION_EXTENSION}\" >> ${B}/.config
317}
318
319addtask kernel_configme before do_configure after do_patch
320
321python do_kernel_configcheck() {
322 import re, string, sys
323
324 # if KMETA isn't set globally by a recipe using this routine, we need to
325 # set the default to 'meta'. Otherwise, kconf_check is not passed a valid
326 # meta-series for processing
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500327 kmeta = d.getVar("KMETA") or "meta"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500328 if not os.path.exists(kmeta):
329 kmeta = "." + kmeta
330
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500331 pathprefix = "export PATH=%s:%s; " % (d.getVar('PATH'), "${S}/scripts/util/")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600332
333 cmd = d.expand("scc --configs -o ${S}/.kernel-meta")
334 ret, configs = oe.utils.getstatusoutput("%s%s" % (pathprefix, cmd))
335
336 cmd = d.expand("cd ${S}; kconf_check --report -o ${S}/%s/cfg/ ${B}/.config ${S} %s" % (kmeta,configs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500337 ret, result = oe.utils.getstatusoutput("%s%s" % (pathprefix, cmd))
338
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500339 config_check_visibility = int(d.getVar("KCONF_AUDIT_LEVEL") or 0)
340 bsp_check_visibility = int(d.getVar("KCONF_BSP_AUDIT_LEVEL") or 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500341
342 # if config check visibility is non-zero, report dropped configuration values
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600343 mismatch_file = d.expand("${S}/%s/cfg/mismatch.txt" % kmeta)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500344 if os.path.exists(mismatch_file):
345 if config_check_visibility:
346 with open (mismatch_file, "r") as myfile:
347 results = myfile.read()
348 bb.warn( "[kernel config]: specified values did not make it into the kernel's final configuration:\n\n%s" % results)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500349}
350
351# Ensure that the branches (BSP and meta) are on the locations specified by
352# their SRCREV values. If they are NOT on the right commits, the branches
353# are corrected to the proper commit.
354do_validate_branches() {
355 set +e
356 cd ${S}
357
358 machine_branch="${@ get_machine_branch(d, "${KBRANCH}" )}"
359 machine_srcrev="${SRCREV_machine}"
360
361 # if SRCREV is AUTOREV it shows up as AUTOINC there's nothing to
362 # check and we can exit early
363 if [ "${machine_srcrev}" = "AUTOINC" ]; then
364 bbnote "SRCREV validation is not required for AUTOREV"
365 elif [ "${machine_srcrev}" = "" ]; then
366 if [ "${SRCREV}" != "AUTOINC" ] && [ "${SRCREV}" != "INVALID" ]; then
367 # SRCREV_machine_<MACHINE> was not set. This means that a custom recipe
368 # that doesn't use the SRCREV_FORMAT "machine_meta" is being built. In
369 # this case, we need to reset to the give SRCREV before heading to patching
370 bbnote "custom recipe is being built, forcing SRCREV to ${SRCREV}"
371 force_srcrev="${SRCREV}"
372 fi
373 else
374 git cat-file -t ${machine_srcrev} > /dev/null
375 if [ $? -ne 0 ]; then
376 bberror "${machine_srcrev} is not a valid commit ID."
377 bbfatal_log "The kernel source tree may be out of sync"
378 fi
379 force_srcrev=${machine_srcrev}
380 fi
381
382 git checkout -q -f ${machine_branch}
383 if [ -n "${force_srcrev}" ]; then
384 # see if the branch we are about to patch has been properly reset to the defined
385 # SRCREV .. if not, we reset it.
386 branch_head=`git rev-parse HEAD`
387 if [ "${force_srcrev}" != "${branch_head}" ]; then
388 current_branch=`git rev-parse --abbrev-ref HEAD`
389 git branch "$current_branch-orig"
390 git reset --hard ${force_srcrev}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500391 # We've checked out HEAD, make sure we cleanup kgit-s2q fence post check
392 # so the patches are applied as expected otherwise no patching
393 # would be done in some corner cases.
394 kgit-s2q --clean
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500395 fi
396 fi
397}
398
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500399OE_TERMINAL_EXPORTS += "KBUILD_OUTPUT"
400KBUILD_OUTPUT = "${B}"
401
402python () {
403 # If diffconfig is available, ensure it runs after kernel_configme
404 if 'do_diffconfig' in d:
405 bb.build.addtask('do_diffconfig', None, 'do_kernel_configme', d)
406}