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