blob: c339b9bdfb4b1e53f66ddc6dc75ac90ad8168e14 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# The WICVARS variable is used to define list of bitbake variables used in wic code
8# variables from this list is written to <image>.env file
9WICVARS ?= "\
10 APPEND \
11 ASSUME_PROVIDED \
12 BBLAYERS \
13 DEPLOY_DIR_IMAGE \
14 FAKEROOTCMD \
15 HOSTTOOLS_DIR \
16 IMAGE_BASENAME \
17 IMAGE_BOOT_FILES \
18 IMAGE_EFI_BOOT_FILES \
19 IMAGE_LINK_NAME \
20 IMAGE_ROOTFS \
21 IMGDEPLOYDIR \
22 INITRAMFS_FSTYPES \
23 INITRAMFS_IMAGE \
24 INITRAMFS_IMAGE_BUNDLE \
25 INITRAMFS_LINK_NAME \
26 INITRD \
27 INITRD_LIVE \
28 ISODIR \
29 KERNEL_IMAGETYPE \
30 MACHINE \
31 PSEUDO_IGNORE_PATHS \
32 RECIPE_SYSROOT_NATIVE \
33 ROOTFS_SIZE \
34 STAGING_DATADIR \
35 STAGING_DIR \
36 STAGING_DIR_HOST \
37 STAGING_LIBDIR \
38 TARGET_SYS \
39"
40
41inherit ${@bb.utils.contains('INITRAMFS_IMAGE_BUNDLE', '1', 'kernel-artifact-names', '', d)}
42
43WKS_FILE ??= "${IMAGE_BASENAME}.${MACHINE}.wks"
44WKS_FILES ?= "${WKS_FILE} ${IMAGE_BASENAME}.wks"
45WKS_SEARCH_PATH ?= "${THISDIR}:${@':'.join('%s/wic' % p for p in '${BBPATH}'.split(':'))}:${@':'.join('%s/scripts/lib/wic/canned-wks' % l for l in '${BBPATH}:${COREBASE}'.split(':'))}"
46WKS_FULL_PATH = "${@wks_search(d.getVar('WKS_FILES').split(), d.getVar('WKS_SEARCH_PATH')) or ''}"
47
48def wks_search(files, search_path):
49 for f in files:
50 if os.path.isabs(f):
51 if os.path.exists(f):
52 return f
53 else:
54 searched = bb.utils.which(search_path, f)
55 if searched:
56 return searched
57
58WIC_CREATE_EXTRA_ARGS ?= ""
59
60IMAGE_CMD:wic () {
61 out="${IMGDEPLOYDIR}/${IMAGE_NAME}"
62 build_wic="${WORKDIR}/build-wic"
63 tmp_wic="${WORKDIR}/tmp-wic"
64 wks="${WKS_FULL_PATH}"
65 if [ -e "$tmp_wic" ]; then
66 # Ensure we don't have any junk leftover from a previously interrupted
67 # do_image_wic execution
68 rm -rf "$tmp_wic"
69 fi
70 if [ -z "$wks" ]; then
71 bbfatal "No kickstart files from WKS_FILES were found: ${WKS_FILES}. Please set WKS_FILE or WKS_FILES appropriately."
72 fi
73 BUILDDIR="${TOPDIR}" PSEUDO_UNLOAD=1 wic create "$wks" --vars "${STAGING_DIR}/${MACHINE}/imgdata/" -e "${IMAGE_BASENAME}" -o "$build_wic/" -w "$tmp_wic" ${WIC_CREATE_EXTRA_ARGS}
74 mv "$build_wic/$(basename "${wks%.wks}")"*.direct "$out${IMAGE_NAME_SUFFIX}.wic"
75}
76IMAGE_CMD:wic[vardepsexclude] = "WKS_FULL_PATH WKS_FILES TOPDIR"
77do_image_wic[cleandirs] = "${WORKDIR}/build-wic"
78
79PSEUDO_IGNORE_PATHS .= ",${WORKDIR}/build-wic"
80
81# Rebuild when the wks file or vars in WICVARS change
82USING_WIC = "${@bb.utils.contains_any('IMAGE_FSTYPES', 'wic ' + ' '.join('wic.%s' % c for c in '${CONVERSIONTYPES}'.split()), '1', '', d)}"
83WKS_FILE_CHECKSUM = "${@'${WKS_FULL_PATH}:%s' % os.path.exists('${WKS_FULL_PATH}') if '${USING_WIC}' else ''}"
84do_image_wic[file-checksums] += "${WKS_FILE_CHECKSUM}"
85do_image_wic[depends] += "${@' '.join('%s-native:do_populate_sysroot' % r for r in ('parted', 'gptfdisk', 'dosfstools', 'mtools'))}"
86
87# We ensure all artfacts are deployed (e.g virtual/bootloader)
88do_image_wic[recrdeptask] += "do_deploy"
89do_image_wic[deptask] += "do_image_complete"
90
91WKS_FILE_DEPENDS_DEFAULT = '${@bb.utils.contains_any("BUILD_ARCH", [ 'x86_64', 'i686' ], "syslinux-native", "",d)}'
92WKS_FILE_DEPENDS_DEFAULT += "bmap-tools-native cdrtools-native btrfs-tools-native squashfs-tools-native e2fsprogs-native erofs-utils-native"
93# Unified kernel images need objcopy
94WKS_FILE_DEPENDS_DEFAULT += "virtual/${MLPREFIX}${TARGET_PREFIX}binutils"
95WKS_FILE_DEPENDS_BOOTLOADERS = ""
96WKS_FILE_DEPENDS_BOOTLOADERS:x86 = "syslinux grub-efi systemd-boot os-release"
97WKS_FILE_DEPENDS_BOOTLOADERS:x86-64 = "syslinux grub-efi systemd-boot os-release"
98WKS_FILE_DEPENDS_BOOTLOADERS:x86-x32 = "syslinux grub-efi"
99
100WKS_FILE_DEPENDS ??= "${WKS_FILE_DEPENDS_DEFAULT} ${WKS_FILE_DEPENDS_BOOTLOADERS}"
101
102DEPENDS += "${@ '${WKS_FILE_DEPENDS}' if d.getVar('USING_WIC') else '' }"
103
104python do_write_wks_template () {
105 """Write out expanded template contents to WKS_FULL_PATH."""
106 import re
107
108 template_body = d.getVar('_WKS_TEMPLATE')
109
110 # Remove any remnant variable references left behind by the expansion
111 # due to undefined variables
112 expand_var_regexp = re.compile(r"\${[^{}@\n\t :]+}")
113 while True:
114 new_body = re.sub(expand_var_regexp, '', template_body)
115 if new_body == template_body:
116 break
117 else:
118 template_body = new_body
119
120 wks_file = d.getVar('WKS_FULL_PATH')
121 with open(wks_file, 'w') as f:
122 f.write(template_body)
123 f.close()
124 # Copy the finalized wks file to the deploy directory for later use
125 depdir = d.getVar('IMGDEPLOYDIR')
126 basename = d.getVar('IMAGE_BASENAME')
127 bb.utils.copyfile(wks_file, "%s/%s" % (depdir, basename + '-' + os.path.basename(wks_file)))
128}
129
130do_flush_pseudodb() {
131 ${FAKEROOTENV} ${FAKEROOTCMD} -S
132}
133
134python () {
135 if d.getVar('USING_WIC'):
136 wks_file_u = d.getVar('WKS_FULL_PATH', False)
137 wks_file = d.expand(wks_file_u)
138 base, ext = os.path.splitext(wks_file)
139 if ext == '.in' and os.path.exists(wks_file):
140 wks_out_file = os.path.join(d.getVar('WORKDIR'), os.path.basename(base))
141 d.setVar('WKS_FULL_PATH', wks_out_file)
142 d.setVar('WKS_TEMPLATE_PATH', wks_file_u)
143 d.setVar('WKS_FILE_CHECKSUM', '${WKS_TEMPLATE_PATH}:True')
144
145 # We need to re-parse each time the file changes, and bitbake
146 # needs to be told about that explicitly.
147 bb.parse.mark_dependency(d, wks_file)
148
149 try:
150 with open(wks_file, 'r') as f:
151 body = f.read()
152 except (IOError, OSError) as exc:
153 pass
154 else:
155 # Previously, I used expandWithRefs to get the dependency list
156 # and add it to WICVARS, but there's no point re-parsing the
157 # file in process_wks_template as well, so just put it in
158 # a variable and let the metadata deal with the deps.
159 d.setVar('_WKS_TEMPLATE', body)
160 bb.build.addtask('do_write_wks_template', 'do_image_wic', 'do_image', d)
161 bb.build.addtask('do_image_wic', 'do_image_complete', None, d)
162}
163
164#
165# Write environment variables used by wic
166# to tmp/sysroots/<machine>/imgdata/<image>.env
167#
168python do_rootfs_wicenv () {
169 wicvars = d.getVar('WICVARS')
170 if not wicvars:
171 return
172
173 stdir = d.getVar('STAGING_DIR')
174 outdir = os.path.join(stdir, d.getVar('MACHINE'), 'imgdata')
175 bb.utils.mkdirhier(outdir)
176 basename = d.getVar('IMAGE_BASENAME')
177 with open(os.path.join(outdir, basename) + '.env', 'w') as envf:
178 for var in wicvars.split():
179 value = d.getVar(var)
180 if value:
181 envf.write('%s="%s"\n' % (var, value.strip()))
182 envf.close()
183 # Copy .env file to deploy directory for later use with stand alone wic
184 depdir = d.getVar('IMGDEPLOYDIR')
185 bb.utils.copyfile(os.path.join(outdir, basename) + '.env', os.path.join(depdir, basename) + '.env')
186}
187addtask do_flush_pseudodb after do_rootfs before do_image do_image_qa
188addtask do_rootfs_wicenv after do_image before do_image_wic
189do_rootfs_wicenv[vardeps] += "${WICVARS}"
190do_rootfs_wicenv[prefuncs] = 'set_image_size'