1eb8dc403SDave Cobbley# ex:ts=4:sw=4:sts=4:et
2eb8dc403SDave Cobbley# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3eb8dc403SDave Cobbley#
4eb8dc403SDave Cobbley# This bbclass is used for creating archive for:
5eb8dc403SDave Cobbley#  1) original (or unpacked) source: ARCHIVER_MODE[src] = "original"
6eb8dc403SDave Cobbley#  2) patched source: ARCHIVER_MODE[src] = "patched" (default)
7eb8dc403SDave Cobbley#  3) configured source: ARCHIVER_MODE[src] = "configured"
8*82c905dcSAndrew Geissler#  4) source mirror: ARCHIVE_MODE[src] = "mirror"
9*82c905dcSAndrew Geissler#  5) The patches between do_unpack and do_patch:
10eb8dc403SDave Cobbley#     ARCHIVER_MODE[diff] = "1"
11eb8dc403SDave Cobbley#     And you can set the one that you'd like to exclude from the diff:
12eb8dc403SDave Cobbley#     ARCHIVER_MODE[diff-exclude] ?= ".pc autom4te.cache patches"
13*82c905dcSAndrew Geissler#  6) The environment data, similar to 'bitbake -e recipe':
14eb8dc403SDave Cobbley#     ARCHIVER_MODE[dumpdata] = "1"
15*82c905dcSAndrew Geissler#  7) The recipe (.bb and .inc): ARCHIVER_MODE[recipe] = "1"
16*82c905dcSAndrew Geissler#  8) Whether output the .src.rpm package:
17eb8dc403SDave Cobbley#     ARCHIVER_MODE[srpm] = "1"
18*82c905dcSAndrew Geissler#  9) Filter the license, the recipe whose license in
19eb8dc403SDave Cobbley#     COPYLEFT_LICENSE_INCLUDE will be included, and in
20eb8dc403SDave Cobbley#     COPYLEFT_LICENSE_EXCLUDE will be excluded.
21eb8dc403SDave Cobbley#     COPYLEFT_LICENSE_INCLUDE = 'GPL* LGPL*'
22eb8dc403SDave Cobbley#     COPYLEFT_LICENSE_EXCLUDE = 'CLOSED Proprietary'
23*82c905dcSAndrew Geissler# 10) The recipe type that will be archived:
24eb8dc403SDave Cobbley#     COPYLEFT_RECIPE_TYPES = 'target'
25*82c905dcSAndrew Geissler# 11) The source mirror mode:
26*82c905dcSAndrew Geissler#     ARCHIVER_MODE[mirror] = "split" (default): Sources are split into
27*82c905dcSAndrew Geissler#     per-recipe directories in a similar way to other archiver modes.
28*82c905dcSAndrew Geissler#     Post-processing may be required to produce a single mirror directory.
29*82c905dcSAndrew Geissler#     This does however allow inspection of duplicate sources and more
30*82c905dcSAndrew Geissler#     intelligent handling.
31*82c905dcSAndrew Geissler#     ARCHIVER_MODE[mirror] = "combined": All sources are placed into a single
32*82c905dcSAndrew Geissler#     directory suitable for direct use as a mirror. Duplicate sources are
33*82c905dcSAndrew Geissler#     ignored.
34*82c905dcSAndrew Geissler# 12) Source mirror exclusions:
35*82c905dcSAndrew Geissler#     ARCHIVER_MIRROR_EXCLUDE is a list of prefixes to exclude from the mirror.
36*82c905dcSAndrew Geissler#     This may be used for sources which you are already publishing yourself
37*82c905dcSAndrew Geissler#     (e.g. if the URI starts with 'https://mysite.com/' and your mirror is
38*82c905dcSAndrew Geissler#     going to be published to the same site). It may also be used to exclude
39*82c905dcSAndrew Geissler#     local files (with the prefix 'file://') if these will be provided as part
40*82c905dcSAndrew Geissler#     of an archive of the layers themselves.
41eb8dc403SDave Cobbley#
42eb8dc403SDave Cobbley
43eb8dc403SDave Cobbley# Create archive for all the recipe types
44eb8dc403SDave CobbleyCOPYLEFT_RECIPE_TYPES ?= 'target native nativesdk cross crosssdk cross-canadian'
45eb8dc403SDave Cobbleyinherit copyleft_filter
46eb8dc403SDave Cobbley
47eb8dc403SDave CobbleyARCHIVER_MODE[srpm] ?= "0"
48eb8dc403SDave CobbleyARCHIVER_MODE[src] ?= "patched"
49eb8dc403SDave CobbleyARCHIVER_MODE[diff] ?= "0"
50eb8dc403SDave CobbleyARCHIVER_MODE[diff-exclude] ?= ".pc autom4te.cache patches"
51eb8dc403SDave CobbleyARCHIVER_MODE[dumpdata] ?= "0"
52eb8dc403SDave CobbleyARCHIVER_MODE[recipe] ?= "0"
53*82c905dcSAndrew GeisslerARCHIVER_MODE[mirror] ?= "split"
54eb8dc403SDave Cobbley
55eb8dc403SDave CobbleyDEPLOY_DIR_SRC ?= "${DEPLOY_DIR}/sources"
56eb8dc403SDave CobbleyARCHIVER_TOPDIR ?= "${WORKDIR}/deploy-sources"
57eb8dc403SDave CobbleyARCHIVER_OUTDIR = "${ARCHIVER_TOPDIR}/${TARGET_SYS}/${PF}/"
5819323693SBrad BishopARCHIVER_RPMTOPDIR ?= "${WORKDIR}/deploy-sources-rpm"
5919323693SBrad BishopARCHIVER_RPMOUTDIR = "${ARCHIVER_RPMTOPDIR}/${TARGET_SYS}/${PF}/"
60eb8dc403SDave CobbleyARCHIVER_WORKDIR = "${WORKDIR}/archiver-work/"
61eb8dc403SDave Cobbley
62*82c905dcSAndrew Geissler# When producing a combined mirror directory, allow duplicates for the case
63*82c905dcSAndrew Geissler# where multiple recipes use the same SRC_URI.
64*82c905dcSAndrew GeisslerARCHIVER_COMBINED_MIRRORDIR = "${ARCHIVER_TOPDIR}/mirror"
65*82c905dcSAndrew GeisslerSSTATE_DUPWHITELIST += "${DEPLOY_DIR_SRC}/mirror"
6619323693SBrad Bishop
67eb8dc403SDave Cobbleydo_dumpdata[dirs] = "${ARCHIVER_OUTDIR}"
68eb8dc403SDave Cobbleydo_ar_recipe[dirs] = "${ARCHIVER_OUTDIR}"
69eb8dc403SDave Cobbleydo_ar_original[dirs] = "${ARCHIVER_OUTDIR} ${ARCHIVER_WORKDIR}"
70eb8dc403SDave Cobbleydo_deploy_archives[dirs] = "${WORKDIR}"
71eb8dc403SDave Cobbley
72eb8dc403SDave Cobbley# This is a convenience for the shell script to use it
73eb8dc403SDave Cobbley
74eb8dc403SDave Cobbley
75eb8dc403SDave Cobbleypython () {
76eb8dc403SDave Cobbley    pn = d.getVar('PN')
77eb8dc403SDave Cobbley    assume_provided = (d.getVar("ASSUME_PROVIDED") or "").split()
78eb8dc403SDave Cobbley    if pn in assume_provided:
79eb8dc403SDave Cobbley        for p in d.getVar("PROVIDES").split():
80eb8dc403SDave Cobbley            if p != pn:
81eb8dc403SDave Cobbley                pn = p
82eb8dc403SDave Cobbley                break
83eb8dc403SDave Cobbley
84eb8dc403SDave Cobbley    included, reason = copyleft_should_include(d)
85eb8dc403SDave Cobbley    if not included:
86eb8dc403SDave Cobbley        bb.debug(1, 'archiver: %s is excluded: %s' % (pn, reason))
87eb8dc403SDave Cobbley        return
88eb8dc403SDave Cobbley    else:
89eb8dc403SDave Cobbley        bb.debug(1, 'archiver: %s is included: %s' % (pn, reason))
90eb8dc403SDave Cobbley
91eb8dc403SDave Cobbley
92eb8dc403SDave Cobbley    # glibc-locale: do_fetch, do_unpack and do_patch tasks have been deleted,
93eb8dc403SDave Cobbley    # so avoid archiving source here.
94eb8dc403SDave Cobbley    if pn.startswith('glibc-locale'):
95eb8dc403SDave Cobbley        return
96eb8dc403SDave Cobbley
97eb8dc403SDave Cobbley    # We just archive gcc-source for all the gcc related recipes
98eb8dc403SDave Cobbley    if d.getVar('BPN') in ['gcc', 'libgcc'] \
99eb8dc403SDave Cobbley            and not pn.startswith('gcc-source'):
100eb8dc403SDave Cobbley        bb.debug(1, 'archiver: %s is excluded, covered by gcc-source' % pn)
101eb8dc403SDave Cobbley        return
102eb8dc403SDave Cobbley
10379641f25SBrad Bishop    def hasTask(task):
10479641f25SBrad Bishop        return bool(d.getVarFlag(task, "task", False)) and not bool(d.getVarFlag(task, "noexec", False))
10579641f25SBrad Bishop
106eb8dc403SDave Cobbley    ar_src = d.getVarFlag('ARCHIVER_MODE', 'src')
107eb8dc403SDave Cobbley    ar_dumpdata = d.getVarFlag('ARCHIVER_MODE', 'dumpdata')
108eb8dc403SDave Cobbley    ar_recipe = d.getVarFlag('ARCHIVER_MODE', 'recipe')
109eb8dc403SDave Cobbley
110eb8dc403SDave Cobbley    if ar_src == "original":
111eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_original' % pn)
112eb8dc403SDave Cobbley        # 'patched' and 'configured' invoke do_unpack_and_patch because
113eb8dc403SDave Cobbley        # do_ar_patched resp. do_ar_configured depend on it, but for 'original'
114eb8dc403SDave Cobbley        # we have to add it explicitly.
115eb8dc403SDave Cobbley        if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
116eb8dc403SDave Cobbley            d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_unpack_and_patch' % pn)
117eb8dc403SDave Cobbley    elif ar_src == "patched":
118eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_patched' % pn)
119eb8dc403SDave Cobbley    elif ar_src == "configured":
120eb8dc403SDave Cobbley        # We can't use "addtask do_ar_configured after do_configure" since it
121eb8dc403SDave Cobbley        # will cause the deptask of do_populate_sysroot to run not matter what
122eb8dc403SDave Cobbley        # archives we need, so we add the depends here.
123eb8dc403SDave Cobbley
124eb8dc403SDave Cobbley        # There is a corner case with "gcc-source-${PV}" recipes, they don't have
125eb8dc403SDave Cobbley        # the "do_configure" task, so we need to use "do_preconfigure"
1261a4b7ee2SBrad Bishop        if hasTask("do_preconfigure"):
127eb8dc403SDave Cobbley            d.appendVarFlag('do_ar_configured', 'depends', ' %s:do_preconfigure' % pn)
1281a4b7ee2SBrad Bishop        elif hasTask("do_configure"):
129eb8dc403SDave Cobbley            d.appendVarFlag('do_ar_configured', 'depends', ' %s:do_configure' % pn)
130eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_configured' % pn)
131*82c905dcSAndrew Geissler    elif ar_src == "mirror":
132*82c905dcSAndrew Geissler        d.appendVarFlag('do_deploy_archives', 'depends', '%s:do_ar_mirror' % pn)
133eb8dc403SDave Cobbley
134eb8dc403SDave Cobbley    elif ar_src:
135eb8dc403SDave Cobbley        bb.fatal("Invalid ARCHIVER_MODE[src]: %s" % ar_src)
136eb8dc403SDave Cobbley
137eb8dc403SDave Cobbley    if ar_dumpdata == "1":
138eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_dumpdata' % pn)
139eb8dc403SDave Cobbley
140eb8dc403SDave Cobbley    if ar_recipe == "1":
141eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_recipe' % pn)
142eb8dc403SDave Cobbley
143eb8dc403SDave Cobbley    # Output the SRPM package
144eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'srpm') == "1" and d.getVar('PACKAGES'):
14579641f25SBrad Bishop        if "package_rpm" not in d.getVar('PACKAGE_CLASSES'):
14679641f25SBrad Bishop            bb.fatal("ARCHIVER_MODE[srpm] needs package_rpm in PACKAGE_CLASSES")
14779641f25SBrad Bishop
14879641f25SBrad Bishop        # Some recipes do not have any packaging tasks
14979641f25SBrad Bishop        if hasTask("do_package_write_rpm"):
150eb8dc403SDave Cobbley            d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_package_write_rpm' % pn)
15119323693SBrad Bishop            d.appendVarFlag('do_package_write_rpm', 'dirs', ' ${ARCHIVER_RPMTOPDIR}')
15219323693SBrad Bishop            d.appendVarFlag('do_package_write_rpm', 'sstate-inputdirs', ' ${ARCHIVER_RPMTOPDIR}')
15319323693SBrad Bishop            d.appendVarFlag('do_package_write_rpm', 'sstate-outputdirs', ' ${DEPLOY_DIR_SRC}')
154eb8dc403SDave Cobbley            if ar_dumpdata == "1":
155eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_dumpdata' % pn)
156eb8dc403SDave Cobbley            if ar_recipe == "1":
157eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_recipe' % pn)
158eb8dc403SDave Cobbley            if ar_src == "original":
159eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_original' % pn)
160eb8dc403SDave Cobbley            elif ar_src == "patched":
161eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_patched' % pn)
162eb8dc403SDave Cobbley            elif ar_src == "configured":
163eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_configured' % pn)
164eb8dc403SDave Cobbley}
165eb8dc403SDave Cobbley
166eb8dc403SDave Cobbley# Take all the sources for a recipe and puts them in WORKDIR/archiver-work/.
167eb8dc403SDave Cobbley# Files in SRC_URI are copied directly, anything that's a directory
168eb8dc403SDave Cobbley# (e.g. git repositories) is "unpacked" and then put into a tarball.
169eb8dc403SDave Cobbleypython do_ar_original() {
170eb8dc403SDave Cobbley
171eb8dc403SDave Cobbley    import shutil, tempfile
172eb8dc403SDave Cobbley
173eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'src') != "original":
174eb8dc403SDave Cobbley        return
175eb8dc403SDave Cobbley
176eb8dc403SDave Cobbley    ar_outdir = d.getVar('ARCHIVER_OUTDIR')
177eb8dc403SDave Cobbley    bb.note('Archiving the original source...')
178eb8dc403SDave Cobbley    urls = d.getVar("SRC_URI").split()
179eb8dc403SDave Cobbley    # destsuffix (git fetcher) and subdir (everything else) are allowed to be
180eb8dc403SDave Cobbley    # absolute paths (for example, destsuffix=${S}/foobar).
181eb8dc403SDave Cobbley    # That messes with unpacking inside our tmpdir below, because the fetchers
182eb8dc403SDave Cobbley    # will then unpack in that directory and completely ignore the tmpdir.
183eb8dc403SDave Cobbley    # That breaks parallel tasks relying on ${S}, like do_compile.
184eb8dc403SDave Cobbley    #
185eb8dc403SDave Cobbley    # To solve this, we remove these parameters from all URLs.
186eb8dc403SDave Cobbley    # We do this even for relative paths because it makes the content of the
187eb8dc403SDave Cobbley    # archives more useful (no extra paths that are only used during
188eb8dc403SDave Cobbley    # compilation).
189eb8dc403SDave Cobbley    for i, url in enumerate(urls):
190eb8dc403SDave Cobbley        decoded = bb.fetch2.decodeurl(url)
191eb8dc403SDave Cobbley        for param in ('destsuffix', 'subdir'):
192eb8dc403SDave Cobbley            if param in decoded[5]:
193eb8dc403SDave Cobbley                del decoded[5][param]
194eb8dc403SDave Cobbley        encoded = bb.fetch2.encodeurl(decoded)
195eb8dc403SDave Cobbley        urls[i] = encoded
196eb8dc403SDave Cobbley    fetch = bb.fetch2.Fetch(urls, d)
197eb8dc403SDave Cobbley    tarball_suffix = {}
198eb8dc403SDave Cobbley    for url in fetch.urls:
199eb8dc403SDave Cobbley        local = fetch.localpath(url).rstrip("/");
200eb8dc403SDave Cobbley        if os.path.isfile(local):
201eb8dc403SDave Cobbley            shutil.copy(local, ar_outdir)
202eb8dc403SDave Cobbley        elif os.path.isdir(local):
203eb8dc403SDave Cobbley            tmpdir = tempfile.mkdtemp(dir=d.getVar('ARCHIVER_WORKDIR'))
204eb8dc403SDave Cobbley            fetch.unpack(tmpdir, (url,))
205eb8dc403SDave Cobbley            # To handle recipes with more than one source, we add the "name"
206eb8dc403SDave Cobbley            # URL parameter as suffix. We treat it as an error when
207eb8dc403SDave Cobbley            # there's more than one URL without a name, or a name gets reused.
208eb8dc403SDave Cobbley            # This is an additional safety net, in practice the name has
209eb8dc403SDave Cobbley            # to be set when using the git fetcher, otherwise SRCREV cannot
210eb8dc403SDave Cobbley            # be set separately for each URL.
211eb8dc403SDave Cobbley            params = bb.fetch2.decodeurl(url)[5]
212eb8dc403SDave Cobbley            type = bb.fetch2.decodeurl(url)[0]
213eb8dc403SDave Cobbley            location = bb.fetch2.decodeurl(url)[2]
214eb8dc403SDave Cobbley            name = params.get('name', '')
215eb8dc403SDave Cobbley            if type.lower() == 'file':
216eb8dc403SDave Cobbley                name_tmp = location.rstrip("*").rstrip("/")
217eb8dc403SDave Cobbley                name = os.path.basename(name_tmp)
218eb8dc403SDave Cobbley            else:
219eb8dc403SDave Cobbley                if name in tarball_suffix:
220eb8dc403SDave Cobbley                    if not name:
221eb8dc403SDave Cobbley                        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))
222eb8dc403SDave Cobbley                    else:
223eb8dc403SDave Cobbley                        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))
224eb8dc403SDave Cobbley            tarball_suffix[name] = url
225eb8dc403SDave Cobbley            create_tarball(d, tmpdir + '/.', name, ar_outdir)
226eb8dc403SDave Cobbley
227eb8dc403SDave Cobbley    # Emit patch series files for 'original'
228eb8dc403SDave Cobbley    bb.note('Writing patch series files...')
229eb8dc403SDave Cobbley    for patch in src_patches(d):
230eb8dc403SDave Cobbley        _, _, local, _, _, parm = bb.fetch.decodeurl(patch)
231eb8dc403SDave Cobbley        patchdir = parm.get('patchdir')
232eb8dc403SDave Cobbley        if patchdir:
233eb8dc403SDave Cobbley            series = os.path.join(ar_outdir, 'series.subdir.%s' % patchdir.replace('/', '_'))
234eb8dc403SDave Cobbley        else:
235eb8dc403SDave Cobbley            series = os.path.join(ar_outdir, 'series')
236eb8dc403SDave Cobbley
237eb8dc403SDave Cobbley        with open(series, 'a') as s:
238eb8dc403SDave Cobbley            s.write('%s -p%s\n' % (os.path.basename(local), parm['striplevel']))
239eb8dc403SDave Cobbley}
240eb8dc403SDave Cobbley
241eb8dc403SDave Cobbleypython do_ar_patched() {
242eb8dc403SDave Cobbley
243eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'src') != 'patched':
244eb8dc403SDave Cobbley        return
245eb8dc403SDave Cobbley
246eb8dc403SDave Cobbley    # Get the ARCHIVER_OUTDIR before we reset the WORKDIR
247eb8dc403SDave Cobbley    ar_outdir = d.getVar('ARCHIVER_OUTDIR')
248a34c030eSBrad Bishop    if not is_work_shared(d):
249eb8dc403SDave Cobbley        ar_workdir = d.getVar('ARCHIVER_WORKDIR')
250eb8dc403SDave Cobbley        d.setVar('WORKDIR', ar_workdir)
251a34c030eSBrad Bishop    bb.note('Archiving the patched source...')
252eb8dc403SDave Cobbley    create_tarball(d, d.getVar('S'), 'patched', ar_outdir)
253eb8dc403SDave Cobbley}
254eb8dc403SDave Cobbley
255eb8dc403SDave Cobbleypython do_ar_configured() {
256eb8dc403SDave Cobbley    import shutil
257eb8dc403SDave Cobbley
258eb8dc403SDave Cobbley    # Forcibly expand the sysroot paths as we're about to change WORKDIR
259eb8dc403SDave Cobbley    d.setVar('STAGING_DIR_HOST', d.getVar('STAGING_DIR_HOST'))
260eb8dc403SDave Cobbley    d.setVar('STAGING_DIR_TARGET', d.getVar('STAGING_DIR_TARGET'))
261eb8dc403SDave Cobbley    d.setVar('RECIPE_SYSROOT', d.getVar('RECIPE_SYSROOT'))
262eb8dc403SDave Cobbley    d.setVar('RECIPE_SYSROOT_NATIVE', d.getVar('RECIPE_SYSROOT_NATIVE'))
263eb8dc403SDave Cobbley
264eb8dc403SDave Cobbley    ar_outdir = d.getVar('ARCHIVER_OUTDIR')
265eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'src') == 'configured':
266eb8dc403SDave Cobbley        bb.note('Archiving the configured source...')
267eb8dc403SDave Cobbley        pn = d.getVar('PN')
268eb8dc403SDave Cobbley        # "gcc-source-${PV}" recipes don't have "do_configure"
269eb8dc403SDave Cobbley        # task, so we need to run "do_preconfigure" instead
270eb8dc403SDave Cobbley        if pn.startswith("gcc-source-"):
271eb8dc403SDave Cobbley            d.setVar('WORKDIR', d.getVar('ARCHIVER_WORKDIR'))
272eb8dc403SDave Cobbley            bb.build.exec_func('do_preconfigure', d)
273eb8dc403SDave Cobbley
274eb8dc403SDave Cobbley        # The libtool-native's do_configure will remove the
275eb8dc403SDave Cobbley        # ${STAGING_DATADIR}/aclocal/libtool.m4, so we can't re-run the
276eb8dc403SDave Cobbley        # do_configure, we archive the already configured ${S} to
277eb8dc403SDave Cobbley        # instead of.
278eb8dc403SDave Cobbley        elif pn != 'libtool-native':
2791a4b7ee2SBrad Bishop            def runTask(task):
2801a4b7ee2SBrad Bishop                prefuncs = d.getVarFlag(task, 'prefuncs') or ''
2811a4b7ee2SBrad Bishop                for func in prefuncs.split():
282eb8dc403SDave Cobbley                    if func != "sysroot_cleansstate":
283eb8dc403SDave Cobbley                        bb.build.exec_func(func, d)
2841a4b7ee2SBrad Bishop                bb.build.exec_func(task, d)
2851a4b7ee2SBrad Bishop                postfuncs = d.getVarFlag(task, 'postfuncs') or ''
2861a4b7ee2SBrad Bishop                for func in postfuncs.split():
2871a4b7ee2SBrad Bishop                    if func != 'do_qa_configure':
288eb8dc403SDave Cobbley                        bb.build.exec_func(func, d)
2891a4b7ee2SBrad Bishop
2901a4b7ee2SBrad Bishop            # Change the WORKDIR to make do_configure run in another dir.
2911a4b7ee2SBrad Bishop            d.setVar('WORKDIR', d.getVar('ARCHIVER_WORKDIR'))
2921a4b7ee2SBrad Bishop
2931a4b7ee2SBrad Bishop            preceeds = bb.build.preceedtask('do_configure', False, d)
2941a4b7ee2SBrad Bishop            for task in preceeds:
2951a4b7ee2SBrad Bishop                if task != 'do_patch' and task != 'do_prepare_recipe_sysroot':
2961a4b7ee2SBrad Bishop                    runTask(task)
2971a4b7ee2SBrad Bishop            runTask('do_configure')
2981a4b7ee2SBrad Bishop
299eb8dc403SDave Cobbley        srcdir = d.getVar('S')
300eb8dc403SDave Cobbley        builddir = d.getVar('B')
301eb8dc403SDave Cobbley        if srcdir != builddir:
302eb8dc403SDave Cobbley            if os.path.exists(builddir):
303eb8dc403SDave Cobbley                oe.path.copytree(builddir, os.path.join(srcdir, \
304eb8dc403SDave Cobbley                    'build.%s.ar_configured' % d.getVar('PF')))
305eb8dc403SDave Cobbley        create_tarball(d, srcdir, 'configured', ar_outdir)
306eb8dc403SDave Cobbley}
307eb8dc403SDave Cobbley
308*82c905dcSAndrew Geisslerpython do_ar_mirror() {
309*82c905dcSAndrew Geissler    import subprocess
310*82c905dcSAndrew Geissler
311*82c905dcSAndrew Geissler    src_uri = (d.getVar('SRC_URI') or '').split()
312*82c905dcSAndrew Geissler    if len(src_uri) == 0:
313*82c905dcSAndrew Geissler        return
314*82c905dcSAndrew Geissler
315*82c905dcSAndrew Geissler    dl_dir = d.getVar('DL_DIR')
316*82c905dcSAndrew Geissler    mirror_exclusions = (d.getVar('ARCHIVER_MIRROR_EXCLUDE') or '').split()
317*82c905dcSAndrew Geissler    mirror_mode = d.getVarFlag('ARCHIVER_MODE', 'mirror')
318*82c905dcSAndrew Geissler    have_mirror_tarballs = d.getVar('BB_GENERATE_MIRROR_TARBALLS')
319*82c905dcSAndrew Geissler
320*82c905dcSAndrew Geissler    if mirror_mode == 'combined':
321*82c905dcSAndrew Geissler        destdir = d.getVar('ARCHIVER_COMBINED_MIRRORDIR')
322*82c905dcSAndrew Geissler    elif mirror_mode == 'split':
323*82c905dcSAndrew Geissler        destdir = d.getVar('ARCHIVER_OUTDIR')
324*82c905dcSAndrew Geissler    else:
325*82c905dcSAndrew Geissler        bb.fatal('Invalid ARCHIVER_MODE[mirror]: %s' % (mirror_mode))
326*82c905dcSAndrew Geissler
327*82c905dcSAndrew Geissler    if not have_mirror_tarballs:
328*82c905dcSAndrew Geissler        bb.fatal('Using `ARCHIVER_MODE[src] = "mirror"` depends on setting `BB_GENERATE_MIRROR_TARBALLS = "1"`')
329*82c905dcSAndrew Geissler
330*82c905dcSAndrew Geissler    def is_excluded(url):
331*82c905dcSAndrew Geissler        for prefix in mirror_exclusions:
332*82c905dcSAndrew Geissler            if url.startswith(prefix):
333*82c905dcSAndrew Geissler                return True
334*82c905dcSAndrew Geissler        return False
335*82c905dcSAndrew Geissler
336*82c905dcSAndrew Geissler    bb.note('Archiving the source as a mirror...')
337*82c905dcSAndrew Geissler
338*82c905dcSAndrew Geissler    bb.utils.mkdirhier(destdir)
339*82c905dcSAndrew Geissler
340*82c905dcSAndrew Geissler    fetcher = bb.fetch2.Fetch(src_uri, d)
341*82c905dcSAndrew Geissler
342*82c905dcSAndrew Geissler    for url in fetcher.urls:
343*82c905dcSAndrew Geissler        if is_excluded(url):
344*82c905dcSAndrew Geissler            bb.note('Skipping excluded url: %s' % (url))
345*82c905dcSAndrew Geissler            continue
346*82c905dcSAndrew Geissler
347*82c905dcSAndrew Geissler        bb.note('Archiving url: %s' % (url))
348*82c905dcSAndrew Geissler        ud = fetcher.ud[url]
349*82c905dcSAndrew Geissler        ud.setup_localpath(d)
350*82c905dcSAndrew Geissler        localpath = None
351*82c905dcSAndrew Geissler
352*82c905dcSAndrew Geissler        # Check for mirror tarballs first. We will archive the first mirror
353*82c905dcSAndrew Geissler        # tarball that we find as it's assumed that we just need one.
354*82c905dcSAndrew Geissler        for mirror_fname in ud.mirrortarballs:
355*82c905dcSAndrew Geissler            mirror_path = os.path.join(dl_dir, mirror_fname)
356*82c905dcSAndrew Geissler            if os.path.exists(mirror_path):
357*82c905dcSAndrew Geissler                bb.note('Found mirror tarball: %s' % (mirror_path))
358*82c905dcSAndrew Geissler                localpath = mirror_path
359*82c905dcSAndrew Geissler                break
360*82c905dcSAndrew Geissler
361*82c905dcSAndrew Geissler        if len(ud.mirrortarballs) and not localpath:
362*82c905dcSAndrew Geissler            bb.warn('Mirror tarballs are listed for a source but none are present. ' \
363*82c905dcSAndrew Geissler                    'Falling back to original download.\n' \
364*82c905dcSAndrew Geissler                    'SRC_URI = %s' % (url))
365*82c905dcSAndrew Geissler
366*82c905dcSAndrew Geissler        # Check original download
367*82c905dcSAndrew Geissler        if not localpath:
368*82c905dcSAndrew Geissler            bb.note('Using original download: %s' % (ud.localpath))
369*82c905dcSAndrew Geissler            localpath = ud.localpath
370*82c905dcSAndrew Geissler
371*82c905dcSAndrew Geissler        if not localpath or not os.path.exists(localpath):
372*82c905dcSAndrew Geissler            bb.fatal('Original download is missing for a source.\n' \
373*82c905dcSAndrew Geissler                        'SRC_URI = %s' % (url))
374*82c905dcSAndrew Geissler
375*82c905dcSAndrew Geissler        # We now have an appropriate localpath
376*82c905dcSAndrew Geissler        bb.note('Copying source mirror')
377*82c905dcSAndrew Geissler        cmd = 'cp -fpPRH %s %s' % (localpath, destdir)
378*82c905dcSAndrew Geissler        subprocess.check_call(cmd, shell=True)
379*82c905dcSAndrew Geissler}
380*82c905dcSAndrew Geissler
381c4ea075dSBrad Bishopdef exclude_useless_paths(tarinfo):
382c4ea075dSBrad Bishop    if tarinfo.isdir():
383c4ea075dSBrad Bishop        if tarinfo.name.endswith('/temp') or tarinfo.name.endswith('/patches') or tarinfo.name.endswith('/.pc'):
384c4ea075dSBrad Bishop            return None
385c4ea075dSBrad Bishop        elif tarinfo.name == 'temp' or tarinfo.name == 'patches' or tarinfo.name == '.pc':
386c4ea075dSBrad Bishop            return None
387c4ea075dSBrad Bishop    return tarinfo
388c4ea075dSBrad Bishop
389eb8dc403SDave Cobbleydef create_tarball(d, srcdir, suffix, ar_outdir):
390eb8dc403SDave Cobbley    """
391eb8dc403SDave Cobbley    create the tarball from srcdir
392eb8dc403SDave Cobbley    """
393eb8dc403SDave Cobbley    import tarfile
394eb8dc403SDave Cobbley
395eb8dc403SDave Cobbley    # Make sure we are only creating a single tarball for gcc sources
396eb8dc403SDave Cobbley    if (d.getVar('SRC_URI') == ""):
397eb8dc403SDave Cobbley        return
398eb8dc403SDave Cobbley
399eb8dc403SDave Cobbley    # For the kernel archive, srcdir may just be a link to the
400eb8dc403SDave Cobbley    # work-shared location. Use os.path.realpath to make sure
401eb8dc403SDave Cobbley    # that we archive the actual directory and not just the link.
402eb8dc403SDave Cobbley    srcdir = os.path.realpath(srcdir)
403eb8dc403SDave Cobbley
404eb8dc403SDave Cobbley    bb.utils.mkdirhier(ar_outdir)
405eb8dc403SDave Cobbley    if suffix:
406eb8dc403SDave Cobbley        filename = '%s-%s.tar.gz' % (d.getVar('PF'), suffix)
407eb8dc403SDave Cobbley    else:
408eb8dc403SDave Cobbley        filename = '%s.tar.gz' % d.getVar('PF')
409eb8dc403SDave Cobbley    tarname = os.path.join(ar_outdir, filename)
410eb8dc403SDave Cobbley
411eb8dc403SDave Cobbley    bb.note('Creating %s' % tarname)
412eb8dc403SDave Cobbley    tar = tarfile.open(tarname, 'w:gz')
413c4ea075dSBrad Bishop    tar.add(srcdir, arcname=os.path.basename(srcdir), filter=exclude_useless_paths)
414eb8dc403SDave Cobbley    tar.close()
415eb8dc403SDave Cobbley
416eb8dc403SDave Cobbley# creating .diff.gz between source.orig and source
417eb8dc403SDave Cobbleydef create_diff_gz(d, src_orig, src, ar_outdir):
418eb8dc403SDave Cobbley
419eb8dc403SDave Cobbley    import subprocess
420eb8dc403SDave Cobbley
421eb8dc403SDave Cobbley    if not os.path.isdir(src) or not os.path.isdir(src_orig):
422eb8dc403SDave Cobbley        return
423eb8dc403SDave Cobbley
424eb8dc403SDave Cobbley    # The diff --exclude can't exclude the file with path, so we copy
425eb8dc403SDave Cobbley    # the patched source, and remove the files that we'd like to
426eb8dc403SDave Cobbley    # exclude.
427eb8dc403SDave Cobbley    src_patched = src + '.patched'
428eb8dc403SDave Cobbley    oe.path.copyhardlinktree(src, src_patched)
429eb8dc403SDave Cobbley    for i in d.getVarFlag('ARCHIVER_MODE', 'diff-exclude').split():
430eb8dc403SDave Cobbley        bb.utils.remove(os.path.join(src_orig, i), recurse=True)
431eb8dc403SDave Cobbley        bb.utils.remove(os.path.join(src_patched, i), recurse=True)
432eb8dc403SDave Cobbley
433eb8dc403SDave Cobbley    dirname = os.path.dirname(src)
434eb8dc403SDave Cobbley    basename = os.path.basename(src)
435eb8dc403SDave Cobbley    bb.utils.mkdirhier(ar_outdir)
436eb8dc403SDave Cobbley    cwd = os.getcwd()
437eb8dc403SDave Cobbley    try:
438eb8dc403SDave Cobbley        os.chdir(dirname)
439eb8dc403SDave Cobbley        out_file = os.path.join(ar_outdir, '%s-diff.gz' % d.getVar('PF'))
440eb8dc403SDave Cobbley        diff_cmd = 'diff -Naur %s.orig %s.patched | gzip -c > %s' % (basename, basename, out_file)
441eb8dc403SDave Cobbley        subprocess.check_call(diff_cmd, shell=True)
442eb8dc403SDave Cobbley        bb.utils.remove(src_patched, recurse=True)
443eb8dc403SDave Cobbley    finally:
444eb8dc403SDave Cobbley        os.chdir(cwd)
445eb8dc403SDave Cobbley
446eb8dc403SDave Cobbleydef is_work_shared(d):
447eb8dc403SDave Cobbley    pn = d.getVar('PN')
448eb8dc403SDave Cobbley    return bb.data.inherits_class('kernel', d) or pn.startswith('gcc-source')
449eb8dc403SDave Cobbley
450eb8dc403SDave Cobbley# Run do_unpack and do_patch
451eb8dc403SDave Cobbleypython do_unpack_and_patch() {
452eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'src') not in \
453eb8dc403SDave Cobbley            [ 'patched', 'configured'] and \
454eb8dc403SDave Cobbley            d.getVarFlag('ARCHIVER_MODE', 'diff') != '1':
455eb8dc403SDave Cobbley        return
456eb8dc403SDave Cobbley    ar_outdir = d.getVar('ARCHIVER_OUTDIR')
457eb8dc403SDave Cobbley    ar_workdir = d.getVar('ARCHIVER_WORKDIR')
458eb8dc403SDave Cobbley    ar_sysroot_native = d.getVar('STAGING_DIR_NATIVE')
459eb8dc403SDave Cobbley    pn = d.getVar('PN')
460eb8dc403SDave Cobbley
461eb8dc403SDave Cobbley    # The kernel class functions require it to be on work-shared, so we dont change WORKDIR
462eb8dc403SDave Cobbley    if not is_work_shared(d):
463eb8dc403SDave Cobbley        # Change the WORKDIR to make do_unpack do_patch run in another dir.
464eb8dc403SDave Cobbley        d.setVar('WORKDIR', ar_workdir)
465eb8dc403SDave Cobbley        # Restore the original path to recipe's native sysroot (it's relative to WORKDIR).
466eb8dc403SDave Cobbley        d.setVar('STAGING_DIR_NATIVE', ar_sysroot_native)
467eb8dc403SDave Cobbley
468eb8dc403SDave Cobbley        # The changed 'WORKDIR' also caused 'B' changed, create dir 'B' for the
469eb8dc403SDave Cobbley        # possibly requiring of the following tasks (such as some recipes's
470eb8dc403SDave Cobbley        # do_patch required 'B' existed).
471eb8dc403SDave Cobbley        bb.utils.mkdirhier(d.getVar('B'))
472eb8dc403SDave Cobbley
473eb8dc403SDave Cobbley        bb.build.exec_func('do_unpack', d)
474eb8dc403SDave Cobbley
475eb8dc403SDave Cobbley    # Save the original source for creating the patches
476eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
477eb8dc403SDave Cobbley        src = d.getVar('S').rstrip('/')
478eb8dc403SDave Cobbley        src_orig = '%s.orig' % src
479eb8dc403SDave Cobbley        oe.path.copytree(src, src_orig)
480eb8dc403SDave Cobbley
481eb8dc403SDave Cobbley    # Make sure gcc and kernel sources are patched only once
482eb8dc403SDave Cobbley    if not (d.getVar('SRC_URI') == "" or is_work_shared(d)):
483eb8dc403SDave Cobbley        bb.build.exec_func('do_patch', d)
484eb8dc403SDave Cobbley
485eb8dc403SDave Cobbley    # Create the patches
486eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
487eb8dc403SDave Cobbley        bb.note('Creating diff gz...')
488eb8dc403SDave Cobbley        create_diff_gz(d, src_orig, src, ar_outdir)
489eb8dc403SDave Cobbley        bb.utils.remove(src_orig, recurse=True)
490eb8dc403SDave Cobbley}
491eb8dc403SDave Cobbley
492eb8dc403SDave Cobbley# BBINCLUDED is special (excluded from basehash signature
493eb8dc403SDave Cobbley# calculation). Using it in a task signature can cause "basehash
494eb8dc403SDave Cobbley# changed" errors.
495eb8dc403SDave Cobbley#
496eb8dc403SDave Cobbley# Depending on BBINCLUDED also causes do_ar_recipe to run again
497eb8dc403SDave Cobbley# for unrelated changes, like adding or removing buildhistory.bbclass.
498eb8dc403SDave Cobbley#
499eb8dc403SDave Cobbley# For these reasons we ignore the dependency completely. The versioning
500eb8dc403SDave Cobbley# of the output file ensures that we create it each time the recipe
501eb8dc403SDave Cobbley# gets rebuilt, at least as long as a PR server is used. We also rely
502eb8dc403SDave Cobbley# on that mechanism to catch changes in the file content, because the
503eb8dc403SDave Cobbley# file content is not part of of the task signature either.
504eb8dc403SDave Cobbleydo_ar_recipe[vardepsexclude] += "BBINCLUDED"
505eb8dc403SDave Cobbleypython do_ar_recipe () {
506eb8dc403SDave Cobbley    """
507eb8dc403SDave Cobbley    archive the recipe, including .bb and .inc.
508eb8dc403SDave Cobbley    """
509eb8dc403SDave Cobbley    import re
510eb8dc403SDave Cobbley    import shutil
511eb8dc403SDave Cobbley
512eb8dc403SDave Cobbley    require_re = re.compile( r"require\s+(.+)" )
513eb8dc403SDave Cobbley    include_re = re.compile( r"include\s+(.+)" )
514eb8dc403SDave Cobbley    bbfile = d.getVar('FILE')
515eb8dc403SDave Cobbley    outdir = os.path.join(d.getVar('WORKDIR'), \
516eb8dc403SDave Cobbley            '%s-recipe' % d.getVar('PF'))
517eb8dc403SDave Cobbley    bb.utils.mkdirhier(outdir)
518eb8dc403SDave Cobbley    shutil.copy(bbfile, outdir)
519eb8dc403SDave Cobbley
520eb8dc403SDave Cobbley    pn = d.getVar('PN')
521eb8dc403SDave Cobbley    bbappend_files = d.getVar('BBINCLUDED').split()
522eb8dc403SDave Cobbley    # If recipe name is aa, we need to match files like aa.bbappend and aa_1.1.bbappend
523eb8dc403SDave Cobbley    # Files like aa1.bbappend or aa1_1.1.bbappend must be excluded.
524eb8dc403SDave Cobbley    bbappend_re = re.compile( r".*/%s_[^/]*\.bbappend$" % re.escape(pn))
525eb8dc403SDave Cobbley    bbappend_re1 = re.compile( r".*/%s\.bbappend$" % re.escape(pn))
526eb8dc403SDave Cobbley    for file in bbappend_files:
527eb8dc403SDave Cobbley        if bbappend_re.match(file) or bbappend_re1.match(file):
528eb8dc403SDave Cobbley            shutil.copy(file, outdir)
529eb8dc403SDave Cobbley
530eb8dc403SDave Cobbley    dirname = os.path.dirname(bbfile)
531eb8dc403SDave Cobbley    bbpath = '%s:%s' % (dirname, d.getVar('BBPATH'))
532eb8dc403SDave Cobbley    f = open(bbfile, 'r')
533eb8dc403SDave Cobbley    for line in f.readlines():
534eb8dc403SDave Cobbley        incfile = None
535eb8dc403SDave Cobbley        if require_re.match(line):
536eb8dc403SDave Cobbley            incfile = require_re.match(line).group(1)
537eb8dc403SDave Cobbley        elif include_re.match(line):
538eb8dc403SDave Cobbley            incfile = include_re.match(line).group(1)
539eb8dc403SDave Cobbley        if incfile:
540eb8dc403SDave Cobbley            incfile = d.expand(incfile)
5411d80a2eaSBrad Bishop        if incfile:
542eb8dc403SDave Cobbley            incfile = bb.utils.which(bbpath, incfile)
543eb8dc403SDave Cobbley        if incfile:
544eb8dc403SDave Cobbley            shutil.copy(incfile, outdir)
545eb8dc403SDave Cobbley
546eb8dc403SDave Cobbley    create_tarball(d, outdir, 'recipe', d.getVar('ARCHIVER_OUTDIR'))
547eb8dc403SDave Cobbley    bb.utils.remove(outdir, recurse=True)
548eb8dc403SDave Cobbley}
549eb8dc403SDave Cobbley
550eb8dc403SDave Cobbleypython do_dumpdata () {
551eb8dc403SDave Cobbley    """
552eb8dc403SDave Cobbley    dump environment data to ${PF}-showdata.dump
553eb8dc403SDave Cobbley    """
554eb8dc403SDave Cobbley
555eb8dc403SDave Cobbley    dumpfile = os.path.join(d.getVar('ARCHIVER_OUTDIR'), \
556eb8dc403SDave Cobbley        '%s-showdata.dump' % d.getVar('PF'))
557eb8dc403SDave Cobbley    bb.note('Dumping metadata into %s' % dumpfile)
558eb8dc403SDave Cobbley    with open(dumpfile, "w") as f:
559eb8dc403SDave Cobbley        # emit variables and shell functions
560eb8dc403SDave Cobbley        bb.data.emit_env(f, d, True)
561eb8dc403SDave Cobbley        # emit the metadata which isn't valid shell
562eb8dc403SDave Cobbley        for e in d.keys():
563eb8dc403SDave Cobbley            if d.getVarFlag(e, "python", False):
564eb8dc403SDave Cobbley                f.write("\npython %s () {\n%s}\n" % (e, d.getVar(e, False)))
565eb8dc403SDave Cobbley}
566eb8dc403SDave Cobbley
567eb8dc403SDave CobbleySSTATETASKS += "do_deploy_archives"
568eb8dc403SDave Cobbleydo_deploy_archives () {
569eb8dc403SDave Cobbley    echo "Deploying source archive files from ${ARCHIVER_TOPDIR} to ${DEPLOY_DIR_SRC}."
570eb8dc403SDave Cobbley}
571eb8dc403SDave Cobbleypython do_deploy_archives_setscene () {
572eb8dc403SDave Cobbley    sstate_setscene(d)
573eb8dc403SDave Cobbley}
574eb8dc403SDave Cobbleydo_deploy_archives[dirs] = "${ARCHIVER_TOPDIR}"
575eb8dc403SDave Cobbleydo_deploy_archives[sstate-inputdirs] = "${ARCHIVER_TOPDIR}"
576eb8dc403SDave Cobbleydo_deploy_archives[sstate-outputdirs] = "${DEPLOY_DIR_SRC}"
577eb8dc403SDave Cobbleyaddtask do_deploy_archives_setscene
578eb8dc403SDave Cobbley
579eb8dc403SDave Cobbleyaddtask do_ar_original after do_unpack
580eb8dc403SDave Cobbleyaddtask do_unpack_and_patch after do_patch
581eb8dc403SDave Cobbleyaddtask do_ar_patched after do_unpack_and_patch
582eb8dc403SDave Cobbleyaddtask do_ar_configured after do_unpack_and_patch
583*82c905dcSAndrew Geissleraddtask do_ar_mirror after do_fetch
584eb8dc403SDave Cobbleyaddtask do_dumpdata
585eb8dc403SDave Cobbleyaddtask do_ar_recipe
586eb8dc403SDave Cobbleyaddtask do_deploy_archives before do_build
587eb8dc403SDave Cobbley
588eb8dc403SDave Cobbleypython () {
589eb8dc403SDave Cobbley    # Add tasks in the correct order, specifically for linux-yocto to avoid race condition.
590eb8dc403SDave Cobbley    # sstatesig.py:sstate_rundepfilter has special support that excludes this dependency
591eb8dc403SDave Cobbley    # so that do_kernel_configme does not need to run again when do_unpack_and_patch
592eb8dc403SDave Cobbley    # gets added or removed (by adding or removing archiver.bbclass).
593eb8dc403SDave Cobbley    if bb.data.inherits_class('kernel-yocto', d):
594eb8dc403SDave Cobbley        bb.build.addtask('do_kernel_configme', 'do_configure', 'do_unpack_and_patch', d)
595eb8dc403SDave Cobbley}
596