archiver.bbclass 25 KB

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