blob: 2d0bbfbd4228265e7b103d14d04a83da76973285 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007#
8# This bbclass is used for creating archive for:
Andrew Geissler82c905d2020-04-13 13:39:40 -05009# 1) original (or unpacked) source: ARCHIVER_MODE[src] = "original"
10# 2) patched source: ARCHIVER_MODE[src] = "patched" (default)
11# 3) configured source: ARCHIVER_MODE[src] = "configured"
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000012# 4) source mirror: ARCHIVER_MODE[src] = "mirror"
Andrew Geissler82c905d2020-04-13 13:39:40 -050013# 5) The patches between do_unpack and do_patch:
14# ARCHIVER_MODE[diff] = "1"
15# And you can set the one that you'd like to exclude from the diff:
16# ARCHIVER_MODE[diff-exclude] ?= ".pc autom4te.cache patches"
17# 6) The environment data, similar to 'bitbake -e recipe':
18# ARCHIVER_MODE[dumpdata] = "1"
19# 7) The recipe (.bb and .inc): ARCHIVER_MODE[recipe] = "1"
20# 8) Whether output the .src.rpm package:
21# ARCHIVER_MODE[srpm] = "1"
22# 9) Filter the license, the recipe whose license in
23# COPYLEFT_LICENSE_INCLUDE will be included, and in
24# COPYLEFT_LICENSE_EXCLUDE will be excluded.
25# COPYLEFT_LICENSE_INCLUDE = 'GPL* LGPL*'
26# COPYLEFT_LICENSE_EXCLUDE = 'CLOSED Proprietary'
27# 10) The recipe type that will be archived:
28# COPYLEFT_RECIPE_TYPES = 'target'
29# 11) The source mirror mode:
30# ARCHIVER_MODE[mirror] = "split" (default): Sources are split into
31# per-recipe directories in a similar way to other archiver modes.
32# Post-processing may be required to produce a single mirror directory.
33# This does however allow inspection of duplicate sources and more
34# intelligent handling.
35# ARCHIVER_MODE[mirror] = "combined": All sources are placed into a single
36# directory suitable for direct use as a mirror. Duplicate sources are
37# ignored.
38# 12) Source mirror exclusions:
39# ARCHIVER_MIRROR_EXCLUDE is a list of prefixes to exclude from the mirror.
40# This may be used for sources which you are already publishing yourself
41# (e.g. if the URI starts with 'https://mysite.com/' and your mirror is
42# going to be published to the same site). It may also be used to exclude
43# local files (with the prefix 'file://') if these will be provided as part
44# of an archive of the layers themselves.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045#
46
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047# Create archive for all the recipe types
48COPYLEFT_RECIPE_TYPES ?= 'target native nativesdk cross crosssdk cross-canadian'
49inherit copyleft_filter
50
51ARCHIVER_MODE[srpm] ?= "0"
52ARCHIVER_MODE[src] ?= "patched"
53ARCHIVER_MODE[diff] ?= "0"
54ARCHIVER_MODE[diff-exclude] ?= ".pc autom4te.cache patches"
55ARCHIVER_MODE[dumpdata] ?= "0"
56ARCHIVER_MODE[recipe] ?= "0"
Andrew Geissler82c905d2020-04-13 13:39:40 -050057ARCHIVER_MODE[mirror] ?= "split"
Andrew Geissler595f6302022-01-24 19:11:47 +000058ARCHIVER_MODE[compression] ?= "xz"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059
60DEPLOY_DIR_SRC ?= "${DEPLOY_DIR}/sources"
Andrew Geisslerf0343792020-11-18 10:42:21 -060061ARCHIVER_TOPDIR ?= "${WORKDIR}/archiver-sources"
Andrew Geissler615f2f12022-07-15 14:00:58 -050062ARCHIVER_ARCH = "${TARGET_SYS}"
63ARCHIVER_OUTDIR = "${ARCHIVER_TOPDIR}/${ARCHIVER_ARCH}/${PF}/"
Brad Bishop19323692019-04-05 15:28:33 -040064ARCHIVER_RPMTOPDIR ?= "${WORKDIR}/deploy-sources-rpm"
Andrew Geissler615f2f12022-07-15 14:00:58 -050065ARCHIVER_RPMOUTDIR = "${ARCHIVER_RPMTOPDIR}/${ARCHIVER_ARCH}/${PF}/"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066ARCHIVER_WORKDIR = "${WORKDIR}/archiver-work/"
67
Andrew Geissler82c905d2020-04-13 13:39:40 -050068# When producing a combined mirror directory, allow duplicates for the case
69# where multiple recipes use the same SRC_URI.
70ARCHIVER_COMBINED_MIRRORDIR = "${ARCHIVER_TOPDIR}/mirror"
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000071SSTATE_ALLOW_OVERLAP_FILES += "${DEPLOY_DIR_SRC}/mirror"
Brad Bishop19323692019-04-05 15:28:33 -040072
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073do_dumpdata[dirs] = "${ARCHIVER_OUTDIR}"
74do_ar_recipe[dirs] = "${ARCHIVER_OUTDIR}"
75do_ar_original[dirs] = "${ARCHIVER_OUTDIR} ${ARCHIVER_WORKDIR}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076
77# This is a convenience for the shell script to use it
78
Patrick Williamse760df82023-05-26 11:10:49 -050079def include_package(d, pn):
80
81 included, reason = copyleft_should_include(d)
82 if not included:
83 bb.debug(1, 'archiver: %s is excluded: %s' % (pn, reason))
84 return False
85
86 else:
87 bb.debug(1, 'archiver: %s is included: %s' % (pn, reason))
88
89 # glibc-locale: do_fetch, do_unpack and do_patch tasks have been deleted,
90 # so avoid archiving source here.
91 if pn.startswith('glibc-locale'):
92 return False
93
94 # We just archive gcc-source for all the gcc related recipes
95 if d.getVar('BPN') in ['gcc', 'libgcc'] \
96 and not pn.startswith('gcc-source'):
97 bb.debug(1, 'archiver: %s is excluded, covered by gcc-source' % pn)
98 return False
99
100 return True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
102python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 pn = d.getVar('PN')
104 assume_provided = (d.getVar("ASSUME_PROVIDED") or "").split()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500105 if pn in assume_provided:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 for p in d.getVar("PROVIDES").split():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 if p != pn:
108 pn = p
109 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110
Patrick Williamse760df82023-05-26 11:10:49 -0500111 if not include_package(d, pn):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500112 return
113
Andrew Geissler615f2f12022-07-15 14:00:58 -0500114 # TARGET_SYS in ARCHIVER_ARCH will break the stamp for gcc-source in multiconfig
115 if pn.startswith('gcc-source'):
116 d.setVar('ARCHIVER_ARCH', "allarch")
117
Brad Bishop79641f22019-09-10 07:20:22 -0400118 def hasTask(task):
119 return bool(d.getVarFlag(task, "task", False)) and not bool(d.getVarFlag(task, "noexec", False))
120
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 ar_src = d.getVarFlag('ARCHIVER_MODE', 'src')
122 ar_dumpdata = d.getVarFlag('ARCHIVER_MODE', 'dumpdata')
123 ar_recipe = d.getVarFlag('ARCHIVER_MODE', 'recipe')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124
125 if ar_src == "original":
126 d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_original' % pn)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127 # 'patched' and 'configured' invoke do_unpack_and_patch because
128 # do_ar_patched resp. do_ar_configured depend on it, but for 'original'
129 # we have to add it explicitly.
130 if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
131 d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_unpack_and_patch' % pn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 elif ar_src == "patched":
133 d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_patched' % pn)
134 elif ar_src == "configured":
135 # We can't use "addtask do_ar_configured after do_configure" since it
Andrew Geisslerc926e172021-05-07 16:11:35 -0500136 # will cause the deptask of do_populate_sysroot to run no matter what
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 # archives we need, so we add the depends here.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500138
139 # There is a corner case with "gcc-source-${PV}" recipes, they don't have
140 # the "do_configure" task, so we need to use "do_preconfigure"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800141 if hasTask("do_preconfigure"):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500142 d.appendVarFlag('do_ar_configured', 'depends', ' %s:do_preconfigure' % pn)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800143 elif hasTask("do_configure"):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144 d.appendVarFlag('do_ar_configured', 'depends', ' %s:do_configure' % pn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145 d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_configured' % pn)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500146 elif ar_src == "mirror":
147 d.appendVarFlag('do_deploy_archives', 'depends', '%s:do_ar_mirror' % pn)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 elif ar_src:
150 bb.fatal("Invalid ARCHIVER_MODE[src]: %s" % ar_src)
151
152 if ar_dumpdata == "1":
153 d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_dumpdata' % pn)
154
155 if ar_recipe == "1":
156 d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_recipe' % pn)
157
Brad Bishop316dfdd2018-06-25 12:45:53 -0400158 # Output the SRPM package
159 if d.getVarFlag('ARCHIVER_MODE', 'srpm') == "1" and d.getVar('PACKAGES'):
Brad Bishop79641f22019-09-10 07:20:22 -0400160 if "package_rpm" not in d.getVar('PACKAGE_CLASSES'):
161 bb.fatal("ARCHIVER_MODE[srpm] needs package_rpm in PACKAGE_CLASSES")
162
163 # Some recipes do not have any packaging tasks
164 if hasTask("do_package_write_rpm"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_package_write_rpm' % pn)
Brad Bishop19323692019-04-05 15:28:33 -0400166 d.appendVarFlag('do_package_write_rpm', 'dirs', ' ${ARCHIVER_RPMTOPDIR}')
167 d.appendVarFlag('do_package_write_rpm', 'sstate-inputdirs', ' ${ARCHIVER_RPMTOPDIR}')
168 d.appendVarFlag('do_package_write_rpm', 'sstate-outputdirs', ' ${DEPLOY_DIR_SRC}')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 if ar_dumpdata == "1":
170 d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_dumpdata' % pn)
171 if ar_recipe == "1":
172 d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_recipe' % pn)
173 if ar_src == "original":
174 d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_original' % pn)
175 elif ar_src == "patched":
176 d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_patched' % pn)
177 elif ar_src == "configured":
178 d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_configured' % pn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179}
180
Andrew Geisslerc926e172021-05-07 16:11:35 -0500181# Take all the sources for a recipe and put them in WORKDIR/archiver-work/.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182# Files in SRC_URI are copied directly, anything that's a directory
183# (e.g. git repositories) is "unpacked" and then put into a tarball.
184python do_ar_original() {
185
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500186 import shutil, tempfile
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500188 if d.getVarFlag('ARCHIVER_MODE', 'src') != "original":
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 return
190
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500191 ar_outdir = d.getVar('ARCHIVER_OUTDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192 bb.note('Archiving the original source...')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500193 urls = d.getVar("SRC_URI").split()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600194 # destsuffix (git fetcher) and subdir (everything else) are allowed to be
195 # absolute paths (for example, destsuffix=${S}/foobar).
196 # That messes with unpacking inside our tmpdir below, because the fetchers
197 # will then unpack in that directory and completely ignore the tmpdir.
198 # That breaks parallel tasks relying on ${S}, like do_compile.
199 #
200 # To solve this, we remove these parameters from all URLs.
201 # We do this even for relative paths because it makes the content of the
202 # archives more useful (no extra paths that are only used during
203 # compilation).
204 for i, url in enumerate(urls):
205 decoded = bb.fetch2.decodeurl(url)
206 for param in ('destsuffix', 'subdir'):
207 if param in decoded[5]:
208 del decoded[5][param]
209 encoded = bb.fetch2.encodeurl(decoded)
210 urls[i] = encoded
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500211
212 # Cleanup SRC_URI before call bb.fetch2.Fetch() since now SRC_URI is in the
213 # variable "urls", otherwise there might be errors like:
214 # The SRCREV_FORMAT variable must be set when multiple SCMs are used
215 ld = bb.data.createCopy(d)
216 ld.setVar('SRC_URI', '')
217 fetch = bb.fetch2.Fetch(urls, ld)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600218 tarball_suffix = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500219 for url in fetch.urls:
220 local = fetch.localpath(url).rstrip("/");
221 if os.path.isfile(local):
222 shutil.copy(local, ar_outdir)
223 elif os.path.isdir(local):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500224 tmpdir = tempfile.mkdtemp(dir=d.getVar('ARCHIVER_WORKDIR'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 fetch.unpack(tmpdir, (url,))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600226 # To handle recipes with more than one source, we add the "name"
227 # URL parameter as suffix. We treat it as an error when
228 # there's more than one URL without a name, or a name gets reused.
229 # This is an additional safety net, in practice the name has
230 # to be set when using the git fetcher, otherwise SRCREV cannot
231 # be set separately for each URL.
232 params = bb.fetch2.decodeurl(url)[5]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500233 type = bb.fetch2.decodeurl(url)[0]
234 location = bb.fetch2.decodeurl(url)[2]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600235 name = params.get('name', '')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500236 if type.lower() == 'file':
237 name_tmp = location.rstrip("*").rstrip("/")
238 name = os.path.basename(name_tmp)
239 else:
240 if name in tarball_suffix:
241 if not name:
242 bb.fatal("Cannot determine archive names for original source because 'name' URL parameter is unset in more than one URL. Add it to at least one of these: %s %s" % (tarball_suffix[name], url))
243 else:
244 bb.fatal("Cannot determine archive names for original source because 'name=' URL parameter '%s' is used twice. Make it unique in: %s %s" % (tarball_suffix[name], url))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600245 tarball_suffix[name] = url
246 create_tarball(d, tmpdir + '/.', name, ar_outdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500247
248 # Emit patch series files for 'original'
249 bb.note('Writing patch series files...')
250 for patch in src_patches(d):
251 _, _, local, _, _, parm = bb.fetch.decodeurl(patch)
252 patchdir = parm.get('patchdir')
253 if patchdir:
254 series = os.path.join(ar_outdir, 'series.subdir.%s' % patchdir.replace('/', '_'))
255 else:
256 series = os.path.join(ar_outdir, 'series')
257
258 with open(series, 'a') as s:
259 s.write('%s -p%s\n' % (os.path.basename(local), parm['striplevel']))
260}
261
262python do_ar_patched() {
263
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500264 if d.getVarFlag('ARCHIVER_MODE', 'src') != 'patched':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500265 return
266
267 # Get the ARCHIVER_OUTDIR before we reset the WORKDIR
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500268 ar_outdir = d.getVar('ARCHIVER_OUTDIR')
Brad Bishopa34c0302019-09-23 22:34:48 -0400269 if not is_work_shared(d):
270 ar_workdir = d.getVar('ARCHIVER_WORKDIR')
271 d.setVar('WORKDIR', ar_workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272 bb.note('Archiving the patched source...')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500273 create_tarball(d, d.getVar('S'), 'patched', ar_outdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274}
275
276python do_ar_configured() {
277 import shutil
278
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500279 # Forcibly expand the sysroot paths as we're about to change WORKDIR
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500280 d.setVar('STAGING_DIR_HOST', d.getVar('STAGING_DIR_HOST'))
281 d.setVar('STAGING_DIR_TARGET', d.getVar('STAGING_DIR_TARGET'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500282 d.setVar('RECIPE_SYSROOT', d.getVar('RECIPE_SYSROOT'))
283 d.setVar('RECIPE_SYSROOT_NATIVE', d.getVar('RECIPE_SYSROOT_NATIVE'))
284
285 ar_outdir = d.getVar('ARCHIVER_OUTDIR')
286 if d.getVarFlag('ARCHIVER_MODE', 'src') == 'configured':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287 bb.note('Archiving the configured source...')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500288 pn = d.getVar('PN')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500289 # "gcc-source-${PV}" recipes don't have "do_configure"
290 # task, so we need to run "do_preconfigure" instead
291 if pn.startswith("gcc-source-"):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500292 d.setVar('WORKDIR', d.getVar('ARCHIVER_WORKDIR'))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500293 bb.build.exec_func('do_preconfigure', d)
294
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500295 # The libtool-native's do_configure will remove the
296 # ${STAGING_DATADIR}/aclocal/libtool.m4, so we can't re-run the
297 # do_configure, we archive the already configured ${S} to
298 # instead of.
Patrick Williams213cb262021-08-07 19:21:33 -0500299 # The kernel class functions require it to be on work-shared, we
300 # don't unpack, patch, configure again, just archive the already
301 # configured ${S}
302 elif not (pn == 'libtool-native' or is_work_shared(d)):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800303 def runTask(task):
304 prefuncs = d.getVarFlag(task, 'prefuncs') or ''
305 for func in prefuncs.split():
306 if func != "sysroot_cleansstate":
307 bb.build.exec_func(func, d)
308 bb.build.exec_func(task, d)
309 postfuncs = d.getVarFlag(task, 'postfuncs') or ''
310 for func in postfuncs.split():
311 if func != 'do_qa_configure':
312 bb.build.exec_func(func, d)
313
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500314 # Change the WORKDIR to make do_configure run in another dir.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500315 d.setVar('WORKDIR', d.getVar('ARCHIVER_WORKDIR'))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800316
317 preceeds = bb.build.preceedtask('do_configure', False, d)
318 for task in preceeds:
319 if task != 'do_patch' and task != 'do_prepare_recipe_sysroot':
320 runTask(task)
321 runTask('do_configure')
322
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500323 srcdir = d.getVar('S')
324 builddir = d.getVar('B')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500325 if srcdir != builddir:
326 if os.path.exists(builddir):
327 oe.path.copytree(builddir, os.path.join(srcdir, \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500328 'build.%s.ar_configured' % d.getVar('PF')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500329 create_tarball(d, srcdir, 'configured', ar_outdir)
330}
331
Andrew Geissler82c905d2020-04-13 13:39:40 -0500332python do_ar_mirror() {
333 import subprocess
334
335 src_uri = (d.getVar('SRC_URI') or '').split()
336 if len(src_uri) == 0:
337 return
338
339 dl_dir = d.getVar('DL_DIR')
340 mirror_exclusions = (d.getVar('ARCHIVER_MIRROR_EXCLUDE') or '').split()
341 mirror_mode = d.getVarFlag('ARCHIVER_MODE', 'mirror')
342 have_mirror_tarballs = d.getVar('BB_GENERATE_MIRROR_TARBALLS')
343
344 if mirror_mode == 'combined':
345 destdir = d.getVar('ARCHIVER_COMBINED_MIRRORDIR')
346 elif mirror_mode == 'split':
347 destdir = d.getVar('ARCHIVER_OUTDIR')
348 else:
349 bb.fatal('Invalid ARCHIVER_MODE[mirror]: %s' % (mirror_mode))
350
351 if not have_mirror_tarballs:
352 bb.fatal('Using `ARCHIVER_MODE[src] = "mirror"` depends on setting `BB_GENERATE_MIRROR_TARBALLS = "1"`')
353
354 def is_excluded(url):
355 for prefix in mirror_exclusions:
356 if url.startswith(prefix):
357 return True
358 return False
359
360 bb.note('Archiving the source as a mirror...')
361
362 bb.utils.mkdirhier(destdir)
363
364 fetcher = bb.fetch2.Fetch(src_uri, d)
365
Andrew Geissler5a43b432020-06-13 10:46:56 -0500366 for ud in fetcher.expanded_urldata():
367 if is_excluded(ud.url):
368 bb.note('Skipping excluded url: %s' % (ud.url))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500369 continue
370
Andrew Geissler5a43b432020-06-13 10:46:56 -0500371 bb.note('Archiving url: %s' % (ud.url))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500372 ud.setup_localpath(d)
373 localpath = None
374
375 # Check for mirror tarballs first. We will archive the first mirror
376 # tarball that we find as it's assumed that we just need one.
377 for mirror_fname in ud.mirrortarballs:
378 mirror_path = os.path.join(dl_dir, mirror_fname)
379 if os.path.exists(mirror_path):
380 bb.note('Found mirror tarball: %s' % (mirror_path))
381 localpath = mirror_path
382 break
383
384 if len(ud.mirrortarballs) and not localpath:
385 bb.warn('Mirror tarballs are listed for a source but none are present. ' \
386 'Falling back to original download.\n' \
Andrew Geissler5a43b432020-06-13 10:46:56 -0500387 'SRC_URI = %s' % (ud.url))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500388
389 # Check original download
390 if not localpath:
391 bb.note('Using original download: %s' % (ud.localpath))
392 localpath = ud.localpath
393
394 if not localpath or not os.path.exists(localpath):
395 bb.fatal('Original download is missing for a source.\n' \
Andrew Geissler5a43b432020-06-13 10:46:56 -0500396 'SRC_URI = %s' % (ud.url))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500397
398 # We now have an appropriate localpath
399 bb.note('Copying source mirror')
400 cmd = 'cp -fpPRH %s %s' % (localpath, destdir)
401 subprocess.check_call(cmd, shell=True)
402}
403
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500404def create_tarball(d, srcdir, suffix, ar_outdir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500405 """
406 create the tarball from srcdir
407 """
Patrick Williams169d7bc2024-01-05 11:33:25 -0600408 import subprocess
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500409
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500410 # Make sure we are only creating a single tarball for gcc sources
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500411 if (d.getVar('SRC_URI') == ""):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500412 return
413
Brad Bishop316dfdd2018-06-25 12:45:53 -0400414 # For the kernel archive, srcdir may just be a link to the
415 # work-shared location. Use os.path.realpath to make sure
416 # that we archive the actual directory and not just the link.
417 srcdir = os.path.realpath(srcdir)
418
Andrew Geisslereff27472021-10-29 15:35:00 -0500419 compression_method = d.getVarFlag('ARCHIVER_MODE', 'compression')
Patrick Williams169d7bc2024-01-05 11:33:25 -0600420 if compression_method == "xz":
421 compression_cmd = "xz %s" % d.getVar('XZ_DEFAULTS')
422 # To keep compatibility with ARCHIVER_MODE[compression]
423 elif compression_method == "gz":
424 compression_cmd = "gzip"
425 elif compression_method == "bz2":
426 compression_cmd = "bzip2"
427 else:
428 bb.fatal("Unsupported compression_method: %s" % compression_method)
429
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500430 bb.utils.mkdirhier(ar_outdir)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500431 if suffix:
Andrew Geisslereff27472021-10-29 15:35:00 -0500432 filename = '%s-%s.tar.%s' % (d.getVar('PF'), suffix, compression_method)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500433 else:
Andrew Geisslereff27472021-10-29 15:35:00 -0500434 filename = '%s.tar.%s' % (d.getVar('PF'), compression_method)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500435 tarname = os.path.join(ar_outdir, filename)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500436
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500437 bb.note('Creating %s' % tarname)
Patrick Williams169d7bc2024-01-05 11:33:25 -0600438 dirname = os.path.dirname(srcdir)
439 basename = os.path.basename(srcdir)
440 exclude = "--exclude=temp --exclude=patches --exclude='.pc'"
441 tar_cmd = "tar %s -cf - %s | %s > %s" % (exclude, basename, compression_cmd, tarname)
442 subprocess.check_call(tar_cmd, cwd=dirname, shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500443
444# creating .diff.gz between source.orig and source
445def create_diff_gz(d, src_orig, src, ar_outdir):
446
447 import subprocess
448
449 if not os.path.isdir(src) or not os.path.isdir(src_orig):
450 return
451
452 # The diff --exclude can't exclude the file with path, so we copy
453 # the patched source, and remove the files that we'd like to
454 # exclude.
455 src_patched = src + '.patched'
456 oe.path.copyhardlinktree(src, src_patched)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500457 for i in d.getVarFlag('ARCHIVER_MODE', 'diff-exclude').split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500458 bb.utils.remove(os.path.join(src_orig, i), recurse=True)
459 bb.utils.remove(os.path.join(src_patched, i), recurse=True)
460
461 dirname = os.path.dirname(src)
462 basename = os.path.basename(src)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500463 bb.utils.mkdirhier(ar_outdir)
464 cwd = os.getcwd()
465 try:
466 os.chdir(dirname)
467 out_file = os.path.join(ar_outdir, '%s-diff.gz' % d.getVar('PF'))
468 diff_cmd = 'diff -Naur %s.orig %s.patched | gzip -c > %s' % (basename, basename, out_file)
469 subprocess.check_call(diff_cmd, shell=True)
470 bb.utils.remove(src_patched, recurse=True)
471 finally:
472 os.chdir(cwd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500473
Brad Bishop316dfdd2018-06-25 12:45:53 -0400474def is_work_shared(d):
Patrick Williams169d7bc2024-01-05 11:33:25 -0600475 sharedworkdir = os.path.join(d.getVar('TMPDIR'), 'work-shared')
476 return d.getVar('S').startswith(sharedworkdir)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400477
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500478# Run do_unpack and do_patch
479python do_unpack_and_patch() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500480 if d.getVarFlag('ARCHIVER_MODE', 'src') not in \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500481 [ 'patched', 'configured'] and \
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500482 d.getVarFlag('ARCHIVER_MODE', 'diff') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500483 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500484 ar_outdir = d.getVar('ARCHIVER_OUTDIR')
485 ar_workdir = d.getVar('ARCHIVER_WORKDIR')
486 ar_sysroot_native = d.getVar('STAGING_DIR_NATIVE')
487 pn = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500488
Andrew Geisslerc926e172021-05-07 16:11:35 -0500489 # The kernel class functions require it to be on work-shared, so we don't change WORKDIR
Brad Bishop316dfdd2018-06-25 12:45:53 -0400490 if not is_work_shared(d):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500491 # Change the WORKDIR to make do_unpack do_patch run in another dir.
492 d.setVar('WORKDIR', ar_workdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500493 # Restore the original path to recipe's native sysroot (it's relative to WORKDIR).
494 d.setVar('STAGING_DIR_NATIVE', ar_sysroot_native)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500495
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500496 # The changed 'WORKDIR' also caused 'B' changed, create dir 'B' for the
497 # possibly requiring of the following tasks (such as some recipes's
498 # do_patch required 'B' existed).
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500499 bb.utils.mkdirhier(d.getVar('B'))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500500
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500501 bb.build.exec_func('do_unpack', d)
502
503 # Save the original source for creating the patches
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500504 if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
505 src = d.getVar('S').rstrip('/')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500506 src_orig = '%s.orig' % src
507 oe.path.copytree(src, src_orig)
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500508
Patrick Williams213cb262021-08-07 19:21:33 -0500509 if bb.data.inherits_class('dos2unix', d):
510 bb.build.exec_func('do_convert_crlf_to_lf', d)
511
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500512 # Make sure gcc and kernel sources are patched only once
Brad Bishop316dfdd2018-06-25 12:45:53 -0400513 if not (d.getVar('SRC_URI') == "" or is_work_shared(d)):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500514 bb.build.exec_func('do_patch', d)
515
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500516 # Create the patches
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500517 if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500518 bb.note('Creating diff gz...')
519 create_diff_gz(d, src_orig, src, ar_outdir)
520 bb.utils.remove(src_orig, recurse=True)
521}
522
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500523# BBINCLUDED is special (excluded from basehash signature
524# calculation). Using it in a task signature can cause "basehash
525# changed" errors.
526#
527# Depending on BBINCLUDED also causes do_ar_recipe to run again
528# for unrelated changes, like adding or removing buildhistory.bbclass.
529#
530# For these reasons we ignore the dependency completely. The versioning
531# of the output file ensures that we create it each time the recipe
532# gets rebuilt, at least as long as a PR server is used. We also rely
533# on that mechanism to catch changes in the file content, because the
Andrew Geisslerc926e172021-05-07 16:11:35 -0500534# file content is not part of the task signature either.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500535do_ar_recipe[vardepsexclude] += "BBINCLUDED"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500536python do_ar_recipe () {
537 """
538 archive the recipe, including .bb and .inc.
539 """
540 import re
541 import shutil
542
543 require_re = re.compile( r"require\s+(.+)" )
544 include_re = re.compile( r"include\s+(.+)" )
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500545 bbfile = d.getVar('FILE')
546 outdir = os.path.join(d.getVar('WORKDIR'), \
547 '%s-recipe' % d.getVar('PF'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500548 bb.utils.mkdirhier(outdir)
549 shutil.copy(bbfile, outdir)
550
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500551 pn = d.getVar('PN')
552 bbappend_files = d.getVar('BBINCLUDED').split()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500553 # If recipe name is aa, we need to match files like aa.bbappend and aa_1.1.bbappend
554 # Files like aa1.bbappend or aa1_1.1.bbappend must be excluded.
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500555 bbappend_re = re.compile( r".*/%s_[^/]*\.bbappend$" % re.escape(pn))
556 bbappend_re1 = re.compile( r".*/%s\.bbappend$" % re.escape(pn))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500557 for file in bbappend_files:
558 if bbappend_re.match(file) or bbappend_re1.match(file):
559 shutil.copy(file, outdir)
560
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500561 dirname = os.path.dirname(bbfile)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500562 bbpath = '%s:%s' % (dirname, d.getVar('BBPATH'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500563 f = open(bbfile, 'r')
564 for line in f.readlines():
565 incfile = None
566 if require_re.match(line):
567 incfile = require_re.match(line).group(1)
568 elif include_re.match(line):
569 incfile = include_re.match(line).group(1)
570 if incfile:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500571 incfile = d.expand(incfile)
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500572 if incfile:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500573 incfile = bb.utils.which(bbpath, incfile)
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500574 if incfile:
575 shutil.copy(incfile, outdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500576
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500577 create_tarball(d, outdir, 'recipe', d.getVar('ARCHIVER_OUTDIR'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500578 bb.utils.remove(outdir, recurse=True)
579}
580
581python do_dumpdata () {
582 """
583 dump environment data to ${PF}-showdata.dump
584 """
585
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500586 dumpfile = os.path.join(d.getVar('ARCHIVER_OUTDIR'), \
587 '%s-showdata.dump' % d.getVar('PF'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500588 bb.note('Dumping metadata into %s' % dumpfile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500589 with open(dumpfile, "w") as f:
590 # emit variables and shell functions
591 bb.data.emit_env(f, d, True)
592 # emit the metadata which isn't valid shell
593 for e in d.keys():
594 if d.getVarFlag(e, "python", False):
595 f.write("\npython %s () {\n%s}\n" % (e, d.getVar(e, False)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500596}
597
598SSTATETASKS += "do_deploy_archives"
599do_deploy_archives () {
Andrew Geissler78b72792022-06-14 06:47:25 -0500600 bbnote "Deploying source archive files from ${ARCHIVER_TOPDIR} to ${DEPLOY_DIR_SRC}."
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500601}
602python do_deploy_archives_setscene () {
603 sstate_setscene(d)
604}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500605do_deploy_archives[dirs] = "${ARCHIVER_TOPDIR}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500606do_deploy_archives[sstate-inputdirs] = "${ARCHIVER_TOPDIR}"
607do_deploy_archives[sstate-outputdirs] = "${DEPLOY_DIR_SRC}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500608addtask do_deploy_archives_setscene
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500609
610addtask do_ar_original after do_unpack
Andrew Geissler475cb722020-07-10 16:00:51 -0500611addtask do_unpack_and_patch after do_patch do_preconfigure
612addtask do_ar_patched after do_unpack_and_patch
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500613addtask do_ar_configured after do_unpack_and_patch
Andrew Geissler82c905d2020-04-13 13:39:40 -0500614addtask do_ar_mirror after do_fetch
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500615addtask do_dumpdata
616addtask do_ar_recipe
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500617addtask do_deploy_archives
618do_build[recrdeptask] += "do_deploy_archives"
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600619do_rootfs[recrdeptask] += "do_deploy_archives"
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500620do_populate_sdk[recrdeptask] += "do_deploy_archives"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500621
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500622python () {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500623 # Add tasks in the correct order, specifically for linux-yocto to avoid race condition.
624 # sstatesig.py:sstate_rundepfilter has special support that excludes this dependency
625 # so that do_kernel_configme does not need to run again when do_unpack_and_patch
626 # gets added or removed (by adding or removing archiver.bbclass).
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500627 if bb.data.inherits_class('kernel-yocto', d):
628 bb.build.addtask('do_kernel_configme', 'do_configure', 'do_unpack_and_patch', d)
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500629}