license_image.bbclass 13 KB

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