license_image.bbclass 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. python write_package_manifest() {
  2. # Get list of installed packages
  3. license_image_dir = d.expand('${LICENSE_DIRECTORY}/${IMAGE_NAME}')
  4. bb.utils.mkdirhier(license_image_dir)
  5. from oe.rootfs import image_list_installed_packages
  6. from oe.utils import format_pkg_list
  7. pkgs = image_list_installed_packages(d)
  8. output = format_pkg_list(pkgs)
  9. open(os.path.join(license_image_dir, 'package.manifest'),
  10. 'w+').write(output)
  11. }
  12. python license_create_manifest() {
  13. import oe.packagedata
  14. from oe.rootfs import image_list_installed_packages
  15. build_images_from_feeds = d.getVar('BUILD_IMAGES_FROM_FEEDS')
  16. if build_images_from_feeds == "1":
  17. return 0
  18. pkg_dic = {}
  19. for pkg in sorted(image_list_installed_packages(d)):
  20. pkg_info = os.path.join(d.getVar('PKGDATA_DIR'),
  21. 'runtime-reverse', pkg)
  22. pkg_name = os.path.basename(os.readlink(pkg_info))
  23. pkg_dic[pkg_name] = oe.packagedata.read_pkgdatafile(pkg_info)
  24. if not "LICENSE" in pkg_dic[pkg_name].keys():
  25. pkg_lic_name = "LICENSE_" + pkg_name
  26. pkg_dic[pkg_name]["LICENSE"] = pkg_dic[pkg_name][pkg_lic_name]
  27. rootfs_license_manifest = os.path.join(d.getVar('LICENSE_DIRECTORY'),
  28. d.getVar('IMAGE_NAME'), 'license.manifest')
  29. write_license_files(d, rootfs_license_manifest, pkg_dic, rootfs=True)
  30. }
  31. def write_license_files(d, license_manifest, pkg_dic, rootfs=True):
  32. import re
  33. import stat
  34. bad_licenses = (d.getVar("INCOMPATIBLE_LICENSE") or "").split()
  35. bad_licenses = [canonical_license(d, l) for l in bad_licenses]
  36. bad_licenses = expand_wildcard_licenses(d, bad_licenses)
  37. whitelist = []
  38. for lic in bad_licenses:
  39. whitelist.extend((d.getVar("WHITELIST_" + lic) or "").split())
  40. with open(license_manifest, "w") as license_file:
  41. for pkg in sorted(pkg_dic):
  42. if bad_licenses and pkg not in whitelist:
  43. try:
  44. licenses = incompatible_pkg_license(d, bad_licenses, pkg_dic[pkg]["LICENSE"])
  45. if licenses:
  46. bb.fatal("Package %s cannot be installed into the image because it has incompatible license(s): %s" %(pkg, ' '.join(licenses)))
  47. (pkg_dic[pkg]["LICENSE"], pkg_dic[pkg]["LICENSES"]) = \
  48. oe.license.manifest_licenses(pkg_dic[pkg]["LICENSE"],
  49. bad_licenses, canonical_license, d)
  50. except oe.license.LicenseError as exc:
  51. bb.fatal('%s: %s' % (d.getVar('P'), exc))
  52. else:
  53. pkg_dic[pkg]["LICENSES"] = re.sub(r'[|&()*]', ' ', pkg_dic[pkg]["LICENSE"])
  54. pkg_dic[pkg]["LICENSES"] = re.sub(r' *', ' ', pkg_dic[pkg]["LICENSES"])
  55. pkg_dic[pkg]["LICENSES"] = pkg_dic[pkg]["LICENSES"].split()
  56. if pkg in whitelist:
  57. bb.warn("Including %s with an incompatible license %s into the image, because it has been whitelisted." %(pkg, pkg_dic[pkg]["LICENSE"]))
  58. if not "IMAGE_MANIFEST" in pkg_dic[pkg]:
  59. # Rootfs manifest
  60. license_file.write("PACKAGE NAME: %s\n" % pkg)
  61. license_file.write("PACKAGE VERSION: %s\n" % pkg_dic[pkg]["PV"])
  62. license_file.write("RECIPE NAME: %s\n" % pkg_dic[pkg]["PN"])
  63. license_file.write("LICENSE: %s\n\n" % pkg_dic[pkg]["LICENSE"])
  64. # If the package doesn't contain any file, that is, its size is 0, the license
  65. # isn't relevant as far as the final image is concerned. So doing license check
  66. # doesn't make much sense, skip it.
  67. if pkg_dic[pkg]["PKGSIZE_%s" % pkg] == "0":
  68. continue
  69. else:
  70. # Image manifest
  71. license_file.write("RECIPE NAME: %s\n" % pkg_dic[pkg]["PN"])
  72. license_file.write("VERSION: %s\n" % pkg_dic[pkg]["PV"])
  73. license_file.write("LICENSE: %s\n" % pkg_dic[pkg]["LICENSE"])
  74. license_file.write("FILES: %s\n\n" % pkg_dic[pkg]["FILES"])
  75. for lic in pkg_dic[pkg]["LICENSES"]:
  76. lic_file = os.path.join(d.getVar('LICENSE_DIRECTORY'),
  77. pkg_dic[pkg]["PN"], "generic_%s" %
  78. re.sub(r'\+', '', lic))
  79. # add explicity avoid of CLOSED license because isn't generic
  80. if lic == "CLOSED":
  81. continue
  82. if not os.path.exists(lic_file):
  83. bb.warn("The license listed %s was not in the "\
  84. "licenses collected for recipe %s"
  85. % (lic, pkg_dic[pkg]["PN"]))
  86. # Two options here:
  87. # - Just copy the manifest
  88. # - Copy the manifest and the license directories
  89. # With both options set we see a .5 M increase in core-image-minimal
  90. copy_lic_manifest = d.getVar('COPY_LIC_MANIFEST')
  91. copy_lic_dirs = d.getVar('COPY_LIC_DIRS')
  92. if rootfs and copy_lic_manifest == "1":
  93. rootfs_license_dir = os.path.join(d.getVar('IMAGE_ROOTFS'),
  94. 'usr', 'share', 'common-licenses')
  95. bb.utils.mkdirhier(rootfs_license_dir)
  96. rootfs_license_manifest = os.path.join(rootfs_license_dir,
  97. os.path.split(license_manifest)[1])
  98. if not os.path.exists(rootfs_license_manifest):
  99. oe.path.copyhardlink(license_manifest, rootfs_license_manifest)
  100. if copy_lic_dirs == "1":
  101. for pkg in sorted(pkg_dic):
  102. pkg_rootfs_license_dir = os.path.join(rootfs_license_dir, pkg)
  103. bb.utils.mkdirhier(pkg_rootfs_license_dir)
  104. pkg_license_dir = os.path.join(d.getVar('LICENSE_DIRECTORY'),
  105. pkg_dic[pkg]["PN"])
  106. pkg_manifest_licenses = [canonical_license(d, lic) \
  107. for lic in pkg_dic[pkg]["LICENSES"]]
  108. licenses = os.listdir(pkg_license_dir)
  109. for lic in licenses:
  110. rootfs_license = os.path.join(rootfs_license_dir, lic)
  111. pkg_license = os.path.join(pkg_license_dir, lic)
  112. pkg_rootfs_license = os.path.join(pkg_rootfs_license_dir, lic)
  113. if re.match(r"^generic_.*$", lic):
  114. generic_lic = canonical_license(d,
  115. re.search(r"^generic_(.*)$", lic).group(1))
  116. # Do not copy generic license into package if isn't
  117. # declared into LICENSES of the package.
  118. if not re.sub(r'\+$', '', generic_lic) in \
  119. [re.sub(r'\+', '', lic) for lic in \
  120. pkg_manifest_licenses]:
  121. continue
  122. if oe.license.license_ok(generic_lic,
  123. bad_licenses) == False:
  124. continue
  125. if not os.path.exists(rootfs_license):
  126. oe.path.copyhardlink(pkg_license, rootfs_license)
  127. if not os.path.exists(pkg_rootfs_license):
  128. os.symlink(os.path.join('..', lic), pkg_rootfs_license)
  129. else:
  130. if (oe.license.license_ok(canonical_license(d,
  131. lic), bad_licenses) == False or
  132. os.path.exists(pkg_rootfs_license)):
  133. continue
  134. oe.path.copyhardlink(pkg_license, pkg_rootfs_license)
  135. # Fixup file ownership and permissions
  136. for walkroot, dirs, files in os.walk(rootfs_license_dir):
  137. for f in files:
  138. p = os.path.join(walkroot, f)
  139. os.lchown(p, 0, 0)
  140. if not os.path.islink(p):
  141. os.chmod(p, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
  142. for dir in dirs:
  143. p = os.path.join(walkroot, dir)
  144. os.lchown(p, 0, 0)
  145. os.chmod(p, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
  146. def license_deployed_manifest(d):
  147. """
  148. Write the license manifest for the deployed recipes.
  149. The deployed recipes usually includes the bootloader
  150. and extra files to boot the target.
  151. """
  152. dep_dic = {}
  153. man_dic = {}
  154. lic_dir = d.getVar("LICENSE_DIRECTORY")
  155. dep_dic = get_deployed_dependencies(d)
  156. for dep in dep_dic.keys():
  157. man_dic[dep] = {}
  158. # It is necessary to mark this will be used for image manifest
  159. man_dic[dep]["IMAGE_MANIFEST"] = True
  160. man_dic[dep]["PN"] = dep
  161. man_dic[dep]["FILES"] = \
  162. " ".join(get_deployed_files(dep_dic[dep]))
  163. with open(os.path.join(lic_dir, dep, "recipeinfo"), "r") as f:
  164. for line in f.readlines():
  165. key,val = line.split(": ", 1)
  166. man_dic[dep][key] = val[:-1]
  167. lic_manifest_dir = os.path.join(d.getVar('LICENSE_DIRECTORY'),
  168. d.getVar('IMAGE_NAME'))
  169. bb.utils.mkdirhier(lic_manifest_dir)
  170. image_license_manifest = os.path.join(lic_manifest_dir, 'image_license.manifest')
  171. write_license_files(d, image_license_manifest, man_dic, rootfs=False)
  172. link_name = d.getVar('IMAGE_LINK_NAME')
  173. if link_name:
  174. lic_manifest_symlink_dir = os.path.join(d.getVar('LICENSE_DIRECTORY'),
  175. link_name)
  176. # remove old symlink
  177. if os.path.islink(lic_manifest_symlink_dir):
  178. os.unlink(lic_manifest_symlink_dir)
  179. # create the image dir symlink
  180. os.symlink(lic_manifest_dir, lic_manifest_symlink_dir)
  181. def get_deployed_dependencies(d):
  182. """
  183. Get all the deployed dependencies of an image
  184. """
  185. deploy = {}
  186. # Get all the dependencies for the current task (rootfs).
  187. taskdata = d.getVar("BB_TASKDEPDATA", False)
  188. depends = list(set([dep[0] for dep
  189. in list(taskdata.values())
  190. if not dep[0].endswith("-native")]))
  191. # To verify what was deployed it checks the rootfs dependencies against
  192. # the SSTATE_MANIFESTS for "deploy" task.
  193. # The manifest file name contains the arch. Because we are not running
  194. # in the recipe context it is necessary to check every arch used.
  195. sstate_manifest_dir = d.getVar("SSTATE_MANIFESTS")
  196. archs = list(set(d.getVar("SSTATE_ARCHS").split()))
  197. for dep in depends:
  198. for arch in archs:
  199. sstate_manifest_file = os.path.join(sstate_manifest_dir,
  200. "manifest-%s-%s.deploy" % (arch, dep))
  201. if os.path.exists(sstate_manifest_file):
  202. deploy[dep] = sstate_manifest_file
  203. break
  204. return deploy
  205. get_deployed_dependencies[vardepsexclude] = "BB_TASKDEPDATA"
  206. def get_deployed_files(man_file):
  207. """
  208. Get the files deployed from the sstate manifest
  209. """
  210. dep_files = []
  211. excluded_files = []
  212. with open(man_file, "r") as manifest:
  213. all_files = manifest.read()
  214. for f in all_files.splitlines():
  215. if ((not (os.path.islink(f) or os.path.isdir(f))) and
  216. not os.path.basename(f) in excluded_files):
  217. dep_files.append(os.path.basename(f))
  218. return dep_files
  219. ROOTFS_POSTPROCESS_COMMAND_prepend = "write_package_manifest; license_create_manifest; "
  220. do_rootfs[recrdeptask] += "do_populate_lic"
  221. python do_populate_lic_deploy() {
  222. license_deployed_manifest(d)
  223. }
  224. addtask populate_lic_deploy before do_build after do_image_complete
  225. do_populate_lic_deploy[recrdeptask] += "do_populate_lic do_deploy"