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"
8eb8dc403SDave Cobbley# 4) The patches between do_unpack and do_patch:
9eb8dc403SDave Cobbley#    ARCHIVER_MODE[diff] = "1"
10eb8dc403SDave Cobbley#    And you can set the one that you'd like to exclude from the diff:
11eb8dc403SDave Cobbley#    ARCHIVER_MODE[diff-exclude] ?= ".pc autom4te.cache patches"
12eb8dc403SDave Cobbley# 5) The environment data, similar to 'bitbake -e recipe':
13eb8dc403SDave Cobbley#    ARCHIVER_MODE[dumpdata] = "1"
14eb8dc403SDave Cobbley# 6) The recipe (.bb and .inc): ARCHIVER_MODE[recipe] = "1"
15eb8dc403SDave Cobbley# 7) Whether output the .src.rpm package:
16eb8dc403SDave Cobbley#    ARCHIVER_MODE[srpm] = "1"
17eb8dc403SDave Cobbley# 8) Filter the license, the recipe whose license in
18eb8dc403SDave Cobbley#    COPYLEFT_LICENSE_INCLUDE will be included, and in
19eb8dc403SDave Cobbley#    COPYLEFT_LICENSE_EXCLUDE will be excluded.
20eb8dc403SDave Cobbley#    COPYLEFT_LICENSE_INCLUDE = 'GPL* LGPL*'
21eb8dc403SDave Cobbley#    COPYLEFT_LICENSE_EXCLUDE = 'CLOSED Proprietary'
22eb8dc403SDave Cobbley# 9) The recipe type that will be archived:
23eb8dc403SDave Cobbley#    COPYLEFT_RECIPE_TYPES = 'target'
24eb8dc403SDave Cobbley#
25eb8dc403SDave Cobbley
26eb8dc403SDave Cobbley# Create archive for all the recipe types
27eb8dc403SDave CobbleyCOPYLEFT_RECIPE_TYPES ?= 'target native nativesdk cross crosssdk cross-canadian'
28eb8dc403SDave Cobbleyinherit copyleft_filter
29eb8dc403SDave Cobbley
30eb8dc403SDave CobbleyARCHIVER_MODE[srpm] ?= "0"
31eb8dc403SDave CobbleyARCHIVER_MODE[src] ?= "patched"
32eb8dc403SDave CobbleyARCHIVER_MODE[diff] ?= "0"
33eb8dc403SDave CobbleyARCHIVER_MODE[diff-exclude] ?= ".pc autom4te.cache patches"
34eb8dc403SDave CobbleyARCHIVER_MODE[dumpdata] ?= "0"
35eb8dc403SDave CobbleyARCHIVER_MODE[recipe] ?= "0"
36eb8dc403SDave Cobbley
37eb8dc403SDave CobbleyDEPLOY_DIR_SRC ?= "${DEPLOY_DIR}/sources"
38eb8dc403SDave CobbleyARCHIVER_TOPDIR ?= "${WORKDIR}/deploy-sources"
39eb8dc403SDave CobbleyARCHIVER_OUTDIR = "${ARCHIVER_TOPDIR}/${TARGET_SYS}/${PF}/"
4019323693SBrad BishopARCHIVER_RPMTOPDIR ?= "${WORKDIR}/deploy-sources-rpm"
4119323693SBrad BishopARCHIVER_RPMOUTDIR = "${ARCHIVER_RPMTOPDIR}/${TARGET_SYS}/${PF}/"
42eb8dc403SDave CobbleyARCHIVER_WORKDIR = "${WORKDIR}/archiver-work/"
43eb8dc403SDave Cobbley
4419323693SBrad Bishop
45eb8dc403SDave Cobbleydo_dumpdata[dirs] = "${ARCHIVER_OUTDIR}"
46eb8dc403SDave Cobbleydo_ar_recipe[dirs] = "${ARCHIVER_OUTDIR}"
47eb8dc403SDave Cobbleydo_ar_original[dirs] = "${ARCHIVER_OUTDIR} ${ARCHIVER_WORKDIR}"
48eb8dc403SDave Cobbleydo_deploy_archives[dirs] = "${WORKDIR}"
49eb8dc403SDave Cobbley
50eb8dc403SDave Cobbley# This is a convenience for the shell script to use it
51eb8dc403SDave Cobbley
52eb8dc403SDave Cobbley
53eb8dc403SDave Cobbleypython () {
54eb8dc403SDave Cobbley    pn = d.getVar('PN')
55eb8dc403SDave Cobbley    assume_provided = (d.getVar("ASSUME_PROVIDED") or "").split()
56eb8dc403SDave Cobbley    if pn in assume_provided:
57eb8dc403SDave Cobbley        for p in d.getVar("PROVIDES").split():
58eb8dc403SDave Cobbley            if p != pn:
59eb8dc403SDave Cobbley                pn = p
60eb8dc403SDave Cobbley                break
61eb8dc403SDave Cobbley
62eb8dc403SDave Cobbley    included, reason = copyleft_should_include(d)
63eb8dc403SDave Cobbley    if not included:
64eb8dc403SDave Cobbley        bb.debug(1, 'archiver: %s is excluded: %s' % (pn, reason))
65eb8dc403SDave Cobbley        return
66eb8dc403SDave Cobbley    else:
67eb8dc403SDave Cobbley        bb.debug(1, 'archiver: %s is included: %s' % (pn, reason))
68eb8dc403SDave Cobbley
69eb8dc403SDave Cobbley
70eb8dc403SDave Cobbley    # glibc-locale: do_fetch, do_unpack and do_patch tasks have been deleted,
71eb8dc403SDave Cobbley    # so avoid archiving source here.
72eb8dc403SDave Cobbley    if pn.startswith('glibc-locale'):
73eb8dc403SDave Cobbley        return
74eb8dc403SDave Cobbley
75eb8dc403SDave Cobbley    # We just archive gcc-source for all the gcc related recipes
76eb8dc403SDave Cobbley    if d.getVar('BPN') in ['gcc', 'libgcc'] \
77eb8dc403SDave Cobbley            and not pn.startswith('gcc-source'):
78eb8dc403SDave Cobbley        bb.debug(1, 'archiver: %s is excluded, covered by gcc-source' % pn)
79eb8dc403SDave Cobbley        return
80eb8dc403SDave Cobbley
8179641f25SBrad Bishop    def hasTask(task):
8279641f25SBrad Bishop        return bool(d.getVarFlag(task, "task", False)) and not bool(d.getVarFlag(task, "noexec", False))
8379641f25SBrad Bishop
84eb8dc403SDave Cobbley    ar_src = d.getVarFlag('ARCHIVER_MODE', 'src')
85eb8dc403SDave Cobbley    ar_dumpdata = d.getVarFlag('ARCHIVER_MODE', 'dumpdata')
86eb8dc403SDave Cobbley    ar_recipe = d.getVarFlag('ARCHIVER_MODE', 'recipe')
87eb8dc403SDave Cobbley
88eb8dc403SDave Cobbley    if ar_src == "original":
89eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_original' % pn)
90eb8dc403SDave Cobbley        # 'patched' and 'configured' invoke do_unpack_and_patch because
91eb8dc403SDave Cobbley        # do_ar_patched resp. do_ar_configured depend on it, but for 'original'
92eb8dc403SDave Cobbley        # we have to add it explicitly.
93eb8dc403SDave Cobbley        if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
94eb8dc403SDave Cobbley            d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_unpack_and_patch' % pn)
95eb8dc403SDave Cobbley    elif ar_src == "patched":
96eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_patched' % pn)
97eb8dc403SDave Cobbley    elif ar_src == "configured":
98eb8dc403SDave Cobbley        # We can't use "addtask do_ar_configured after do_configure" since it
99eb8dc403SDave Cobbley        # will cause the deptask of do_populate_sysroot to run not matter what
100eb8dc403SDave Cobbley        # archives we need, so we add the depends here.
101eb8dc403SDave Cobbley
102eb8dc403SDave Cobbley        # There is a corner case with "gcc-source-${PV}" recipes, they don't have
103eb8dc403SDave Cobbley        # the "do_configure" task, so we need to use "do_preconfigure"
1041a4b7ee2SBrad Bishop        if hasTask("do_preconfigure"):
105eb8dc403SDave Cobbley            d.appendVarFlag('do_ar_configured', 'depends', ' %s:do_preconfigure' % pn)
1061a4b7ee2SBrad Bishop        elif hasTask("do_configure"):
107eb8dc403SDave Cobbley            d.appendVarFlag('do_ar_configured', 'depends', ' %s:do_configure' % pn)
108eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_configured' % pn)
109eb8dc403SDave Cobbley
110eb8dc403SDave Cobbley    elif ar_src:
111eb8dc403SDave Cobbley        bb.fatal("Invalid ARCHIVER_MODE[src]: %s" % ar_src)
112eb8dc403SDave Cobbley
113eb8dc403SDave Cobbley    if ar_dumpdata == "1":
114eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_dumpdata' % pn)
115eb8dc403SDave Cobbley
116eb8dc403SDave Cobbley    if ar_recipe == "1":
117eb8dc403SDave Cobbley        d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_ar_recipe' % pn)
118eb8dc403SDave Cobbley
119eb8dc403SDave Cobbley    # Output the SRPM package
120eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'srpm') == "1" and d.getVar('PACKAGES'):
12179641f25SBrad Bishop        if "package_rpm" not in d.getVar('PACKAGE_CLASSES'):
12279641f25SBrad Bishop            bb.fatal("ARCHIVER_MODE[srpm] needs package_rpm in PACKAGE_CLASSES")
12379641f25SBrad Bishop
12479641f25SBrad Bishop        # Some recipes do not have any packaging tasks
12579641f25SBrad Bishop        if hasTask("do_package_write_rpm"):
126eb8dc403SDave Cobbley            d.appendVarFlag('do_deploy_archives', 'depends', ' %s:do_package_write_rpm' % pn)
12719323693SBrad Bishop            d.appendVarFlag('do_package_write_rpm', 'dirs', ' ${ARCHIVER_RPMTOPDIR}')
12819323693SBrad Bishop            d.appendVarFlag('do_package_write_rpm', 'sstate-inputdirs', ' ${ARCHIVER_RPMTOPDIR}')
12919323693SBrad Bishop            d.appendVarFlag('do_package_write_rpm', 'sstate-outputdirs', ' ${DEPLOY_DIR_SRC}')
130eb8dc403SDave Cobbley            if ar_dumpdata == "1":
131eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_dumpdata' % pn)
132eb8dc403SDave Cobbley            if ar_recipe == "1":
133eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_recipe' % pn)
134eb8dc403SDave Cobbley            if ar_src == "original":
135eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_original' % pn)
136eb8dc403SDave Cobbley            elif ar_src == "patched":
137eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_patched' % pn)
138eb8dc403SDave Cobbley            elif ar_src == "configured":
139eb8dc403SDave Cobbley                d.appendVarFlag('do_package_write_rpm', 'depends', ' %s:do_ar_configured' % pn)
140eb8dc403SDave Cobbley}
141eb8dc403SDave Cobbley
142eb8dc403SDave Cobbley# Take all the sources for a recipe and puts them in WORKDIR/archiver-work/.
143eb8dc403SDave Cobbley# Files in SRC_URI are copied directly, anything that's a directory
144eb8dc403SDave Cobbley# (e.g. git repositories) is "unpacked" and then put into a tarball.
145eb8dc403SDave Cobbleypython do_ar_original() {
146eb8dc403SDave Cobbley
147eb8dc403SDave Cobbley    import shutil, tempfile
148eb8dc403SDave Cobbley
149eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'src') != "original":
150eb8dc403SDave Cobbley        return
151eb8dc403SDave Cobbley
152eb8dc403SDave Cobbley    ar_outdir = d.getVar('ARCHIVER_OUTDIR')
153eb8dc403SDave Cobbley    bb.note('Archiving the original source...')
154eb8dc403SDave Cobbley    urls = d.getVar("SRC_URI").split()
155eb8dc403SDave Cobbley    # destsuffix (git fetcher) and subdir (everything else) are allowed to be
156eb8dc403SDave Cobbley    # absolute paths (for example, destsuffix=${S}/foobar).
157eb8dc403SDave Cobbley    # That messes with unpacking inside our tmpdir below, because the fetchers
158eb8dc403SDave Cobbley    # will then unpack in that directory and completely ignore the tmpdir.
159eb8dc403SDave Cobbley    # That breaks parallel tasks relying on ${S}, like do_compile.
160eb8dc403SDave Cobbley    #
161eb8dc403SDave Cobbley    # To solve this, we remove these parameters from all URLs.
162eb8dc403SDave Cobbley    # We do this even for relative paths because it makes the content of the
163eb8dc403SDave Cobbley    # archives more useful (no extra paths that are only used during
164eb8dc403SDave Cobbley    # compilation).
165eb8dc403SDave Cobbley    for i, url in enumerate(urls):
166eb8dc403SDave Cobbley        decoded = bb.fetch2.decodeurl(url)
167eb8dc403SDave Cobbley        for param in ('destsuffix', 'subdir'):
168eb8dc403SDave Cobbley            if param in decoded[5]:
169eb8dc403SDave Cobbley                del decoded[5][param]
170eb8dc403SDave Cobbley        encoded = bb.fetch2.encodeurl(decoded)
171eb8dc403SDave Cobbley        urls[i] = encoded
172eb8dc403SDave Cobbley    fetch = bb.fetch2.Fetch(urls, d)
173eb8dc403SDave Cobbley    tarball_suffix = {}
174eb8dc403SDave Cobbley    for url in fetch.urls:
175eb8dc403SDave Cobbley        local = fetch.localpath(url).rstrip("/");
176eb8dc403SDave Cobbley        if os.path.isfile(local):
177eb8dc403SDave Cobbley            shutil.copy(local, ar_outdir)
178eb8dc403SDave Cobbley        elif os.path.isdir(local):
179eb8dc403SDave Cobbley            tmpdir = tempfile.mkdtemp(dir=d.getVar('ARCHIVER_WORKDIR'))
180eb8dc403SDave Cobbley            fetch.unpack(tmpdir, (url,))
181eb8dc403SDave Cobbley            # To handle recipes with more than one source, we add the "name"
182eb8dc403SDave Cobbley            # URL parameter as suffix. We treat it as an error when
183eb8dc403SDave Cobbley            # there's more than one URL without a name, or a name gets reused.
184eb8dc403SDave Cobbley            # This is an additional safety net, in practice the name has
185eb8dc403SDave Cobbley            # to be set when using the git fetcher, otherwise SRCREV cannot
186eb8dc403SDave Cobbley            # be set separately for each URL.
187eb8dc403SDave Cobbley            params = bb.fetch2.decodeurl(url)[5]
188eb8dc403SDave Cobbley            type = bb.fetch2.decodeurl(url)[0]
189eb8dc403SDave Cobbley            location = bb.fetch2.decodeurl(url)[2]
190eb8dc403SDave Cobbley            name = params.get('name', '')
191eb8dc403SDave Cobbley            if type.lower() == 'file':
192eb8dc403SDave Cobbley                name_tmp = location.rstrip("*").rstrip("/")
193eb8dc403SDave Cobbley                name = os.path.basename(name_tmp)
194eb8dc403SDave Cobbley            else:
195eb8dc403SDave Cobbley                if name in tarball_suffix:
196eb8dc403SDave Cobbley                    if not name:
197eb8dc403SDave 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))
198eb8dc403SDave Cobbley                    else:
199eb8dc403SDave 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))
200eb8dc403SDave Cobbley            tarball_suffix[name] = url
201eb8dc403SDave Cobbley            create_tarball(d, tmpdir + '/.', name, ar_outdir)
202eb8dc403SDave Cobbley
203eb8dc403SDave Cobbley    # Emit patch series files for 'original'
204eb8dc403SDave Cobbley    bb.note('Writing patch series files...')
205eb8dc403SDave Cobbley    for patch in src_patches(d):
206eb8dc403SDave Cobbley        _, _, local, _, _, parm = bb.fetch.decodeurl(patch)
207eb8dc403SDave Cobbley        patchdir = parm.get('patchdir')
208eb8dc403SDave Cobbley        if patchdir:
209eb8dc403SDave Cobbley            series = os.path.join(ar_outdir, 'series.subdir.%s' % patchdir.replace('/', '_'))
210eb8dc403SDave Cobbley        else:
211eb8dc403SDave Cobbley            series = os.path.join(ar_outdir, 'series')
212eb8dc403SDave Cobbley
213eb8dc403SDave Cobbley        with open(series, 'a') as s:
214eb8dc403SDave Cobbley            s.write('%s -p%s\n' % (os.path.basename(local), parm['striplevel']))
215eb8dc403SDave Cobbley}
216eb8dc403SDave Cobbley
217eb8dc403SDave Cobbleypython do_ar_patched() {
218eb8dc403SDave Cobbley
219eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'src') != 'patched':
220eb8dc403SDave Cobbley        return
221eb8dc403SDave Cobbley
222eb8dc403SDave Cobbley    # Get the ARCHIVER_OUTDIR before we reset the WORKDIR
223eb8dc403SDave Cobbley    ar_outdir = d.getVar('ARCHIVER_OUTDIR')
224a34c030eSBrad Bishop    if not is_work_shared(d):
225eb8dc403SDave Cobbley        ar_workdir = d.getVar('ARCHIVER_WORKDIR')
226eb8dc403SDave Cobbley        d.setVar('WORKDIR', ar_workdir)
227a34c030eSBrad Bishop    bb.note('Archiving the patched source...')
228eb8dc403SDave Cobbley    create_tarball(d, d.getVar('S'), 'patched', ar_outdir)
229eb8dc403SDave Cobbley}
230eb8dc403SDave Cobbley
231eb8dc403SDave Cobbleypython do_ar_configured() {
232eb8dc403SDave Cobbley    import shutil
233eb8dc403SDave Cobbley
234eb8dc403SDave Cobbley    # Forcibly expand the sysroot paths as we're about to change WORKDIR
235eb8dc403SDave Cobbley    d.setVar('STAGING_DIR_HOST', d.getVar('STAGING_DIR_HOST'))
236eb8dc403SDave Cobbley    d.setVar('STAGING_DIR_TARGET', d.getVar('STAGING_DIR_TARGET'))
237eb8dc403SDave Cobbley    d.setVar('RECIPE_SYSROOT', d.getVar('RECIPE_SYSROOT'))
238eb8dc403SDave Cobbley    d.setVar('RECIPE_SYSROOT_NATIVE', d.getVar('RECIPE_SYSROOT_NATIVE'))
239eb8dc403SDave Cobbley
240eb8dc403SDave Cobbley    ar_outdir = d.getVar('ARCHIVER_OUTDIR')
241eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'src') == 'configured':
242eb8dc403SDave Cobbley        bb.note('Archiving the configured source...')
243eb8dc403SDave Cobbley        pn = d.getVar('PN')
244eb8dc403SDave Cobbley        # "gcc-source-${PV}" recipes don't have "do_configure"
245eb8dc403SDave Cobbley        # task, so we need to run "do_preconfigure" instead
246eb8dc403SDave Cobbley        if pn.startswith("gcc-source-"):
247eb8dc403SDave Cobbley            d.setVar('WORKDIR', d.getVar('ARCHIVER_WORKDIR'))
248eb8dc403SDave Cobbley            bb.build.exec_func('do_preconfigure', d)
249eb8dc403SDave Cobbley
250eb8dc403SDave Cobbley        # The libtool-native's do_configure will remove the
251eb8dc403SDave Cobbley        # ${STAGING_DATADIR}/aclocal/libtool.m4, so we can't re-run the
252eb8dc403SDave Cobbley        # do_configure, we archive the already configured ${S} to
253eb8dc403SDave Cobbley        # instead of.
254eb8dc403SDave Cobbley        elif pn != 'libtool-native':
2551a4b7ee2SBrad Bishop            def runTask(task):
2561a4b7ee2SBrad Bishop                prefuncs = d.getVarFlag(task, 'prefuncs') or ''
2571a4b7ee2SBrad Bishop                for func in prefuncs.split():
258eb8dc403SDave Cobbley                    if func != "sysroot_cleansstate":
259eb8dc403SDave Cobbley                        bb.build.exec_func(func, d)
2601a4b7ee2SBrad Bishop                bb.build.exec_func(task, d)
2611a4b7ee2SBrad Bishop                postfuncs = d.getVarFlag(task, 'postfuncs') or ''
2621a4b7ee2SBrad Bishop                for func in postfuncs.split():
2631a4b7ee2SBrad Bishop                    if func != 'do_qa_configure':
264eb8dc403SDave Cobbley                        bb.build.exec_func(func, d)
2651a4b7ee2SBrad Bishop
2661a4b7ee2SBrad Bishop            # Change the WORKDIR to make do_configure run in another dir.
2671a4b7ee2SBrad Bishop            d.setVar('WORKDIR', d.getVar('ARCHIVER_WORKDIR'))
2681a4b7ee2SBrad Bishop
2691a4b7ee2SBrad Bishop            preceeds = bb.build.preceedtask('do_configure', False, d)
2701a4b7ee2SBrad Bishop            for task in preceeds:
2711a4b7ee2SBrad Bishop                if task != 'do_patch' and task != 'do_prepare_recipe_sysroot':
2721a4b7ee2SBrad Bishop                    runTask(task)
2731a4b7ee2SBrad Bishop            runTask('do_configure')
2741a4b7ee2SBrad Bishop
275eb8dc403SDave Cobbley        srcdir = d.getVar('S')
276eb8dc403SDave Cobbley        builddir = d.getVar('B')
277eb8dc403SDave Cobbley        if srcdir != builddir:
278eb8dc403SDave Cobbley            if os.path.exists(builddir):
279eb8dc403SDave Cobbley                oe.path.copytree(builddir, os.path.join(srcdir, \
280eb8dc403SDave Cobbley                    'build.%s.ar_configured' % d.getVar('PF')))
281eb8dc403SDave Cobbley        create_tarball(d, srcdir, 'configured', ar_outdir)
282eb8dc403SDave Cobbley}
283eb8dc403SDave Cobbley
284c4ea075dSBrad Bishopdef exclude_useless_paths(tarinfo):
285c4ea075dSBrad Bishop    if tarinfo.isdir():
286c4ea075dSBrad Bishop        if tarinfo.name.endswith('/temp') or tarinfo.name.endswith('/patches') or tarinfo.name.endswith('/.pc'):
287c4ea075dSBrad Bishop            return None
288c4ea075dSBrad Bishop        elif tarinfo.name == 'temp' or tarinfo.name == 'patches' or tarinfo.name == '.pc':
289c4ea075dSBrad Bishop            return None
290c4ea075dSBrad Bishop    return tarinfo
291c4ea075dSBrad Bishop
292eb8dc403SDave Cobbleydef create_tarball(d, srcdir, suffix, ar_outdir):
293eb8dc403SDave Cobbley    """
294eb8dc403SDave Cobbley    create the tarball from srcdir
295eb8dc403SDave Cobbley    """
296eb8dc403SDave Cobbley    import tarfile
297eb8dc403SDave Cobbley
298eb8dc403SDave Cobbley    # Make sure we are only creating a single tarball for gcc sources
299eb8dc403SDave Cobbley    if (d.getVar('SRC_URI') == ""):
300eb8dc403SDave Cobbley        return
301eb8dc403SDave Cobbley
302eb8dc403SDave Cobbley    # For the kernel archive, srcdir may just be a link to the
303eb8dc403SDave Cobbley    # work-shared location. Use os.path.realpath to make sure
304eb8dc403SDave Cobbley    # that we archive the actual directory and not just the link.
305eb8dc403SDave Cobbley    srcdir = os.path.realpath(srcdir)
306eb8dc403SDave Cobbley
307eb8dc403SDave Cobbley    bb.utils.mkdirhier(ar_outdir)
308eb8dc403SDave Cobbley    if suffix:
309eb8dc403SDave Cobbley        filename = '%s-%s.tar.gz' % (d.getVar('PF'), suffix)
310eb8dc403SDave Cobbley    else:
311eb8dc403SDave Cobbley        filename = '%s.tar.gz' % d.getVar('PF')
312eb8dc403SDave Cobbley    tarname = os.path.join(ar_outdir, filename)
313eb8dc403SDave Cobbley
314eb8dc403SDave Cobbley    bb.note('Creating %s' % tarname)
315eb8dc403SDave Cobbley    tar = tarfile.open(tarname, 'w:gz')
316c4ea075dSBrad Bishop    tar.add(srcdir, arcname=os.path.basename(srcdir), filter=exclude_useless_paths)
317eb8dc403SDave Cobbley    tar.close()
318eb8dc403SDave Cobbley
319eb8dc403SDave Cobbley# creating .diff.gz between source.orig and source
320eb8dc403SDave Cobbleydef create_diff_gz(d, src_orig, src, ar_outdir):
321eb8dc403SDave Cobbley
322eb8dc403SDave Cobbley    import subprocess
323eb8dc403SDave Cobbley
324eb8dc403SDave Cobbley    if not os.path.isdir(src) or not os.path.isdir(src_orig):
325eb8dc403SDave Cobbley        return
326eb8dc403SDave Cobbley
327eb8dc403SDave Cobbley    # The diff --exclude can't exclude the file with path, so we copy
328eb8dc403SDave Cobbley    # the patched source, and remove the files that we'd like to
329eb8dc403SDave Cobbley    # exclude.
330eb8dc403SDave Cobbley    src_patched = src + '.patched'
331eb8dc403SDave Cobbley    oe.path.copyhardlinktree(src, src_patched)
332eb8dc403SDave Cobbley    for i in d.getVarFlag('ARCHIVER_MODE', 'diff-exclude').split():
333eb8dc403SDave Cobbley        bb.utils.remove(os.path.join(src_orig, i), recurse=True)
334eb8dc403SDave Cobbley        bb.utils.remove(os.path.join(src_patched, i), recurse=True)
335eb8dc403SDave Cobbley
336eb8dc403SDave Cobbley    dirname = os.path.dirname(src)
337eb8dc403SDave Cobbley    basename = os.path.basename(src)
338eb8dc403SDave Cobbley    bb.utils.mkdirhier(ar_outdir)
339eb8dc403SDave Cobbley    cwd = os.getcwd()
340eb8dc403SDave Cobbley    try:
341eb8dc403SDave Cobbley        os.chdir(dirname)
342eb8dc403SDave Cobbley        out_file = os.path.join(ar_outdir, '%s-diff.gz' % d.getVar('PF'))
343eb8dc403SDave Cobbley        diff_cmd = 'diff -Naur %s.orig %s.patched | gzip -c > %s' % (basename, basename, out_file)
344eb8dc403SDave Cobbley        subprocess.check_call(diff_cmd, shell=True)
345eb8dc403SDave Cobbley        bb.utils.remove(src_patched, recurse=True)
346eb8dc403SDave Cobbley    finally:
347eb8dc403SDave Cobbley        os.chdir(cwd)
348eb8dc403SDave Cobbley
349eb8dc403SDave Cobbleydef is_work_shared(d):
350eb8dc403SDave Cobbley    pn = d.getVar('PN')
351eb8dc403SDave Cobbley    return bb.data.inherits_class('kernel', d) or pn.startswith('gcc-source')
352eb8dc403SDave Cobbley
353eb8dc403SDave Cobbley# Run do_unpack and do_patch
354eb8dc403SDave Cobbleypython do_unpack_and_patch() {
355eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'src') not in \
356eb8dc403SDave Cobbley            [ 'patched', 'configured'] and \
357eb8dc403SDave Cobbley            d.getVarFlag('ARCHIVER_MODE', 'diff') != '1':
358eb8dc403SDave Cobbley        return
359eb8dc403SDave Cobbley    ar_outdir = d.getVar('ARCHIVER_OUTDIR')
360eb8dc403SDave Cobbley    ar_workdir = d.getVar('ARCHIVER_WORKDIR')
361eb8dc403SDave Cobbley    ar_sysroot_native = d.getVar('STAGING_DIR_NATIVE')
362eb8dc403SDave Cobbley    pn = d.getVar('PN')
363eb8dc403SDave Cobbley
364eb8dc403SDave Cobbley    # The kernel class functions require it to be on work-shared, so we dont change WORKDIR
365eb8dc403SDave Cobbley    if not is_work_shared(d):
366eb8dc403SDave Cobbley        # Change the WORKDIR to make do_unpack do_patch run in another dir.
367eb8dc403SDave Cobbley        d.setVar('WORKDIR', ar_workdir)
368eb8dc403SDave Cobbley        # Restore the original path to recipe's native sysroot (it's relative to WORKDIR).
369eb8dc403SDave Cobbley        d.setVar('STAGING_DIR_NATIVE', ar_sysroot_native)
370eb8dc403SDave Cobbley
371eb8dc403SDave Cobbley        # The changed 'WORKDIR' also caused 'B' changed, create dir 'B' for the
372eb8dc403SDave Cobbley        # possibly requiring of the following tasks (such as some recipes's
373eb8dc403SDave Cobbley        # do_patch required 'B' existed).
374eb8dc403SDave Cobbley        bb.utils.mkdirhier(d.getVar('B'))
375eb8dc403SDave Cobbley
376eb8dc403SDave Cobbley        bb.build.exec_func('do_unpack', d)
377eb8dc403SDave Cobbley
378eb8dc403SDave Cobbley    # Save the original source for creating the patches
379eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
380eb8dc403SDave Cobbley        src = d.getVar('S').rstrip('/')
381eb8dc403SDave Cobbley        src_orig = '%s.orig' % src
382eb8dc403SDave Cobbley        oe.path.copytree(src, src_orig)
383eb8dc403SDave Cobbley
384eb8dc403SDave Cobbley    # Make sure gcc and kernel sources are patched only once
385eb8dc403SDave Cobbley    if not (d.getVar('SRC_URI') == "" or is_work_shared(d)):
386eb8dc403SDave Cobbley        bb.build.exec_func('do_patch', d)
387eb8dc403SDave Cobbley
388eb8dc403SDave Cobbley    # Create the patches
389eb8dc403SDave Cobbley    if d.getVarFlag('ARCHIVER_MODE', 'diff') == '1':
390eb8dc403SDave Cobbley        bb.note('Creating diff gz...')
391eb8dc403SDave Cobbley        create_diff_gz(d, src_orig, src, ar_outdir)
392eb8dc403SDave Cobbley        bb.utils.remove(src_orig, recurse=True)
393eb8dc403SDave Cobbley}
394eb8dc403SDave Cobbley
395eb8dc403SDave Cobbley# BBINCLUDED is special (excluded from basehash signature
396eb8dc403SDave Cobbley# calculation). Using it in a task signature can cause "basehash
397eb8dc403SDave Cobbley# changed" errors.
398eb8dc403SDave Cobbley#
399eb8dc403SDave Cobbley# Depending on BBINCLUDED also causes do_ar_recipe to run again
400eb8dc403SDave Cobbley# for unrelated changes, like adding or removing buildhistory.bbclass.
401eb8dc403SDave Cobbley#
402eb8dc403SDave Cobbley# For these reasons we ignore the dependency completely. The versioning
403eb8dc403SDave Cobbley# of the output file ensures that we create it each time the recipe
404eb8dc403SDave Cobbley# gets rebuilt, at least as long as a PR server is used. We also rely
405eb8dc403SDave Cobbley# on that mechanism to catch changes in the file content, because the
406eb8dc403SDave Cobbley# file content is not part of of the task signature either.
407eb8dc403SDave Cobbleydo_ar_recipe[vardepsexclude] += "BBINCLUDED"
408eb8dc403SDave Cobbleypython do_ar_recipe () {
409eb8dc403SDave Cobbley    """
410eb8dc403SDave Cobbley    archive the recipe, including .bb and .inc.
411eb8dc403SDave Cobbley    """
412eb8dc403SDave Cobbley    import re
413eb8dc403SDave Cobbley    import shutil
414eb8dc403SDave Cobbley
415eb8dc403SDave Cobbley    require_re = re.compile( r"require\s+(.+)" )
416eb8dc403SDave Cobbley    include_re = re.compile( r"include\s+(.+)" )
417eb8dc403SDave Cobbley    bbfile = d.getVar('FILE')
418eb8dc403SDave Cobbley    outdir = os.path.join(d.getVar('WORKDIR'), \
419eb8dc403SDave Cobbley            '%s-recipe' % d.getVar('PF'))
420eb8dc403SDave Cobbley    bb.utils.mkdirhier(outdir)
421eb8dc403SDave Cobbley    shutil.copy(bbfile, outdir)
422eb8dc403SDave Cobbley
423eb8dc403SDave Cobbley    pn = d.getVar('PN')
424eb8dc403SDave Cobbley    bbappend_files = d.getVar('BBINCLUDED').split()
425eb8dc403SDave Cobbley    # If recipe name is aa, we need to match files like aa.bbappend and aa_1.1.bbappend
426eb8dc403SDave Cobbley    # Files like aa1.bbappend or aa1_1.1.bbappend must be excluded.
427eb8dc403SDave Cobbley    bbappend_re = re.compile( r".*/%s_[^/]*\.bbappend$" % re.escape(pn))
428eb8dc403SDave Cobbley    bbappend_re1 = re.compile( r".*/%s\.bbappend$" % re.escape(pn))
429eb8dc403SDave Cobbley    for file in bbappend_files:
430eb8dc403SDave Cobbley        if bbappend_re.match(file) or bbappend_re1.match(file):
431eb8dc403SDave Cobbley            shutil.copy(file, outdir)
432eb8dc403SDave Cobbley
433eb8dc403SDave Cobbley    dirname = os.path.dirname(bbfile)
434eb8dc403SDave Cobbley    bbpath = '%s:%s' % (dirname, d.getVar('BBPATH'))
435eb8dc403SDave Cobbley    f = open(bbfile, 'r')
436eb8dc403SDave Cobbley    for line in f.readlines():
437eb8dc403SDave Cobbley        incfile = None
438eb8dc403SDave Cobbley        if require_re.match(line):
439eb8dc403SDave Cobbley            incfile = require_re.match(line).group(1)
440eb8dc403SDave Cobbley        elif include_re.match(line):
441eb8dc403SDave Cobbley            incfile = include_re.match(line).group(1)
442eb8dc403SDave Cobbley        if incfile:
443eb8dc403SDave Cobbley            incfile = d.expand(incfile)
444*1d80a2eaSBrad Bishop        if incfile:
445eb8dc403SDave Cobbley            incfile = bb.utils.which(bbpath, incfile)
446eb8dc403SDave Cobbley        if incfile:
447eb8dc403SDave Cobbley            shutil.copy(incfile, outdir)
448eb8dc403SDave Cobbley
449eb8dc403SDave Cobbley    create_tarball(d, outdir, 'recipe', d.getVar('ARCHIVER_OUTDIR'))
450eb8dc403SDave Cobbley    bb.utils.remove(outdir, recurse=True)
451eb8dc403SDave Cobbley}
452eb8dc403SDave Cobbley
453eb8dc403SDave Cobbleypython do_dumpdata () {
454eb8dc403SDave Cobbley    """
455eb8dc403SDave Cobbley    dump environment data to ${PF}-showdata.dump
456eb8dc403SDave Cobbley    """
457eb8dc403SDave Cobbley
458eb8dc403SDave Cobbley    dumpfile = os.path.join(d.getVar('ARCHIVER_OUTDIR'), \
459eb8dc403SDave Cobbley        '%s-showdata.dump' % d.getVar('PF'))
460eb8dc403SDave Cobbley    bb.note('Dumping metadata into %s' % dumpfile)
461eb8dc403SDave Cobbley    with open(dumpfile, "w") as f:
462eb8dc403SDave Cobbley        # emit variables and shell functions
463eb8dc403SDave Cobbley        bb.data.emit_env(f, d, True)
464eb8dc403SDave Cobbley        # emit the metadata which isn't valid shell
465eb8dc403SDave Cobbley        for e in d.keys():
466eb8dc403SDave Cobbley            if d.getVarFlag(e, "python", False):
467eb8dc403SDave Cobbley                f.write("\npython %s () {\n%s}\n" % (e, d.getVar(e, False)))
468eb8dc403SDave Cobbley}
469eb8dc403SDave Cobbley
470eb8dc403SDave CobbleySSTATETASKS += "do_deploy_archives"
471eb8dc403SDave Cobbleydo_deploy_archives () {
472eb8dc403SDave Cobbley    echo "Deploying source archive files from ${ARCHIVER_TOPDIR} to ${DEPLOY_DIR_SRC}."
473eb8dc403SDave Cobbley}
474eb8dc403SDave Cobbleypython do_deploy_archives_setscene () {
475eb8dc403SDave Cobbley    sstate_setscene(d)
476eb8dc403SDave Cobbley}
477eb8dc403SDave Cobbleydo_deploy_archives[dirs] = "${ARCHIVER_TOPDIR}"
478eb8dc403SDave Cobbleydo_deploy_archives[sstate-inputdirs] = "${ARCHIVER_TOPDIR}"
479eb8dc403SDave Cobbleydo_deploy_archives[sstate-outputdirs] = "${DEPLOY_DIR_SRC}"
480eb8dc403SDave Cobbleyaddtask do_deploy_archives_setscene
481eb8dc403SDave Cobbley
482eb8dc403SDave Cobbleyaddtask do_ar_original after do_unpack
483eb8dc403SDave Cobbleyaddtask do_unpack_and_patch after do_patch
484eb8dc403SDave Cobbleyaddtask do_ar_patched after do_unpack_and_patch
485eb8dc403SDave Cobbleyaddtask do_ar_configured after do_unpack_and_patch
486eb8dc403SDave Cobbleyaddtask do_dumpdata
487eb8dc403SDave Cobbleyaddtask do_ar_recipe
488eb8dc403SDave Cobbleyaddtask do_deploy_archives before do_build
489eb8dc403SDave Cobbley
490eb8dc403SDave Cobbleypython () {
491eb8dc403SDave Cobbley    # Add tasks in the correct order, specifically for linux-yocto to avoid race condition.
492eb8dc403SDave Cobbley    # sstatesig.py:sstate_rundepfilter has special support that excludes this dependency
493eb8dc403SDave Cobbley    # so that do_kernel_configme does not need to run again when do_unpack_and_patch
494eb8dc403SDave Cobbley    # gets added or removed (by adding or removing archiver.bbclass).
495eb8dc403SDave Cobbley    if bb.data.inherits_class('kernel-yocto', d):
496eb8dc403SDave Cobbley        bb.build.addtask('do_kernel_configme', 'do_configure', 'do_unpack_and_patch', d)
497eb8dc403SDave Cobbley}
498